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.

HemfPlusPath.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.awt.geom.Path2D;
  17. import java.awt.geom.Point2D;
  18. import java.io.IOException;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.function.BiFunction;
  22. import org.apache.poi.hemf.draw.HemfDrawProperties;
  23. import org.apache.poi.hemf.draw.HemfGraphics;
  24. import org.apache.poi.hemf.record.emfplus.HemfPlusDraw.EmfPlusCompressed;
  25. import org.apache.poi.hemf.record.emfplus.HemfPlusDraw.EmfPlusRelativePosition;
  26. import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectData;
  27. import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectType;
  28. import org.apache.poi.util.BitField;
  29. import org.apache.poi.util.BitFieldFactory;
  30. import org.apache.poi.util.LittleEndianConsts;
  31. import org.apache.poi.util.LittleEndianInputStream;
  32. public class HemfPlusPath {
  33. /** The PathPointType enumeration defines types of points on a graphics path. */
  34. public enum EmfPlusPathPointType {
  35. /** Specifies that the point is the starting point of a path. */
  36. START,
  37. /** Specifies that the point is one of the two endpoints of a line. */
  38. LINE,
  39. // not defined
  40. UNUSED,
  41. /** Specifies that the point is an endpoint or control point of a cubic Bezier curve */
  42. BEZIER;
  43. }
  44. public static class EmfPlusPath implements EmfPlusObjectData, EmfPlusCompressed, EmfPlusRelativePosition {
  45. /**
  46. * If set, the point types in the PathPointTypes array are specified by EmfPlusPathPointTypeRLE objects,
  47. * which use run-length encoding (RLE) compression, and/or EmfPlusPathPointType objects.
  48. * If clear, the point types in the PathPointTypes array are specified by EmfPlusPathPointType objects.
  49. */
  50. private static final BitField RLE_COMPRESSED = BitFieldFactory.getInstance(0x00001000);
  51. /** Specifies that a line segment that passes through the point is dashed. */
  52. private static final BitField POINT_TYPE_DASHED = BitFieldFactory.getInstance(0x10);
  53. /** Specifies that the point is a position marker. */
  54. private static final BitField POINT_TYPE_MARKER = BitFieldFactory.getInstance(0x20);
  55. /** Specifies that the point is the endpoint of a subpath. */
  56. private static final BitField POINT_TYPE_CLOSE = BitFieldFactory.getInstance(0x80);
  57. private static final BitField POINT_TYPE_ENUM = BitFieldFactory.getInstance(0x0F);
  58. private static final BitField POINT_RLE_BEZIER = BitFieldFactory.getInstance(0x80);
  59. private static final BitField POINT_RLE_COUNT = BitFieldFactory.getInstance(0x3F);
  60. private final HemfPlusHeader.EmfPlusGraphicsVersion graphicsVersion = new HemfPlusHeader.EmfPlusGraphicsVersion();
  61. private int pointFlags;
  62. private Point2D[] pathPoints;
  63. private byte[] pointTypes;
  64. @Override
  65. public long init(LittleEndianInputStream leis, long dataSize, EmfPlusObjectType objectType, int flags) throws IOException {
  66. long size = graphicsVersion.init(leis);
  67. // A 32-bit unsigned integer that specifies the number of points and associated point types that
  68. // are defined by this object.
  69. int pointCount = leis.readInt();
  70. // A 16-bit unsigned integer that specifies how to interpret the points
  71. // and associated point types that are defined by this object.
  72. pointFlags = leis.readShort();
  73. leis.skipFully(LittleEndianConsts.SHORT_SIZE);
  74. size += 2* LittleEndianConsts.INT_SIZE;
  75. BiFunction<LittleEndianInputStream,Point2D,Integer> readPoint;
  76. if (isRelativePosition()) {
  77. readPoint = HemfPlusDraw::readPointR;
  78. } else if (isCompressed()) {
  79. readPoint = HemfPlusDraw::readPointS;
  80. } else {
  81. readPoint = HemfPlusDraw::readPointF;
  82. }
  83. pathPoints = new Point2D[pointCount];
  84. for (int i=0; i<pointCount; i++) {
  85. pathPoints[i] = new Point2D.Double();
  86. size += readPoint.apply(leis,pathPoints[i]);
  87. }
  88. pointTypes = new byte[pointCount];
  89. final boolean isRLE = RLE_COMPRESSED.isSet(pointFlags);
  90. if (isRLE) {
  91. for (int i=0, rleCount; i<pointCount; i+=rleCount, size+=2) {
  92. rleCount = POINT_RLE_COUNT.getValue(leis.readByte());
  93. Arrays.fill(pointTypes, pointCount, pointCount+rleCount, leis.readByte());
  94. }
  95. } else {
  96. leis.readFully(pointTypes);
  97. size += pointCount;
  98. }
  99. int padding = (int)((4 - (size % 4)) % 4);
  100. leis.skipFully(padding);
  101. size += padding;
  102. return size;
  103. }
  104. @Override
  105. public HemfPlusHeader.EmfPlusGraphicsVersion getGraphicsVersion() {
  106. return graphicsVersion;
  107. }
  108. public boolean isPointDashed(int index) {
  109. return POINT_TYPE_DASHED.isSet(pointTypes[index]);
  110. }
  111. public boolean isPointMarker(int index) {
  112. return POINT_TYPE_MARKER.isSet(pointTypes[index]);
  113. }
  114. public boolean isPointClosed(int index) {
  115. return POINT_TYPE_CLOSE.isSet(pointTypes[index]);
  116. }
  117. public EmfPlusPathPointType getPointType(int index) {
  118. return EmfPlusPathPointType.values()[POINT_TYPE_ENUM.getValue(pointTypes[index])];
  119. }
  120. @Override
  121. public int getFlags() {
  122. return pointFlags;
  123. }
  124. @Override
  125. public void applyObject(HemfGraphics ctx, List<? extends EmfPlusObjectData> continuedObjectData) {
  126. HemfDrawProperties prop = ctx.getProperties();
  127. Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO);
  128. prop.setPath(path);
  129. for (int idx=0; idx < pathPoints.length; idx++) {
  130. Point2D p1 = pathPoints[idx];
  131. switch (getPointType(idx)) {
  132. case START:
  133. path.moveTo(p1.getX(), p1.getY());
  134. break;
  135. case LINE:
  136. path.lineTo(p1.getX(), p1.getY());
  137. break;
  138. case BEZIER: {
  139. Point2D p2 = pathPoints[++idx];
  140. Point2D p3 = pathPoints[++idx];
  141. path.curveTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
  142. break;
  143. }
  144. }
  145. if (isPointClosed(idx)) {
  146. path.closePath();
  147. }
  148. }
  149. }
  150. }
  151. }