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

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