您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

HSLFFreeformShape.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.hslf.usermodel;
  16. import java.awt.geom.AffineTransform;
  17. import java.awt.geom.Path2D;
  18. import java.awt.geom.PathIterator;
  19. import java.awt.geom.Point2D;
  20. import java.awt.geom.Rectangle2D;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import org.apache.poi.ddf.AbstractEscherOptRecord;
  24. import org.apache.poi.ddf.EscherArrayProperty;
  25. import org.apache.poi.ddf.EscherContainerRecord;
  26. import org.apache.poi.ddf.EscherProperties;
  27. import org.apache.poi.ddf.EscherSimpleProperty;
  28. import org.apache.poi.sl.usermodel.FreeformShape;
  29. import org.apache.poi.sl.usermodel.ShapeContainer;
  30. import org.apache.poi.sl.usermodel.ShapeType;
  31. import org.apache.poi.util.LittleEndian;
  32. import org.apache.poi.util.POILogFactory;
  33. import org.apache.poi.util.POILogger;
  34. import org.apache.poi.util.Units;
  35. /**
  36. * A "Freeform" shape.
  37. *
  38. * <p>
  39. * Shapes drawn with the "Freeform" tool have cubic bezier curve segments in the smooth sections
  40. * and straight-line segments in the straight sections. This object closely corresponds to <code>java.awt.geom.GeneralPath</code>.
  41. * </p>
  42. */
  43. public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformShape<HSLFShape,HSLFTextParagraph> {
  44. private static final POILogger LOG = POILogFactory.getLogger(HSLFFreeformShape.class);
  45. enum ShapePath {
  46. LINES(0),
  47. LINES_CLOSED(1),
  48. CURVES(2),
  49. CURVES_CLOSED(3),
  50. COMPLEX(4);
  51. private final int flag;
  52. ShapePath(int flag) {
  53. this.flag = flag;
  54. }
  55. public int getFlag() {
  56. return flag;
  57. }
  58. static ShapePath valueOf(int flag) {
  59. for (ShapePath v : values()) {
  60. if (v.flag == flag) {
  61. return v;
  62. }
  63. }
  64. return null;
  65. }
  66. }
  67. /**
  68. * Create a Freeform object and initialize it from the supplied Record container.
  69. *
  70. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  71. * @param parent the parent of the shape
  72. */
  73. protected HSLFFreeformShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  74. super(escherRecord, parent);
  75. }
  76. /**
  77. * Create a new Freeform. This constructor is used when a new shape is created.
  78. *
  79. * @param parent the parent of this Shape. For example, if this text box is a cell
  80. * in a table then the parent is Table.
  81. */
  82. public HSLFFreeformShape(ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  83. super((EscherContainerRecord)null, parent);
  84. createSpContainer(ShapeType.NOT_PRIMITIVE, parent instanceof HSLFGroupShape);
  85. }
  86. /**
  87. * Create a new Freeform. This constructor is used when a new shape is created.
  88. *
  89. */
  90. public HSLFFreeformShape(){
  91. this(null);
  92. }
  93. @Override
  94. public int setPath(Path2D path) {
  95. Rectangle2D bounds = path.getBounds2D();
  96. PathIterator it = path.getPathIterator(null);
  97. List<byte[]> segInfo = new ArrayList<>();
  98. List<Point2D.Double> pntInfo = new ArrayList<>();
  99. boolean isClosed = false;
  100. int numPoints = 0;
  101. while (!it.isDone()) {
  102. double[] vals = new double[6];
  103. int type = it.currentSegment(vals);
  104. switch (type) {
  105. case PathIterator.SEG_MOVETO:
  106. pntInfo.add(new Point2D.Double(vals[0], vals[1]));
  107. segInfo.add(SEGMENTINFO_MOVETO);
  108. numPoints++;
  109. break;
  110. case PathIterator.SEG_LINETO:
  111. pntInfo.add(new Point2D.Double(vals[0], vals[1]));
  112. segInfo.add(SEGMENTINFO_LINETO);
  113. segInfo.add(SEGMENTINFO_ESCAPE);
  114. numPoints++;
  115. break;
  116. case PathIterator.SEG_CUBICTO:
  117. pntInfo.add(new Point2D.Double(vals[0], vals[1]));
  118. pntInfo.add(new Point2D.Double(vals[2], vals[3]));
  119. pntInfo.add(new Point2D.Double(vals[4], vals[5]));
  120. segInfo.add(SEGMENTINFO_CUBICTO);
  121. segInfo.add(SEGMENTINFO_ESCAPE2);
  122. numPoints++;
  123. break;
  124. case PathIterator.SEG_QUADTO:
  125. //TODO: figure out how to convert SEG_QUADTO into SEG_CUBICTO
  126. LOG.log(POILogger.WARN, "SEG_QUADTO is not supported");
  127. break;
  128. case PathIterator.SEG_CLOSE:
  129. pntInfo.add(pntInfo.get(0));
  130. segInfo.add(SEGMENTINFO_LINETO);
  131. segInfo.add(SEGMENTINFO_ESCAPE);
  132. segInfo.add(SEGMENTINFO_LINETO);
  133. segInfo.add(SEGMENTINFO_CLOSE);
  134. isClosed = true;
  135. numPoints++;
  136. break;
  137. default:
  138. LOG.log(POILogger.WARN, "Ignoring invalid segment type "+type);
  139. break;
  140. }
  141. it.next();
  142. }
  143. if(!isClosed) {
  144. segInfo.add(SEGMENTINFO_LINETO);
  145. }
  146. segInfo.add(SEGMENTINFO_END);
  147. AbstractEscherOptRecord opt = getEscherOptRecord();
  148. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__SHAPEPATH, 0x4));
  149. EscherArrayProperty verticesProp = new EscherArrayProperty((short)(EscherProperties.GEOMETRY__VERTICES + 0x4000), false, null);
  150. verticesProp.setNumberOfElementsInArray(pntInfo.size());
  151. verticesProp.setNumberOfElementsInMemory(pntInfo.size());
  152. verticesProp.setSizeOfElements(8);
  153. for (int i = 0; i < pntInfo.size(); i++) {
  154. Point2D.Double pnt = pntInfo.get(i);
  155. byte[] data = new byte[8];
  156. LittleEndian.putInt(data, 0, Units.pointsToMaster(pnt.getX() - bounds.getX()));
  157. LittleEndian.putInt(data, 4, Units.pointsToMaster(pnt.getY() - bounds.getY()));
  158. verticesProp.setElement(i, data);
  159. }
  160. opt.addEscherProperty(verticesProp);
  161. EscherArrayProperty segmentsProp = new EscherArrayProperty((short)(EscherProperties.GEOMETRY__SEGMENTINFO + 0x4000), false, null);
  162. segmentsProp.setNumberOfElementsInArray(segInfo.size());
  163. segmentsProp.setNumberOfElementsInMemory(segInfo.size());
  164. segmentsProp.setSizeOfElements(0x2);
  165. for (int i = 0; i < segInfo.size(); i++) {
  166. byte[] seg = segInfo.get(i);
  167. segmentsProp.setElement(i, seg);
  168. }
  169. opt.addEscherProperty(segmentsProp);
  170. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__RIGHT, Units.pointsToMaster(bounds.getWidth())));
  171. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__BOTTOM, Units.pointsToMaster(bounds.getHeight())));
  172. opt.sortProperties();
  173. setAnchor(bounds);
  174. return numPoints;
  175. }
  176. @Override
  177. public Path2D getPath(){
  178. Path2D path2D = new Path2D.Double();
  179. getGeometry(path2D);
  180. Rectangle2D bounds = path2D.getBounds2D();
  181. Rectangle2D anchor = getAnchor();
  182. AffineTransform at = new AffineTransform();
  183. at.translate(anchor.getX(), anchor.getY());
  184. at.scale(
  185. anchor.getWidth()/bounds.getWidth(),
  186. anchor.getHeight()/bounds.getHeight()
  187. );
  188. path2D.transform(at);
  189. return path2D;
  190. }
  191. }