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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package org.apache.poi.xslf.usermodel;
  2. import junit.framework.TestCase;
  3. import java.awt.Rectangle;
  4. import java.awt.Color;
  5. import java.awt.geom.Rectangle2D;
  6. import java.io.FileOutputStream;
  7. import org.apache.poi.xssf.dev.XSSFDump;
  8. import org.apache.poi.xslf.util.PPTX2PNG;
  9. /**
  10. * Created by IntelliJ IDEA.
  11. * User: yegor
  12. * Date: Nov 10, 2011
  13. * Time: 1:43:25 PM
  14. * To change this template use File | Settings | File Templates.
  15. */
  16. public class TestXSLFTextParagraph extends TestCase {
  17. public void testWrappingWidth() throws Exception {
  18. XMLSlideShow ppt = new XMLSlideShow();
  19. XSLFSlide slide = ppt.createSlide();
  20. XSLFTextShape sh = slide.createAutoShape();
  21. sh.setLineColor(Color.black);
  22. XSLFTextParagraph p = sh.addNewTextParagraph();
  23. p.addNewTextRun().setText(
  24. "Paragraph formatting allows for more granular control " +
  25. "of text within a shape. Properties here apply to all text " +
  26. "residing within the corresponding paragraph.");
  27. Rectangle2D anchor = new Rectangle(50, 50, 300, 200);
  28. sh.setAnchor(anchor);
  29. double leftInset = sh.getLeftInset();
  30. double rightInset = sh.getRightInset();
  31. assertEquals(7.2, leftInset);
  32. assertEquals(7.2, rightInset);
  33. double leftMargin = p.getLeftMargin();
  34. assertEquals(0.0, leftMargin);
  35. double indent = p.getIndent();
  36. assertEquals(0.0, indent); // default
  37. double expectedWidth;
  38. // Case 1: bullet=false, leftMargin=0, indent=0.
  39. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  40. assertEquals(285.6, expectedWidth); // 300 - 7.2 - 7.2 - 0
  41. assertEquals(expectedWidth, p.getWrappingWidth(true));
  42. assertEquals(expectedWidth, p.getWrappingWidth(false));
  43. p.setLeftMargin(36); // 0.5"
  44. leftMargin = p.getLeftMargin();
  45. assertEquals(36.0, leftMargin);
  46. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  47. assertEquals(249.6, expectedWidth, 1E-5); // 300 - 7.2 - 7.2 - 36
  48. assertEquals(expectedWidth, p.getWrappingWidth(true));
  49. assertEquals(expectedWidth, p.getWrappingWidth(false));
  50. // increase insets, the wrapping width should get smaller
  51. sh.setLeftInset(10);
  52. sh.setRightInset(10);
  53. leftInset = sh.getLeftInset();
  54. rightInset = sh.getRightInset();
  55. assertEquals(10.0, leftInset);
  56. assertEquals(10.0, rightInset);
  57. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  58. assertEquals(244.0, expectedWidth); // 300 - 10 - 10 - 36
  59. assertEquals(expectedWidth, p.getWrappingWidth(true));
  60. assertEquals(expectedWidth, p.getWrappingWidth(false));
  61. // set a positive indent of a 0.5 inch. This means "First Line" indentation:
  62. // |<--- indent -->|Here goes first line of the text
  63. // Here go other lines (second and subsequent)
  64. p.setIndent(36.0); // 0.5"
  65. indent = p.getIndent();
  66. assertEquals(36.0, indent);
  67. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin - indent;
  68. assertEquals(208.0, expectedWidth); // 300 - 10 - 10 - 36 - 6.4
  69. assertEquals(expectedWidth, p.getWrappingWidth(true)); // first line is indented
  70. // other lines are not indented
  71. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  72. assertEquals(244.0, expectedWidth); // 300 - 10 - 10 - 36
  73. assertEquals(expectedWidth, p.getWrappingWidth(false));
  74. // set a negative indent of a 1 inch. This means "Hanging" indentation:
  75. // Here goes first line of the text
  76. // |<--- indent -->|Here go other lines (second and subsequent)
  77. p.setIndent(-72.0); // 1"
  78. indent = p.getIndent();
  79. assertEquals(-72.0, indent);
  80. expectedWidth = anchor.getWidth() - leftInset - rightInset;
  81. assertEquals(280.0, expectedWidth); // 300 - 10 - 10
  82. assertEquals(expectedWidth, p.getWrappingWidth(true)); // first line is NOT indented
  83. // other lines are indented by leftMargin (the value of indent is not used)
  84. expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
  85. assertEquals(244.0, expectedWidth); // 300 - 10 - 10 - 36
  86. assertEquals(expectedWidth, p.getWrappingWidth(false));
  87. }
  88. }