Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HemfImageRenderer.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.hemf.draw;
  16. import static org.apache.poi.hwmf.draw.HwmfImageRenderer.getOuterBounds;
  17. import java.awt.Graphics2D;
  18. import java.awt.Insets;
  19. import java.awt.RenderingHints;
  20. import java.awt.geom.Dimension2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.nio.charset.Charset;
  27. import org.apache.poi.common.usermodel.GenericRecord;
  28. import org.apache.poi.hemf.usermodel.HemfPicture;
  29. import org.apache.poi.hwmf.draw.HwmfGraphicsState;
  30. import org.apache.poi.hwmf.draw.HwmfImageRenderer;
  31. import org.apache.poi.sl.draw.BitmapImageRenderer;
  32. import org.apache.poi.sl.draw.Drawable;
  33. import org.apache.poi.sl.draw.EmbeddedExtractor;
  34. import org.apache.poi.sl.draw.ImageRenderer;
  35. import org.apache.poi.sl.usermodel.PictureData;
  36. import org.apache.poi.util.Units;
  37. @SuppressWarnings("unused")
  38. public class HemfImageRenderer implements ImageRenderer, EmbeddedExtractor {
  39. HemfPicture image;
  40. double alpha;
  41. boolean charsetInitialized = false;
  42. @Override
  43. public boolean canRender(String contentType) {
  44. return PictureData.PictureType.EMF.contentType.equalsIgnoreCase(contentType);
  45. }
  46. @Override
  47. public void loadImage(InputStream data, String contentType) throws IOException {
  48. if (!PictureData.PictureType.EMF.contentType.equals(contentType)) {
  49. throw new IOException("Invalid picture type");
  50. }
  51. image = new HemfPicture(data);
  52. }
  53. @Override
  54. public void loadImage(byte[] data, String contentType) throws IOException {
  55. if (!PictureData.PictureType.EMF.contentType.equals(contentType)) {
  56. throw new IOException("Invalid picture type");
  57. }
  58. image = new HemfPicture(new ByteArrayInputStream(data));
  59. }
  60. @Override
  61. public void setAlpha(double alpha) {
  62. this.alpha = alpha;
  63. }
  64. @Override
  65. public BufferedImage getImage() {
  66. return getImage(getDimension());
  67. }
  68. @Override
  69. public BufferedImage getImage(Dimension2D dim) {
  70. if (image == null) {
  71. return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  72. }
  73. BufferedImage bufImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
  74. Graphics2D g = bufImg.createGraphics();
  75. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  76. g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  77. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  78. g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  79. image.draw(g, new Rectangle2D.Double(0,0,dim.getWidth(),dim.getHeight()));
  80. g.dispose();
  81. return BitmapImageRenderer.setAlpha(bufImg, alpha);
  82. }
  83. @Override
  84. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor) {
  85. return drawImage(graphics, anchor, null);
  86. }
  87. @Override
  88. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor, Insets clip) {
  89. if (image == null) {
  90. return false;
  91. }
  92. Charset cs = (Charset)graphics.getRenderingHint(Drawable.DEFAULT_CHARSET);
  93. if (cs != null && !charsetInitialized) {
  94. setDefaultCharset(cs);
  95. }
  96. HwmfGraphicsState graphicsState = new HwmfGraphicsState();
  97. graphicsState.backup(graphics);
  98. try {
  99. if (clip != null) {
  100. graphics.clip(anchor);
  101. } else {
  102. clip = new Insets(0, 0, 0, 0);
  103. }
  104. image.draw(graphics, getOuterBounds(anchor, clip));
  105. } finally {
  106. graphicsState.restore(graphics);
  107. }
  108. return true;
  109. }
  110. @Override
  111. public GenericRecord getGenericRecord() {
  112. return image;
  113. }
  114. @Override
  115. public Iterable<EmbeddedPart> getEmbeddings() {
  116. return HwmfImageRenderer.getEmbeddings(image.getEmbeddings());
  117. }
  118. @Override
  119. public Rectangle2D getNativeBounds() {
  120. return image.getBounds();
  121. }
  122. @Override
  123. public Rectangle2D getBounds() {
  124. return Units.pointsToPixel(image == null ? new Rectangle2D.Double() : image.getBoundsInPoints());
  125. }
  126. @Override
  127. public void setDefaultCharset(Charset defaultCharset) {
  128. image.setDefaultCharset(defaultCharset);
  129. charsetInitialized = true;
  130. }
  131. }