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.

HemfPicture.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.usermodel;
  16. import static java.lang.Math.abs;
  17. import java.awt.Graphics2D;
  18. import java.awt.Shape;
  19. import java.awt.geom.AffineTransform;
  20. import java.awt.geom.Dimension2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.InputStream;
  23. import java.nio.charset.Charset;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Spliterator;
  29. import java.util.function.Consumer;
  30. import java.util.function.Supplier;
  31. import org.apache.poi.common.usermodel.GenericRecord;
  32. import org.apache.poi.hemf.draw.HemfDrawProperties;
  33. import org.apache.poi.hemf.draw.HemfGraphics;
  34. import org.apache.poi.hemf.record.emf.HemfHeader;
  35. import org.apache.poi.hemf.record.emf.HemfRecord;
  36. import org.apache.poi.hemf.record.emf.HemfRecordIterator;
  37. import org.apache.poi.hemf.record.emf.HemfWindowing;
  38. import org.apache.poi.hwmf.usermodel.HwmfCharsetAware;
  39. import org.apache.poi.hwmf.usermodel.HwmfEmbedded;
  40. import org.apache.poi.util.Dimension2DDouble;
  41. import org.apache.poi.util.Internal;
  42. import org.apache.poi.util.LittleEndianInputStream;
  43. import org.apache.poi.util.LocaleUtil;
  44. import org.apache.poi.util.Units;
  45. /**
  46. * Read-only EMF extractor. Lots remain
  47. */
  48. @Internal
  49. public class HemfPicture implements Iterable<HemfRecord>, GenericRecord {
  50. private final LittleEndianInputStream stream;
  51. private final List<HemfRecord> records = new ArrayList<>();
  52. private boolean isParsed = false;
  53. private Charset defaultCharset = LocaleUtil.CHARSET_1252;
  54. public HemfPicture(InputStream is) {
  55. this(new LittleEndianInputStream(is));
  56. }
  57. public HemfPicture(LittleEndianInputStream is) {
  58. stream = is;
  59. }
  60. public HemfHeader getHeader() {
  61. return (HemfHeader)getRecords().get(0);
  62. }
  63. public List<HemfRecord> getRecords() {
  64. if (!isParsed) {
  65. // in case the (first) parsing throws an exception, we can provide the
  66. // records up to that point
  67. isParsed = true;
  68. HemfHeader[] header = new HemfHeader[1];
  69. new HemfRecordIterator(stream).forEachRemaining(r -> {
  70. if (r instanceof HemfHeader) {
  71. header[0] = (HemfHeader) r;
  72. }
  73. r.setHeader(header[0]);
  74. if (r instanceof HwmfCharsetAware) {
  75. ((HwmfCharsetAware)r).setCharsetProvider(this::getDefaultCharset);
  76. }
  77. records.add(r);
  78. });
  79. }
  80. return records;
  81. }
  82. @Override
  83. public Iterator<HemfRecord> iterator() {
  84. return getRecords().iterator();
  85. }
  86. @Override
  87. public Spliterator<HemfRecord> spliterator() {
  88. return getRecords().spliterator();
  89. }
  90. @Override
  91. public void forEach(Consumer<? super HemfRecord> action) {
  92. getRecords().forEach(action);
  93. }
  94. /**
  95. * Returns the bounding box in device-independent units - usually this is in .01 millimeter units
  96. *
  97. * @return the bounding box in device-independent units
  98. */
  99. public Rectangle2D getBounds() {
  100. Rectangle2D dim = getHeader().getFrameRectangle();
  101. double x = dim.getX(), y = dim.getY();
  102. double width = dim.getWidth(), height = dim.getHeight();
  103. if (dim.isEmpty() || Math.rint(width) == 0 || Math.rint(height) == 0) {
  104. for (HemfRecord r : getRecords()) {
  105. if (r instanceof HemfWindowing.EmfSetWindowExtEx) {
  106. HemfWindowing.EmfSetWindowExtEx extEx = (HemfWindowing.EmfSetWindowExtEx)r;
  107. Dimension2D d = extEx.getSize();
  108. width = d.getWidth();
  109. height = d.getHeight();
  110. // keep searching - sometimes there's another record
  111. }
  112. if (r instanceof HemfWindowing.EmfSetWindowOrgEx) {
  113. HemfWindowing.EmfSetWindowOrgEx orgEx = (HemfWindowing.EmfSetWindowOrgEx)r;
  114. x = orgEx.getX();
  115. y = orgEx.getY();
  116. }
  117. }
  118. }
  119. return new Rectangle2D.Double(x, y, width, height);
  120. }
  121. /**
  122. * Return the image bounds in points
  123. *
  124. * @return the image bounds in points
  125. */
  126. public Rectangle2D getBoundsInPoints() {
  127. return Units.pixelToPoints(getHeader().getBoundsRectangle());
  128. }
  129. /**
  130. * Return the image size in points
  131. *
  132. * @return the image size in points
  133. */
  134. public Dimension2D getSize() {
  135. final Rectangle2D b = getBoundsInPoints();
  136. return new Dimension2DDouble(abs(b.getWidth()), abs(b.getHeight()));
  137. }
  138. private static double minX(Rectangle2D bounds) {
  139. return Math.min(bounds.getMinX(), bounds.getMaxX());
  140. }
  141. private static double minY(Rectangle2D bounds) {
  142. return Math.min(bounds.getMinY(), bounds.getMaxY());
  143. }
  144. public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
  145. final Shape clip = ctx.getClip();
  146. final AffineTransform at = ctx.getTransform();
  147. try {
  148. Rectangle2D emfBounds = getHeader().getBoundsRectangle();
  149. // scale output bounds to image bounds
  150. ctx.translate(graphicsBounds.getCenterX(), graphicsBounds.getCenterY());
  151. ctx.scale(graphicsBounds.getWidth()/emfBounds.getWidth(), graphicsBounds.getHeight()/emfBounds.getHeight());
  152. ctx.translate(-emfBounds.getCenterX(), -emfBounds.getCenterY());
  153. HemfGraphics g = new HemfGraphics(ctx, emfBounds);
  154. HemfDrawProperties prop = g.getProperties();
  155. prop.setWindowOrg(emfBounds.getX(), emfBounds.getY());
  156. prop.setWindowExt(emfBounds.getWidth(), emfBounds.getHeight());
  157. prop.setViewportOrg(emfBounds.getX(), emfBounds.getY());
  158. prop.setViewportExt(emfBounds.getWidth(), emfBounds.getHeight());
  159. for (HemfRecord r : getRecords()) {
  160. try {
  161. g.draw(r);
  162. } catch (RuntimeException ignored) {
  163. }
  164. }
  165. } finally {
  166. ctx.setTransform(at);
  167. ctx.setClip(clip);
  168. }
  169. }
  170. public Iterable<HwmfEmbedded> getEmbeddings() {
  171. return () -> new HemfEmbeddedIterator(HemfPicture.this);
  172. }
  173. @Override
  174. public List<? extends GenericRecord> getGenericChildren() {
  175. return getRecords();
  176. }
  177. @Override
  178. public Map<String, Supplier<?>> getGenericProperties() {
  179. return null;
  180. }
  181. public void setDefaultCharset(Charset defaultCharset) {
  182. this.defaultCharset = defaultCharset;
  183. }
  184. public Charset getDefaultCharset() {
  185. return defaultCharset;
  186. }
  187. }