You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSLFTextRun.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getPropVal;
  17. import java.awt.Color;
  18. import org.apache.poi.hslf.exceptions.HSLFException;
  19. import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;
  20. import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
  21. import org.apache.poi.hslf.model.textproperties.TextProp;
  22. import org.apache.poi.hslf.model.textproperties.TextPropCollection;
  23. import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
  24. import org.apache.poi.sl.draw.DrawPaint;
  25. import org.apache.poi.sl.usermodel.PaintStyle;
  26. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  27. import org.apache.poi.sl.usermodel.TextRun;
  28. import org.apache.poi.util.POILogFactory;
  29. import org.apache.poi.util.POILogger;
  30. /**
  31. * Represents a run of text, all with the same style
  32. *
  33. */
  34. public final class HSLFTextRun implements TextRun {
  35. protected POILogger logger = POILogFactory.getLogger(this.getClass());
  36. /** The TextRun we belong to */
  37. private HSLFTextParagraph parentParagraph;
  38. private String _runText = "";
  39. private String _fontFamily;
  40. /**
  41. * Our paragraph and character style.
  42. * Note - we may share these styles with other RichTextRuns
  43. */
  44. private TextPropCollection characterStyle = new TextPropCollection(1, TextPropType.character);
  45. /**
  46. * Create a new wrapper around a rich text string
  47. * @param parentParagraph the parent paragraph
  48. */
  49. public HSLFTextRun(HSLFTextParagraph parentParagraph) {
  50. this.parentParagraph = parentParagraph;
  51. }
  52. public TextPropCollection getCharacterStyle() {
  53. return characterStyle;
  54. }
  55. public void setCharacterStyle(TextPropCollection characterStyle) {
  56. assert(characterStyle != null);
  57. this.characterStyle = characterStyle;
  58. }
  59. /**
  60. * Supply the SlideShow we belong to
  61. */
  62. public void updateSheet() {
  63. if (_fontFamily != null) {
  64. setFontFamily(_fontFamily);
  65. _fontFamily = null;
  66. }
  67. }
  68. /**
  69. * Get the length of the text
  70. */
  71. public int getLength() {
  72. return _runText.length();
  73. }
  74. /**
  75. * Fetch the text, in raw storage form
  76. */
  77. public String getRawText() {
  78. return _runText;
  79. }
  80. /**
  81. * Change the text
  82. */
  83. public void setText(String text) {
  84. if (text == null) {
  85. throw new HSLFException("text must not be null");
  86. }
  87. String newText = HSLFTextParagraph.toInternalString(text);
  88. if (!newText.equals(_runText)) {
  89. _runText = newText;
  90. if (HSLFSlideShow.getLoadSavePhase() == HSLFSlideShow.LoadSavePhase.LOADED) {
  91. parentParagraph.setDirty();
  92. }
  93. }
  94. }
  95. // --------------- Internal helpers on rich text properties -------
  96. /**
  97. * Fetch the value of the given flag in the CharFlagsTextProp.
  98. * Returns false if the CharFlagsTextProp isn't present, since the
  99. * text property won't be set if there's no CharFlagsTextProp.
  100. */
  101. private boolean isCharFlagsTextPropVal(int index) {
  102. return getFlag(index);
  103. }
  104. protected boolean getFlag(int index) {
  105. if (characterStyle == null) return false;
  106. BitMaskTextProp prop = (BitMaskTextProp)characterStyle.findByName(CharFlagsTextProp.NAME);
  107. if (prop == null || !prop.getSubPropMatches()[index]) {
  108. int txtype = parentParagraph.getRunType();
  109. HSLFSheet sheet = parentParagraph.getSheet();
  110. if (sheet != null) {
  111. HSLFMasterSheet master = sheet.getMasterSheet();
  112. if (master != null){
  113. prop = (BitMaskTextProp)master.getStyleAttribute(txtype, parentParagraph.getIndentLevel(), CharFlagsTextProp.NAME, true);
  114. }
  115. } else {
  116. logger.log(POILogger.WARN, "MasterSheet is not available");
  117. }
  118. }
  119. return prop == null ? false : prop.getSubValue(index);
  120. }
  121. /**
  122. * Set the value of the given flag in the CharFlagsTextProp, adding
  123. * it if required.
  124. */
  125. private void setCharFlagsTextPropVal(int index, boolean value) {
  126. // TODO: check if paragraph/chars can be handled the same ...
  127. if (getFlag(index) != value) {
  128. setFlag(index, value);
  129. parentParagraph.setDirty();
  130. }
  131. }
  132. /**
  133. * Sets the value of the given Paragraph TextProp, add if required
  134. * @param propName The name of the Paragraph TextProp
  135. * @param val The value to set for the TextProp
  136. */
  137. public void setCharTextPropVal(String propName, Integer val) {
  138. HSLFTextParagraph.setPropVal(characterStyle, propName, val);
  139. parentParagraph.setDirty();
  140. }
  141. // --------------- Friendly getters / setters on rich text properties -------
  142. @Override
  143. public boolean isBold() {
  144. return isCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX);
  145. }
  146. @Override
  147. public void setBold(boolean bold) {
  148. setCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX, bold);
  149. }
  150. @Override
  151. public boolean isItalic() {
  152. return isCharFlagsTextPropVal(CharFlagsTextProp.ITALIC_IDX);
  153. }
  154. @Override
  155. public void setItalic(boolean italic) {
  156. setCharFlagsTextPropVal(CharFlagsTextProp.ITALIC_IDX, italic);
  157. }
  158. @Override
  159. public boolean isUnderlined() {
  160. return isCharFlagsTextPropVal(CharFlagsTextProp.UNDERLINE_IDX);
  161. }
  162. @Override
  163. public void setUnderlined(boolean underlined) {
  164. setCharFlagsTextPropVal(CharFlagsTextProp.UNDERLINE_IDX, underlined);
  165. }
  166. /**
  167. * Does the text have a shadow?
  168. */
  169. public boolean isShadowed() {
  170. return isCharFlagsTextPropVal(CharFlagsTextProp.SHADOW_IDX);
  171. }
  172. /**
  173. * Does the text have a shadow?
  174. */
  175. public void setShadowed(boolean flag) {
  176. setCharFlagsTextPropVal(CharFlagsTextProp.SHADOW_IDX, flag);
  177. }
  178. /**
  179. * Is this text embossed?
  180. */
  181. public boolean isEmbossed() {
  182. return isCharFlagsTextPropVal(CharFlagsTextProp.RELIEF_IDX);
  183. }
  184. /**
  185. * Is this text embossed?
  186. */
  187. public void setEmbossed(boolean flag) {
  188. setCharFlagsTextPropVal(CharFlagsTextProp.RELIEF_IDX, flag);
  189. }
  190. @Override
  191. public boolean isStrikethrough() {
  192. return isCharFlagsTextPropVal(CharFlagsTextProp.STRIKETHROUGH_IDX);
  193. }
  194. @Override
  195. public void setStrikethrough(boolean flag) {
  196. setCharFlagsTextPropVal(CharFlagsTextProp.STRIKETHROUGH_IDX, flag);
  197. }
  198. /**
  199. * Gets the subscript/superscript option
  200. *
  201. * @return the percentage of the font size. If the value is positive, it is superscript, otherwise it is subscript
  202. */
  203. public int getSuperscript() {
  204. TextProp tp = getPropVal(characterStyle, "superscript", parentParagraph);
  205. return tp == null ? 0 : tp.getValue();
  206. }
  207. /**
  208. * Sets the subscript/superscript option
  209. *
  210. * @param val the percentage of the font size. If the value is positive, it is superscript, otherwise it is subscript
  211. */
  212. public void setSuperscript(int val) {
  213. setCharTextPropVal("superscript", val);
  214. }
  215. @Override
  216. public Double getFontSize() {
  217. TextProp tp = getPropVal(characterStyle, "font.size", parentParagraph);
  218. return tp == null ? null : (double)tp.getValue();
  219. }
  220. @Override
  221. public void setFontSize(Double fontSize) {
  222. Integer iFontSize = (fontSize == null) ? null : fontSize.intValue();
  223. setCharTextPropVal("font.size", iFontSize);
  224. }
  225. /**
  226. * Gets the font index
  227. */
  228. public int getFontIndex() {
  229. TextProp tp = getPropVal(characterStyle, "font.index", parentParagraph);
  230. return tp == null ? -1 : tp.getValue();
  231. }
  232. /**
  233. * Sets the font index
  234. */
  235. public void setFontIndex(int idx) {
  236. setCharTextPropVal("font.index", idx);
  237. }
  238. @Override
  239. public void setFontFamily(String fontFamily) {
  240. HSLFSheet sheet = parentParagraph.getSheet();
  241. @SuppressWarnings("resource")
  242. HSLFSlideShow slideShow = (sheet == null) ? null : sheet.getSlideShow();
  243. if (sheet == null || slideShow == null) {
  244. //we can't set font since slideshow is not assigned yet
  245. _fontFamily = fontFamily;
  246. return;
  247. }
  248. // Get the index for this font (adding if needed)
  249. Integer fontIdx = (fontFamily == null) ? null : slideShow.getFontCollection().addFont(fontFamily);
  250. setCharTextPropVal("font.index", fontIdx);
  251. }
  252. @Override
  253. public String getFontFamily() {
  254. HSLFSheet sheet = parentParagraph.getSheet();
  255. @SuppressWarnings("resource")
  256. HSLFSlideShow slideShow = (sheet == null) ? null : sheet.getSlideShow();
  257. if (sheet == null || slideShow == null) {
  258. return _fontFamily;
  259. }
  260. TextProp tp = getPropVal(characterStyle, "font.index", parentParagraph);
  261. if (tp == null) { return null; }
  262. return slideShow.getFontCollection().getFontWithId(tp.getValue());
  263. }
  264. /**
  265. * @return font color as PaintStyle
  266. */
  267. @Override
  268. public SolidPaint getFontColor() {
  269. TextProp tp = getPropVal(characterStyle, "font.color", parentParagraph);
  270. if (tp == null) return null;
  271. Color color = HSLFTextParagraph.getColorFromColorIndexStruct(tp.getValue(), parentParagraph.getSheet());
  272. SolidPaint ps = DrawPaint.createSolidPaint(color);
  273. return ps;
  274. }
  275. /**
  276. * Sets color of the text, as a int bgr.
  277. * (PowerPoint stores as BlueGreenRed, not the more
  278. * usual RedGreenBlue)
  279. * @see java.awt.Color
  280. */
  281. public void setFontColor(int bgr) {
  282. setCharTextPropVal("font.color", bgr);
  283. }
  284. @Override
  285. public void setFontColor(Color color) {
  286. setFontColor(DrawPaint.createSolidPaint(color));
  287. }
  288. @Override
  289. public void setFontColor(PaintStyle color) {
  290. if (!(color instanceof SolidPaint)) {
  291. throw new IllegalArgumentException("HSLF only supports solid paint");
  292. }
  293. // In PowerPont RGB bytes are swapped, as BGR
  294. SolidPaint sp = (SolidPaint)color;
  295. Color c = DrawPaint.applyColorTransform(sp.getSolidColor());
  296. int rgb = new Color(c.getBlue(), c.getGreen(), c.getRed(), 254).getRGB();
  297. setFontColor(rgb);
  298. }
  299. protected void setFlag(int index, boolean value) {
  300. BitMaskTextProp prop = (BitMaskTextProp)characterStyle.addWithName(CharFlagsTextProp.NAME);
  301. prop.setSubValue(value, index);
  302. }
  303. public HSLFTextParagraph getTextParagraph() {
  304. return parentParagraph;
  305. }
  306. public TextCap getTextCap() {
  307. return TextCap.NONE;
  308. }
  309. @Override
  310. public boolean isSubscript() {
  311. return getSuperscript() < 0;
  312. }
  313. @Override
  314. public boolean isSuperscript() {
  315. return getSuperscript() > 0;
  316. }
  317. public byte getPitchAndFamily() {
  318. return 0;
  319. }
  320. }