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.

XSLFTextParagraph.java 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Objects;
  22. import java.util.function.Consumer;
  23. import java.util.function.Function;
  24. import java.util.function.Supplier;
  25. import org.apache.poi.ooxml.util.POIXMLUnits;
  26. import org.apache.poi.sl.draw.DrawPaint;
  27. import org.apache.poi.sl.usermodel.AutoNumberingScheme;
  28. import org.apache.poi.sl.usermodel.PaintStyle;
  29. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  30. import org.apache.poi.sl.usermodel.TabStop.TabStopType;
  31. import org.apache.poi.sl.usermodel.TextParagraph;
  32. import org.apache.poi.util.Beta;
  33. import org.apache.poi.util.Internal;
  34. import org.apache.poi.util.Units;
  35. import org.apache.poi.xslf.model.ParagraphPropertyFetcher;
  36. import org.apache.poi.xslf.model.ParagraphPropertyFetcher.ParaPropFetcher;
  37. import org.apache.xmlbeans.XmlCursor;
  38. import org.apache.xmlbeans.XmlObject;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.*;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
  42. /**
  43. * Represents a paragraph of text within the containing text body.
  44. * The paragraph is the highest level text separation mechanism.
  45. *
  46. * @since POI-3.8
  47. */
  48. @Beta
  49. public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagraph,XSLFTextRun> {
  50. private final CTTextParagraph _p;
  51. private final List<XSLFTextRun> _runs;
  52. private final XSLFTextShape _shape;
  53. @FunctionalInterface
  54. private interface Procedure {
  55. void accept();
  56. }
  57. XSLFTextParagraph(CTTextParagraph p, XSLFTextShape shape) {
  58. _p = p;
  59. _runs = new ArrayList<>();
  60. _shape = shape;
  61. try (XmlCursor c = _p.newCursor()) {
  62. if (c.toFirstChild()) {
  63. do {
  64. XmlObject r = c.getObject();
  65. if (r instanceof CTTextLineBreak) {
  66. _runs.add(new XSLFLineBreak((CTTextLineBreak)r, this));
  67. } else if (r instanceof CTRegularTextRun || r instanceof CTTextField) {
  68. _runs.add(new XSLFTextRun(r, this));
  69. }
  70. } while (c.toNextSibling());
  71. }
  72. }
  73. }
  74. public String getText() {
  75. StringBuilder out = new StringBuilder();
  76. for (XSLFTextRun r : _runs) {
  77. out.append(r.getRawText());
  78. }
  79. return out.toString();
  80. }
  81. @Internal
  82. public CTTextParagraph getXmlObject() {
  83. return _p;
  84. }
  85. @Override
  86. public XSLFTextShape getParentShape() {
  87. return _shape;
  88. }
  89. @Override
  90. public List<XSLFTextRun> getTextRuns() {
  91. return Collections.unmodifiableList(_runs);
  92. }
  93. @Override
  94. public Iterator<XSLFTextRun> iterator() {
  95. return getTextRuns().iterator();
  96. }
  97. /**
  98. * Add a new run of text
  99. *
  100. * @return a new run of text
  101. */
  102. public XSLFTextRun addNewTextRun() {
  103. CTRegularTextRun r = _p.addNewR();
  104. CTTextCharacterProperties rPr = r.addNewRPr();
  105. rPr.setLang("en-US");
  106. XSLFTextRun run = newTextRun(r);
  107. _runs.add(run);
  108. return run;
  109. }
  110. /**
  111. * Remove a text run
  112. *
  113. * @param textRun a run of text
  114. * @return whether the run was removed
  115. * @since POI 5.2.2
  116. */
  117. public boolean removeTextRun(XSLFTextRun textRun) {
  118. if (_runs.remove(textRun)) {
  119. XmlObject xo = textRun.getXmlObject();
  120. if (xo instanceof CTRegularTextRun) {
  121. for (int i = 0; i < getXmlObject().sizeOfRArray(); i++) {
  122. if (getXmlObject().getRArray(i).equals(xo)) {
  123. getXmlObject().removeR(i);
  124. return true;
  125. }
  126. }
  127. } else if (xo instanceof CTTextField) {
  128. for (int i = 0; i < getXmlObject().sizeOfFldArray(); i++) {
  129. if (getXmlObject().getFldArray(i).equals(xo)) {
  130. getXmlObject().removeFld(i);
  131. return true;
  132. }
  133. }
  134. } else if (xo instanceof CTTextLineBreak) {
  135. for (int i = 0; i < getXmlObject().sizeOfBrArray(); i++) {
  136. if (getXmlObject().getBrArray(i).equals(xo)) {
  137. getXmlObject().removeBr(i);
  138. return true;
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Insert a line break
  148. *
  149. * @return text run representing this line break ('\n')
  150. */
  151. @SuppressWarnings("WeakerAccess")
  152. public XSLFTextRun addLineBreak() {
  153. XSLFLineBreak run = new XSLFLineBreak(_p.addNewBr(), this);
  154. CTTextCharacterProperties brProps = run.getRPr(true);
  155. if (!_runs.isEmpty()) {
  156. // by default line break has the font size of the last text run
  157. CTTextCharacterProperties prevRun = _runs.get(_runs.size() - 1).getRPr(true);
  158. brProps.set(prevRun);
  159. // don't copy hlink properties
  160. if (brProps.isSetHlinkClick()) {
  161. brProps.unsetHlinkClick();
  162. }
  163. if (brProps.isSetHlinkMouseOver()) {
  164. brProps.unsetHlinkMouseOver();
  165. }
  166. }
  167. _runs.add(run);
  168. return run;
  169. }
  170. @Override
  171. public TextAlign getTextAlign() {
  172. return fetchParagraphProperty((props,val) -> {
  173. if (props.isSetAlgn()) {
  174. val.accept(TextAlign.values()[props.getAlgn().intValue() - 1]);
  175. }
  176. });
  177. }
  178. @Override
  179. public void setTextAlign(TextAlign align) {
  180. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  181. if(align == null) {
  182. if(pr.isSetAlgn()) {
  183. pr.unsetAlgn();
  184. }
  185. } else {
  186. pr.setAlgn(STTextAlignType.Enum.forInt(align.ordinal() + 1));
  187. }
  188. }
  189. @Override
  190. public FontAlign getFontAlign() {
  191. return fetchParagraphProperty((props,val) -> {
  192. if (props.isSetFontAlgn()) {
  193. val.accept(FontAlign.values()[props.getFontAlgn().intValue() - 1]);
  194. }
  195. });
  196. }
  197. /**
  198. * Specifies the font alignment that is to be applied to the paragraph.
  199. * Possible values for this include auto, top, center, baseline and bottom.
  200. * see {@link org.apache.poi.sl.usermodel.TextParagraph.FontAlign}.
  201. *
  202. * @param align font align
  203. */
  204. @SuppressWarnings("unused")
  205. public void setFontAlign(FontAlign align) {
  206. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  207. if(align == null) {
  208. if(pr.isSetFontAlgn()) {
  209. pr.unsetFontAlgn();
  210. }
  211. } else {
  212. pr.setFontAlgn(STTextFontAlignType.Enum.forInt(align.ordinal() + 1));
  213. }
  214. }
  215. /**
  216. * @return the font to be used on bullet characters within a given paragraph
  217. */
  218. @SuppressWarnings("WeakerAccess")
  219. public String getBulletFont() {
  220. return fetchParagraphProperty((props, val) -> {
  221. if (props.isSetBuFont()) {
  222. val.accept(props.getBuFont().getTypeface());
  223. }
  224. });
  225. }
  226. @SuppressWarnings("WeakerAccess")
  227. public void setBulletFont(String typeface) {
  228. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  229. CTTextFont font = pr.isSetBuFont() ? pr.getBuFont() : pr.addNewBuFont();
  230. font.setTypeface(typeface);
  231. }
  232. /**
  233. * @return the character to be used in place of the standard bullet point
  234. */
  235. @SuppressWarnings("WeakerAccess")
  236. public String getBulletCharacter() {
  237. return fetchParagraphProperty((props, val) -> {
  238. if (props.isSetBuChar()) {
  239. val.accept(props.getBuChar().getChar());
  240. }
  241. });
  242. }
  243. @SuppressWarnings("WeakerAccess")
  244. public void setBulletCharacter(String str) {
  245. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  246. CTTextCharBullet c = pr.isSetBuChar() ? pr.getBuChar() : pr.addNewBuChar();
  247. c.setChar(str);
  248. }
  249. /**
  250. *
  251. * @return the color of bullet characters within a given paragraph.
  252. * A <code>null</code> value means to use the text font color.
  253. */
  254. @SuppressWarnings("WeakerAccess")
  255. public PaintStyle getBulletFontColor() {
  256. Color col = fetchParagraphProperty(this::fetchBulletFontColor);
  257. return (col == null) ? null : DrawPaint.createSolidPaint(col);
  258. }
  259. private void fetchBulletFontColor(CTTextParagraphProperties props, Consumer<Color> val) {
  260. final XSLFSheet sheet = getParentShape().getSheet();
  261. final XSLFTheme theme = sheet.getTheme();
  262. if(props.isSetBuClr()) {
  263. XSLFColor c = new XSLFColor(props.getBuClr(), theme, null, sheet);
  264. val.accept(c.getColor());
  265. }
  266. }
  267. @SuppressWarnings("WeakerAccess")
  268. public void setBulletFontColor(Color color) {
  269. setBulletFontColor(DrawPaint.createSolidPaint(color));
  270. }
  271. /**
  272. * Set the color to be used on bullet characters within a given paragraph.
  273. *
  274. * @param color the bullet color
  275. */
  276. @SuppressWarnings("WeakerAccess")
  277. public void setBulletFontColor(PaintStyle color) {
  278. if (!(color instanceof SolidPaint)) {
  279. throw new IllegalArgumentException("Currently XSLF only supports SolidPaint");
  280. }
  281. // TODO: implement setting bullet color to null
  282. SolidPaint sp = (SolidPaint)color;
  283. Color col = DrawPaint.applyColorTransform(sp.getSolidColor());
  284. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  285. CTColor c = pr.isSetBuClr() ? pr.getBuClr() : pr.addNewBuClr();
  286. CTSRgbColor clr = c.isSetSrgbClr() ? c.getSrgbClr() : c.addNewSrgbClr();
  287. clr.setVal(new byte[]{(byte) col.getRed(), (byte) col.getGreen(), (byte) col.getBlue()});
  288. }
  289. /**
  290. * Returns the bullet size that is to be used within a paragraph.
  291. * This may be specified in two different ways, percentage spacing and font point spacing:
  292. * <p>
  293. * If bulletSize &gt;= 0, then bulletSize is a percentage of the font size.
  294. * If bulletSize &lt; 0, then it specifies the size in points
  295. * </p>
  296. *
  297. * @return the bullet size
  298. */
  299. @SuppressWarnings("WeakerAccess")
  300. public Double getBulletFontSize() {
  301. return fetchParagraphProperty(XSLFTextParagraph::fetchBulletFontSize);
  302. }
  303. private static void fetchBulletFontSize(CTTextParagraphProperties props, Consumer<Double> val) {
  304. if(props.isSetBuSzPct()) {
  305. val.accept(POIXMLUnits.parsePercent(props.getBuSzPct().xgetVal()) * 0.001);
  306. }
  307. if(props.isSetBuSzPts()) {
  308. val.accept( - props.getBuSzPts().getVal() * 0.01);
  309. }
  310. }
  311. /**
  312. * Sets the bullet size that is to be used within a paragraph.
  313. * This may be specified in two different ways, percentage spacing and font point spacing:
  314. * <p>
  315. * If bulletSize &gt;= 0, then bulletSize is a percentage of the font size.
  316. * If bulletSize &lt; 0, then it specifies the size in points
  317. * </p>
  318. */
  319. @SuppressWarnings("WeakerAccess")
  320. public void setBulletFontSize(double bulletSize) {
  321. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  322. if(bulletSize >= 0) {
  323. CTTextBulletSizePercent pt = pr.isSetBuSzPct() ? pr.getBuSzPct() : pr.addNewBuSzPct();
  324. pt.setVal(Integer.toString((int)(bulletSize*1000)));
  325. if(pr.isSetBuSzPts()) {
  326. pr.unsetBuSzPts();
  327. }
  328. } else {
  329. CTTextBulletSizePoint pt = pr.isSetBuSzPts() ? pr.getBuSzPts() : pr.addNewBuSzPts();
  330. pt.setVal((int)(-bulletSize*100));
  331. if(pr.isSetBuSzPct()) {
  332. pr.unsetBuSzPct();
  333. }
  334. }
  335. }
  336. /**
  337. * @return the auto numbering scheme, or null if not defined
  338. */
  339. @SuppressWarnings("WeakerAccess")
  340. public AutoNumberingScheme getAutoNumberingScheme() {
  341. return fetchParagraphProperty(XSLFTextParagraph::fetchAutoNumberingScheme);
  342. }
  343. private static void fetchAutoNumberingScheme(CTTextParagraphProperties props, Consumer<AutoNumberingScheme> val) {
  344. if (props.isSetBuAutoNum()) {
  345. AutoNumberingScheme ans = AutoNumberingScheme.forOoxmlID(props.getBuAutoNum().getType().intValue());
  346. if (ans != null) {
  347. val.accept(ans);
  348. }
  349. }
  350. }
  351. /**
  352. * @return the auto numbering starting number, or null if not defined
  353. */
  354. @SuppressWarnings("WeakerAccess")
  355. public Integer getAutoNumberingStartAt() {
  356. return fetchParagraphProperty((props, val) -> {
  357. if (props.isSetBuAutoNum() && props.getBuAutoNum().isSetStartAt()) {
  358. val.accept(props.getBuAutoNum().getStartAt());
  359. }
  360. });
  361. }
  362. @Override
  363. public void setIndent(Double indent) {
  364. if ((indent == null) && !_p.isSetPPr()) {
  365. return;
  366. }
  367. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  368. if(indent == null) {
  369. if(pr.isSetIndent()) {
  370. pr.unsetIndent();
  371. }
  372. } else {
  373. pr.setIndent(Units.toEMU(indent));
  374. }
  375. }
  376. @Override
  377. public Double getIndent() {
  378. return fetchParagraphProperty((props, val) -> {
  379. if (props.isSetIndent()) {
  380. val.accept(Units.toPoints(props.getIndent()));
  381. }
  382. });
  383. }
  384. @Override
  385. public void setLeftMargin(Double leftMargin) {
  386. if (leftMargin == null && !_p.isSetPPr()) {
  387. return;
  388. }
  389. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  390. if (leftMargin == null) {
  391. if(pr.isSetMarL()) {
  392. pr.unsetMarL();
  393. }
  394. } else {
  395. pr.setMarL(Units.toEMU(leftMargin));
  396. }
  397. }
  398. /**
  399. * @return the left margin (in points) of the paragraph, null if unset
  400. */
  401. @Override
  402. public Double getLeftMargin() {
  403. return fetchParagraphProperty((props, val) -> {
  404. if (props.isSetMarL()) {
  405. val.accept(Units.toPoints(props.getMarL()));
  406. }
  407. });
  408. }
  409. @Override
  410. public void setRightMargin(Double rightMargin) {
  411. if (rightMargin == null && !_p.isSetPPr()) {
  412. return;
  413. }
  414. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  415. if(rightMargin == null) {
  416. if(pr.isSetMarR()) {
  417. pr.unsetMarR();
  418. }
  419. } else {
  420. pr.setMarR(Units.toEMU(rightMargin));
  421. }
  422. }
  423. /**
  424. *
  425. * @return the right margin of the paragraph, null if unset
  426. */
  427. @Override
  428. public Double getRightMargin() {
  429. return fetchParagraphProperty((props, val) -> {
  430. if (props.isSetMarR()) {
  431. val.accept(Units.toPoints(props.getMarR()));
  432. }
  433. });
  434. }
  435. @Override
  436. public Double getDefaultTabSize() {
  437. return fetchParagraphProperty((props, val) -> {
  438. if (props.isSetDefTabSz()) {
  439. val.accept(Units.toPoints(POIXMLUnits.parseLength(props.xgetDefTabSz())));
  440. }
  441. });
  442. }
  443. @SuppressWarnings("WeakerAccess")
  444. public double getTabStop(final int idx) {
  445. Double d = fetchParagraphProperty((props,val) -> fetchTabStop(idx,props,val));
  446. return (d == null) ? 0. : d;
  447. }
  448. private static void fetchTabStop(final int idx, CTTextParagraphProperties props, Consumer<Double> val) {
  449. if (props.isSetTabLst()) {
  450. CTTextTabStopList tabStops = props.getTabLst();
  451. if(idx < tabStops.sizeOfTabArray() ) {
  452. CTTextTabStop ts = tabStops.getTabArray(idx);
  453. val.accept(Units.toPoints(POIXMLUnits.parseLength(ts.xgetPos())));
  454. }
  455. }
  456. }
  457. @SuppressWarnings("WeakerAccess")
  458. public void addTabStop(double value) {
  459. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  460. CTTextTabStopList tabStops = pr.isSetTabLst() ? pr.getTabLst() : pr.addNewTabLst();
  461. tabStops.addNewTab().setPos(Units.toEMU(value));
  462. }
  463. @Override
  464. public void setLineSpacing(Double lineSpacing) {
  465. setSpacing(lineSpacing, props -> props::getLnSpc, props -> props::addNewLnSpc, props -> props::unsetLnSpc);
  466. }
  467. @Override
  468. public Double getLineSpacing() {
  469. final Double lnSpc = getSpacing(props -> props::getLnSpc);
  470. if (lnSpc != null && lnSpc > 0) {
  471. // check if the percentage value is scaled
  472. final CTTextNormalAutofit normAutofit = getParentShape().getTextBodyPr().getNormAutofit();
  473. if (normAutofit != null) {
  474. final double scale = 1 - POIXMLUnits.parsePercent(normAutofit.xgetLnSpcReduction()) / 100_000.;
  475. return lnSpc * scale;
  476. }
  477. }
  478. return lnSpc;
  479. }
  480. @Override
  481. public void setSpaceBefore(Double spaceBefore) {
  482. setSpacing(spaceBefore, props -> props::getSpcBef, props -> props::addNewSpcBef, props -> props::unsetSpcBef);
  483. }
  484. @Override
  485. public Double getSpaceBefore() {
  486. return getSpacing(props -> props::getSpcBef);
  487. }
  488. @Override
  489. public void setSpaceAfter(Double spaceAfter) {
  490. setSpacing(spaceAfter, props -> props::getSpcAft, props -> props::addNewSpcAft, props -> props::unsetSpcAft);
  491. }
  492. @Override
  493. public Double getSpaceAfter() {
  494. return getSpacing(props -> props::getSpcAft);
  495. }
  496. private void setSpacing(final Double space,
  497. final Function<CTTextParagraphProperties,Supplier<CTTextSpacing>> getSpc,
  498. final Function<CTTextParagraphProperties,Supplier<CTTextSpacing>> addSpc,
  499. final Function<CTTextParagraphProperties,Procedure> unsetSpc
  500. ) {
  501. final CTTextParagraphProperties pPr = (space == null || _p.isSetPPr()) ? _p.getPPr() : _p.addNewPPr();
  502. if (pPr == null) {
  503. return;
  504. }
  505. CTTextSpacing spc = getSpc.apply(pPr).get();
  506. if (space == null) {
  507. if (spc != null) {
  508. // unset the space before on null input
  509. unsetSpc.apply(pPr).accept();
  510. }
  511. return;
  512. }
  513. if (spc == null) {
  514. spc = addSpc.apply(pPr).get();
  515. }
  516. if (space >= 0) {
  517. if (spc.isSetSpcPts()) {
  518. spc.unsetSpcPts();
  519. }
  520. final CTTextSpacingPercent pct = spc.isSetSpcPct() ? spc.getSpcPct() : spc.addNewSpcPct();
  521. pct.setVal((int)(space*1000));
  522. } else {
  523. if (spc.isSetSpcPct()) {
  524. spc.unsetSpcPct();
  525. }
  526. final CTTextSpacingPoint pts = spc.isSetSpcPts() ? spc.getSpcPts() : spc.addNewSpcPts();
  527. pts.setVal((int)(-space*100));
  528. }
  529. }
  530. private Double getSpacing(final Function<CTTextParagraphProperties,Supplier<CTTextSpacing>> getSpc) {
  531. return fetchParagraphProperty((props,val) -> fetchSpacing(getSpc,props,val));
  532. }
  533. private static void fetchSpacing(final Function<CTTextParagraphProperties,Supplier<CTTextSpacing>> getSpc,
  534. CTTextParagraphProperties props, Consumer<Double> val) {
  535. final CTTextSpacing spc = getSpc.apply(props).get();
  536. if (spc != null) {
  537. if (spc.isSetSpcPct()) {
  538. val.accept( POIXMLUnits.parsePercent(spc.getSpcPct().xgetVal())*0.001 );
  539. } else if (spc.isSetSpcPts()) {
  540. val.accept( -spc.getSpcPts().getVal()*0.01 );
  541. }
  542. }
  543. }
  544. @Override
  545. public void setIndentLevel(int level) {
  546. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  547. pr.setLvl(level);
  548. }
  549. @Override
  550. public int getIndentLevel() {
  551. CTTextParagraphProperties pr = _p.getPPr();
  552. return (pr == null || !pr.isSetLvl()) ? 0 : pr.getLvl();
  553. }
  554. /**
  555. * Returns whether this paragraph has bullets
  556. */
  557. public boolean isBullet() {
  558. Boolean b = fetchParagraphProperty(XSLFTextParagraph::fetchIsBullet);
  559. return b == null ? false : b;
  560. }
  561. private static void fetchIsBullet(CTTextParagraphProperties props, Consumer<Boolean> val) {
  562. if (props.isSetBuNone()) {
  563. val.accept(false);
  564. } else if(props.isSetBuFont() || props.isSetBuChar()) {
  565. val.accept(true);
  566. }
  567. }
  568. /**
  569. *
  570. * @param flag whether text in this paragraph has bullets
  571. */
  572. public void setBullet(boolean flag) {
  573. if(isBullet() == flag) {
  574. return;
  575. }
  576. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  577. if(flag) {
  578. pr.addNewBuFont().setTypeface("Arial");
  579. pr.addNewBuChar().setChar("\u2022");
  580. } else {
  581. if (pr.isSetBuFont()) {
  582. pr.unsetBuFont();
  583. }
  584. if (pr.isSetBuChar()) {
  585. pr.unsetBuChar();
  586. }
  587. if (pr.isSetBuAutoNum()) {
  588. pr.unsetBuAutoNum();
  589. }
  590. if (pr.isSetBuBlip()) {
  591. pr.unsetBuBlip();
  592. }
  593. if (pr.isSetBuClr()) {
  594. pr.unsetBuClr();
  595. }
  596. if (pr.isSetBuClrTx()) {
  597. pr.unsetBuClrTx();
  598. }
  599. if (pr.isSetBuFont()) {
  600. pr.unsetBuFont();
  601. }
  602. if (pr.isSetBuFontTx()) {
  603. pr.unsetBuFontTx();
  604. }
  605. if (pr.isSetBuSzPct()) {
  606. pr.unsetBuSzPct();
  607. }
  608. if (pr.isSetBuSzPts()) {
  609. pr.unsetBuSzPts();
  610. }
  611. if (pr.isSetBuSzTx()) {
  612. pr.unsetBuSzTx();
  613. }
  614. pr.addNewBuNone();
  615. }
  616. }
  617. /**
  618. * Specifies that automatic numbered bullet points should be applied to this paragraph
  619. *
  620. * @param scheme type of auto-numbering
  621. * @param startAt the number that will start number for a given sequence of automatically
  622. numbered bullets (1-based).
  623. */
  624. @SuppressWarnings("WeakerAccess")
  625. public void setBulletAutoNumber(AutoNumberingScheme scheme, int startAt) {
  626. if(startAt < 1) {
  627. throw new IllegalArgumentException("Start Number must be greater or equal that 1") ;
  628. }
  629. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  630. CTTextAutonumberBullet lst = pr.isSetBuAutoNum() ? pr.getBuAutoNum() : pr.addNewBuAutoNum();
  631. lst.setType(STTextAutonumberScheme.Enum.forInt(scheme.ooxmlId));
  632. lst.setStartAt(startAt);
  633. }
  634. @Override
  635. public String toString() {
  636. return "[" + getClass() + "]" + getText();
  637. }
  638. /**
  639. * @return master style text paragraph properties, or <code>null</code> if
  640. * there are no master slides or the master slides do not contain a text paragraph
  641. */
  642. @Internal
  643. public CTTextParagraphProperties getDefaultMasterStyle() {
  644. CTPlaceholder ph = _shape.getPlaceholderDetails().getCTPlaceholder(false);
  645. String defaultStyleSelector;
  646. switch(ph == null ? -1 : ph.getType().intValue()) {
  647. case STPlaceholderType.INT_TITLE:
  648. case STPlaceholderType.INT_CTR_TITLE:
  649. defaultStyleSelector = "titleStyle";
  650. break;
  651. case -1: // no placeholder means plain text box
  652. case STPlaceholderType.INT_FTR:
  653. case STPlaceholderType.INT_SLD_NUM:
  654. case STPlaceholderType.INT_DT:
  655. defaultStyleSelector = "otherStyle";
  656. break;
  657. default:
  658. defaultStyleSelector = "bodyStyle";
  659. break;
  660. }
  661. int level = getIndentLevel();
  662. // wind up and find the root master sheet which must be slide master
  663. final String nsPML = "http://schemas.openxmlformats.org/presentationml/2006/main";
  664. XSLFSheet masterSheet = _shape.getSheet();
  665. for (XSLFSheet m = masterSheet; m != null; m = (XSLFSheet)m.getMasterSheet()) {
  666. masterSheet = m;
  667. XmlObject xo = masterSheet.getXmlObject();
  668. try (XmlCursor cur = xo.newCursor()) {
  669. cur.push();
  670. if ((cur.toChild(nsPML, "txStyles") && cur.toChild(nsPML, defaultStyleSelector)) ||
  671. (cur.pop() && cur.toChild(nsPML, "notesStyle"))) {
  672. while (level >= 0) {
  673. cur.push();
  674. if (cur.toChild(XSLFRelation.NS_DRAWINGML, "lvl" +(level+1)+ "pPr")) {
  675. return (CTTextParagraphProperties)cur.getObject();
  676. }
  677. cur.pop();
  678. level--;
  679. }
  680. }
  681. }
  682. }
  683. return null;
  684. }
  685. private <T> T fetchParagraphProperty(ParaPropFetcher<T> fetcher) {
  686. final XSLFTextShape shape = getParentShape();
  687. return new ParagraphPropertyFetcher<>(this, fetcher).fetchProperty(shape);
  688. }
  689. void copy(XSLFTextParagraph other) {
  690. if (other == this) {
  691. return;
  692. }
  693. CTTextParagraph thisP = getXmlObject();
  694. CTTextParagraph otherP = other.getXmlObject();
  695. if (thisP.isSetPPr()) {
  696. thisP.unsetPPr();
  697. }
  698. if (thisP.isSetEndParaRPr()) {
  699. thisP.unsetEndParaRPr();
  700. }
  701. _runs.clear();
  702. for (int i=thisP.sizeOfBrArray(); i>0; i--) {
  703. thisP.removeBr(i-1);
  704. }
  705. for (int i=thisP.sizeOfRArray(); i>0; i--) {
  706. thisP.removeR(i-1);
  707. }
  708. for (int i=thisP.sizeOfFldArray(); i>0; i--) {
  709. thisP.removeFld(i-1);
  710. }
  711. for (XSLFTextRun tr : other.getTextRuns()) {
  712. XmlObject xo = tr.getXmlObject().copy();
  713. XSLFTextRun run = addNewTextRun();
  714. run.getXmlObject().set(xo);
  715. run.copy(tr);
  716. }
  717. // set properties again, in case we are based on a different
  718. // template
  719. TextAlign srcAlign = other.getTextAlign();
  720. if(srcAlign != getTextAlign()) {
  721. setTextAlign(srcAlign);
  722. }
  723. boolean isBullet = other.isBullet();
  724. if(isBullet != isBullet()) {
  725. setBullet(isBullet);
  726. if(isBullet) {
  727. String buFont = other.getBulletFont();
  728. if(buFont != null && !buFont.equals(getBulletFont())) {
  729. setBulletFont(buFont);
  730. }
  731. String buChar = other.getBulletCharacter();
  732. if(buChar != null && !buChar.equals(getBulletCharacter())) {
  733. setBulletCharacter(buChar);
  734. }
  735. PaintStyle buColor = other.getBulletFontColor();
  736. if(buColor != null && !buColor.equals(getBulletFontColor())) {
  737. setBulletFontColor(buColor);
  738. }
  739. Double buSize = other.getBulletFontSize();
  740. if(doubleNotEquals(buSize, getBulletFontSize())) {
  741. setBulletFontSize(buSize);
  742. }
  743. }
  744. }
  745. Double leftMargin = other.getLeftMargin();
  746. if (doubleNotEquals(leftMargin, getLeftMargin())) {
  747. setLeftMargin(leftMargin);
  748. }
  749. Double indent = other.getIndent();
  750. if (doubleNotEquals(indent, getIndent())) {
  751. setIndent(indent);
  752. }
  753. Double spaceAfter = other.getSpaceAfter();
  754. if (doubleNotEquals(spaceAfter, getSpaceAfter())) {
  755. setSpaceAfter(spaceAfter);
  756. }
  757. Double spaceBefore = other.getSpaceBefore();
  758. if (doubleNotEquals(spaceBefore, getSpaceBefore())) {
  759. setSpaceBefore(spaceBefore);
  760. }
  761. Double lineSpacing = other.getLineSpacing();
  762. if (doubleNotEquals(lineSpacing, getLineSpacing())) {
  763. setLineSpacing(lineSpacing);
  764. }
  765. }
  766. private static boolean doubleNotEquals(Double d1, Double d2) {
  767. return !Objects.equals(d1, d2);
  768. }
  769. @Override
  770. public Double getDefaultFontSize() {
  771. CTTextCharacterProperties endPr = _p.getEndParaRPr();
  772. if (endPr == null || !endPr.isSetSz()) {
  773. // inherit the font size from the master style
  774. CTTextParagraphProperties masterStyle = getDefaultMasterStyle();
  775. if (masterStyle != null) {
  776. endPr = masterStyle.getDefRPr();
  777. }
  778. }
  779. return (endPr == null || !endPr.isSetSz()) ? 12 : (endPr.getSz() / 100.);
  780. }
  781. @Override
  782. public String getDefaultFontFamily() {
  783. String family = (_runs.isEmpty() ? null : _runs.get(0).getFontFamily());
  784. return (family == null) ? "Arial" : family;
  785. }
  786. @Override
  787. public BulletStyle getBulletStyle() {
  788. if (!isBullet()) {
  789. return null;
  790. }
  791. return new BulletStyle() {
  792. @Override
  793. public String getBulletCharacter() {
  794. return XSLFTextParagraph.this.getBulletCharacter();
  795. }
  796. @Override
  797. public String getBulletFont() {
  798. return XSLFTextParagraph.this.getBulletFont();
  799. }
  800. @Override
  801. public Double getBulletFontSize() {
  802. return XSLFTextParagraph.this.getBulletFontSize();
  803. }
  804. @Override
  805. public PaintStyle getBulletFontColor() {
  806. return XSLFTextParagraph.this.getBulletFontColor();
  807. }
  808. @Override
  809. public void setBulletFontColor(Color color) {
  810. setBulletFontColor(DrawPaint.createSolidPaint(color));
  811. }
  812. @Override
  813. public void setBulletFontColor(PaintStyle color) {
  814. XSLFTextParagraph.this.setBulletFontColor(color);
  815. }
  816. @Override
  817. public AutoNumberingScheme getAutoNumberingScheme() {
  818. return XSLFTextParagraph.this.getAutoNumberingScheme();
  819. }
  820. @Override
  821. public Integer getAutoNumberingStartAt() {
  822. return XSLFTextParagraph.this.getAutoNumberingStartAt();
  823. }
  824. };
  825. }
  826. @Override
  827. public void setBulletStyle(Object... styles) {
  828. if (styles.length == 0) {
  829. setBullet(false);
  830. } else {
  831. setBullet(true);
  832. for (Object ostyle : styles) {
  833. if (ostyle instanceof Number) {
  834. setBulletFontSize(((Number)ostyle).doubleValue());
  835. } else if (ostyle instanceof Color) {
  836. setBulletFontColor((Color)ostyle);
  837. } else if (ostyle instanceof Character) {
  838. setBulletCharacter(ostyle.toString());
  839. } else if (ostyle instanceof String) {
  840. setBulletFont((String)ostyle);
  841. } else if (ostyle instanceof AutoNumberingScheme) {
  842. setBulletAutoNumber((AutoNumberingScheme)ostyle, 0);
  843. }
  844. }
  845. }
  846. }
  847. @Override
  848. public List<XSLFTabStop> getTabStops() {
  849. return fetchParagraphProperty(XSLFTextParagraph::fetchTabStops);
  850. }
  851. private static void fetchTabStops(CTTextParagraphProperties props, Consumer<List<XSLFTabStop>> val) {
  852. if (props.isSetTabLst()) {
  853. final List<XSLFTabStop> list = new ArrayList<>();
  854. //noinspection deprecation
  855. for (final CTTextTabStop ta : props.getTabLst().getTabArray()) {
  856. list.add(new XSLFTabStop(ta));
  857. }
  858. val.accept(list);
  859. }
  860. }
  861. @Override
  862. public void addTabStops(double positionInPoints, TabStopType tabStopType) {
  863. final XSLFSheet sheet = getParentShape().getSheet();
  864. final CTTextParagraphProperties tpp;
  865. if (sheet instanceof XSLFSlideMaster) {
  866. tpp = getDefaultMasterStyle();
  867. } else {
  868. final CTTextParagraph xo = getXmlObject();
  869. tpp = (xo.isSetPPr()) ? xo.getPPr() : xo.addNewPPr();
  870. }
  871. if (tpp == null) {
  872. return;
  873. }
  874. final CTTextTabStopList stl = (tpp.isSetTabLst()) ? tpp.getTabLst() : tpp.addNewTabLst();
  875. XSLFTabStop tab = new XSLFTabStop(stl.addNewTab());
  876. tab.setPositionInPoints(positionInPoints);
  877. tab.setType(tabStopType);
  878. }
  879. @Override
  880. public void clearTabStops() {
  881. final XSLFSheet sheet = getParentShape().getSheet();
  882. CTTextParagraphProperties tpp = (sheet instanceof XSLFSlideMaster) ? getDefaultMasterStyle() : getXmlObject().getPPr();
  883. if (tpp != null && tpp.isSetTabLst()) {
  884. tpp.unsetTabLst();
  885. }
  886. }
  887. /**
  888. * Helper method for appending text and keeping paragraph and character properties.
  889. * The character properties are moved to the end paragraph marker
  890. */
  891. /* package */ void clearButKeepProperties() {
  892. CTTextParagraph thisP = getXmlObject();
  893. for (int i=thisP.sizeOfBrArray(); i>0; i--) {
  894. thisP.removeBr(i-1);
  895. }
  896. for (int i=thisP.sizeOfFldArray(); i>0; i--) {
  897. thisP.removeFld(i-1);
  898. }
  899. if (!_runs.isEmpty()) {
  900. int size = _runs.size();
  901. XSLFTextRun lastRun = _runs.get(size-1);
  902. CTTextCharacterProperties cpOther = lastRun.getRPr(false);
  903. if (cpOther != null) {
  904. if (thisP.isSetEndParaRPr()) {
  905. thisP.unsetEndParaRPr();
  906. }
  907. CTTextCharacterProperties cp = thisP.addNewEndParaRPr();
  908. cp.set(cpOther);
  909. }
  910. for (int i=size; i>0; i--) {
  911. thisP.removeR(i-1);
  912. }
  913. _runs.clear();
  914. }
  915. }
  916. @Override
  917. public boolean isHeaderOrFooter() {
  918. CTPlaceholder ph = _shape.getPlaceholderDetails().getCTPlaceholder(false);
  919. int phId = (ph == null ? -1 : ph.getType().intValue());
  920. switch (phId) {
  921. case STPlaceholderType.INT_SLD_NUM:
  922. case STPlaceholderType.INT_DT:
  923. case STPlaceholderType.INT_FTR:
  924. case STPlaceholderType.INT_HDR:
  925. return true;
  926. default:
  927. return false;
  928. }
  929. }
  930. /**
  931. * Helper method to allow subclasses to provide their own text run
  932. *
  933. * @param r the xml reference
  934. *
  935. * @return a new text paragraph
  936. *
  937. * @since POI 3.15-beta2
  938. */
  939. protected XSLFTextRun newTextRun(XmlObject r) {
  940. return new XSLFTextRun(r, this);
  941. }
  942. @SuppressWarnings("WeakerAccess")
  943. protected XSLFTextRun newTextRun(CTTextLineBreak r) {
  944. return new XSLFLineBreak(r, this);
  945. }
  946. }