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.

HwmfPicture.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.hwmf.usermodel;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics2D;
  18. import java.awt.geom.AffineTransform;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.BufferedInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.apache.poi.hwmf.draw.HwmfGraphics;
  27. import org.apache.poi.hwmf.record.HwmfHeader;
  28. import org.apache.poi.hwmf.record.HwmfPlaceableHeader;
  29. import org.apache.poi.hwmf.record.HwmfRecord;
  30. import org.apache.poi.hwmf.record.HwmfRecordType;
  31. import org.apache.poi.hwmf.record.HwmfWindowing.WmfSetWindowExt;
  32. import org.apache.poi.hwmf.record.HwmfWindowing.WmfSetWindowOrg;
  33. import org.apache.poi.util.IOUtils;
  34. import org.apache.poi.util.LittleEndianInputStream;
  35. import org.apache.poi.util.POILogFactory;
  36. import org.apache.poi.util.POILogger;
  37. import org.apache.poi.util.RecordFormatException;
  38. import org.apache.poi.util.Units;
  39. public class HwmfPicture {
  40. /** Max. record length - processing longer records will throw an exception */
  41. public static final int MAX_RECORD_LENGTH = 50_000_000;
  42. private static final POILogger logger = POILogFactory.getLogger(HwmfPicture.class);
  43. final List<HwmfRecord> records = new ArrayList<>();
  44. final HwmfPlaceableHeader placeableHeader;
  45. final HwmfHeader header;
  46. public HwmfPicture(InputStream inputStream) throws IOException {
  47. try (LittleEndianInputStream leis = new LittleEndianInputStream(inputStream)) {
  48. placeableHeader = HwmfPlaceableHeader.readHeader(leis);
  49. header = new HwmfHeader(leis);
  50. for (;;) {
  51. long recordSize;
  52. int recordFunction;
  53. try {
  54. // recordSize in DWORDs
  55. long recordSizeLong = leis.readUInt()*2;
  56. if (recordSizeLong > Integer.MAX_VALUE) {
  57. throw new RecordFormatException("record size can't be > "+Integer.MAX_VALUE);
  58. } else if (recordSizeLong < 0L) {
  59. throw new RecordFormatException("record size can't be < 0");
  60. }
  61. recordSize = (int)recordSizeLong;
  62. recordFunction = leis.readShort();
  63. } catch (Exception e) {
  64. logger.log(POILogger.ERROR, "unexpected eof - wmf file was truncated");
  65. break;
  66. }
  67. // 4 bytes (recordSize) + 2 bytes (recordFunction)
  68. int consumedSize = 6;
  69. HwmfRecordType wrt = HwmfRecordType.getById(recordFunction);
  70. if (wrt == null) {
  71. throw new IOException("unexpected record type: "+recordFunction);
  72. }
  73. if (wrt == HwmfRecordType.eof) {
  74. break;
  75. }
  76. if (wrt.constructor == null) {
  77. throw new IOException("unsupported record type: "+recordFunction);
  78. }
  79. final HwmfRecord wr = wrt.constructor.get();
  80. records.add(wr);
  81. consumedSize += wr.init(leis, recordSize, recordFunction);
  82. int remainingSize = (int)(recordSize - consumedSize);
  83. if (remainingSize < 0) {
  84. throw new RecordFormatException("read too many bytes. record size: "+recordSize + "; comsumed size: "+consumedSize);
  85. } else if(remainingSize > 0) {
  86. long skipped = IOUtils.skipFully(leis, remainingSize);
  87. if (skipped != (long)remainingSize) {
  88. throw new RecordFormatException("Tried to skip "+remainingSize + " but skipped: "+skipped);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. public List<HwmfRecord> getRecords() {
  95. return Collections.unmodifiableList(records);
  96. }
  97. public void draw(Graphics2D ctx) {
  98. Dimension dim = getSize();
  99. int width = Units.pointsToPixel(dim.getWidth());
  100. // keep aspect ratio for height
  101. int height = Units.pointsToPixel(dim.getHeight());
  102. Rectangle2D bounds = new Rectangle2D.Double(0,0,width,height);
  103. draw(ctx, bounds);
  104. }
  105. public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
  106. AffineTransform at = ctx.getTransform();
  107. try {
  108. Rectangle2D wmfBounds = getBounds();
  109. // scale output bounds to image bounds
  110. ctx.translate(graphicsBounds.getX(), graphicsBounds.getY());
  111. ctx.scale(graphicsBounds.getWidth()/wmfBounds.getWidth(), graphicsBounds.getHeight()/wmfBounds.getHeight());
  112. HwmfGraphics g = new HwmfGraphics(ctx, wmfBounds);
  113. for (HwmfRecord r : records) {
  114. r.draw(g);
  115. }
  116. } finally {
  117. ctx.setTransform(at);
  118. }
  119. }
  120. /**
  121. * Returns the bounding box in device-independent units. Usually this is taken from the placeable header.
  122. *
  123. * @return the bounding box
  124. */
  125. public Rectangle2D getBounds() {
  126. if (placeableHeader != null) {
  127. return placeableHeader.getBounds();
  128. } else {
  129. WmfSetWindowOrg wOrg = null;
  130. WmfSetWindowExt wExt = null;
  131. for (HwmfRecord r : getRecords()) {
  132. if (wOrg != null && wExt != null) {
  133. break;
  134. }
  135. if (r instanceof WmfSetWindowOrg) {
  136. wOrg = (WmfSetWindowOrg)r;
  137. } else if (r instanceof WmfSetWindowExt) {
  138. wExt = (WmfSetWindowExt)r;
  139. }
  140. }
  141. if (wOrg == null || wExt == null) {
  142. throw new RuntimeException("invalid wmf file - window records are incomplete.");
  143. }
  144. return new Rectangle2D.Double(wOrg.getX(), wOrg.getY(), wExt.getSize().getWidth(), wExt.getSize().getHeight());
  145. }
  146. }
  147. public HwmfPlaceableHeader getPlaceableHeader() {
  148. return placeableHeader;
  149. }
  150. public HwmfHeader getHeader() {
  151. return header;
  152. }
  153. /**
  154. * Return the image size in points
  155. *
  156. * @return the image size in points
  157. */
  158. public Dimension getSize() {
  159. double inch = (placeableHeader == null) ? 1440 : placeableHeader.getUnitsPerInch();
  160. Rectangle2D bounds = getBounds();
  161. //coefficient to translate from WMF dpi to 72dpi
  162. double coeff = Units.POINT_DPI/inch;
  163. return new Dimension((int)Math.round(bounds.getWidth()*coeff), (int)Math.round(bounds.getHeight()*coeff));
  164. }
  165. }