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 18KB

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