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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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.hslf.usermodel;
  16. import static org.apache.poi.hslf.record.RecordTypes.OEPlaceholderAtom;
  17. import static org.apache.poi.hslf.record.RecordTypes.RoundTripHFPlaceholder12;
  18. import java.awt.Graphics2D;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.apache.poi.ddf.AbstractEscherOptRecord;
  25. import org.apache.poi.ddf.EscherContainerRecord;
  26. import org.apache.poi.ddf.EscherProperties;
  27. import org.apache.poi.ddf.EscherSimpleProperty;
  28. import org.apache.poi.ddf.EscherTextboxRecord;
  29. import org.apache.poi.hslf.exceptions.HSLFException;
  30. import org.apache.poi.hslf.model.HSLFMetroShape;
  31. import org.apache.poi.hslf.model.textproperties.TextPropCollection;
  32. import org.apache.poi.hslf.record.EscherTextboxWrapper;
  33. import org.apache.poi.hslf.record.OEPlaceholderAtom;
  34. import org.apache.poi.hslf.record.PPDrawing;
  35. import org.apache.poi.hslf.record.RoundTripHFPlaceholder12;
  36. import org.apache.poi.hslf.record.StyleTextPropAtom;
  37. import org.apache.poi.hslf.record.TextBytesAtom;
  38. import org.apache.poi.hslf.record.TextCharsAtom;
  39. import org.apache.poi.hslf.record.TextHeaderAtom;
  40. import org.apache.poi.sl.draw.DrawFactory;
  41. import org.apache.poi.sl.draw.DrawTextShape;
  42. import org.apache.poi.sl.usermodel.Insets2D;
  43. import org.apache.poi.sl.usermodel.Placeholder;
  44. import org.apache.poi.sl.usermodel.ShapeContainer;
  45. import org.apache.poi.sl.usermodel.TextParagraph;
  46. import org.apache.poi.sl.usermodel.TextRun;
  47. import org.apache.poi.sl.usermodel.TextShape;
  48. import org.apache.poi.sl.usermodel.VerticalAlignment;
  49. import org.apache.poi.util.POILogFactory;
  50. import org.apache.poi.util.POILogger;
  51. import org.apache.poi.util.Units;
  52. /**
  53. * A common superclass of all shapes that can hold text.
  54. */
  55. public abstract class HSLFTextShape extends HSLFSimpleShape
  56. implements TextShape<HSLFShape,HSLFTextParagraph> {
  57. private static final POILogger LOG = POILogFactory.getLogger(HSLFTextShape.class);
  58. /**
  59. * How to anchor the text
  60. */
  61. private enum HSLFTextAnchor {
  62. TOP (0, VerticalAlignment.TOP, false, false),
  63. MIDDLE (1, VerticalAlignment.MIDDLE, false, false),
  64. BOTTOM (2, VerticalAlignment.BOTTOM, false, false),
  65. TOP_CENTER (3, VerticalAlignment.TOP, true, false),
  66. MIDDLE_CENTER (4, VerticalAlignment.MIDDLE, true, false),
  67. BOTTOM_CENTER (5, VerticalAlignment.BOTTOM, true, false),
  68. TOP_BASELINE (6, VerticalAlignment.TOP, false, true),
  69. BOTTOM_BASELINE (7, VerticalAlignment.BOTTOM, false, true),
  70. TOP_CENTER_BASELINE (8, VerticalAlignment.TOP, true, true),
  71. BOTTOM_CENTER_BASELINE(9, VerticalAlignment.BOTTOM, true, true);
  72. public final int nativeId;
  73. public final VerticalAlignment vAlign;
  74. public final boolean centered;
  75. public final Boolean baseline;
  76. HSLFTextAnchor(int nativeId, VerticalAlignment vAlign, boolean centered, Boolean baseline) {
  77. this.nativeId = nativeId;
  78. this.vAlign = vAlign;
  79. this.centered = centered;
  80. this.baseline = baseline;
  81. }
  82. static HSLFTextAnchor fromNativeId(int nativeId) {
  83. for (HSLFTextAnchor ta : values()) {
  84. if (ta.nativeId == nativeId) {
  85. return ta;
  86. }
  87. }
  88. return null;
  89. }
  90. }
  91. /**
  92. * Specifies that a line of text will continue on subsequent lines instead
  93. * of extending into or beyond a margin.
  94. * Office Excel 2007, Excel 2010, PowerPoint 97, and PowerPoint 2010 read
  95. * and use this value properly but do not write it.
  96. */
  97. public static final int WrapSquare = 0;
  98. /**
  99. * Specifies a wrapping rule that is equivalent to that of WrapSquare
  100. * Excel 97, Excel 2000, Excel 2002, and Office Excel 2003 use this value.
  101. * All other product versions listed at the beginning of this appendix ignore this value.
  102. */
  103. public static final int WrapByPoints = 1;
  104. /**
  105. * Specifies that a line of text will extend into or beyond a margin instead
  106. * of continuing on subsequent lines.
  107. * Excel 97, Word 97, Excel 2000, Word 2000, Excel 2002,
  108. * and Office Excel 2003 do not use this value.
  109. */
  110. public static final int WrapNone = 2;
  111. /**
  112. * Specifies a wrapping rule that is undefined and MUST be ignored.
  113. */
  114. public static final int WrapTopBottom = 3;
  115. /**
  116. * Specifies a wrapping rule that is undefined and MUST be ignored.
  117. */
  118. public static final int WrapThrough = 4;
  119. /**
  120. * TextRun object which holds actual text and format data
  121. */
  122. private List<HSLFTextParagraph> _paragraphs = new ArrayList<HSLFTextParagraph>();
  123. /**
  124. * Escher container which holds text attributes such as
  125. * TextHeaderAtom, TextBytesAtom or TextCharsAtom, StyleTextPropAtom etc.
  126. */
  127. private EscherTextboxWrapper _txtbox;
  128. /**
  129. * This setting is used for supporting a deprecated alignment
  130. *
  131. * @see <a href=""></a>
  132. */
  133. // boolean alignToBaseline = false;
  134. /**
  135. * Create a TextBox object and initialize it from the supplied Record container.
  136. *
  137. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  138. * @param parent the parent of the shape
  139. */
  140. protected HSLFTextShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  141. super(escherRecord, parent);
  142. }
  143. /**
  144. * Create a new TextBox. This constructor is used when a new shape is created.
  145. *
  146. * @param parent the parent of this Shape. For example, if this text box is a cell
  147. * in a table then the parent is Table.
  148. */
  149. public HSLFTextShape(ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  150. super(null, parent);
  151. createSpContainer(parent instanceof HSLFGroupShape);
  152. }
  153. /**
  154. * Create a new TextBox. This constructor is used when a new shape is created.
  155. *
  156. */
  157. public HSLFTextShape(){
  158. this(null);
  159. }
  160. /**
  161. * Set default properties for the TextRun.
  162. * Depending on the text and shape type the defaults are different:
  163. * TextBox: align=left, valign=top
  164. * AutoShape: align=center, valign=middle
  165. *
  166. */
  167. protected void setDefaultTextProperties(HSLFTextParagraph _txtrun){
  168. }
  169. /**
  170. * When a textbox is added to a sheet we need to tell upper-level
  171. * <code>PPDrawing</code> about it.
  172. *
  173. * @param sh the sheet we are adding to
  174. */
  175. @Override
  176. protected void afterInsert(HSLFSheet sh){
  177. super.afterInsert(sh);
  178. storeText();
  179. EscherTextboxWrapper thisTxtbox = getEscherTextboxWrapper();
  180. if(thisTxtbox != null){
  181. getSpContainer().addChildRecord(thisTxtbox.getEscherRecord());
  182. PPDrawing ppdrawing = sh.getPPDrawing();
  183. ppdrawing.addTextboxWrapper(thisTxtbox);
  184. // Ensure the escher layer knows about the added records
  185. try {
  186. thisTxtbox.writeOut(null);
  187. } catch (IOException e){
  188. throw new HSLFException(e);
  189. }
  190. boolean isInitialAnchor = getAnchor().equals(new Rectangle2D.Double());
  191. boolean isFilledTxt = !"".equals(getText());
  192. if (isInitialAnchor && isFilledTxt) {
  193. resizeToFitText();
  194. }
  195. }
  196. for (HSLFTextParagraph htp : _paragraphs) {
  197. htp.setShapeId(getShapeId());
  198. }
  199. sh.onAddTextShape(this);
  200. }
  201. protected EscherTextboxWrapper getEscherTextboxWrapper(){
  202. if(_txtbox != null) {
  203. return _txtbox;
  204. }
  205. EscherTextboxRecord textRecord = getEscherChild(EscherTextboxRecord.RECORD_ID);
  206. if (textRecord == null) {
  207. return null;
  208. }
  209. HSLFSheet sheet = getSheet();
  210. if (sheet != null) {
  211. PPDrawing drawing = sheet.getPPDrawing();
  212. if (drawing != null) {
  213. EscherTextboxWrapper wrappers[] = drawing.getTextboxWrappers();
  214. if (wrappers != null) {
  215. for (EscherTextboxWrapper w : wrappers) {
  216. // check for object identity
  217. if (textRecord == w.getEscherRecord()) {
  218. _txtbox = w;
  219. return _txtbox;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. _txtbox = new EscherTextboxWrapper(textRecord);
  226. return _txtbox;
  227. }
  228. private void createEmptyParagraph() {
  229. TextHeaderAtom tha = (TextHeaderAtom)_txtbox.findFirstOfType(TextHeaderAtom._type);
  230. if (tha == null) {
  231. tha = new TextHeaderAtom();
  232. tha.setParentRecord(_txtbox);
  233. _txtbox.appendChildRecord(tha);
  234. }
  235. TextBytesAtom tba = (TextBytesAtom)_txtbox.findFirstOfType(TextBytesAtom._type);
  236. TextCharsAtom tca = (TextCharsAtom)_txtbox.findFirstOfType(TextCharsAtom._type);
  237. if (tba == null && tca == null) {
  238. tba = new TextBytesAtom();
  239. tba.setText(new byte[0]);
  240. _txtbox.appendChildRecord(tba);
  241. }
  242. final String text = ((tba != null) ? tba.getText() : tca.getText());
  243. StyleTextPropAtom sta = (StyleTextPropAtom)_txtbox.findFirstOfType(StyleTextPropAtom._type);
  244. TextPropCollection paraStyle = null, charStyle = null;
  245. if (sta == null) {
  246. int parSiz = text.length();
  247. sta = new StyleTextPropAtom(parSiz+1);
  248. if (_paragraphs.isEmpty()) {
  249. paraStyle = sta.addParagraphTextPropCollection(parSiz+1);
  250. charStyle = sta.addCharacterTextPropCollection(parSiz+1);
  251. } else {
  252. for (HSLFTextParagraph htp : _paragraphs) {
  253. int runsLen = 0;
  254. for (HSLFTextRun htr : htp.getTextRuns()) {
  255. runsLen += htr.getLength();
  256. charStyle = sta.addCharacterTextPropCollection(htr.getLength());
  257. htr.setCharacterStyle(charStyle);
  258. }
  259. paraStyle = sta.addParagraphTextPropCollection(runsLen);
  260. htp.setParagraphStyle(paraStyle);
  261. }
  262. assert (paraStyle != null && charStyle != null);
  263. }
  264. _txtbox.appendChildRecord(sta);
  265. } else {
  266. paraStyle = sta.getParagraphStyles().get(0);
  267. charStyle = sta.getCharacterStyles().get(0);
  268. }
  269. if (_paragraphs.isEmpty()) {
  270. HSLFTextParagraph htp = new HSLFTextParagraph(tha, tba, tca, _paragraphs);
  271. htp.setParagraphStyle(paraStyle);
  272. htp.setParentShape(this);
  273. _paragraphs.add(htp);
  274. HSLFTextRun htr = new HSLFTextRun(htp);
  275. htr.setCharacterStyle(charStyle);
  276. htr.setText(text);
  277. htp.addTextRun(htr);
  278. }
  279. }
  280. @Override
  281. public Rectangle2D resizeToFitText() {
  282. return resizeToFitText(null);
  283. }
  284. @Override
  285. public Rectangle2D resizeToFitText(Graphics2D graphics) {
  286. Rectangle2D anchor = getAnchor();
  287. if(anchor.getWidth() == 0.) {
  288. LOG.log(POILogger.WARN, "Width of shape wasn't set. Defaulting to 200px");
  289. anchor.setRect(anchor.getX(), anchor.getY(), 200., anchor.getHeight());
  290. setAnchor(anchor);
  291. }
  292. double height = getTextHeight(graphics);
  293. height += 1; // add a pixel to compensate rounding errors
  294. Insets2D insets = getInsets();
  295. anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), height+insets.top+insets.bottom);
  296. setAnchor(anchor);
  297. return anchor;
  298. }
  299. /**
  300. * Returns the type of the text, from the TextHeaderAtom.
  301. * Possible values can be seen from TextHeaderAtom
  302. * @see org.apache.poi.hslf.record.TextHeaderAtom
  303. */
  304. public int getRunType() {
  305. getEscherTextboxWrapper();
  306. if (_txtbox == null) {
  307. return -1;
  308. }
  309. List<HSLFTextParagraph> paras = HSLFTextParagraph.findTextParagraphs(_txtbox, getSheet());
  310. return (paras.isEmpty()) ? -1 : paras.get(0).getRunType();
  311. }
  312. /**
  313. * Changes the type of the text. Values should be taken
  314. * from TextHeaderAtom. No checking is done to ensure you
  315. * set this to a valid value!
  316. * @see org.apache.poi.hslf.record.TextHeaderAtom
  317. */
  318. public void setRunType(int type) {
  319. getEscherTextboxWrapper();
  320. if (_txtbox == null) {
  321. return;
  322. }
  323. List<HSLFTextParagraph> paras = HSLFTextParagraph.findTextParagraphs(_txtbox, getSheet());
  324. if (!paras.isEmpty()) {
  325. paras.get(0).setRunType(type);
  326. }
  327. }
  328. /**
  329. * Returns the type of vertical alignment for the text.
  330. * One of the <code>Anchor*</code> constants defined in this class.
  331. *
  332. * @return the type of alignment
  333. */
  334. /* package */ HSLFTextAnchor getAlignment(){
  335. AbstractEscherOptRecord opt = getEscherOptRecord();
  336. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.TEXT__ANCHORTEXT);
  337. HSLFTextAnchor align = HSLFTextAnchor.TOP;
  338. if (prop == null){
  339. /**
  340. * If vertical alignment was not found in the shape properties then try to
  341. * fetch the master shape and search for the align property there.
  342. */
  343. int type = getRunType();
  344. HSLFSheet sh = getSheet();
  345. HSLFMasterSheet master = (sh != null) ? sh.getMasterSheet() : null;
  346. HSLFTextShape masterShape = (master != null) ? master.getPlaceholderByTextType(type) : null;
  347. if (masterShape != null && type != TextHeaderAtom.OTHER_TYPE) {
  348. align = masterShape.getAlignment();
  349. } else {
  350. //not found in the master sheet. Use the hardcoded defaults.
  351. switch (type){
  352. case TextHeaderAtom.TITLE_TYPE:
  353. case TextHeaderAtom.CENTER_TITLE_TYPE:
  354. align = HSLFTextAnchor.MIDDLE;
  355. break;
  356. default:
  357. align = HSLFTextAnchor.TOP;
  358. break;
  359. }
  360. }
  361. } else {
  362. align = HSLFTextAnchor.fromNativeId(prop.getPropertyValue());
  363. }
  364. if (align == null) {
  365. align = HSLFTextAnchor.TOP;
  366. }
  367. return align;
  368. }
  369. /**
  370. * Sets the type of alignment for the text.
  371. * One of the <code>Anchor*</code> constants defined in this class.
  372. *
  373. * @param isCentered horizontal centered?
  374. * @param vAlign vertical alignment
  375. * @param baseline aligned to baseline?
  376. */
  377. /* package */ void setAlignment(Boolean isCentered, VerticalAlignment vAlign, boolean baseline) {
  378. for (HSLFTextAnchor hta : HSLFTextAnchor.values()) {
  379. if (
  380. (hta.centered == (isCentered != null && isCentered)) &&
  381. (hta.vAlign == vAlign) &&
  382. (hta.baseline == null || hta.baseline == baseline)
  383. ) {
  384. setEscherProperty(EscherProperties.TEXT__ANCHORTEXT, hta.nativeId);
  385. break;
  386. }
  387. }
  388. }
  389. /**
  390. * @return true, if vertical alignment is relative to baseline
  391. * this is only used for older versions less equals Office 2003
  392. */
  393. public boolean isAlignToBaseline() {
  394. return getAlignment().baseline;
  395. }
  396. /**
  397. * Sets the vertical alignment relative to the baseline
  398. *
  399. * @param alignToBaseline if true, vertical alignment is relative to baseline
  400. */
  401. public void setAlignToBaseline(boolean alignToBaseline) {
  402. setAlignment(isHorizontalCentered(), getVerticalAlignment(), alignToBaseline);
  403. }
  404. @Override
  405. public boolean isHorizontalCentered() {
  406. return getAlignment().centered;
  407. }
  408. @Override
  409. public void setHorizontalCentered(Boolean isCentered) {
  410. setAlignment(isCentered, getVerticalAlignment(), getAlignment().baseline);
  411. }
  412. @Override
  413. public VerticalAlignment getVerticalAlignment() {
  414. return getAlignment().vAlign;
  415. }
  416. @Override
  417. public void setVerticalAlignment(VerticalAlignment vAlign) {
  418. setAlignment(isHorizontalCentered(), vAlign, getAlignment().baseline);
  419. }
  420. /**
  421. * Returns the distance (in points) between the bottom of the text frame
  422. * and the bottom of the inscribed rectangle of the shape that contains the text.
  423. * Default value is 1/20 inch.
  424. *
  425. * @return the botom margin
  426. */
  427. public double getBottomInset(){
  428. return getInset(EscherProperties.TEXT__TEXTBOTTOM, .05);
  429. }
  430. /**
  431. * Sets the botom margin.
  432. * @see #getBottomInset()
  433. *
  434. * @param margin the bottom margin
  435. */
  436. public void setBottomInset(double margin){
  437. setInset(EscherProperties.TEXT__TEXTBOTTOM, margin);
  438. }
  439. /**
  440. * Returns the distance (in points) between the left edge of the text frame
  441. * and the left edge of the inscribed rectangle of the shape that contains
  442. * the text.
  443. * Default value is 1/10 inch.
  444. *
  445. * @return the left margin
  446. */
  447. public double getLeftInset(){
  448. return getInset(EscherProperties.TEXT__TEXTLEFT, .1);
  449. }
  450. /**
  451. * Sets the left margin.
  452. * @see #getLeftInset()
  453. *
  454. * @param margin the left margin
  455. */
  456. public void setLeftInset(double margin){
  457. setInset(EscherProperties.TEXT__TEXTLEFT, margin);
  458. }
  459. /**
  460. * Returns the distance (in points) between the right edge of the
  461. * text frame and the right edge of the inscribed rectangle of the shape
  462. * that contains the text.
  463. * Default value is 1/10 inch.
  464. *
  465. * @return the right margin
  466. */
  467. public double getRightInset(){
  468. return getInset(EscherProperties.TEXT__TEXTRIGHT, .1);
  469. }
  470. /**
  471. * Sets the right margin.
  472. * @see #getRightInset()
  473. *
  474. * @param margin the right margin
  475. */
  476. public void setRightInset(double margin){
  477. setInset(EscherProperties.TEXT__TEXTRIGHT, margin);
  478. }
  479. /**
  480. * Returns the distance (in points) between the top of the text frame
  481. * and the top of the inscribed rectangle of the shape that contains the text.
  482. * Default value is 1/20 inch.
  483. *
  484. * @return the top margin
  485. */
  486. public double getTopInset(){
  487. return getInset(EscherProperties.TEXT__TEXTTOP, .05);
  488. }
  489. /**
  490. * Sets the top margin.
  491. * @see #getTopInset()
  492. *
  493. * @param margin the top margin
  494. */
  495. public void setTopInset(double margin){
  496. setInset(EscherProperties.TEXT__TEXTTOP, margin);
  497. }
  498. /**
  499. * Returns the distance (in points) between the edge of the text frame
  500. * and the edge of the inscribed rectangle of the shape that contains the text.
  501. * Default value is 1/20 inch.
  502. *
  503. * @param propId the id of the inset edge
  504. * @return the inset in points
  505. */
  506. private double getInset(short propId, double defaultInch) {
  507. AbstractEscherOptRecord opt = getEscherOptRecord();
  508. EscherSimpleProperty prop = getEscherProperty(opt, propId);
  509. int val = prop == null ? (int)(Units.toEMU(Units.POINT_DPI)*defaultInch) : prop.getPropertyValue();
  510. return Units.toPoints(val);
  511. }
  512. /**
  513. * @param propId the id of the inset edge
  514. * @param margin the inset in points
  515. */
  516. private void setInset(short propId, double margin){
  517. setEscherProperty(propId, Units.toEMU(margin));
  518. }
  519. /**
  520. * Returns the value indicating word wrap.
  521. *
  522. * @return the value indicating word wrap.
  523. * Must be one of the <code>Wrap*</code> constants defined in this class.
  524. *
  525. * @see <a href="https://msdn.microsoft.com/en-us/library/dd948168(v=office.12).aspx">MSOWRAPMODE</a>
  526. */
  527. public int getWordWrapEx() {
  528. AbstractEscherOptRecord opt = getEscherOptRecord();
  529. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.TEXT__WRAPTEXT);
  530. return prop == null ? WrapSquare : prop.getPropertyValue();
  531. }
  532. /**
  533. * Specifies how the text should be wrapped
  534. *
  535. * @param wrap the value indicating how the text should be wrapped.
  536. * Must be one of the <code>Wrap*</code> constants defined in this class.
  537. */
  538. public void setWordWrapEx(int wrap){
  539. setEscherProperty(EscherProperties.TEXT__WRAPTEXT, wrap);
  540. }
  541. @Override
  542. public boolean getWordWrap(){
  543. int ww = getWordWrapEx();
  544. return (ww != WrapNone);
  545. }
  546. @Override
  547. public void setWordWrap(boolean wrap) {
  548. setWordWrapEx(wrap ? WrapSquare : WrapNone);
  549. }
  550. /**
  551. * @return id for the text.
  552. */
  553. public int getTextId(){
  554. AbstractEscherOptRecord opt = getEscherOptRecord();
  555. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.TEXT__TEXTID);
  556. return prop == null ? 0 : prop.getPropertyValue();
  557. }
  558. /**
  559. * Sets text ID
  560. *
  561. * @param id of the text
  562. */
  563. public void setTextId(int id){
  564. setEscherProperty(EscherProperties.TEXT__TEXTID, id);
  565. }
  566. @Override
  567. public List<HSLFTextParagraph> getTextParagraphs(){
  568. if (!_paragraphs.isEmpty()) {
  569. return _paragraphs;
  570. }
  571. _txtbox = getEscherTextboxWrapper();
  572. if (_txtbox == null) {
  573. _txtbox = new EscherTextboxWrapper();
  574. createEmptyParagraph();
  575. } else {
  576. List<HSLFTextParagraph> pList = HSLFTextParagraph.findTextParagraphs(_txtbox, getSheet());
  577. if (pList == null) {
  578. // there are actually TextBoxRecords without extra data - see #54722
  579. createEmptyParagraph();
  580. } else {
  581. _paragraphs = pList;
  582. }
  583. if (_paragraphs.isEmpty()) {
  584. LOG.log(POILogger.WARN, "TextRecord didn't contained any text lines");
  585. }
  586. }
  587. for (HSLFTextParagraph p : _paragraphs) {
  588. p.setParentShape(this);
  589. }
  590. return _paragraphs;
  591. }
  592. @Override
  593. public void setSheet(HSLFSheet sheet) {
  594. super.setSheet(sheet);
  595. // Initialize _txtrun object.
  596. // (We can't do it in the constructor because the sheet
  597. // is not assigned then, it's only built once we have
  598. // all the records)
  599. List<HSLFTextParagraph> ltp = getTextParagraphs();
  600. HSLFTextParagraph.supplySheet(ltp, sheet);
  601. }
  602. /**
  603. * Return {@link OEPlaceholderAtom}, the atom that describes a placeholder.
  604. *
  605. * @return {@link OEPlaceholderAtom} or {@code null} if not found
  606. */
  607. public OEPlaceholderAtom getPlaceholderAtom(){
  608. return getClientDataRecord(OEPlaceholderAtom.typeID);
  609. }
  610. /**
  611. * Return {@link RoundTripHFPlaceholder12}, the atom that describes a header/footer placeholder.
  612. * Compare the {@link RoundTripHFPlaceholder12#getPlaceholderId()} with
  613. * {@link Placeholder#HEADER} or {@link Placeholder#FOOTER}, to find out
  614. * what kind of placeholder this is.
  615. *
  616. * @return {@link RoundTripHFPlaceholder12} or {@code null} if not found
  617. *
  618. * @since POI 3.14-Beta2
  619. */
  620. public RoundTripHFPlaceholder12 getHFPlaceholderAtom() {
  621. // special case for files saved in Office 2007
  622. return getClientDataRecord(RoundTripHFPlaceholder12.typeID);
  623. }
  624. @Override
  625. public boolean isPlaceholder() {
  626. OEPlaceholderAtom oep = getPlaceholderAtom();
  627. if (oep != null) {
  628. return true;
  629. }
  630. //special case for files saved in Office 2007
  631. RoundTripHFPlaceholder12 hldr = getHFPlaceholderAtom();
  632. if (hldr != null) {
  633. return true;
  634. }
  635. return false;
  636. }
  637. @Override
  638. public Iterator<HSLFTextParagraph> iterator() {
  639. return _paragraphs.iterator();
  640. }
  641. @Override
  642. public Insets2D getInsets() {
  643. Insets2D insets = new Insets2D(getTopInset(), getLeftInset(), getBottomInset(), getRightInset());
  644. return insets;
  645. }
  646. @Override
  647. public void setInsets(Insets2D insets) {
  648. setTopInset(insets.top);
  649. setLeftInset(insets.left);
  650. setBottomInset(insets.bottom);
  651. setRightInset(insets.right);
  652. }
  653. @Override
  654. public double getTextHeight() {
  655. return getTextHeight(null);
  656. }
  657. @Override
  658. public double getTextHeight(Graphics2D graphics) {
  659. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  660. DrawTextShape dts = drawFact.getDrawable(this);
  661. return dts.getTextHeight(graphics);
  662. }
  663. @Override
  664. public TextDirection getTextDirection() {
  665. // see 2.4.5 MSOTXFL
  666. AbstractEscherOptRecord opt = getEscherOptRecord();
  667. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.TEXT__TEXTFLOW);
  668. int msotxfl = (prop == null) ? 0 : prop.getPropertyValue();
  669. switch (msotxfl) {
  670. default:
  671. case 0: // msotxflHorzN
  672. case 4: // msotxflHorzA
  673. return TextDirection.HORIZONTAL;
  674. case 1: // msotxflTtoBA
  675. case 3: // msotxflTtoBN
  676. case 5: // msotxflVertN
  677. return TextDirection.VERTICAL;
  678. case 2: // msotxflBtoT
  679. return TextDirection.VERTICAL_270;
  680. // TextDirection.STACKED is not supported
  681. }
  682. }
  683. @Override
  684. public void setTextDirection(TextDirection orientation) {
  685. AbstractEscherOptRecord opt = getEscherOptRecord();
  686. int msotxfl;
  687. if (orientation == null) {
  688. msotxfl = -1;
  689. } else {
  690. switch (orientation) {
  691. default:
  692. case STACKED:
  693. // not supported -> remove
  694. msotxfl = -1;
  695. break;
  696. case HORIZONTAL:
  697. msotxfl = 0;
  698. break;
  699. case VERTICAL:
  700. msotxfl = 1;
  701. break;
  702. case VERTICAL_270:
  703. // always interpreted as horizontal
  704. msotxfl = 2;
  705. break;
  706. }
  707. }
  708. setEscherProperty(opt, EscherProperties.TEXT__TEXTFLOW, msotxfl);
  709. }
  710. @Override
  711. public Double getTextRotation() {
  712. // see 2.4.6 MSOCDIR
  713. AbstractEscherOptRecord opt = getEscherOptRecord();
  714. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.TEXT__FONTROTATION);
  715. return (prop == null) ? null : (90. * prop.getPropertyValue());
  716. }
  717. @Override
  718. public void setTextRotation(Double rotation) {
  719. AbstractEscherOptRecord opt = getEscherOptRecord();
  720. if (rotation == null) {
  721. opt.removeEscherProperty(EscherProperties.TEXT__FONTROTATION);
  722. } else {
  723. int rot = (int)(Math.round(rotation / 90.) % 4L);
  724. setEscherProperty(EscherProperties.TEXT__FONTROTATION, rot);
  725. }
  726. }
  727. /**
  728. * Returns the raw text content of the shape. This hasn't had any
  729. * changes applied to it, and so is probably unlikely to print
  730. * out nicely.
  731. */
  732. public String getRawText() {
  733. return HSLFTextParagraph.getRawText(getTextParagraphs());
  734. }
  735. @Override
  736. public String getText() {
  737. String rawText = getRawText();
  738. return HSLFTextParagraph.toExternalString(rawText, getRunType());
  739. }
  740. @Override
  741. public HSLFTextRun appendText(String text, boolean newParagraph) {
  742. // init paragraphs
  743. List<HSLFTextParagraph> paras = getTextParagraphs();
  744. HSLFTextRun htr = HSLFTextParagraph.appendText(paras, text, newParagraph);
  745. setTextId(getRawText().hashCode());
  746. return htr;
  747. }
  748. @Override
  749. public HSLFTextRun setText(String text) {
  750. // init paragraphs
  751. List<HSLFTextParagraph> paras = getTextParagraphs();
  752. HSLFTextRun htr = HSLFTextParagraph.setText(paras, text);
  753. setTextId(getRawText().hashCode());
  754. return htr;
  755. }
  756. /**
  757. * Saves the modified paragraphs/textrun to the records.
  758. * Also updates the styles to the correct text length.
  759. */
  760. protected void storeText() {
  761. List<HSLFTextParagraph> paras = getTextParagraphs();
  762. HSLFTextParagraph.storeText(paras);
  763. }
  764. /**
  765. * Returns the array of all hyperlinks in this text run
  766. *
  767. * @return the array of all hyperlinks in this text run or <code>null</code>
  768. * if not found.
  769. */
  770. public List<HSLFHyperlink> getHyperlinks() {
  771. return HSLFHyperlink.find(this);
  772. }
  773. @Override
  774. public void setTextPlaceholder(TextPlaceholder placeholder) {
  775. // TOOD: check for correct placeholder handling - see org.apache.poi.hslf.model.Placeholder
  776. Placeholder ph = null;
  777. int runType;
  778. switch (placeholder) {
  779. default:
  780. case BODY:
  781. runType = TextHeaderAtom.BODY_TYPE;
  782. ph = Placeholder.BODY;
  783. break;
  784. case TITLE:
  785. runType = TextHeaderAtom.TITLE_TYPE;
  786. ph = Placeholder.TITLE;
  787. break;
  788. case CENTER_BODY:
  789. runType = TextHeaderAtom.CENTRE_BODY_TYPE;
  790. ph = Placeholder.BODY;
  791. break;
  792. case CENTER_TITLE:
  793. runType = TextHeaderAtom.CENTER_TITLE_TYPE;
  794. ph = Placeholder.TITLE;
  795. break;
  796. case HALF_BODY:
  797. runType = TextHeaderAtom.HALF_BODY_TYPE;
  798. ph = Placeholder.BODY;
  799. break;
  800. case QUARTER_BODY:
  801. runType = TextHeaderAtom.QUARTER_BODY_TYPE;
  802. ph = Placeholder.BODY;
  803. break;
  804. case NOTES:
  805. runType = TextHeaderAtom.NOTES_TYPE;
  806. break;
  807. case OTHER:
  808. runType = TextHeaderAtom.OTHER_TYPE;
  809. break;
  810. }
  811. setRunType(runType);
  812. if (ph != null) {
  813. setPlaceholder(ph);
  814. }
  815. }
  816. @Override
  817. public TextPlaceholder getTextPlaceholder() {
  818. switch (getRunType()) {
  819. default:
  820. case TextHeaderAtom.BODY_TYPE: return TextPlaceholder.BODY;
  821. case TextHeaderAtom.TITLE_TYPE: return TextPlaceholder.TITLE;
  822. case TextHeaderAtom.NOTES_TYPE: return TextPlaceholder.NOTES;
  823. case TextHeaderAtom.OTHER_TYPE: return TextPlaceholder.OTHER;
  824. case TextHeaderAtom.CENTRE_BODY_TYPE: return TextPlaceholder.CENTER_BODY;
  825. case TextHeaderAtom.CENTER_TITLE_TYPE: return TextPlaceholder.CENTER_TITLE;
  826. case TextHeaderAtom.HALF_BODY_TYPE: return TextPlaceholder.HALF_BODY;
  827. case TextHeaderAtom.QUARTER_BODY_TYPE: return TextPlaceholder.QUARTER_BODY;
  828. }
  829. }
  830. /**
  831. * Get alternative representation of text shape stored as metro blob escher property.
  832. * The returned shape is the first shape in stored group shape of the metro blob
  833. *
  834. * @return null, if there's no alternative representation, otherwise the text shape
  835. */
  836. public TextShape<?,? extends TextParagraph<?,?,? extends TextRun>> getMetroShape() {
  837. HSLFMetroShape<TextShape<?,? extends TextParagraph<?,?,? extends TextRun>>> mbs = new HSLFMetroShape<TextShape<?,? extends TextParagraph<?,?,? extends TextRun>>>(this);
  838. return mbs.getShape();
  839. }
  840. }