PageRenderTime 40ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/pptx/src/org/apache/poi/xslf/usermodel/XSLFTextRun.java

https://github.com/minstrelsy/POI-Android
Java | 546 lines | 367 code | 57 blank | 122 comment | 94 complexity | d2b2f692a7b658d89cb45fae37549298 MD5 | raw file
  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 org.openxmlformats.schemas.drawingml.x2006.main.STTextStrikeType;
  30. //import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  33. import and.awt.Color;
  34. import java.text.AttributedString;
  35. /**
  36. * Represents a run of text within the containing text body. The run element is the
  37. * lowest level text separation mechanism within a text body.
  38. *
  39. * @author Yegor Kozlov
  40. */
  41. @Beta
  42. public class XSLFTextRun {
  43. private final CTRegularTextRun _r;
  44. private final XSLFTextParagraph _p;
  45. XSLFTextRun(CTRegularTextRun r, XSLFTextParagraph p){
  46. _r = r;
  47. _p = p;
  48. }
  49. XSLFTextParagraph getParentParagraph(){
  50. return _p;
  51. }
  52. public String getText(){
  53. return _r.getT();
  54. }
  55. String getRenderableText(){
  56. String txt = _r.getT();
  57. TextCap cap = getTextCap();
  58. StringBuffer buf = new StringBuffer();
  59. for(int i = 0; i < txt.length(); i++) {
  60. char c = txt.charAt(i);
  61. if(c == '\t') {
  62. // TODO: finish support for tabs
  63. buf.append(" ");
  64. } else {
  65. switch (cap){
  66. case ALL:
  67. buf.append(Character.toUpperCase(c));
  68. break;
  69. case SMALL:
  70. buf.append(Character.toLowerCase(c));
  71. break;
  72. default:
  73. buf.append(c);
  74. }
  75. }
  76. }
  77. return buf.toString();
  78. }
  79. /**
  80. * Replace a tab with the effective number of white spaces.
  81. *
  82. * @return
  83. */
  84. private String tab2space(){
  85. // XXX: DD
  86. // AttributedString string = new AttributedString(" ");
  87. // // user can pass an object to convert fonts via a rendering hint
  88. // string.addAttribute(TextAttribute.FAMILY, getFontFamily());
  89. //
  90. // string.addAttribute(TextAttribute.SIZE, (float)getFontSize());
  91. // TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true));
  92. // double wspace = l.getAdvance();
  93. //
  94. // double tabSz = _p.getDefaultTabSize();
  95. //
  96. // int numSpaces = (int)Math.ceil(tabSz / wspace);
  97. int numSpaces = 4;
  98. StringBuffer buf = new StringBuffer();
  99. for(int i = 0; i < numSpaces; i++) {
  100. buf.append(' ');
  101. }
  102. return buf.toString();
  103. }
  104. public void setText(String text){
  105. _r.setT(text);
  106. }
  107. public CTRegularTextRun getXmlObject(){
  108. return _r;
  109. }
  110. public void setFontColor(Color color){
  111. CTTextCharacterProperties rPr = getRPr();
  112. CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
  113. CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
  114. clr.setVal(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()});
  115. if(fill.isSetHslClr()) fill.unsetHslClr();
  116. if(fill.isSetPrstClr()) fill.unsetPrstClr();
  117. if(fill.isSetSchemeClr()) fill.unsetSchemeClr();
  118. if(fill.isSetScrgbClr()) fill.unsetScrgbClr();
  119. if(fill.isSetSysClr()) fill.unsetSysClr();
  120. }
  121. public Color getFontColor(){
  122. final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
  123. CTShapeStyle style = _p.getParentShape().getSpStyle();
  124. final CTSchemeColor phClr = style == null ? null : style.getFontRef().getSchemeClr();
  125. CharacterPropertyFetcher<Color> fetcher = new CharacterPropertyFetcher<Color>(_p.getLevel()){
  126. public boolean fetch(CTTextCharacterProperties props){
  127. CTSolidColorFillProperties solidFill = props.getSolidFill();
  128. if(solidFill != null) {
  129. boolean useCtxColor =
  130. (solidFill.isSetSchemeClr() && solidFill.getSchemeClr().getVal() == STSchemeColorVal.PH_CLR)
  131. || isFetchingFromMaster;
  132. Color c = new XSLFColor(solidFill, theme, useCtxColor ? phClr : null).getColor();
  133. setValue(c);
  134. return true;
  135. }
  136. return false;
  137. }
  138. };
  139. fetchCharacterProperty(fetcher);
  140. return fetcher.getValue();
  141. }
  142. /**
  143. *
  144. * @param fontSize font size in points.
  145. * The value of <code>-1</code> unsets the Sz attribyte from the underlying xml bean
  146. */
  147. public void setFontSize(double fontSize){
  148. CTTextCharacterProperties rPr = getRPr();
  149. if(fontSize == -1.0) {
  150. if(rPr.isSetSz()) rPr.unsetSz();
  151. } else {
  152. if(fontSize < 1.0) {
  153. throw new IllegalArgumentException("Minimum font size is 1pt but was " + fontSize);
  154. }
  155. rPr.setSz((int)(100*fontSize));
  156. }
  157. }
  158. /**
  159. * @return font size in points or -1 if font size is not set.
  160. */
  161. public double getFontSize(){
  162. double scale = 1;
  163. CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTextBodyPr().getNormAutofit();
  164. if(afit != null) scale = (double)afit.getFontScale() / 100000;
  165. CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getLevel()){
  166. public boolean fetch(CTTextCharacterProperties props){
  167. if(props.isSetSz()){
  168. setValue(props.getSz()*0.01);
  169. return true;
  170. }
  171. return false;
  172. }
  173. };
  174. fetchCharacterProperty(fetcher);
  175. return fetcher.getValue() == null ? -1 : fetcher.getValue()*scale;
  176. }
  177. /**
  178. *
  179. * @return the spacing between characters within a text run,
  180. * If this attribute is omitted than a value of 0 or no adjustment is assumed.
  181. */
  182. public double getCharacterSpacing(){
  183. CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getLevel()){
  184. public boolean fetch(CTTextCharacterProperties props){
  185. if(props.isSetSpc()){
  186. setValue(props.getSpc()*0.01);
  187. return true;
  188. }
  189. return false;
  190. }
  191. };
  192. fetchCharacterProperty(fetcher);
  193. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  194. }
  195. /**
  196. * Set the spacing between characters within a text run.
  197. * <p>
  198. * The spacing is specified in points. Positive values will cause the text to expand,
  199. * negative values to condense.
  200. * </p>
  201. *
  202. * @param spc character spacing in points.
  203. */
  204. public void setCharacterSpacing(double spc){
  205. CTTextCharacterProperties rPr = getRPr();
  206. if(spc == 0.0) {
  207. if(rPr.isSetSpc()) rPr.unsetSpc();
  208. } else {
  209. rPr.setSpc((int)(100*spc));
  210. }
  211. }
  212. /**
  213. * Specifies the typeface, or name of the font that is to be used for this text run.
  214. *
  215. * @param typeface the font to apply to this text run.
  216. * The value of <code>null</code> unsets the Typeface attrubute from the underlying xml.
  217. */
  218. public void setFontFamily(String typeface){
  219. setFontFamily(typeface, (byte)-1, (byte)-1, false);
  220. }
  221. public void setFontFamily(String typeface, byte charset, byte pictAndFamily, boolean isSymbol){
  222. CTTextCharacterProperties rPr = getRPr();
  223. if(typeface == null){
  224. if(rPr.isSetLatin()) rPr.unsetLatin();
  225. if(rPr.isSetCs()) rPr.unsetCs();
  226. if(rPr.isSetSym()) rPr.unsetSym();
  227. } else {
  228. if(isSymbol){
  229. CTTextFont font = rPr.isSetSym() ? rPr.getSym() : rPr.addNewSym();
  230. font.setTypeface(typeface);
  231. } else {
  232. CTTextFont latin = rPr.isSetLatin() ? rPr.getLatin() : rPr.addNewLatin();
  233. latin.setTypeface(typeface);
  234. if(charset != -1) latin.setCharset(charset);
  235. if(pictAndFamily != -1) latin.setPitchFamily(pictAndFamily);
  236. }
  237. }
  238. }
  239. /**
  240. * @return font family or null if not set
  241. */
  242. public String getFontFamily(){
  243. final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
  244. CharacterPropertyFetcher<String> visitor = new CharacterPropertyFetcher<String>(_p.getLevel()){
  245. public boolean fetch(CTTextCharacterProperties props){
  246. CTTextFont font = props.getLatin();
  247. if(font != null){
  248. String typeface = font.getTypeface();
  249. if("+mj-lt".equals(typeface)) {
  250. typeface = theme.getMajorFont();
  251. } else if ("+mn-lt".equals(typeface)){
  252. typeface = theme.getMinorFont();
  253. }
  254. setValue(typeface);
  255. return true;
  256. }
  257. return false;
  258. }
  259. };
  260. fetchCharacterProperty(visitor);
  261. return visitor.getValue();
  262. }
  263. public byte getPitchAndFamily(){
  264. final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
  265. CharacterPropertyFetcher<Byte> visitor = new CharacterPropertyFetcher<Byte>(_p.getLevel()){
  266. public boolean fetch(CTTextCharacterProperties props){
  267. CTTextFont font = props.getLatin();
  268. if(font != null){
  269. setValue(font.getPitchFamily());
  270. return true;
  271. }
  272. return false;
  273. }
  274. };
  275. fetchCharacterProperty(visitor);
  276. return visitor.getValue() == null ? 0 : visitor.getValue();
  277. }
  278. /**
  279. * Specifies whether a run of text will be formatted as strikethrough text.
  280. *
  281. * @param strike whether a run of text will be formatted as strikethrough text.
  282. */
  283. public void setStrikethrough(boolean strike) {
  284. getRPr().setStrike(strike ? STTextStrikeType.SNG_STRIKE : STTextStrikeType.NO_STRIKE);
  285. }
  286. /**
  287. * @return whether a run of text will be formatted as strikethrough text. Default is false.
  288. */
  289. public boolean isStrikethrough() {
  290. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  291. public boolean fetch(CTTextCharacterProperties props){
  292. if(props.isSetStrike()){
  293. setValue(props.getStrike() != STTextStrikeType.NO_STRIKE);
  294. return true;
  295. }
  296. return false;
  297. }
  298. };
  299. fetchCharacterProperty(fetcher);
  300. return fetcher.getValue() == null ? false : fetcher.getValue();
  301. }
  302. /**
  303. * @return whether a run of text will be formatted as a superscript text. Default is false.
  304. */
  305. public boolean isSuperscript() {
  306. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  307. public boolean fetch(CTTextCharacterProperties props){
  308. if(props.isSetBaseline()){
  309. setValue(props.getBaseline() > 0);
  310. return true;
  311. }
  312. return false;
  313. }
  314. };
  315. fetchCharacterProperty(fetcher);
  316. return fetcher.getValue() == null ? false : fetcher.getValue();
  317. }
  318. /**
  319. * @return whether a run of text will be formatted as a superscript text. Default is false.
  320. */
  321. public boolean isSubscript() {
  322. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  323. public boolean fetch(CTTextCharacterProperties props){
  324. if(props.isSetBaseline()){
  325. setValue(props.getBaseline() < 0);
  326. return true;
  327. }
  328. return false;
  329. }
  330. };
  331. fetchCharacterProperty(fetcher);
  332. return fetcher.getValue() == null ? false : fetcher.getValue();
  333. }
  334. /**
  335. * @return whether a run of text will be formatted as a superscript text. Default is false.
  336. */
  337. public TextCap getTextCap() {
  338. CharacterPropertyFetcher<TextCap> fetcher = new CharacterPropertyFetcher<TextCap>(_p.getLevel()){
  339. public boolean fetch(CTTextCharacterProperties props){
  340. if(props.isSetCap()){
  341. int idx = props.getCap().intValue() - 1;
  342. setValue(TextCap.values()[idx]);
  343. return true;
  344. }
  345. return false;
  346. }
  347. };
  348. fetchCharacterProperty(fetcher);
  349. return fetcher.getValue() == null ? TextCap.NONE : fetcher.getValue();
  350. }
  351. /**
  352. * Specifies whether this run of text will be formatted as bold text
  353. *
  354. * @param bold whether this run of text will be formatted as bold text
  355. */
  356. public void setBold(boolean bold){
  357. getRPr().setB(bold);
  358. }
  359. /**
  360. * @return whether this run of text is formatted as bold text
  361. */
  362. public boolean isBold(){
  363. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  364. public boolean fetch(CTTextCharacterProperties props){
  365. if(props.isSetB()){
  366. setValue(props.getB());
  367. return true;
  368. }
  369. return false;
  370. }
  371. };
  372. fetchCharacterProperty(fetcher);
  373. return fetcher.getValue() == null ? false : fetcher.getValue();
  374. }
  375. /**
  376. * @param italic whether this run of text is formatted as italic text
  377. */
  378. public void setItalic(boolean italic){
  379. getRPr().setI(italic);
  380. }
  381. /**
  382. * @return whether this run of text is formatted as italic text
  383. */
  384. public boolean isItalic(){
  385. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  386. public boolean fetch(CTTextCharacterProperties props){
  387. if(props.isSetI()){
  388. setValue(props.getI());
  389. return true;
  390. }
  391. return false;
  392. }
  393. };
  394. fetchCharacterProperty(fetcher);
  395. return fetcher.getValue() == null ? false : fetcher.getValue();
  396. }
  397. /**
  398. * @param underline whether this run of text is formatted as underlined text
  399. */
  400. public void setUnderline(boolean underline) {
  401. getRPr().setU(underline ? STTextUnderlineType.SNG : STTextUnderlineType.NONE);
  402. }
  403. /**
  404. * @return whether this run of text is formatted as underlined text
  405. */
  406. public boolean isUnderline(){
  407. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getLevel()){
  408. public boolean fetch(CTTextCharacterProperties props){
  409. if(props.isSetU()){
  410. setValue(props.getU() != STTextUnderlineType.NONE);
  411. return true;
  412. }
  413. return false;
  414. }
  415. };
  416. fetchCharacterProperty(fetcher);
  417. return fetcher.getValue() == null ? false : fetcher.getValue();
  418. }
  419. protected CTTextCharacterProperties getRPr(){
  420. return _r.isSetRPr() ? _r.getRPr() : _r.addNewRPr();
  421. }
  422. @Override
  423. public String toString(){
  424. return "[" + getClass() + "]" + getText();
  425. }
  426. // public XSLFHyperlink createHyperlink(){
  427. // XSLFHyperlink link = new XSLFHyperlink(_r.getRPr().addNewHlinkClick(), this);
  428. // return link;
  429. // }
  430. //
  431. // public XSLFHyperlink getHyperlink(){
  432. // if(!_r.getRPr().isSetHlinkClick()) return null;
  433. //
  434. //
  435. // return new XSLFHyperlink(_r.getRPr().getHlinkClick(), this);
  436. // }
  437. private boolean fetchCharacterProperty(CharacterPropertyFetcher fetcher){
  438. boolean ok = false;
  439. if(_r.isSetRPr()) ok = fetcher.fetch(getRPr());
  440. if(!ok) {
  441. XSLFTextShape shape = _p.getParentShape();
  442. ok = shape.fetchShapeProperty(fetcher);
  443. if(!ok){
  444. CTPlaceholder ph = shape.getCTPlaceholder();
  445. if(ph == null){
  446. // if it is a plain text box then take defaults from presentation.xml
  447. XMLSlideShow ppt = shape.getSheet().getSlideShow();
  448. CTTextParagraphProperties themeProps = ppt.getDefaultParagraphStyle(_p.getLevel());
  449. if(themeProps != null) {
  450. fetcher.isFetchingFromMaster = true;
  451. ok = fetcher.fetch(themeProps);
  452. }
  453. }
  454. if (!ok) {
  455. CTTextParagraphProperties defaultProps = _p.getDefaultMasterStyle();
  456. if(defaultProps != null) {
  457. fetcher.isFetchingFromMaster = true;
  458. ok = fetcher.fetch(defaultProps);
  459. }
  460. }
  461. }
  462. }
  463. return ok;
  464. }
  465. void copy(XSLFTextRun r){
  466. String srcFontFamily = r.getFontFamily();
  467. if(srcFontFamily != null && !srcFontFamily.equals(getFontFamily())){
  468. setFontFamily(srcFontFamily);
  469. }
  470. Color srcFontColor = r.getFontColor();
  471. if(srcFontColor != null && !srcFontColor.equals(getFontColor())){
  472. setFontColor(srcFontColor);
  473. }
  474. double srcFontSize = r.getFontSize();
  475. if(srcFontSize != getFontSize()){
  476. setFontSize(srcFontSize);
  477. }
  478. boolean bold = r.isBold();
  479. if(bold != isBold()) setBold(bold);
  480. boolean italic = r.isItalic();
  481. if(italic != isItalic()) setItalic(italic);
  482. boolean underline = r.isUnderline();
  483. if(underline != isUnderline()) setUnderline(underline);
  484. boolean strike = r.isStrikethrough();
  485. if(strike != isStrikethrough()) setStrikethrough(strike);
  486. }
  487. }