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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Assert.assertNotNull;
  17. import static org.junit.Assert.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.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import org.apache.poi.sl.draw.DrawFactory;
  26. import org.apache.poi.sl.usermodel.PictureData;
  27. import org.apache.poi.sl.usermodel.Shape;
  28. import org.apache.poi.sl.usermodel.ShapeContainer;
  29. import org.apache.poi.sl.usermodel.Slide;
  30. import org.apache.poi.sl.usermodel.SlideShow;
  31. import org.apache.poi.sl.usermodel.SlideShowFactory;
  32. import org.apache.poi.sl.usermodel.TextParagraph;
  33. import org.apache.poi.sl.usermodel.TextRun;
  34. import org.apache.poi.sl.usermodel.TextShape;
  35. public abstract class SlideShowHandler extends POIFSFileHandler {
  36. public void handleSlideShow(SlideShow<?,?> ss) throws IOException {
  37. renderSlides(ss);
  38. readContent(ss);
  39. readPictures(ss);
  40. // write out the file
  41. ByteArrayOutputStream out = writeToArray(ss);
  42. readContent(ss);
  43. // read in the written file
  44. try (SlideShow<?, ?> read = SlideShowFactory.create(new ByteArrayInputStream(out.toByteArray()))) {
  45. assertNotNull(read);
  46. readContent(read);
  47. }
  48. }
  49. private ByteArrayOutputStream writeToArray(SlideShow<?,?> ss) throws IOException {
  50. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  51. try {
  52. ss.write(stream);
  53. } finally {
  54. stream.close();
  55. }
  56. return stream;
  57. }
  58. private void readContent(SlideShow<?,?> ss) {
  59. for (Slide<?,?> s : ss.getSlides()) {
  60. s.getTitle();
  61. readText(s);
  62. readText(s.getNotes());
  63. readText(s.getMasterSheet());
  64. }
  65. }
  66. private void readText(ShapeContainer<?,?> sc) {
  67. if (sc == null) return;
  68. for (Shape<?,?> s : sc) {
  69. if (s instanceof TextShape) {
  70. for (TextParagraph<?,?,?> tp : (TextShape<?,?>)s) {
  71. for (TextRun tr : tp) {
  72. tr.getRawText();
  73. }
  74. }
  75. }
  76. }
  77. }
  78. private void readPictures(SlideShow<?,?> ss) {
  79. for (PictureData pd : ss.getPictureData()) {
  80. Dimension dim = pd.getImageDimension();
  81. assertTrue(dim.getHeight() >= 0);
  82. assertTrue(dim.getWidth() >= 0);
  83. }
  84. }
  85. private void renderSlides(SlideShow<?,?> ss) {
  86. Dimension pgsize = ss.getPageSize();
  87. for (Slide<?,?> s : ss.getSlides()) {
  88. BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_ARGB);
  89. Graphics2D graphics = img.createGraphics();
  90. // default rendering options
  91. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  92. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  93. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  94. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  95. // draw stuff
  96. s.draw(graphics);
  97. graphics.dispose();
  98. img.flush();
  99. }
  100. }
  101. }