Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SlideShowHandler.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 org.apache.poi.sl.draw.Drawable;
  17. import org.apache.poi.sl.usermodel.GroupShape;
  18. import org.apache.poi.sl.usermodel.Notes;
  19. import org.apache.poi.sl.usermodel.PictureData;
  20. import org.apache.poi.sl.usermodel.Shape;
  21. import org.apache.poi.sl.usermodel.SimpleShape;
  22. import org.apache.poi.sl.usermodel.Slide;
  23. import org.apache.poi.sl.usermodel.SlideShow;
  24. import org.apache.poi.sl.usermodel.SlideShowFactory;
  25. import org.apache.poi.sl.usermodel.TextParagraph;
  26. import org.apache.poi.sl.usermodel.TextRun;
  27. import org.apache.poi.sl.usermodel.TextShape;
  28. import java.awt.Dimension;
  29. import java.awt.Graphics2D;
  30. import java.awt.RenderingHints;
  31. import java.awt.image.BufferedImage;
  32. import java.io.ByteArrayInputStream;
  33. import java.io.ByteArrayOutputStream;
  34. import java.io.IOException;
  35. import java.lang.ref.WeakReference;
  36. import static org.junit.Assert.assertNotNull;
  37. import static org.junit.Assert.assertTrue;
  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. ByteArrayOutputStream out = writeToArray(ss);
  45. readContent(ss);
  46. // read in the written file
  47. try (SlideShow<?, ?> read = SlideShowFactory.create(new ByteArrayInputStream(out.toByteArray()))) {
  48. assertNotNull(read);
  49. readContent(read);
  50. }
  51. }
  52. private ByteArrayOutputStream writeToArray(SlideShow<?,?> ss) throws IOException {
  53. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  54. try {
  55. ss.write(stream);
  56. } finally {
  57. stream.close();
  58. }
  59. return stream;
  60. }
  61. private void readContent(SlideShow<?,?> ss) {
  62. for (Slide<?,?> s : ss.getSlides()) {
  63. s.getTitle();
  64. for (Shape<?,?> shape : s) {
  65. readShapes(shape);
  66. }
  67. Notes<?, ?> notes = s.getNotes();
  68. if(notes != null) {
  69. for (Shape<?, ?> shape : notes) {
  70. readShapes(shape);
  71. }
  72. }
  73. for (Shape<?,?> shape : s.getMasterSheet()) {
  74. readShapes(shape);
  75. }
  76. }
  77. }
  78. private void readShapes(Shape<?,?> s) {
  79. // recursively walk group-shapes
  80. if(s instanceof GroupShape) {
  81. GroupShape<? extends Shape, ?> shapes = (GroupShape<? extends Shape, ?>) s;
  82. for (Shape<? extends Shape, ?> shape : shapes) {
  83. readShapes(shape);
  84. }
  85. }
  86. if(s instanceof SimpleShape) {
  87. SimpleShape<?, ?> simpleShape = (SimpleShape<?, ?>) s;
  88. simpleShape.getFillColor();
  89. simpleShape.getFillStyle();
  90. simpleShape.getStrokeStyle();
  91. simpleShape.getLineDecoration();
  92. }
  93. readText(s);
  94. }
  95. private void readText(Shape<?,?> s) {
  96. if (s instanceof TextShape) {
  97. for (TextParagraph<?,?,?> tp : (TextShape<?,?>)s) {
  98. for (TextRun tr : tp) {
  99. tr.getRawText();
  100. }
  101. }
  102. }
  103. }
  104. private void readPictures(SlideShow<?,?> ss) {
  105. for (PictureData pd : ss.getPictureData()) {
  106. Dimension dim = pd.getImageDimension();
  107. assertTrue(dim.getHeight() >= 0);
  108. assertTrue(dim.getWidth() >= 0);
  109. }
  110. }
  111. private void renderSlides(SlideShow<?,?> ss) {
  112. Dimension pgsize = ss.getPageSize();
  113. for (Slide<?,?> s : ss.getSlides()) {
  114. BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_ARGB);
  115. Graphics2D graphics = img.createGraphics();
  116. // default rendering options
  117. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  118. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  119. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  120. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  121. graphics.setRenderingHint(Drawable.BUFFERED_IMAGE, new WeakReference<>(img));
  122. // draw stuff
  123. s.draw(graphics);
  124. graphics.dispose();
  125. img.flush();
  126. }
  127. }
  128. }