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.

HwmfPlaceableHeader.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.record;
  16. import java.awt.geom.Rectangle2D;
  17. import java.io.IOException;
  18. import org.apache.poi.util.LittleEndianConsts;
  19. import org.apache.poi.util.LittleEndianInputStream;
  20. public class HwmfPlaceableHeader {
  21. public static final int WMF_HEADER_MAGIC = 0x9AC6CDD7;
  22. final Rectangle2D bounds;
  23. final int unitsPerInch;
  24. protected HwmfPlaceableHeader(LittleEndianInputStream leis) throws IOException {
  25. /*
  26. * HWmf (2 bytes): The resource handle to the metafile, when the metafile is in memory. When
  27. * the metafile is on disk, this field MUST contain 0x0000. This attribute of the metafile is
  28. * specified in the Type field of the META_HEADER record.
  29. */
  30. leis.readShort(); // ignore
  31. /*
  32. * BoundingBox (8 bytes): The destination rectangle, measured in logical units, for displaying
  33. * the metafile. The size of a logical unit is specified by the Inch field.
  34. */
  35. int x1 = leis.readShort();
  36. int y1 = leis.readShort();
  37. int x2 = leis.readShort();
  38. int y2 = leis.readShort();
  39. bounds = new Rectangle2D.Double(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x2-x1), Math.abs(y2-y1));
  40. /*
  41. * Inch (2 bytes): The number of logical units per inch used to represent the image.
  42. * This value can be used to scale an image.
  43. * By convention, an image is considered to be recorded at 1440 logical units (twips) per inch.
  44. * Thus, a value of 720 specifies that the image SHOULD be rendered at twice its normal size,
  45. * and a value of 2880 specifies that the image SHOULD be rendered at half its normal size.
  46. */
  47. unitsPerInch = leis.readShort();
  48. /*
  49. * Reserved (4 bytes): A field that is not used and MUST be set to 0x00000000.
  50. */
  51. leis.readInt();
  52. /*
  53. * Checksum (2 bytes): A checksum for the previous 10 16-bit values in the header.
  54. * This value can be used to determine whether the metafile has become corrupted.
  55. */
  56. leis.readShort();
  57. // sometimes the placeable header is filled/aligned to dwords.
  58. // check for padding 0 bytes.
  59. leis.mark(LittleEndianConsts.INT_SIZE);
  60. if (leis.readShort() != 0) {
  61. leis.reset();
  62. }
  63. }
  64. public static HwmfPlaceableHeader readHeader(LittleEndianInputStream leis) throws IOException {
  65. leis.mark(LittleEndianConsts.INT_SIZE);
  66. int magic = leis.readInt();
  67. if (magic == WMF_HEADER_MAGIC) {
  68. return new HwmfPlaceableHeader(leis);
  69. } else {
  70. leis.reset();
  71. return null;
  72. }
  73. }
  74. public Rectangle2D getBounds() {
  75. return bounds;
  76. }
  77. public int getUnitsPerInch() {
  78. return unitsPerInch;
  79. }
  80. }