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.

HemfHeader.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.record.emf;
  16. import static org.apache.poi.hemf.record.emf.HemfDraw.readDimensionInt;
  17. import static org.apache.poi.hemf.record.emf.HemfDraw.readRectL;
  18. import static org.apache.poi.hemf.record.emf.HemfRecordIterator.HEADER_SIZE;
  19. import static org.apache.poi.hwmf.record.HwmfDraw.boundsToString;
  20. import static org.apache.poi.hwmf.record.HwmfDraw.dimToString;
  21. import java.awt.geom.Dimension2D;
  22. import java.awt.geom.Rectangle2D;
  23. import java.io.IOException;
  24. import java.nio.charset.StandardCharsets;
  25. import org.apache.poi.util.Dimension2DDouble;
  26. import org.apache.poi.util.Internal;
  27. import org.apache.poi.util.LittleEndianConsts;
  28. import org.apache.poi.util.LittleEndianInputStream;
  29. /**
  30. * Extracts the full header from EMF files.
  31. * @see org.apache.poi.sl.image.ImageHeaderEMF
  32. */
  33. @Internal
  34. public class HemfHeader implements HemfRecord {
  35. private static final int MAX_RECORD_LENGTH = 1_000_000;
  36. private final Rectangle2D boundsRectangle = new Rectangle2D.Double();
  37. private final Rectangle2D frameRectangle = new Rectangle2D.Double();
  38. private long bytes;
  39. private long records;
  40. private int handles;
  41. private String description;
  42. private long nPalEntries;
  43. private boolean hasExtension1;
  44. private long cbPixelFormat;
  45. private long offPixelFormat;
  46. private long bOpenGL;
  47. private boolean hasExtension2;
  48. private final Dimension2D deviceDimension = new Dimension2DDouble();
  49. private final Dimension2D milliDimension = new Dimension2DDouble();
  50. private final Dimension2D microDimension = new Dimension2DDouble();
  51. public Rectangle2D getBoundsRectangle() {
  52. return boundsRectangle;
  53. }
  54. public Rectangle2D getFrameRectangle() {
  55. return frameRectangle;
  56. }
  57. public long getBytes() {
  58. return bytes;
  59. }
  60. public long getRecords() {
  61. return records;
  62. }
  63. public int getHandles() {
  64. return handles;
  65. }
  66. public String getDescription() { return description; }
  67. public long getnPalEntries() {
  68. return nPalEntries;
  69. }
  70. public boolean isHasExtension1() {
  71. return hasExtension1;
  72. }
  73. public long getCbPixelFormat() {
  74. return cbPixelFormat;
  75. }
  76. public long getOffPixelFormat() {
  77. return offPixelFormat;
  78. }
  79. public long getbOpenGL() {
  80. return bOpenGL;
  81. }
  82. public boolean isHasExtension2() {
  83. return hasExtension2;
  84. }
  85. public Dimension2D getDeviceDimension() {
  86. return deviceDimension;
  87. }
  88. public Dimension2D getMilliDimension() {
  89. return milliDimension;
  90. }
  91. public Dimension2D getMicroDimension() {
  92. return microDimension;
  93. }
  94. @Override
  95. public String toString() {
  96. return "HemfHeader{" +
  97. "boundsRectangle: " + boundsToString(boundsRectangle) +
  98. ", frameRectangle: " + boundsToString(frameRectangle) +
  99. ", bytes: " + bytes +
  100. ", records: " + records +
  101. ", handles: " + handles +
  102. ", description: '" + (description == null ? "" : description) + "'" +
  103. ", nPalEntries: " + nPalEntries +
  104. ", hasExtension1: " + hasExtension1 +
  105. ", cbPixelFormat: " + cbPixelFormat +
  106. ", offPixelFormat: " + offPixelFormat +
  107. ", bOpenGL: " + bOpenGL +
  108. ", hasExtension2: " + hasExtension2 +
  109. ", deviceDimension: " + dimToString(deviceDimension) +
  110. ", microDimension: " + dimToString(microDimension) +
  111. ", milliDimension: " + dimToString(milliDimension) +
  112. '}';
  113. }
  114. @Override
  115. public HemfRecordType getEmfRecordType() {
  116. return HemfRecordType.header;
  117. }
  118. @Override
  119. public long init(LittleEndianInputStream leis, long recordSize, long recordId) throws IOException {
  120. if (recordId != HemfRecordType.header.id) {
  121. throw new IOException("Not a valid EMF header. Record type:"+recordId);
  122. }
  123. int startIdx = leis.getReadIndex();
  124. //bounds
  125. long size = readRectL(leis, boundsRectangle);
  126. size += readRectL(leis, frameRectangle);
  127. int recordSignature = leis.readInt();
  128. if (recordSignature != 0x464D4520) {
  129. throw new IOException("bad record signature: " + recordSignature);
  130. }
  131. long version = leis.readInt();
  132. //According to the spec, MSOffice doesn't pay attention to this value.
  133. //It _should_ be 0x00010000
  134. bytes = leis.readUInt();
  135. records = leis.readUInt();
  136. handles = leis.readUShort();
  137. //reserved
  138. leis.skipFully(LittleEndianConsts.SHORT_SIZE);
  139. int nDescription = (int)leis.readUInt();
  140. int offDescription = (int)leis.readUInt();
  141. nPalEntries = leis.readUInt();
  142. size += 8*LittleEndianConsts.INT_SIZE;
  143. size += readDimensionInt(leis, deviceDimension);
  144. size += readDimensionInt(leis, milliDimension);
  145. if (nDescription > 0 && offDescription > 0) {
  146. int skip = (int)(offDescription - (size + HEADER_SIZE));
  147. leis.mark(skip+nDescription*2);
  148. leis.skipFully(skip);
  149. byte[] buf = new byte[(nDescription-1)*2];
  150. leis.readFully(buf);
  151. description = new String(buf, StandardCharsets.UTF_16LE).replace((char)0, ' ').trim();
  152. leis.reset();
  153. }
  154. if (size+12 <= recordSize) {
  155. hasExtension1 = true;
  156. cbPixelFormat = leis.readUInt();
  157. offPixelFormat = leis.readUInt();
  158. bOpenGL = leis.readUInt();
  159. size += 3*LittleEndianConsts.INT_SIZE;
  160. }
  161. if (size+8 <= recordSize) {
  162. hasExtension2 = true;
  163. size += readDimensionInt(leis, microDimension);
  164. }
  165. return size;
  166. }
  167. }