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.

XSLFTextRun.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.xslf.usermodel;
  16. import org.apache.poi.util.Beta;
  17. import org.apache.poi.xslf.model.CharacterPropertyFetcher;
  18. import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
  19. import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
  20. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextNormalAutofit;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.STTextStrikeType;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
  29. import java.awt.Color;
  30. /**
  31. * Represents a run of text within the containing text body. The run element is the
  32. * lowest level text separation mechanism within a text body.
  33. *
  34. * @author Yegor Kozlov
  35. */
  36. @Beta
  37. public class XSLFTextRun {
  38. private final CTRegularTextRun _r;
  39. private final XSLFTextParagraph _p;
  40. XSLFTextRun(CTRegularTextRun r, XSLFTextParagraph p){
  41. _r = r;
  42. _p = p;
  43. }
  44. XSLFTextParagraph getParentParagraph(){
  45. return _p;
  46. }
  47. public String getText(){
  48. return _r.getT();
  49. }
  50. public void setText(String text){
  51. _r.setT(text);
  52. }
  53. public CTRegularTextRun getXmlObject(){
  54. return _r;
  55. }
  56. public void setFontColor(Color color){
  57. CTTextCharacterProperties rPr = getRpR();
  58. CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
  59. CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
  60. clr.setVal(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()});
  61. }
  62. public Color getFontColor(){
  63. final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
  64. CTShapeStyle style = _p.getParentShape().getSpStyle();
  65. final CTSchemeColor shapeStyle = style == null ? null : style.getFontRef().getSchemeClr();
  66. CharacterPropertyFetcher<Color> fetcher = new CharacterPropertyFetcher<Color>(_p.getLevel()){
  67. public boolean fetch(CTTextCharacterProperties props){
  68. CTSolidColorFillProperties solidFill = props.getSolidFill();
  69. if(solidFill != null) {
  70. Color c = new XSLFColor(solidFill, theme, shapeStyle).getColor();
  71. setValue(c);
  72. return true;
  73. }
  74. return false;
  75. }
  76. };
  77. fetchCharacterProperty(fetcher);
  78. return fetcher.getValue();
  79. }
  80. /**
  81. *
  82. * @param fontSize font size in points.
  83. * The value of <code>-1</code> unsets the Sz attribyte from the underlying xml bean
  84. */
  85. public void setFontSize(double fontSize){
  86. CTTextCharacterProperties rPr = getRpR();
  87. if(fontSize == -1.0) {
  88. if(rPr.isSetSz()) rPr.unsetSz();
  89. } else {
  90. rPr.setSz((int)(100*fontSize));
  91. }
  92. }
  93. /**
  94. * @return font size in points or -1 if font size is not set.
  95. */
  96. public double getFontSize(){
  97. double scale = 1;
  98. CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTextBodyPr().getNormAutofit();
  99. if(afit != null) scale = (double)afit.getFontScale() / 100000;
  100. CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getLevel()){
  101. public boolean fetch(CTTextCharacterProperties props){
  102. if(props.isSetSz()){
  103. setValue(props.getSz()*0.01);
  104. return true;
  105. }
  106. return false;
  107. }
  108. };
  109. fetchCharacterProperty(fetcher);
  110. return fetcher.getValue() == null ? -1 : fetcher.getValue()*scale;
  111. }
  112. /**
  113. *
  114. * @return the spacing between characters within a text run,
  115. * If this attribute is omitted than a value of 0 or no adjustment is assumed.
  116. */
  117. public double getCharacterSpacing(){
  118. CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getLevel()){
  119. public boolean fetch(CTTextCharacterProperties props){
  120. if(props.isSetSpc()){
  121. setValue(props.getSpc()*0.01);
  122. return true;
  123. }
  124. return false;
  125. }
  126. };
  127. fetchCharacterProperty(fetcher);
  128. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  129. }
  130. /**
  131. * Specifies the typeface, or name of the font that is to be used for this text run.
  132. *
  133. * @param typeface the font to apply to this text run.
  134. * The value of <code>null</code> unsets the Typeface attrubute from the underlying xml.
  135. */
  136. public void setFontFamily(String typeface){
  137. setFontFamily(typeface, (byte)-1, (byte)-1, false);
  138. }
  139. public void setFontFamily(String typeface, byte charset, byte pictAndFamily, boolean isSymbol){
  140. CTTextCharacterProperties rPr = getRpR();
  141. if(typeface == null){
  142. if(rPr.isSetLatin()) rPr.unsetLatin();
  143. if(rPr.isSetCs()) rPr.unsetCs();
  144. if(rPr.isSetSym()) rPr.unsetSym();
  145. } else {
  146. if(isSymbol){
  147. CTTextFont font = rPr.isSetSym() ? rPr.getSym() : rPr.addNewSym();
  148. font.setTypeface(typeface);
  149. } else {
  150. CTTextFont latin = rPr.isSetLatin() ? rPr.getLatin() : rPr.addNewLatin();
  151. latin.setTypeface(typeface);
  152. if(charset != -1) latin.setCharset(charset);
  153. if(pictAndFamily != -1) latin.setPitchFamily(pictAndFamily);
  154. }
  155. }
  156. }
  157. /**
  158. * @return font family or null if not set
  159. */
  160. public String getFontFamily(){
  161. final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
  162. CharacterPropertyFetcher<String> visitor = new CharacterPropertyFetcher<String>(_p.getLevel()){
  163. public boolean fetch(CTTextCharacterProperties props){
  164. CTTextFont font = props.getLatin();
  165. if(font != null){
  166. String typeface = font.getTypeface();
  167. if("+mj-lt".equals(typeface)) {
  168. typeface = theme.getMajorFont();
  169. } else if ("+mn-lt".equals(typeface)){
  170. typeface = theme.getMinorFont();
  171. }
  172. setValue(typeface);
  173. return true;
  174. }
  175. return false;
  176. }
  177. };
  178. fetchCharacterProperty(visitor);
  179. return visitor.getValue();
  180. }
  181. /**
  182. * Specifies whether a run of text will be formatted as strikethrough text.
  183. *
  184. * @param strike whether a run of text will be formatted as strikethrough text.
  185. */
  186. public void setStrikethrough(boolean strike){
  187. getRpR().setStrike(strike ? STTextStrikeType.SNG_STRIKE : STTextStrikeType.NO_STRIKE);
  188. }
  189. /**
  190. * @return whether a run of text will be formatted as strikethrough text. Default is false.
  191. */
  192. public boolean isStrikethrough() {
  193. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  194. public boolean fetch(CTTextCharacterProperties props){
  195. if(props.isSetStrike()){
  196. setValue(props.getStrike() != STTextStrikeType.NO_STRIKE);
  197. return true;
  198. }
  199. return false;
  200. }
  201. };
  202. fetchCharacterProperty(fetcher);
  203. return fetcher.getValue() == null ? false : fetcher.getValue();
  204. }
  205. /**
  206. * @return whether a run of text will be formatted as a superscript text. Default is false.
  207. */
  208. public boolean isSuperscript() {
  209. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  210. public boolean fetch(CTTextCharacterProperties props){
  211. if(props.isSetBaseline()){
  212. setValue(props.getBaseline() > 0);
  213. return true;
  214. }
  215. return false;
  216. }
  217. };
  218. fetchCharacterProperty(fetcher);
  219. return fetcher.getValue() == null ? false : fetcher.getValue();
  220. }
  221. /**
  222. * @return whether a run of text will be formatted as a superscript text. Default is false.
  223. */
  224. public boolean isSubscript() {
  225. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  226. public boolean fetch(CTTextCharacterProperties props){
  227. if(props.isSetBaseline()){
  228. setValue(props.getBaseline() < 0);
  229. return true;
  230. }
  231. return false;
  232. }
  233. };
  234. fetchCharacterProperty(fetcher);
  235. return fetcher.getValue() == null ? false : fetcher.getValue();
  236. }
  237. /**
  238. * @return whether a run of text will be formatted as a superscript text. Default is false.
  239. */
  240. public TextCap getTextCap() {
  241. CharacterPropertyFetcher<TextCap> fetcher = new CharacterPropertyFetcher<TextCap>(_p.getLevel()){
  242. public boolean fetch(CTTextCharacterProperties props){
  243. if(props.isSetCap()){
  244. int idx = props.getCap().intValue() - 1;
  245. setValue(TextCap.values()[idx]);
  246. return true;
  247. }
  248. return false;
  249. }
  250. };
  251. fetchCharacterProperty(fetcher);
  252. return fetcher.getValue() == null ? TextCap.NONE : fetcher.getValue();
  253. }
  254. /**
  255. * Specifies whether this run of text will be formatted as bold text
  256. *
  257. * @param bold whether this run of text will be formatted as bold text
  258. */
  259. public void setBold(boolean bold){
  260. getRpR().setB(bold);
  261. }
  262. /**
  263. * @return whether this run of text is formatted as bold text
  264. */
  265. public boolean isBold(){
  266. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  267. public boolean fetch(CTTextCharacterProperties props){
  268. if(props.isSetB()){
  269. setValue(props.getB());
  270. return true;
  271. }
  272. return false;
  273. }
  274. };
  275. fetchCharacterProperty(fetcher);
  276. return fetcher.getValue() == null ? false : fetcher.getValue();
  277. }
  278. /**
  279. * @param italic whether this run of text is formatted as italic text
  280. */
  281. public void setItalic(boolean italic){
  282. getRpR().setI(italic);
  283. }
  284. /**
  285. * @return whether this run of text is formatted as italic text
  286. */
  287. public boolean isItalic(){
  288. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  289. public boolean fetch(CTTextCharacterProperties props){
  290. if(props.isSetI()){
  291. setValue(props.getI());
  292. return true;
  293. }
  294. return false;
  295. }
  296. };
  297. fetchCharacterProperty(fetcher);
  298. return fetcher.getValue() == null ? false : fetcher.getValue();
  299. }
  300. /**
  301. * @param underline whether this run of text is formatted as underlined text
  302. */
  303. public void setUnderline(boolean underline){
  304. getRpR().setU(underline ? STTextUnderlineType.SNG : STTextUnderlineType.NONE);
  305. }
  306. /**
  307. * @return whether this run of text is formatted as underlined text
  308. */
  309. public boolean isUnderline(){
  310. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  311. public boolean fetch(CTTextCharacterProperties props){
  312. if(props.isSetU()){
  313. setValue(props.getU() != STTextUnderlineType.NONE);
  314. return true;
  315. }
  316. return false;
  317. }
  318. };
  319. fetchCharacterProperty(fetcher);
  320. return fetcher.getValue() == null ? false : fetcher.getValue();
  321. }
  322. protected CTTextCharacterProperties getRpR(){
  323. return _r.isSetRPr() ? _r.getRPr() : _r.addNewRPr();
  324. }
  325. @Override
  326. public String toString(){
  327. return "[" + getClass() + "]" + getText();
  328. }
  329. public XSLFHyperlink createHyperlink(){
  330. XSLFHyperlink link = new XSLFHyperlink(_r.getRPr().addNewHlinkClick(), this);
  331. return link;
  332. }
  333. public XSLFHyperlink getHyperlink(){
  334. if(!_r.getRPr().isSetHlinkClick()) return null;
  335. return new XSLFHyperlink(_r.getRPr().getHlinkClick(), this);
  336. }
  337. private boolean fetchCharacterProperty(CharacterPropertyFetcher fetcher){
  338. boolean ok = false;
  339. if(_r.isSetRPr()) ok = fetcher.fetch(_r.getRPr());
  340. if(!ok) {
  341. XSLFTextShape shape = _p.getParentShape();
  342. ok = shape.fetchShapeProperty(fetcher);
  343. if(!ok) {
  344. CTTextParagraphProperties defaultProps = _p.getDefaultStyle();
  345. if(defaultProps != null) ok = fetcher.fetch(defaultProps);
  346. }
  347. }
  348. return ok;
  349. }
  350. }