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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.hemf.draw.HemfGraphics;
  18. import org.apache.poi.hemf.draw.HemfGraphics.EmfRenderState;
  19. import org.apache.poi.util.BitField;
  20. import org.apache.poi.util.BitFieldFactory;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.util.LittleEndianConsts;
  23. import org.apache.poi.util.LittleEndianInputStream;
  24. @Internal
  25. public class HemfPlusHeader implements HemfPlusRecord {
  26. /**
  27. * The GraphicsVersion enumeration defines versions of operating system graphics that are used to
  28. * create EMF+ metafiles.
  29. */
  30. public enum GraphicsVersion {
  31. V1(0x0001),
  32. V1_1(0x0002)
  33. ;
  34. public final int id;
  35. GraphicsVersion(int id) {
  36. this.id = id;
  37. }
  38. public static GraphicsVersion valueOf(int id) {
  39. for (GraphicsVersion wrt : values()) {
  40. if (wrt.id == id) return wrt;
  41. }
  42. return null;
  43. }
  44. }
  45. private int flags;
  46. private final EmfPlusGraphicsVersion version = new EmfPlusGraphicsVersion();
  47. private long emfPlusFlags;
  48. private long logicalDpiX;
  49. private long logicalDpiY;
  50. @Override
  51. public HemfPlusRecordType getEmfPlusRecordType() {
  52. return HemfPlusRecordType.header;
  53. }
  54. public int getFlags() {
  55. return flags;
  56. }
  57. @Override
  58. public long init(LittleEndianInputStream leis, long dataSize, long recordId, int flags) throws IOException {
  59. this.flags = flags;
  60. version.init(leis);
  61. assert(version.getMetafileSignature() == 0xDBC01 && version.getGraphicsVersion() != null);
  62. emfPlusFlags = leis.readUInt();
  63. logicalDpiX = leis.readUInt();
  64. logicalDpiY = leis.readUInt();
  65. return 4* LittleEndianConsts.INT_SIZE;
  66. }
  67. public EmfPlusGraphicsVersion getVersion() {
  68. return version;
  69. }
  70. /**
  71. * If set, this flag indicates that this metafile is "dual-mode", which means that it contains two sets of records,
  72. * each of which completely specifies the graphics content. If clear, the graphics content is specified by EMF+
  73. * records, and possibly EMF records that are preceded by an EmfPlusGetDC record. If this flag is set, EMF records
  74. * alone SHOULD suffice to define the graphics content. Note that whether the "dual-mode" flag is set or not, some
  75. * EMF records are always present, namely EMF control records and the EMF records that contain EMF+ records.
  76. *
  77. * @return {@code true} if dual-mode is enabled
  78. */
  79. public boolean isEmfPlusDualMode() {
  80. return (emfPlusFlags & 1) == 1;
  81. }
  82. public long getEmfPlusFlags() {
  83. return emfPlusFlags;
  84. }
  85. public long getLogicalDpiX() {
  86. return logicalDpiX;
  87. }
  88. public long getLogicalDpiY() {
  89. return logicalDpiY;
  90. }
  91. @Override
  92. public void draw(HemfGraphics ctx) {
  93. // currently EMF is better supported than EMF+ ... so if there's a complete set of EMF records available,
  94. // disable EMF+ rendering for now
  95. ctx.setRenderState(isEmfPlusDualMode() ? EmfRenderState.EMF_ONLY : EmfRenderState.EMFPLUS_ONLY);
  96. }
  97. @Override
  98. public String toString() {
  99. return "HemfPlusHeader{" +
  100. "flags=" + flags +
  101. ", version=" + version +
  102. ", emfPlusFlags=" + emfPlusFlags +
  103. ", logicalDpiX=" + logicalDpiX +
  104. ", logicalDpiY=" + logicalDpiY +
  105. '}';
  106. }
  107. public static class EmfPlusGraphicsVersion {
  108. private static final BitField METAFILE_SIGNATURE = BitFieldFactory.getInstance(0xFFFFF000);
  109. private static final BitField GRAPHICS_VERSION = BitFieldFactory.getInstance(0x00000FFF);
  110. private int metafileSignature;
  111. private GraphicsVersion graphicsVersion;
  112. public int getMetafileSignature() {
  113. return metafileSignature;
  114. }
  115. public GraphicsVersion getGraphicsVersion() {
  116. return graphicsVersion;
  117. }
  118. public long init(LittleEndianInputStream leis) throws IOException {
  119. int val = leis.readInt();
  120. // A value that identifies the type of metafile. The value for an EMF+ metafile is 0xDBC01.
  121. metafileSignature = METAFILE_SIGNATURE.getValue(val);
  122. // The version of operating system graphics. This value MUST be defined in the GraphicsVersion enumeration
  123. graphicsVersion = GraphicsVersion.valueOf(GRAPHICS_VERSION.getValue(val));
  124. return LittleEndianConsts.INT_SIZE;
  125. }
  126. public String toString() {
  127. return "{ metafileSignature=0x"+Integer.toHexString(metafileSignature)+
  128. " , graphicsVersion='"+graphicsVersion+"' }";
  129. }
  130. }
  131. }