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.

HemfPlusRegion.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 static org.apache.poi.hemf.record.emfplus.HemfPlusDraw.readRectF;
  17. import java.awt.geom.Rectangle2D;
  18. import java.io.IOException;
  19. import java.util.List;
  20. import java.util.function.Consumer;
  21. import java.util.function.Supplier;
  22. import org.apache.poi.hemf.draw.HemfGraphics;
  23. import org.apache.poi.hemf.record.emfplus.HemfPlusHeader.EmfPlusGraphicsVersion;
  24. import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectData;
  25. import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectType;
  26. import org.apache.poi.hemf.record.emfplus.HemfPlusPath.EmfPlusPath;
  27. import org.apache.poi.util.LittleEndianConsts;
  28. import org.apache.poi.util.LittleEndianInputStream;
  29. public class HemfPlusRegion {
  30. public enum EmfPlusRegionNodeDataType {
  31. /**
  32. * Specifies a region node with child nodes. A Boolean AND operation SHOULD be applied to the left and right
  33. * child nodes specified by an EmfPlusRegionNodeChildNodes object
  34. */
  35. AND(0X00000001, EmfPlusRegionNode::new),
  36. /**
  37. * Specifies a region node with child nodes. A Boolean OR operation SHOULD be applied to the left and right
  38. * child nodes specified by an EmfPlusRegionNodeChildNodes object.
  39. */
  40. OR(0X00000002, EmfPlusRegionNode::new),
  41. /**
  42. * Specifies a region node with child nodes. A Boolean XOR operation SHOULD be applied to the left and right
  43. * child nodes specified by an EmfPlusRegionNodeChildNodes object.
  44. */
  45. XOR(0X00000003, EmfPlusRegionNode::new),
  46. /**
  47. * Specifies a region node with child nodes. A Boolean operation, defined as "the part of region 1 that is excluded
  48. * from region 2", SHOULD be applied to the left and right child nodes specified by an EmfPlusRegionNodeChildNodes object.
  49. */
  50. EXCLUDE(0X00000004, EmfPlusRegionNode::new),
  51. /**
  52. * Specifies a region node with child nodes. A Boolean operation, defined as "the part of region 2 that is excluded
  53. * from region 1", SHOULD be applied to the left and right child nodes specified by an EmfPlusRegionNodeChildNodes object.
  54. */
  55. COMPLEMENT(0X00000005, EmfPlusRegionNode::new),
  56. /**
  57. * Specifies a region node with no child nodes.
  58. * The RegionNodeData field SHOULD specify a boundary with an EmfPlusRectF object.
  59. */
  60. RECT(0X10000000, EmfPlusRegionRect::new),
  61. /**
  62. * Specifies a region node with no child nodes.
  63. * The RegionNodeData field SHOULD specify a boundary with an EmfPlusRegionNodePath object
  64. */
  65. PATH(0X10000001, EmfPlusRegionPath::new),
  66. /** Specifies a region node with no child nodes. The RegionNodeData field SHOULD NOT be present. */
  67. EMPTY(0X10000002, EmfPlusRegionEmpty::new),
  68. /** Specifies a region node with no child nodes, and its bounds are not defined. */
  69. INFINITE(0X10000003, EmfPlusRegionInfinite::new)
  70. ;
  71. public final int id;
  72. public final Supplier<EmfPlusRegionNodeData> constructor;
  73. EmfPlusRegionNodeDataType(int id, Supplier<EmfPlusRegionNodeData> constructor) {
  74. this.id = id;
  75. this.constructor = constructor;
  76. }
  77. public static EmfPlusRegionNodeDataType valueOf(int id) {
  78. for (EmfPlusRegionNodeDataType wrt : values()) {
  79. if (wrt.id == id) return wrt;
  80. }
  81. return null;
  82. }
  83. }
  84. public static class EmfPlusRegion implements EmfPlusObjectData {
  85. private final EmfPlusGraphicsVersion graphicsVersion = new EmfPlusGraphicsVersion();
  86. private EmfPlusRegionNodeData regionNode;
  87. @Override
  88. public long init(LittleEndianInputStream leis, long dataSize, EmfPlusObjectType objectType, int flags) throws IOException {
  89. long size = graphicsVersion.init(leis);
  90. // A 32-bit unsigned integer that specifies the number of child nodes in the RegionNode field.
  91. int nodeCount = leis.readInt();
  92. size += LittleEndianConsts.INT_SIZE;
  93. // An array of RegionNodeCount+1 EmfPlusRegionNode objects. Regions are specified as a binary tree of
  94. // region nodes, and each node MUST either be a terminal node or specify one or two child nodes.
  95. // RegionNode MUST contain at least one element.
  96. size += readNode(leis, d -> regionNode = d);
  97. return size;
  98. }
  99. @Override
  100. public void applyObject(HemfGraphics ctx, List<? extends EmfPlusObjectData> continuedObjectData) {
  101. }
  102. @Override
  103. public EmfPlusGraphicsVersion getGraphicsVersion() {
  104. return graphicsVersion;
  105. }
  106. }
  107. public interface EmfPlusRegionNodeData {
  108. long init(LittleEndianInputStream leis) throws IOException;
  109. }
  110. public static class EmfPlusRegionPath extends EmfPlusPath implements EmfPlusRegionNodeData {
  111. public long init(LittleEndianInputStream leis) throws IOException {
  112. int dataSize = leis.readInt();
  113. return super.init(leis, dataSize, EmfPlusObjectType.PATH, 0) + LittleEndianConsts.INT_SIZE;
  114. }
  115. }
  116. public static class EmfPlusRegionInfinite implements EmfPlusRegionNodeData {
  117. @Override
  118. public long init(LittleEndianInputStream leis) throws IOException {
  119. return 0;
  120. }
  121. }
  122. public static class EmfPlusRegionEmpty implements EmfPlusRegionNodeData {
  123. @Override
  124. public long init(LittleEndianInputStream leis) throws IOException {
  125. return 0;
  126. }
  127. }
  128. public static class EmfPlusRegionRect implements EmfPlusRegionNodeData {
  129. private final Rectangle2D rect = new Rectangle2D.Double();
  130. @Override
  131. public long init(LittleEndianInputStream leis) {
  132. return readRectF(leis, rect);
  133. }
  134. }
  135. public static class EmfPlusRegionNode implements EmfPlusRegionNodeData {
  136. private EmfPlusRegionNodeData left, right;
  137. @Override
  138. public long init(LittleEndianInputStream leis) throws IOException {
  139. long size = readNode(leis, n -> left = n);
  140. size += readNode(leis, n -> right = n);
  141. return size;
  142. }
  143. }
  144. private static long readNode(LittleEndianInputStream leis, Consumer<EmfPlusRegionNodeData> con) throws IOException {
  145. // A 32-bit unsigned integer that specifies the type of data in the RegionNodeData field.
  146. // This value MUST be defined in the RegionNodeDataType enumeration
  147. EmfPlusRegionNodeDataType type = EmfPlusRegionNodeDataType.valueOf(leis.readInt());
  148. assert(type != null);
  149. EmfPlusRegionNodeData nd = type.constructor.get();
  150. con.accept(nd);
  151. return LittleEndianConsts.INT_SIZE + nd.init(leis);
  152. }
  153. }