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.

SlideShowHandler.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.stress;
  16. import static org.junit.jupiter.api.Assertions.assertNotNull;
  17. import static org.junit.jupiter.api.Assertions.assertTrue;
  18. import java.awt.Dimension;
  19. import java.awt.Graphics2D;
  20. import java.awt.RenderingHints;
  21. import java.awt.image.BufferedImage;
  22. import java.io.IOException;
  23. import java.lang.ref.WeakReference;
  24. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  25. import org.apache.poi.sl.draw.Drawable;
  26. import org.apache.poi.sl.usermodel.GroupShape;
  27. import org.apache.poi.sl.usermodel.Notes;
  28. import org.apache.poi.sl.usermodel.PictureData;
  29. import org.apache.poi.sl.usermodel.Shape;
  30. import org.apache.poi.sl.usermodel.SimpleShape;
  31. import org.apache.poi.sl.usermodel.Slide;
  32. import org.apache.poi.sl.usermodel.SlideShow;
  33. import org.apache.poi.sl.usermodel.SlideShowFactory;
  34. import org.apache.poi.sl.usermodel.TextParagraph;
  35. import org.apache.poi.sl.usermodel.TextRun;
  36. import org.apache.poi.sl.usermodel.TextShape;
  37. import org.junit.platform.commons.util.ExceptionUtils;
  38. public abstract class SlideShowHandler extends POIFSFileHandler {
  39. public void handleSlideShow(SlideShow<?,?> ss) throws IOException {
  40. renderSlides(ss);
  41. readContent(ss);
  42. readPictures(ss);
  43. // write out the file
  44. UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream();
  45. ss.write(out);
  46. readContent(ss);
  47. // read in the written file
  48. try (SlideShow<?, ?> read = SlideShowFactory.create(out.toInputStream())) {
  49. assertNotNull(read);
  50. readContent(read);
  51. }
  52. }
  53. private void readContent(SlideShow<?,?> ss) {
  54. for (Slide<?,?> s : ss.getSlides()) {
  55. s.getTitle();
  56. for (Shape<?,?> shape : s) {
  57. readShapes(shape);
  58. }
  59. Notes<?, ?> notes = s.getNotes();
  60. if(notes != null) {
  61. for (Shape<?, ?> shape : notes) {
  62. readShapes(shape);
  63. }
  64. }
  65. for (Shape<?,?> shape : s.getMasterSheet()) {
  66. readShapes(shape);
  67. }
  68. }
  69. }
  70. private void readShapes(Shape<?,?> s) {
  71. // recursively walk group-shapes
  72. if(s instanceof GroupShape) {
  73. GroupShape<? extends Shape<?,?>, ?> shapes = (GroupShape<? extends Shape<?,?>, ?>) s;
  74. for (Shape<? extends Shape<?,?>, ?> shape : shapes) {
  75. readShapes(shape);
  76. }
  77. }
  78. if(s instanceof SimpleShape) {
  79. SimpleShape<?, ?> simpleShape = (SimpleShape<?, ?>) s;
  80. simpleShape.getFillColor();
  81. simpleShape.getFillStyle();
  82. simpleShape.getStrokeStyle();
  83. simpleShape.getLineDecoration();
  84. }
  85. readText(s);
  86. }
  87. private void readText(Shape<?,?> s) {
  88. if (s instanceof TextShape) {
  89. for (TextParagraph<?,?,?> tp : (TextShape<?,?>)s) {
  90. for (TextRun tr : tp) {
  91. tr.getRawText();
  92. }
  93. }
  94. }
  95. }
  96. private void readPictures(SlideShow<?,?> ss) {
  97. for (PictureData pd : ss.getPictureData()) {
  98. Dimension dim = pd.getImageDimension();
  99. assertTrue( dim.getHeight() >= 0, "Expecting a valid height, but had an image with height: " + dim.getHeight() );
  100. assertTrue( dim.getWidth() >= 0, "Expecting a valid width, but had an image with width: " + dim.getWidth() );
  101. }
  102. }
  103. private void renderSlides(SlideShow<?,?> ss) {
  104. Dimension pgSize = ss.getPageSize();
  105. for (Slide<?,?> s : ss.getSlides()) {
  106. BufferedImage img = new BufferedImage(pgSize.width, pgSize.height, BufferedImage.TYPE_INT_ARGB);
  107. Graphics2D graphics = img.createGraphics();
  108. // default rendering options
  109. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  110. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  111. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  112. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  113. graphics.setRenderingHint(Drawable.BUFFERED_IMAGE, new WeakReference<>(img));
  114. try {
  115. // draw stuff
  116. s.draw(graphics);
  117. } catch (ArrayIndexOutOfBoundsException e) {
  118. // We saw exceptions with JDK 8 on Windows in the Jenkins CI which
  119. // seem to only be triggered by some font (maybe Calibri?!)
  120. // We cannot avoid this, so let's try to not make the tests fail in this case
  121. if (!"-1".equals(e.getMessage()) ||
  122. !ExceptionUtils.readStackTrace(e).contains("ExtendedTextSourceLabel.getJustificationInfos")) {
  123. throw e;
  124. }
  125. }
  126. graphics.dispose();
  127. img.flush();
  128. }
  129. }
  130. }