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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package org.apache.poi.xslf.usermodel;
  2. import junit.framework.TestCase;
  3. import org.apache.poi.util.POILogFactory;
  4. import org.apache.poi.util.POILogger;
  5. import org.apache.poi.xslf.XSLFTestDataSamples;
  6. import java.awt.*;
  7. import java.awt.geom.Rectangle2D;
  8. import java.awt.image.BufferedImage;
  9. import java.util.List;
  10. /**
  11. * Created by IntelliJ IDEA.
  12. * User: yegor
  13. * Date: Nov 10, 2011
  14. * Time: 1:43:25 PM
  15. * To change this template use File | Settings | File Templates.
  16. */
  17. public class TestXSLFTextParagraph extends TestCase {
  18. private static POILogger _logger = POILogFactory.getLogger(XSLFTextParagraph.class);
  19. public void testWrappingWidth() throws Exception {
  20. XMLSlideShow ppt = new XMLSlideShow();
  21. XSLFSlide slide = ppt.createSlide();
  22. XSLFTextShape sh = slide.createAutoShape();
  23. sh.setLineColor(Color.black);
  24. XSLFTextParagraph p = sh.addNewTextParagraph();
  25. p.addNewTextRun().setText(
  26. "Paragraph formatting allows for more granular control " +
  27. "of text within a shape. Properties here apply to all text " +
  28. "residing within the corresponding paragraph.");
  29. Rectangle2D anchor = new Rectangle(50, 50, 300, 200);
  30. sh.setAnchor(anchor);
  31. double leftInset = sh.getLeftInset();
  32. double rightInset = sh.getRightInset();
  33. assertEquals(7.2, leftInset);
  34. assertEquals(7.2, rightInset);
  35. double leftMargin = p.getLeftMargin();
  36. assertEquals(0.0, leftMargin);
  37. double indent = p.getIndent();
  38. assertEquals(0.0, indent); // default
  39. double expectedWidth;
  40. // Case 1: bullet=false, leftMargin=0, indent=0.
  41. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  42. assertEquals(285.6, expectedWidth); // 300 - 7.2 - 7.2 - 0
  43. assertEquals(expectedWidth, p.getWrappingWidth(true));
  44. assertEquals(expectedWidth, p.getWrappingWidth(false));
  45. p.setLeftMargin(36); // 0.5"
  46. leftMargin = p.getLeftMargin();
  47. assertEquals(36.0, leftMargin);
  48. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  49. assertEquals(249.6, expectedWidth, 1E-5); // 300 - 7.2 - 7.2 - 36
  50. assertEquals(expectedWidth, p.getWrappingWidth(true));
  51. assertEquals(expectedWidth, p.getWrappingWidth(false));
  52. // increase insets, the wrapping width should get smaller
  53. sh.setLeftInset(10);
  54. sh.setRightInset(10);
  55. leftInset = sh.getLeftInset();
  56. rightInset = sh.getRightInset();
  57. assertEquals(10.0, leftInset);
  58. assertEquals(10.0, rightInset);
  59. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  60. assertEquals(244.0, expectedWidth); // 300 - 10 - 10 - 36
  61. assertEquals(expectedWidth, p.getWrappingWidth(true));
  62. assertEquals(expectedWidth, p.getWrappingWidth(false));
  63. // set a positive indent of a 0.5 inch. This means "First Line" indentation:
  64. // |<--- indent -->|Here goes first line of the text
  65. // Here go other lines (second and subsequent)
  66. p.setIndent(36.0); // 0.5"
  67. indent = p.getIndent();
  68. assertEquals(36.0, indent);
  69. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin - indent;
  70. assertEquals(208.0, expectedWidth); // 300 - 10 - 10 - 36 - 6.4
  71. assertEquals(expectedWidth, p.getWrappingWidth(true)); // first line is indented
  72. // other lines are not indented
  73. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  74. assertEquals(244.0, expectedWidth); // 300 - 10 - 10 - 36
  75. assertEquals(expectedWidth, p.getWrappingWidth(false));
  76. // set a negative indent of a 1 inch. This means "Hanging" indentation:
  77. // Here goes first line of the text
  78. // |<--- indent -->|Here go other lines (second and subsequent)
  79. p.setIndent(-72.0); // 1"
  80. indent = p.getIndent();
  81. assertEquals(-72.0, indent);
  82. expectedWidth = anchor.getWidth() - leftInset - rightInset;
  83. assertEquals(280.0, expectedWidth); // 300 - 10 - 10
  84. assertEquals(expectedWidth, p.getWrappingWidth(true)); // first line is NOT indented
  85. // other lines are indented by leftMargin (the value of indent is not used)
  86. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  87. assertEquals(244.0, expectedWidth); // 300 - 10 - 10 - 36
  88. assertEquals(expectedWidth, p.getWrappingWidth(false));
  89. }
  90. /**
  91. * test breaking test into lines.
  92. * This test requires that the Arial font is available and will run only on windows
  93. */
  94. public void testBreakLines(){
  95. String os = System.getProperty("os.name");
  96. if(os == null || !os.contains("Windows")) {
  97. _logger.log(POILogger.WARN, "Skipping testBreakLines(), it is executed only on Windows machines");
  98. return;
  99. }
  100. XMLSlideShow ppt = new XMLSlideShow();
  101. XSLFSlide slide = ppt.createSlide();
  102. XSLFTextShape sh = slide.createAutoShape();
  103. XSLFTextParagraph p = sh.addNewTextParagraph();
  104. XSLFTextRun r = p.addNewTextRun();
  105. r.setFontFamily("Arial"); // this should always be available
  106. r.setFontSize(12);
  107. r.setText(
  108. "Paragraph formatting allows for more granular control " +
  109. "of text within a shape. Properties here apply to all text " +
  110. "residing within the corresponding paragraph.");
  111. sh.setAnchor(new Rectangle(50, 50, 300, 200));
  112. BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  113. Graphics2D graphics = img.createGraphics();
  114. List<TextFragment> lines;
  115. lines = p.breakText(graphics);
  116. assertEquals(4, lines.size());
  117. // descrease the shape width from 300 pt to 100 pt
  118. sh.setAnchor(new Rectangle(50, 50, 100, 200));
  119. lines = p.breakText(graphics);
  120. assertEquals(12, lines.size());
  121. // descrease the shape width from 300 pt to 100 pt
  122. sh.setAnchor(new Rectangle(50, 50, 600, 200));
  123. lines = p.breakText(graphics);
  124. assertEquals(2, lines.size());
  125. // set left and right margins to 200pt. This leaves 200pt for wrapping text
  126. sh.setLeftInset(200);
  127. sh.setRightInset(200);
  128. lines = p.breakText(graphics);
  129. assertEquals(5, lines.size());
  130. r.setText("Apache POI");
  131. lines = p.breakText(graphics);
  132. assertEquals(1, lines.size());
  133. assertEquals("Apache POI", lines.get(0).getString());
  134. r.setText("Apache\nPOI");
  135. lines = p.breakText(graphics);
  136. assertEquals(2, lines.size());
  137. assertEquals("Apache", lines.get(0).getString());
  138. assertEquals("POI", lines.get(1).getString());
  139. // trailing newlines are ignored
  140. r.setText("Apache\nPOI\n");
  141. lines = p.breakText(graphics);
  142. assertEquals(2, lines.size());
  143. assertEquals("Apache", lines.get(0).getString());
  144. assertEquals("POI", lines.get(1).getString());
  145. XSLFAutoShape sh2 = slide.createAutoShape();
  146. sh2.setAnchor(new Rectangle(50, 50, 300, 200));
  147. XSLFTextParagraph p2 = sh2.addNewTextParagraph();
  148. XSLFTextRun r2 = p2.addNewTextRun();
  149. r2.setFontFamily("serif"); // this should always be available
  150. r2.setFontSize(30);
  151. r2.setText("Apache\n");
  152. XSLFTextRun r3 = p2.addNewTextRun();
  153. r3.setFontFamily("serif"); // this should always be available
  154. r3.setFontSize(10);
  155. r3.setText("POI");
  156. lines = p2.breakText(graphics);
  157. assertEquals(2, lines.size());
  158. assertEquals("Apache", lines.get(0).getString());
  159. assertEquals("POI", lines.get(1).getString());
  160. // the first line is at least two times higher than the second
  161. assertTrue(lines.get(0).getHeight() > lines.get(1).getHeight()*2);
  162. }
  163. public void testThemeInheritance(){
  164. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("prProps.pptx");
  165. XSLFShape[] shapes = ppt.getSlides()[0].getShapes();
  166. XSLFTextShape sh1 = (XSLFTextShape)shapes[0];
  167. assertEquals("Apache", sh1.getText());
  168. assertEquals(TextAlign.CENTER, sh1.getTextParagraphs().get(0).getTextAlign());
  169. XSLFTextShape sh2 = (XSLFTextShape)shapes[1];
  170. assertEquals("Software", sh2.getText());
  171. assertEquals(TextAlign.CENTER, sh2.getTextParagraphs().get(0).getTextAlign());
  172. XSLFTextShape sh3 = (XSLFTextShape)shapes[2];
  173. assertEquals("Foundation", sh3.getText());
  174. assertEquals(TextAlign.CENTER, sh3.getTextParagraphs().get(0).getTextAlign());
  175. }
  176. }