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.

HemfPlusHeader.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.emfplus;
  16. import java.io.IOException;
  17. import org.apache.poi.util.BitField;
  18. import org.apache.poi.util.BitFieldFactory;
  19. import org.apache.poi.util.Internal;
  20. import org.apache.poi.util.LittleEndianConsts;
  21. import org.apache.poi.util.LittleEndianInputStream;
  22. @Internal
  23. public class HemfPlusHeader implements HemfPlusRecord {
  24. /**
  25. * The GraphicsVersion enumeration defines versions of operating system graphics that are used to
  26. * create EMF+ metafiles.
  27. */
  28. public enum GraphicsVersion {
  29. V1(0x0001),
  30. V1_1(0x0002)
  31. ;
  32. public final int id;
  33. GraphicsVersion(int id) {
  34. this.id = id;
  35. }
  36. public static GraphicsVersion valueOf(int id) {
  37. for (GraphicsVersion wrt : values()) {
  38. if (wrt.id == id) return wrt;
  39. }
  40. return null;
  41. }
  42. }
  43. private int flags;
  44. private final EmfPlusGraphicsVersion version = new EmfPlusGraphicsVersion();
  45. private long emfPlusFlags;
  46. private long logicalDpiX;
  47. private long logicalDpiY;
  48. @Override
  49. public HemfPlusRecordType getEmfPlusRecordType() {
  50. return HemfPlusRecordType.header;
  51. }
  52. public int getFlags() {
  53. return flags;
  54. }
  55. @Override
  56. public long init(LittleEndianInputStream leis, long dataSize, long recordId, int flags) throws IOException {
  57. this.flags = flags;
  58. version.init(leis);
  59. assert(version.getMetafileSignature() == 0xDBC01 && version.getGraphicsVersion() != null);
  60. emfPlusFlags = leis.readUInt();
  61. logicalDpiX = leis.readUInt();
  62. logicalDpiY = leis.readUInt();
  63. return 4* LittleEndianConsts.INT_SIZE;
  64. }
  65. public EmfPlusGraphicsVersion getVersion() {
  66. return version;
  67. }
  68. public long getEmfPlusFlags() {
  69. return emfPlusFlags;
  70. }
  71. public long getLogicalDpiX() {
  72. return logicalDpiX;
  73. }
  74. public long getLogicalDpiY() {
  75. return logicalDpiY;
  76. }
  77. @Override
  78. public String toString() {
  79. return "HemfPlusHeader{" +
  80. "flags=" + flags +
  81. ", version=" + version +
  82. ", emfPlusFlags=" + emfPlusFlags +
  83. ", logicalDpiX=" + logicalDpiX +
  84. ", logicalDpiY=" + logicalDpiY +
  85. '}';
  86. }
  87. public static class EmfPlusGraphicsVersion {
  88. private static final BitField METAFILE_SIGNATURE = BitFieldFactory.getInstance(0xFFFFF000);
  89. private static final BitField GRAPHICS_VERSION = BitFieldFactory.getInstance(0x00000FFF);
  90. private int metafileSignature;
  91. private GraphicsVersion graphicsVersion;
  92. public int getMetafileSignature() {
  93. return metafileSignature;
  94. }
  95. public GraphicsVersion getGraphicsVersion() {
  96. return graphicsVersion;
  97. }
  98. public long init(LittleEndianInputStream leis) throws IOException {
  99. int val = leis.readInt();
  100. // A value that identifies the type of metafile. The value for an EMF+ metafile is 0xDBC01.
  101. metafileSignature = METAFILE_SIGNATURE.getValue(val);
  102. // The version of operating system graphics. This value MUST be defined in the GraphicsVersion enumeration
  103. graphicsVersion = GraphicsVersion.valueOf(GRAPHICS_VERSION.getValue(val));
  104. return LittleEndianConsts.INT_SIZE;
  105. }
  106. public String toString() {
  107. return "{ metafileSignature=0x"+Integer.toHexString(metafileSignature)+
  108. " , graphicsVersion='"+graphicsVersion+"' }";
  109. }
  110. }
  111. }