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.

TestXSLFDiagram.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  17. import org.apache.poi.openxml4j.opc.ContentTypes;
  18. import org.apache.poi.sl.usermodel.ColorStyle;
  19. import org.apache.poi.sl.usermodel.PaintStyle;
  20. import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
  21. import org.apache.poi.util.LocaleUtil;
  22. import org.apache.poi.xslf.XSLFTestDataSamples;
  23. import org.junit.jupiter.api.Test;
  24. import java.awt.Color;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import java.util.stream.Collectors;
  28. import static org.junit.jupiter.api.Assertions.*;
  29. public class TestXSLFDiagram {
  30. private static final String SIMPLE_DIAGRAM = "smartart-simple.pptx";
  31. private static final String ROTATED_TEXT_DIAGRAM = "smartart-rotated-text.pptx";
  32. private static List<XSLFDiagram> extractDiagrams(XMLSlideShow slideShow) {
  33. return slideShow.getSlides()
  34. .stream()
  35. .flatMap(s -> extractDiagrams(s).stream())
  36. .collect(Collectors.toList());
  37. }
  38. private static List<XSLFDiagram> extractDiagrams(XSLFSlide slide) {
  39. return slide.getShapes()
  40. .stream()
  41. .filter(s -> s instanceof XSLFDiagram)
  42. .map(s -> (XSLFDiagram) s)
  43. .collect(Collectors.toList());
  44. }
  45. private static String colorToHex(Color color) {
  46. return String.format(LocaleUtil.getUserLocale(), "#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
  47. }
  48. private static Color hexToColor(String hex) {
  49. return Color.decode(hex);
  50. }
  51. @Test
  52. public void testHasDiagram() throws IOException {
  53. try (XMLSlideShow inputPptx = XSLFTestDataSamples.openSampleDocument(SIMPLE_DIAGRAM)) {
  54. List<XSLFDiagram> diagrams = extractDiagrams(inputPptx);
  55. assertEquals(1, diagrams.size());
  56. XSLFDiagram diagram = diagrams.get(0);
  57. assertTrue(diagram.hasDiagram());
  58. }
  59. }
  60. @Test
  61. public void testHasDiagramReadOnlyFile() throws IOException, InvalidFormatException {
  62. try (XMLSlideShow inputPptx = XSLFTestDataSamples.openSampleDocumentReadOnly(SIMPLE_DIAGRAM)) {
  63. List<XSLFDiagram> diagrams = extractDiagrams(inputPptx);
  64. assertEquals(1, diagrams.size());
  65. XSLFDiagram diagram = diagrams.get(0);
  66. assertTrue(diagram.hasDiagram());
  67. }
  68. }
  69. @Test
  70. public void testDiagramContainsShapes() throws IOException {
  71. try (XMLSlideShow inputPptx = XSLFTestDataSamples.openSampleDocument(SIMPLE_DIAGRAM)) {
  72. List<XSLFDiagram> diagrams = extractDiagrams(inputPptx);
  73. assertEquals(1, diagrams.size());
  74. XSLFDiagram diagram = diagrams.get(0);
  75. XSLFGroupShape groupShape = diagram.getGroupShape();
  76. assertNotNull(groupShape);
  77. // The Group gets the same positioning as the SmartArt. This can be much wider/taller than the content inside.
  78. assertEquals(groupShape.getAnchor().getWidth(), 113.375, 1E-4);
  79. assertEquals(groupShape.getAnchor().getHeight(), 74, 1E-4);
  80. assertEquals(groupShape.getAnchor().getX(), -16.75, 1E-4);
  81. assertEquals(groupShape.getAnchor().getY(), 5.5, 1E-4);
  82. List<XSLFShape> shapes = groupShape.getShapes();
  83. // 4 shapes, 3 text boxes, one shape does not have any text inside it
  84. assertEquals(7, shapes.size());
  85. // Shape 1 - Yellow Circle - "abc" center aligned
  86. String accent4Hex = "#ffc000"; // yellow
  87. XSLFAutoShape yellowCircle = (XSLFAutoShape) shapes.get(0);
  88. assertTrue(yellowCircle.getText().isEmpty());
  89. assertEquals(accent4Hex, colorToHex(yellowCircle.getFillColor()));
  90. XSLFTextBox yellowCircleText = (XSLFTextBox) shapes.get(1);
  91. assertEquals(yellowCircleText.getText(), "abc");
  92. assertEquals(TextAlign.CENTER, yellowCircleText.getTextParagraphs().get(0).getTextAlign());
  93. // Shape 2 - Gradient Blue & Purple - "def" left aligned
  94. XSLFAutoShape gradientCircle = (XSLFAutoShape) shapes.get(2);
  95. assertTrue(gradientCircle.getFillPaint() instanceof PaintStyle.GradientPaint);
  96. assertTrue(gradientCircle.getText().isEmpty());
  97. XSLFTextBox gradientCircleText = (XSLFTextBox) shapes.get(3);
  98. assertEquals(gradientCircleText.getText(), "def");
  99. // Even with left justification, the text is rendered on the right side of the circle because SmartArt defines
  100. // a better visual placement for the textbox inside the txXfrm property.
  101. assertEquals(1, gradientCircleText.getTextParagraphs().size());
  102. XSLFTextParagraph paragraph = gradientCircleText.getTextParagraphs().get(0);
  103. assertEquals(TextAlign.LEFT, paragraph.getTextAlign());
  104. assertEquals(1, paragraph.getTextRuns().size());
  105. XSLFTextRun textRun = paragraph.getTextRuns().get(0);
  106. assertTrue(textRun.isBold());
  107. assertTrue(textRun.isItalic());
  108. // Shape 3 - Green Circle with theme color - "ghi" right aligned
  109. XSLFAutoShape greenCircle = (XSLFAutoShape) shapes.get(4);
  110. ColorStyle greenCircleColorStyle = ((PaintStyle.SolidPaint) greenCircle.getFillPaint()).getSolidColor();
  111. // The circle uses the yellow accent4 color but has HSL adjustments that make it green
  112. assertEquals(hexToColor(accent4Hex), greenCircleColorStyle.getColor());
  113. assertEquals(50004, greenCircleColorStyle.getAlpha()); // 50% transparency
  114. assertEquals(6533927, greenCircleColorStyle.getHueOff());
  115. assertEquals(6405, greenCircleColorStyle.getLumOff());
  116. assertEquals(-27185, greenCircleColorStyle.getSatOff());
  117. XSLFTextBox greenCircleText = (XSLFTextBox) shapes.get(5);
  118. assertEquals(greenCircleText.getText(), "ghi");
  119. assertEquals(TextAlign.RIGHT, greenCircleText.getTextParagraphs().get(0).getTextAlign());
  120. // Shape 4 - Circle with Picture Fill - no text
  121. XSLFAutoShape pictureShape = (XSLFAutoShape) shapes.get(6);
  122. assertTrue(pictureShape.getText().isEmpty(), "text is empty?");
  123. XSLFTexturePaint texturePaint = (XSLFTexturePaint) pictureShape.getFillPaint();
  124. assertEquals(ContentTypes.IMAGE_JPEG, texturePaint.getContentType());
  125. }
  126. }
  127. @Test
  128. public void testTextRotationOnShape() throws IOException {
  129. try (XMLSlideShow inputPptx = XSLFTestDataSamples.openSampleDocument(ROTATED_TEXT_DIAGRAM)) {
  130. List<XSLFDiagram> diagrams = extractDiagrams(inputPptx);
  131. assertEquals(1, diagrams.size());
  132. XSLFDiagram diagram = diagrams.get(0);
  133. XSLFGroupShape groupShape = diagram.getGroupShape();
  134. List<XSLFShape> shapes = groupShape.getShapes();
  135. // Text shapes have separate rotation calculation
  136. XSLFTextBox abcText = (XSLFTextBox) shapes.get(1);
  137. assertEquals(-41.3187, abcText.getRotation(), 1E-4);
  138. XSLFTextBox defText = (XSLFTextBox) shapes.get(5);
  139. assertEquals(49.1812, defText.getRotation(), 1E-4);
  140. XSLFTextBox ghiText = (XSLFTextBox) shapes.get(9);
  141. assertEquals(0.0, ghiText.getRotation(), 1E-4);
  142. XSLFTextBox jklText = (XSLFTextBox) shapes.get(11);
  143. assertEquals(0.0, jklText.getRotation(), 1E-4);
  144. }
  145. }
  146. }