Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

XSLFTextParagraph.java 38KB

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