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.

TestXSLFTextParagraph.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 static org.apache.poi.sl.TestCommonSL.sameColor;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.awt.Color;
  22. import java.awt.Graphics2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.awt.image.BufferedImage;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import org.apache.poi.sl.draw.DrawTextFragment;
  28. import org.apache.poi.sl.draw.DrawTextParagraph;
  29. import org.apache.poi.sl.usermodel.AutoNumberingScheme;
  30. import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
  31. import org.apache.poi.xslf.XSLFTestDataSamples;
  32. import org.junit.Assume;
  33. import org.junit.Test;
  34. /**
  35. * @author Yegor Kozlov
  36. */
  37. public class TestXSLFTextParagraph {
  38. // private static POILogger _logger = POILogFactory.getLogger(XSLFTextParagraph.class);
  39. static class DrawTextParagraphProxy extends DrawTextParagraph {
  40. DrawTextParagraphProxy(XSLFTextParagraph p) {
  41. super(p);
  42. }
  43. public void breakText(Graphics2D graphics) {
  44. super.breakText(graphics);
  45. }
  46. public double getWrappingWidth(boolean firstLine, Graphics2D graphics) {
  47. return super.getWrappingWidth(firstLine, graphics);
  48. }
  49. public List<DrawTextFragment> getLines() {
  50. return lines;
  51. }
  52. }
  53. @Test
  54. public void testWrappingWidth() throws IOException {
  55. XMLSlideShow ppt = new XMLSlideShow();
  56. XSLFSlide slide = ppt.createSlide();
  57. XSLFTextShape sh = slide.createAutoShape();
  58. sh.setLineColor(Color.black);
  59. XSLFTextParagraph p = sh.addNewTextParagraph();
  60. p.addNewTextRun().setText(
  61. "Paragraph formatting allows for more granular control " +
  62. "of text within a shape. Properties here apply to all text " +
  63. "residing within the corresponding paragraph.");
  64. Rectangle2D anchor = new Rectangle2D.Double(50, 50, 300, 200);
  65. sh.setAnchor(anchor);
  66. DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
  67. Double leftInset = sh.getLeftInset();
  68. Double rightInset = sh.getRightInset();
  69. assertEquals(7.2, leftInset, 0);
  70. assertEquals(7.2, rightInset, 0);
  71. Double leftMargin = p.getLeftMargin();
  72. assertEquals(0.0, leftMargin, 0);
  73. Double indent = p.getIndent();
  74. assertNull(indent); // default
  75. double expectedWidth;
  76. // Case 1: bullet=false, leftMargin=0, indent=0.
  77. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  78. assertEquals(285.6, expectedWidth, 0); // 300 - 7.2 - 7.2 - 0
  79. assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
  80. assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
  81. p.setLeftMargin(36d); // 0.5"
  82. leftMargin = p.getLeftMargin();
  83. assertEquals(36.0, leftMargin, 0);
  84. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  85. assertEquals(249.6, expectedWidth, 1E-5); // 300 - 7.2 - 7.2 - 36
  86. assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
  87. assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
  88. // increase insets, the wrapping width should get smaller
  89. sh.setLeftInset(10);
  90. sh.setRightInset(10);
  91. leftInset = sh.getLeftInset();
  92. rightInset = sh.getRightInset();
  93. assertEquals(10.0, leftInset, 0);
  94. assertEquals(10.0, rightInset, 0);
  95. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  96. assertEquals(244.0, expectedWidth, 0); // 300 - 10 - 10 - 36
  97. assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
  98. assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
  99. // set a positive indent of a 0.5 inch. This means "First Line" indentation:
  100. // |<--- indent -->|Here goes first line of the text
  101. // Here go other lines (second and subsequent)
  102. p.setIndent(36.0); // 0.5"
  103. indent = p.getIndent();
  104. assertEquals(36.0, indent, 0);
  105. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin - indent;
  106. assertEquals(208.0, expectedWidth, 0); // 300 - 10 - 10 - 36 - 6.4
  107. assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0); // first line is indented
  108. // other lines are not indented
  109. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  110. assertEquals(244.0, expectedWidth, 0); // 300 - 10 - 10 - 36
  111. assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
  112. // set a negative indent of a 1 inch. This means "Hanging" indentation:
  113. // Here goes first line of the text
  114. // |<--- indent -->|Here go other lines (second and subsequent)
  115. p.setIndent(-72.0); // 1"
  116. indent = p.getIndent();
  117. assertEquals(-72.0, indent, 0);
  118. expectedWidth = anchor.getWidth() - leftInset - rightInset;
  119. assertEquals(280.0, expectedWidth, 0); // 300 - 10 - 10
  120. assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0); // first line is NOT indented
  121. // other lines are indented by leftMargin (the value of indent is not used)
  122. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  123. assertEquals(244.0, expectedWidth, 0); // 300 - 10 - 10 - 36
  124. assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
  125. ppt.close();
  126. }
  127. /**
  128. * test breaking test into lines.
  129. * This test requires that the Arial font is available and will run only on windows
  130. */
  131. @Test
  132. public void testBreakLines() throws IOException {
  133. String os = System.getProperty("os.name");
  134. Assume.assumeTrue("Skipping testBreakLines(), it is executed only on Windows machines", (os != null && os.contains("Windows")));
  135. XMLSlideShow ppt = new XMLSlideShow();
  136. XSLFSlide slide = ppt.createSlide();
  137. XSLFTextShape sh = slide.createAutoShape();
  138. XSLFTextParagraph p = sh.addNewTextParagraph();
  139. XSLFTextRun r = p.addNewTextRun();
  140. r.setFontFamily("Arial"); // this should always be available
  141. r.setFontSize(12d);
  142. r.setText(
  143. "Paragraph formatting allows for more granular control " +
  144. "of text within a shape. Properties here apply to all text " +
  145. "residing within the corresponding paragraph.");
  146. sh.setAnchor(new Rectangle2D.Double(50, 50, 300, 200));
  147. DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
  148. BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  149. Graphics2D graphics = img.createGraphics();
  150. List<DrawTextFragment> lines;
  151. dtp.breakText(graphics);
  152. lines = dtp.getLines();
  153. assertEquals(4, lines.size());
  154. // decrease the shape width from 300 pt to 100 pt
  155. sh.setAnchor(new Rectangle2D.Double(50, 50, 100, 200));
  156. dtp.breakText(graphics);
  157. lines = dtp.getLines();
  158. assertEquals(12, lines.size());
  159. // decrease the shape width from 300 pt to 100 pt
  160. sh.setAnchor(new Rectangle2D.Double(50, 50, 600, 200));
  161. dtp.breakText(graphics);
  162. lines = dtp.getLines();
  163. assertEquals(2, lines.size());
  164. // set left and right margins to 200pt. This leaves 200pt for wrapping text
  165. sh.setLeftInset(200);
  166. sh.setRightInset(200);
  167. dtp.breakText(graphics);
  168. lines = dtp.getLines();
  169. assertEquals(5, lines.size());
  170. r.setText("Apache POI");
  171. dtp.breakText(graphics);
  172. lines = dtp.getLines();
  173. assertEquals(1, lines.size());
  174. assertEquals("Apache POI", lines.get(0).getString());
  175. r.setText("Apache\nPOI");
  176. dtp.breakText(graphics);
  177. lines = dtp.getLines();
  178. assertEquals(2, lines.size());
  179. assertEquals("Apache", lines.get(0).getString());
  180. assertEquals("POI", lines.get(1).getString());
  181. // trailing newlines are ignored
  182. r.setText("Apache\nPOI\n");
  183. dtp.breakText(graphics);
  184. lines = dtp.getLines();
  185. assertEquals(2, lines.size());
  186. assertEquals("Apache", lines.get(0).getString());
  187. assertEquals("POI", lines.get(1).getString());
  188. XSLFAutoShape sh2 = slide.createAutoShape();
  189. sh2.setAnchor(new Rectangle2D.Double(50, 50, 300, 200));
  190. XSLFTextParagraph p2 = sh2.addNewTextParagraph();
  191. XSLFTextRun r2 = p2.addNewTextRun();
  192. r2.setFontFamily("serif"); // this should always be available
  193. r2.setFontSize(30d);
  194. r2.setText("Apache\n");
  195. XSLFTextRun r3 = p2.addNewTextRun();
  196. r3.setFontFamily("serif"); // this should always be available
  197. r3.setFontSize(10d);
  198. r3.setText("POI");
  199. dtp = new DrawTextParagraphProxy(p2);
  200. dtp.breakText(graphics);
  201. lines = dtp.getLines();
  202. assertEquals(2, lines.size());
  203. assertEquals("Apache", lines.get(0).getString());
  204. assertEquals("POI", lines.get(1).getString());
  205. // the first line is at least two times higher than the second
  206. assertTrue(lines.get(0).getHeight() > lines.get(1).getHeight()*2);
  207. ppt.close();
  208. }
  209. @Test
  210. public void testThemeInheritance() throws IOException {
  211. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("prProps.pptx");
  212. List<XSLFShape> shapes = ppt.getSlides().get(0).getShapes();
  213. XSLFTextShape sh1 = (XSLFTextShape)shapes.get(0);
  214. assertEquals("Apache", sh1.getText());
  215. assertEquals(TextAlign.CENTER, sh1.getTextParagraphs().get(0).getTextAlign());
  216. XSLFTextShape sh2 = (XSLFTextShape)shapes.get(1);
  217. assertEquals("Software", sh2.getText());
  218. assertEquals(TextAlign.CENTER, sh2.getTextParagraphs().get(0).getTextAlign());
  219. XSLFTextShape sh3 = (XSLFTextShape)shapes.get(2);
  220. assertEquals("Foundation", sh3.getText());
  221. assertEquals(TextAlign.CENTER, sh3.getTextParagraphs().get(0).getTextAlign());
  222. ppt.close();
  223. }
  224. @Test
  225. public void testParagraphProperties() throws IOException {
  226. XMLSlideShow ppt = new XMLSlideShow();
  227. XSLFSlide slide = ppt.createSlide();
  228. XSLFTextShape sh = slide.createAutoShape();
  229. XSLFTextParagraph p = sh.addNewTextParagraph();
  230. assertFalse(p.isBullet());
  231. p.setBullet(true);
  232. assertTrue(p.isBullet());
  233. assertEquals("\u2022", p.getBulletCharacter());
  234. p.setBulletCharacter("*");
  235. assertEquals("*", p.getBulletCharacter());
  236. assertEquals("Arial", p.getBulletFont());
  237. p.setBulletFont("Calibri");
  238. assertEquals("Calibri", p.getBulletFont());
  239. assertEquals(null, p.getBulletFontColor());
  240. p.setBulletFontColor(Color.red);
  241. assertTrue(sameColor(Color.red, p.getBulletFontColor()));
  242. assertNull(p.getBulletFontSize());
  243. p.setBulletFontSize(200.);
  244. assertEquals(200., p.getBulletFontSize(), 0);
  245. p.setBulletFontSize(-20.);
  246. assertEquals(-20.0, p.getBulletFontSize(), 0);
  247. assertEquals(72.0, p.getDefaultTabSize(), 0);
  248. assertNull(p.getIndent());
  249. p.setIndent(72.0);
  250. assertEquals(72.0, p.getIndent(), 0);
  251. p.setIndent(-1d); // the value of -1.0 resets to the defaults (not any more ...)
  252. assertEquals(-1d, p.getIndent(), 0);
  253. p.setIndent(null);
  254. assertNull(p.getIndent());
  255. assertEquals(0.0, p.getLeftMargin(), 0);
  256. p.setLeftMargin(72.0);
  257. assertEquals(72.0, p.getLeftMargin(), 0);
  258. p.setLeftMargin(-1.0); // the value of -1.0 resets to the defaults
  259. assertEquals(-1.0, p.getLeftMargin(), 0);
  260. p.setLeftMargin(null);
  261. assertEquals(0d, p.getLeftMargin(), 0); // default will be taken from master
  262. assertEquals(0, p.getIndentLevel());
  263. p.setIndentLevel(1);
  264. assertEquals(1, p.getIndentLevel());
  265. p.setIndentLevel(2);
  266. assertEquals(2, p.getIndentLevel());
  267. assertNull(p.getLineSpacing());
  268. p.setLineSpacing(200.);
  269. assertEquals(200.0, p.getLineSpacing(), 0);
  270. p.setLineSpacing(-15.);
  271. assertEquals(-15.0, p.getLineSpacing(), 0);
  272. assertNull(p.getSpaceAfter());
  273. p.setSpaceAfter(200.);
  274. assertEquals(200.0, p.getSpaceAfter(), 0);
  275. p.setSpaceAfter(-15.);
  276. assertEquals(-15.0, p.getSpaceAfter(), 0);
  277. assertNull(p.getSpaceBefore());
  278. p.setSpaceBefore(200.);
  279. assertEquals(200.0, p.getSpaceBefore(), 0);
  280. p.setSpaceBefore(-15.);
  281. assertEquals(-15.0, p.getSpaceBefore(), 0);
  282. assertEquals(TextAlign.LEFT, p.getTextAlign());
  283. p.setTextAlign(TextAlign.RIGHT);
  284. assertEquals(TextAlign.RIGHT, p.getTextAlign());
  285. p.setBullet(false);
  286. assertFalse(p.isBullet());
  287. p.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenBoth, 1);
  288. double tabStop = p.getTabStop(0);
  289. assertEquals(0.0, tabStop, 0);
  290. p.addTabStop(100.);
  291. assertEquals(100., p.getTabStop(0), 0);
  292. assertEquals(72.0, p.getDefaultTabSize(), 0);
  293. ppt.close();
  294. }
  295. @Test(expected = IllegalStateException.class)
  296. public void testLineBreak() throws IOException {
  297. XMLSlideShow ppt = new XMLSlideShow();
  298. try {
  299. XSLFSlide slide = ppt.createSlide();
  300. XSLFTextShape sh = slide.createAutoShape();
  301. XSLFTextParagraph p = sh.addNewTextParagraph();
  302. XSLFTextRun r1 = p.addNewTextRun();
  303. r1.setText("Hello,");
  304. XSLFTextRun r2 = p.addLineBreak();
  305. assertEquals("\n", r2.getRawText());
  306. r2.setFontSize(10.0);
  307. assertEquals(10.0, r2.getFontSize(), 0);
  308. XSLFTextRun r3 = p.addNewTextRun();
  309. r3.setText("World!");
  310. r3.setFontSize(20.0);
  311. XSLFTextRun r4 = p.addLineBreak();
  312. assertEquals(20.0, r4.getFontSize(), 0);
  313. assertEquals("Hello,\nWorld!\n",sh.getText());
  314. // "You cannot change text of a line break, it is always '\\n'"
  315. r2.setText("aaa");
  316. } finally {
  317. ppt.close();
  318. }
  319. }
  320. }