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.

EMFHandler.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.util;
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.Dimension2D;
  22. import java.awt.geom.Rectangle2D;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.nio.charset.Charset;
  27. import java.util.Collections;
  28. import org.apache.poi.common.usermodel.GenericRecord;
  29. import org.apache.poi.sl.draw.BitmapImageRenderer;
  30. import org.apache.poi.sl.draw.DrawPictureShape;
  31. import org.apache.poi.sl.draw.EmbeddedExtractor;
  32. import org.apache.poi.sl.draw.EmbeddedExtractor.EmbeddedPart;
  33. import org.apache.poi.sl.draw.ImageRenderer;
  34. import org.apache.poi.sl.usermodel.PictureData;
  35. import org.apache.poi.util.Internal;
  36. @Internal
  37. class EMFHandler extends MFProxy {
  38. private ImageRenderer imgr = null;
  39. private InputStream is;
  40. @Override
  41. public void parse(File file) throws IOException {
  42. // stream needs to be kept open until the instance is closed
  43. is = file.toURI().toURL().openStream();
  44. parse(is);
  45. }
  46. @Override
  47. public void parse(InputStream is) throws IOException {
  48. imgr = DrawPictureShape.getImageRenderer(null, getContentType());
  49. if (imgr instanceof BitmapImageRenderer) {
  50. throw new PPTX2PNG.NoScratchpadException();
  51. }
  52. // stream needs to be kept open
  53. imgr.loadImage(is, getContentType());
  54. if (ignoreParse) {
  55. try {
  56. imgr.getDimension();
  57. } catch (Exception e) {
  58. // if (!quite) {
  59. // e.printStackTrace(System.err);
  60. // }
  61. }
  62. }
  63. }
  64. protected String getContentType() {
  65. return PictureData.PictureType.EMF.contentType;
  66. }
  67. @Override
  68. public Dimension2D getSize() {
  69. return imgr.getDimension();
  70. }
  71. @Override
  72. public String getTitle() {
  73. return "";
  74. }
  75. @Override
  76. public void draw(Graphics2D ctx) {
  77. Dimension2D dim = getSize();
  78. imgr.drawImage(ctx, new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight()));
  79. }
  80. @Override
  81. public void close() throws IOException {
  82. if (is != null) {
  83. try {
  84. is.close();
  85. } finally {
  86. is = null;
  87. }
  88. }
  89. }
  90. @Override
  91. public GenericRecord getRoot() {
  92. return imgr.getGenericRecord();
  93. }
  94. @Override
  95. public Iterable<EmbeddedPart> getEmbeddings(int slideNo) {
  96. return (imgr instanceof EmbeddedExtractor)
  97. ? ((EmbeddedExtractor) imgr).getEmbeddings()
  98. : Collections.emptyList();
  99. }
  100. @Override
  101. void setDefaultCharset(Charset charset) {
  102. imgr.setDefaultCharset(charset);
  103. }
  104. }