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

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