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.

XSLFTextShape.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import org.apache.poi.util.Beta;
  21. import org.apache.poi.util.Units;
  22. import org.apache.poi.xslf.model.PropertyFetcher;
  23. import org.apache.poi.xslf.model.TextBodyPropertyFetcher;
  24. import org.apache.xmlbeans.XmlObject;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBodyProperties;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.STTextVerticalType;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.STTextWrappingType;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
  35. import java.awt.Graphics2D;
  36. import java.awt.geom.Rectangle2D;
  37. import java.awt.image.BufferedImage;
  38. import java.util.ArrayList;
  39. import java.util.Iterator;
  40. import java.util.List;
  41. /**
  42. * Represents a shape that can hold text.
  43. *
  44. * @author Yegor Kozlov
  45. */
  46. @Beta
  47. public abstract class XSLFTextShape extends XSLFSimpleShape implements Iterable<XSLFTextParagraph>{
  48. private final List<XSLFTextParagraph> _paragraphs;
  49. /*package*/ XSLFTextShape(XmlObject shape, XSLFSheet sheet) {
  50. super(shape, sheet);
  51. _paragraphs = new ArrayList<XSLFTextParagraph>();
  52. CTTextBody txBody = getTextBody(false);
  53. if (txBody != null) {
  54. for (CTTextParagraph p : txBody.getPList()) {
  55. _paragraphs.add(new XSLFTextParagraph(p, this));
  56. }
  57. }
  58. }
  59. public Iterator<XSLFTextParagraph> iterator(){
  60. return _paragraphs.iterator();
  61. }
  62. /**
  63. *
  64. * @return text contained within this shape or empty string
  65. */
  66. public String getText() {
  67. StringBuilder out = new StringBuilder();
  68. for (XSLFTextParagraph p : _paragraphs) {
  69. if (out.length() > 0) out.append('\n');
  70. out.append(p.getText());
  71. }
  72. return out.toString();
  73. }
  74. /**
  75. * unset text from this shape
  76. */
  77. public void clearText(){
  78. _paragraphs.clear();
  79. CTTextBody txBody = getTextBody(true);
  80. txBody.setPArray(null); // remove any existing paragraphs
  81. }
  82. public void setText(String text){
  83. clearText();
  84. addNewTextParagraph().addNewTextRun().setText(text);
  85. }
  86. /**
  87. *
  88. * @return text paragraphs in this shape
  89. */
  90. public List<XSLFTextParagraph> getTextParagraphs() {
  91. return _paragraphs;
  92. }
  93. /**
  94. * add a new paragraph run to this shape
  95. *
  96. * @return created paragraph run
  97. */
  98. public XSLFTextParagraph addNewTextParagraph() {
  99. CTTextBody txBody = getTextBody(true);
  100. CTTextParagraph p = txBody.addNewP();
  101. XSLFTextParagraph paragraph = new XSLFTextParagraph(p, this);
  102. _paragraphs.add(paragraph);
  103. return paragraph;
  104. }
  105. /**
  106. * Sets the type of vertical alignment for the text.
  107. *
  108. * @param anchor - the type of alignment.
  109. * A <code>null</code> values unsets this property.
  110. */
  111. public void setVerticalAlignment(VerticalAlignment anchor){
  112. CTTextBodyProperties bodyPr = getTextBodyPr();
  113. if (bodyPr != null) {
  114. if(anchor == null) {
  115. if(bodyPr.isSetAnchor()) bodyPr.unsetAnchor();
  116. } else {
  117. bodyPr.setAnchor(STTextAnchoringType.Enum.forInt(anchor.ordinal() + 1));
  118. }
  119. }
  120. }
  121. /**
  122. * Returns the type of vertical alignment for the text.
  123. *
  124. * @return the type of vertical alignment
  125. */
  126. public VerticalAlignment getVerticalAlignment(){
  127. PropertyFetcher<VerticalAlignment> fetcher = new TextBodyPropertyFetcher<VerticalAlignment>(){
  128. public boolean fetch(CTTextBodyProperties props){
  129. if(props.isSetAnchor()){
  130. int val = props.getAnchor().intValue();
  131. setValue(VerticalAlignment.values()[val - 1]);
  132. return true;
  133. }
  134. return false;
  135. }
  136. };
  137. fetchShapeProperty(fetcher);
  138. return fetcher.getValue() == null ? VerticalAlignment.TOP : fetcher.getValue();
  139. }
  140. /**
  141. *
  142. * @param orientation vertical orientation of the text
  143. */
  144. public void setTextDirection(TextDirection orientation){
  145. CTTextBodyProperties bodyPr = getTextBodyPr();
  146. if (bodyPr != null) {
  147. if(orientation == null) {
  148. if(bodyPr.isSetVert()) bodyPr.unsetVert();
  149. } else {
  150. bodyPr.setVert(STTextVerticalType.Enum.forInt(orientation.ordinal() + 1));
  151. }
  152. }
  153. }
  154. /**
  155. * @return vertical orientation of the text
  156. */
  157. public TextDirection getTextDirection(){
  158. CTTextBodyProperties bodyPr = getTextBodyPr();
  159. if (bodyPr != null) {
  160. STTextVerticalType.Enum val = bodyPr.getVert();
  161. if(val != null){
  162. return TextDirection.values()[val.intValue() - 1];
  163. }
  164. }
  165. return TextDirection.HORIZONTAL;
  166. }
  167. /**
  168. * Returns the distance (in points) between the bottom of the text frame
  169. * and the bottom of the inscribed rectangle of the shape that contains the text.
  170. *
  171. * @return the bottom inset in points
  172. */
  173. public double getBottomInset(){
  174. PropertyFetcher<Double> fetcher = new TextBodyPropertyFetcher<Double>(){
  175. public boolean fetch(CTTextBodyProperties props){
  176. if(props.isSetBIns()){
  177. double val = Units.toPoints(props.getBIns());
  178. setValue(val);
  179. return true;
  180. }
  181. return false;
  182. }
  183. };
  184. fetchShapeProperty(fetcher);
  185. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  186. }
  187. /**
  188. * Returns the distance (in points) between the left edge of the text frame
  189. * and the left edge of the inscribed rectangle of the shape that contains
  190. * the text.
  191. *
  192. * @return the left inset in points
  193. */
  194. public double getLeftInset(){
  195. PropertyFetcher<Double> fetcher = new TextBodyPropertyFetcher<Double>(){
  196. public boolean fetch(CTTextBodyProperties props){
  197. if(props.isSetLIns()){
  198. double val = Units.toPoints(props.getLIns());
  199. setValue(val);
  200. return true;
  201. }
  202. return false;
  203. }
  204. };
  205. fetchShapeProperty(fetcher);
  206. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  207. }
  208. /**
  209. * Returns the distance (in points) between the right edge of the
  210. * text frame and the right edge of the inscribed rectangle of the shape
  211. * that contains the text.
  212. *
  213. * @return the right inset in points
  214. */
  215. public double getRightInset(){
  216. PropertyFetcher<Double> fetcher = new TextBodyPropertyFetcher<Double>(){
  217. public boolean fetch(CTTextBodyProperties props){
  218. if(props.isSetRIns()){
  219. double val = Units.toPoints(props.getRIns());
  220. setValue(val);
  221. return true;
  222. }
  223. return false;
  224. }
  225. };
  226. fetchShapeProperty(fetcher);
  227. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  228. }
  229. /**
  230. * Returns the distance (in points) between the top of the text frame
  231. * and the top of the inscribed rectangle of the shape that contains the text.
  232. *
  233. * @return the top inset in points
  234. */
  235. public double getTopInset(){
  236. PropertyFetcher<Double> fetcher = new TextBodyPropertyFetcher<Double>(){
  237. public boolean fetch(CTTextBodyProperties props){
  238. if(props.isSetTIns()){
  239. double val = Units.toPoints(props.getTIns());
  240. setValue(val);
  241. return true;
  242. }
  243. return false;
  244. }
  245. };
  246. fetchShapeProperty(fetcher);
  247. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  248. }
  249. /**
  250. * Sets the botom margin.
  251. * @see #getBottomInset()
  252. *
  253. * @param margin the bottom margin
  254. */
  255. public void setBottomInset(double margin){
  256. CTTextBodyProperties bodyPr = getTextBodyPr();
  257. if (bodyPr != null) {
  258. if(margin == -1) bodyPr.unsetBIns();
  259. else bodyPr.setBIns(Units.toEMU(margin));
  260. }
  261. }
  262. /**
  263. * Sets the left margin.
  264. * @see #getLeftInset()
  265. *
  266. * @param margin the left margin
  267. */
  268. public void setLeftInset(double margin){
  269. CTTextBodyProperties bodyPr = getTextBodyPr();
  270. if (bodyPr != null) {
  271. if(margin == -1) bodyPr.unsetLIns();
  272. else bodyPr.setLIns(Units.toEMU(margin));
  273. }
  274. }
  275. /**
  276. * Sets the right margin.
  277. * @see #getRightInset()
  278. *
  279. * @param margin the right margin
  280. */
  281. public void setRightInset(double margin){
  282. CTTextBodyProperties bodyPr = getTextBodyPr();
  283. if (bodyPr != null) {
  284. if(margin == -1) bodyPr.unsetRIns();
  285. else bodyPr.setRIns(Units.toEMU(margin));
  286. }
  287. }
  288. /**
  289. * Sets the top margin.
  290. * @see #getTopInset()
  291. *
  292. * @param margin the top margin
  293. */
  294. public void setTopInset(double margin){
  295. CTTextBodyProperties bodyPr = getTextBodyPr();
  296. if (bodyPr != null) {
  297. if(margin == -1) bodyPr.unsetTIns();
  298. else bodyPr.setTIns(Units.toEMU(margin));
  299. }
  300. }
  301. /**
  302. * @return whether to wrap words within the bounding rectangle
  303. */
  304. public boolean getWordWrap(){
  305. PropertyFetcher<Boolean> fetcher = new TextBodyPropertyFetcher<Boolean>(){
  306. public boolean fetch(CTTextBodyProperties props){
  307. if(props.isSetWrap()){
  308. setValue(props.getWrap() == STTextWrappingType.SQUARE);
  309. return true;
  310. }
  311. return false;
  312. }
  313. };
  314. fetchShapeProperty(fetcher);
  315. return fetcher.getValue() == null ? true : fetcher.getValue();
  316. }
  317. /**
  318. *
  319. * @param wrap whether to wrap words within the bounding rectangle
  320. */
  321. public void setWordWrap(boolean wrap){
  322. CTTextBodyProperties bodyPr = getTextBodyPr();
  323. if (bodyPr != null) {
  324. bodyPr.setWrap(wrap ? STTextWrappingType.SQUARE : STTextWrappingType.NONE);
  325. }
  326. }
  327. /**
  328. *
  329. * Specifies that a shape should be auto-fit to fully contain the text described within it.
  330. * Auto-fitting is when text within a shape is scaled in order to contain all the text inside
  331. *
  332. * @param value type of autofit
  333. */
  334. public void setTextAutofit(TextAutofit value){
  335. CTTextBodyProperties bodyPr = getTextBodyPr();
  336. if (bodyPr != null) {
  337. if(bodyPr.isSetSpAutoFit()) bodyPr.unsetSpAutoFit();
  338. if(bodyPr.isSetNoAutofit()) bodyPr.unsetNoAutofit();
  339. if(bodyPr.isSetNormAutofit()) bodyPr.unsetNormAutofit();
  340. switch(value){
  341. case NONE: bodyPr.addNewNoAutofit(); break;
  342. case NORMAL: bodyPr.addNewNormAutofit(); break;
  343. case SHAPE: bodyPr.addNewSpAutoFit(); break;
  344. }
  345. }
  346. }
  347. /**
  348. *
  349. * @return type of autofit
  350. */
  351. public TextAutofit getTextAutofit(){
  352. CTTextBodyProperties bodyPr = getTextBodyPr();
  353. if (bodyPr != null) {
  354. if(bodyPr.isSetNoAutofit()) return TextAutofit.NONE;
  355. else if (bodyPr.isSetNormAutofit()) return TextAutofit.NORMAL;
  356. else if (bodyPr.isSetSpAutoFit()) return TextAutofit.SHAPE;
  357. }
  358. return TextAutofit.NORMAL;
  359. }
  360. protected CTTextBodyProperties getTextBodyPr(){
  361. CTTextBody textBody = getTextBody(false);
  362. return textBody == null ? null : textBody.getBodyPr();
  363. }
  364. protected abstract CTTextBody getTextBody(boolean create);
  365. public Placeholder getTextType(){
  366. CTPlaceholder ph;
  367. XmlObject[] obj = getXmlObject().selectPath(
  368. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:nvPr/p:ph");
  369. if(obj.length == 1){
  370. ph = (CTPlaceholder)obj[0];
  371. int val = ph.getType().intValue();
  372. return Placeholder.values()[val - 1];
  373. }
  374. else {
  375. return null;
  376. }
  377. }
  378. /**
  379. * Specifies that the corresponding shape should be represented by the generating application
  380. * as a placeholder. When a shape is considered a placeholder by the generating application
  381. * it can have special properties to alert the user that they may enter content into the shape.
  382. * Different types of placeholders are allowed and can be specified by using the placeholder
  383. * type attribute for this element
  384. *
  385. * @param placeholder
  386. */
  387. public void setPlaceholder(Placeholder placeholder){
  388. CTShape sh = (CTShape)getXmlObject();
  389. CTApplicationNonVisualDrawingProps nv = sh.getNvSpPr().getNvPr();
  390. if(placeholder == null) {
  391. if(nv.isSetPh()) nv.unsetPh();
  392. } else {
  393. nv.addNewPh().setType(STPlaceholderType.Enum.forInt(placeholder.ordinal() + 1));
  394. }
  395. }
  396. /**
  397. * Compute the cumulative height occupied by the text
  398. */
  399. private double getTextHeight(){
  400. // dry-run in a 1x1 image and return the vertical advance
  401. BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  402. return drawParagraphs(img.createGraphics(), 0, 0);
  403. }
  404. /**
  405. * break the contained text into lines
  406. */
  407. private void breakText(Graphics2D graphics){
  408. for(XSLFTextParagraph p : _paragraphs) p.breakText(graphics);
  409. }
  410. @Override
  411. public void drawContent(Graphics2D graphics) {
  412. breakText(graphics);
  413. Rectangle2D anchor = getAnchor();
  414. double x = anchor.getX() + getLeftInset();
  415. double y = anchor.getY();
  416. // first dry-run to calculate the total height of the text
  417. double textHeight = getTextHeight();
  418. switch (getVerticalAlignment()){
  419. case TOP:
  420. y += getTopInset();
  421. break;
  422. case BOTTOM:
  423. y += anchor.getHeight() - textHeight - getBottomInset();
  424. break;
  425. default:
  426. case MIDDLE:
  427. double delta = anchor.getHeight() - textHeight -
  428. getTopInset() - getBottomInset();
  429. y += getTopInset() + delta/2;
  430. break;
  431. }
  432. drawParagraphs(graphics, x, y);
  433. }
  434. /**
  435. * pain the paragraphs starting from top left (x,y)
  436. *
  437. * @return the vertical advance, i.e. the cumulative space occupied by the text
  438. */
  439. private double drawParagraphs(Graphics2D graphics, double x, double y) {
  440. double y0 = y;
  441. for(int i = 0; i < _paragraphs.size(); i++){
  442. XSLFTextParagraph p = _paragraphs.get(i);
  443. List<XSLFTextParagraph.TextFragment> lines = p.getTextLines();
  444. if(i > 0 && lines.size() > 0) {
  445. // the amount of vertical white space before the paragraph
  446. double spaceBefore = p.getSpaceBefore();
  447. if(spaceBefore > 0) {
  448. // positive value means percentage spacing of the height of the first line, e.g.
  449. // the higher the first line, the bigger the space before the paragraph
  450. y += spaceBefore*0.01*lines.get(0).getHeight();
  451. } else {
  452. // negative value means the absolute spacing in points
  453. y += -spaceBefore;
  454. }
  455. }
  456. y += p.draw(graphics, x, y);
  457. if(i < _paragraphs.size() - 1) {
  458. double spaceAfter = p.getSpaceAfter();
  459. if(spaceAfter > 0) {
  460. // positive value means percentage spacing of the height of the last line, e.g.
  461. // the higher the last line, the bigger the space after the paragraph
  462. y += spaceAfter*0.01*lines.get(lines.size() - 1).getHeight();
  463. } else {
  464. // negative value means the absolute spacing in points
  465. y += -spaceAfter;
  466. }
  467. }
  468. }
  469. return y - y0;
  470. }
  471. }