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.

HSLFAutoShape.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.Arc2D;
  17. import java.awt.geom.Path2D;
  18. import java.awt.geom.Point2D;
  19. import java.awt.geom.Rectangle2D;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.apache.poi.ddf.AbstractEscherOptRecord;
  23. import org.apache.poi.ddf.EscherArrayProperty;
  24. import org.apache.poi.ddf.EscherContainerRecord;
  25. import org.apache.poi.ddf.EscherProperties;
  26. import org.apache.poi.ddf.EscherProperty;
  27. import org.apache.poi.ddf.EscherSimpleProperty;
  28. import org.apache.poi.sl.draw.binding.CTAdjPoint2D;
  29. import org.apache.poi.sl.draw.binding.CTCustomGeometry2D;
  30. import org.apache.poi.sl.draw.binding.CTPath2D;
  31. import org.apache.poi.sl.draw.binding.CTPath2DArcTo;
  32. import org.apache.poi.sl.draw.binding.CTPath2DCubicBezierTo;
  33. import org.apache.poi.sl.draw.binding.CTPath2DLineTo;
  34. import org.apache.poi.sl.draw.binding.CTPath2DList;
  35. import org.apache.poi.sl.draw.binding.CTPath2DMoveTo;
  36. import org.apache.poi.sl.draw.binding.ObjectFactory;
  37. import org.apache.poi.sl.draw.geom.CustomGeometry;
  38. import org.apache.poi.sl.usermodel.AutoShape;
  39. import org.apache.poi.sl.usermodel.ShapeContainer;
  40. import org.apache.poi.sl.usermodel.ShapeType;
  41. import org.apache.poi.sl.usermodel.VerticalAlignment;
  42. import org.apache.poi.ss.usermodel.ShapeTypes;
  43. import org.apache.poi.util.BitField;
  44. import org.apache.poi.util.BitFieldFactory;
  45. import org.apache.poi.util.LittleEndian;
  46. import org.apache.poi.util.POILogFactory;
  47. import org.apache.poi.util.POILogger;
  48. /**
  49. * Represents an AutoShape.<p>
  50. *
  51. * AutoShapes are drawing objects with a particular shape that may be customized through smart resizing and adjustments.
  52. * See {@link ShapeTypes}
  53. */
  54. public class HSLFAutoShape extends HSLFTextShape implements AutoShape<HSLFShape,HSLFTextParagraph> {
  55. private static final POILogger LOG = POILogFactory.getLogger(HSLFAutoShape.class);
  56. static final byte[] SEGMENTINFO_MOVETO = new byte[]{0x00, 0x40};
  57. static final byte[] SEGMENTINFO_LINETO = new byte[]{0x00, (byte)0xAC};
  58. static final byte[] SEGMENTINFO_ESCAPE = new byte[]{0x01, 0x00};
  59. static final byte[] SEGMENTINFO_ESCAPE2 = new byte[]{0x01, 0x20};
  60. static final byte[] SEGMENTINFO_CUBICTO = new byte[]{0x00, (byte)0xAD};
  61. // OpenOffice inserts 0xB3 instead of 0xAD.
  62. // protected static final byte[] SEGMENTINFO_CUBICTO2 = new byte[]{0x00, (byte)0xB3};
  63. static final byte[] SEGMENTINFO_CLOSE = new byte[]{0x01, (byte)0x60};
  64. static final byte[] SEGMENTINFO_END = new byte[]{0x00, (byte)0x80};
  65. private static final BitField PATH_INFO = BitFieldFactory.getInstance(0xE000);
  66. private static final BitField ESCAPE_INFO = BitFieldFactory.getInstance(0x1F00);
  67. enum PathInfo {
  68. lineTo(0),curveTo(1),moveTo(2),close(3),end(4),escape(5),clientEscape(6);
  69. private final int flag;
  70. PathInfo(int flag) {
  71. this.flag = flag;
  72. }
  73. public int getFlag() {
  74. return flag;
  75. }
  76. static PathInfo valueOf(int flag) {
  77. for (PathInfo v : values()) {
  78. if (v.flag == flag) {
  79. return v;
  80. }
  81. }
  82. return null;
  83. }
  84. }
  85. enum EscapeInfo {
  86. EXTENSION(0x0000),
  87. ANGLE_ELLIPSE_TO(0x0001),
  88. ANGLE_ELLIPSE(0x0002),
  89. ARC_TO(0x0003),
  90. ARC(0x0004),
  91. CLOCKWISE_ARC_TO(0x0005),
  92. CLOCKWISE_ARC(0x0006),
  93. ELLIPTICAL_QUADRANT_X(0x0007),
  94. ELLIPTICAL_QUADRANT_Y(0x0008),
  95. QUADRATIC_BEZIER(0x0009),
  96. NO_FILL(0X000A),
  97. NO_LINE(0X000B),
  98. AUTO_LINE(0X000C),
  99. AUTO_CURVE(0X000D),
  100. CORNER_LINE(0X000E),
  101. CORNER_CURVE(0X000F),
  102. SMOOTH_LINE(0X0010),
  103. SMOOTH_CURVE(0X0011),
  104. SYMMETRIC_LINE(0X0012),
  105. SYMMETRIC_CURVE(0X0013),
  106. FREEFORM(0X0014),
  107. FILL_COLOR(0X0015),
  108. LINE_COLOR(0X0016);
  109. private final int flag;
  110. EscapeInfo(int flag) {
  111. this.flag = flag;
  112. }
  113. public int getFlag() {
  114. return flag;
  115. }
  116. static EscapeInfo valueOf(int flag) {
  117. for (EscapeInfo v : values()) {
  118. if (v.flag == flag) {
  119. return v;
  120. }
  121. }
  122. return null;
  123. }
  124. }
  125. protected HSLFAutoShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  126. super(escherRecord, parent);
  127. }
  128. public HSLFAutoShape(ShapeType type, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  129. super(null, parent);
  130. createSpContainer(type, parent instanceof HSLFGroupShape);
  131. }
  132. public HSLFAutoShape(ShapeType type){
  133. this(type, null);
  134. }
  135. protected EscherContainerRecord createSpContainer(ShapeType shapeType, boolean isChild){
  136. EscherContainerRecord ecr = super.createSpContainer(isChild);
  137. setShapeType(shapeType);
  138. //set default properties for an autoshape
  139. setEscherProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x40000);
  140. setEscherProperty(EscherProperties.FILL__FILLCOLOR, 0x8000004);
  141. setEscherProperty(EscherProperties.FILL__FILLCOLOR, 0x8000004);
  142. setEscherProperty(EscherProperties.FILL__FILLBACKCOLOR, 0x8000000);
  143. setEscherProperty(EscherProperties.FILL__NOFILLHITTEST, 0x100010);
  144. setEscherProperty(EscherProperties.LINESTYLE__COLOR, 0x8000001);
  145. setEscherProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80008);
  146. setEscherProperty(EscherProperties.SHADOWSTYLE__COLOR, 0x8000002);
  147. return ecr;
  148. }
  149. @Override
  150. protected void setDefaultTextProperties(HSLFTextParagraph _txtrun){
  151. setVerticalAlignment(VerticalAlignment.MIDDLE);
  152. setHorizontalCentered(true);
  153. setWordWrap(false);
  154. }
  155. /**
  156. * Gets adjust value which controls smart resizing of the auto-shape.<p>
  157. *
  158. * The adjustment values are given in shape coordinates:
  159. * the origin is at the top-left, positive-x is to the right, positive-y is down.
  160. * The region from (0,0) to (S,S) maps to the geometry box of the shape (S=21600 is a constant).
  161. *
  162. * @param idx the adjust index in the [0, 9] range
  163. * @return the adjustment value
  164. */
  165. public int getAdjustmentValue(int idx){
  166. if(idx < 0 || idx > 9) throw new IllegalArgumentException("The index of an adjustment value must be in the [0, 9] range");
  167. return getEscherProperty((short)(EscherProperties.GEOMETRY__ADJUSTVALUE + idx));
  168. }
  169. /**
  170. * Sets adjust value which controls smart resizing of the auto-shape.<p>
  171. *
  172. * The adjustment values are given in shape coordinates:
  173. * the origin is at the top-left, positive-x is to the right, positive-y is down.
  174. * The region from (0,0) to (S,S) maps to the geometry box of the shape (S=21600 is a constant).
  175. *
  176. * @param idx the adjust index in the [0, 9] range
  177. * @param val the adjustment value
  178. */
  179. public void setAdjustmentValue(int idx, int val){
  180. if(idx < 0 || idx > 9) throw new IllegalArgumentException("The index of an adjustment value must be in the [0, 9] range");
  181. setEscherProperty((short)(EscherProperties.GEOMETRY__ADJUSTVALUE + idx), val);
  182. }
  183. @Override
  184. public CustomGeometry getGeometry() {
  185. return getGeometry(new Path2D.Double());
  186. }
  187. CustomGeometry getGeometry(Path2D path2D) {
  188. final ObjectFactory of = new ObjectFactory();
  189. final CTCustomGeometry2D cusGeo = of.createCTCustomGeometry2D();
  190. cusGeo.setAvLst(of.createCTGeomGuideList());
  191. cusGeo.setGdLst(of.createCTGeomGuideList());
  192. cusGeo.setAhLst(of.createCTAdjustHandleList());
  193. cusGeo.setCxnLst(of.createCTConnectionSiteList());
  194. final AbstractEscherOptRecord opt = getEscherOptRecord();
  195. EscherArrayProperty verticesProp = getShapeProp(opt, EscherProperties.GEOMETRY__VERTICES);
  196. EscherArrayProperty segmentsProp = getShapeProp(opt, EscherProperties.GEOMETRY__SEGMENTINFO);
  197. // return empty path if either GEOMETRY__VERTICES or GEOMETRY__SEGMENTINFO is missing, see Bugzilla 54188
  198. //sanity check
  199. if(verticesProp == null) {
  200. LOG.log(POILogger.WARN, "Freeform is missing GEOMETRY__VERTICES ");
  201. return super.getGeometry();
  202. }
  203. if(segmentsProp == null) {
  204. LOG.log(POILogger.WARN, "Freeform is missing GEOMETRY__SEGMENTINFO ");
  205. return super.getGeometry();
  206. }
  207. final Iterator<byte[]> vertIter = verticesProp.iterator();
  208. final Iterator<byte[]> segIter = segmentsProp.iterator();
  209. final int[] xyPoints = new int[2];
  210. boolean isClosed = false;
  211. final CTPath2DList pathLst = of.createCTPath2DList();
  212. final CTPath2D pathCT = of.createCTPath2D();
  213. final List<Object> moveLst = pathCT.getCloseOrMoveToOrLnTo();
  214. pathLst.getPath().add(pathCT);
  215. cusGeo.setPathLst(pathLst);
  216. while (segIter.hasNext()) {
  217. byte[] segElem = segIter.next();
  218. HSLFFreeformShape.PathInfo pi = getPathInfo(segElem);
  219. if (pi == null) {
  220. continue;
  221. }
  222. switch (pi) {
  223. case escape: {
  224. handleEscapeInfo(pathCT, path2D, segElem, vertIter);
  225. break;
  226. }
  227. case moveTo:
  228. if (vertIter.hasNext()) {
  229. final CTPath2DMoveTo m = of.createCTPath2DMoveTo();
  230. m.setPt(fillPoint(vertIter.next(), xyPoints));
  231. moveLst.add(m);
  232. path2D.moveTo(xyPoints[0], xyPoints[1]);
  233. }
  234. break;
  235. case lineTo:
  236. if (vertIter.hasNext()) {
  237. final CTPath2DLineTo m = of.createCTPath2DLineTo();
  238. m.setPt(fillPoint(vertIter.next(), xyPoints));
  239. moveLst.add(m);
  240. path2D.lineTo(xyPoints[0], xyPoints[1]);
  241. }
  242. break;
  243. case curveTo: {
  244. final CTPath2DCubicBezierTo m = of.createCTPath2DCubicBezierTo();
  245. List<CTAdjPoint2D> mLst = m.getPt();
  246. int[] pts = new int[6];
  247. for (int i=0; vertIter.hasNext() && i<3; i++) {
  248. mLst.add(fillPoint(vertIter.next(), xyPoints));
  249. pts[i*2] = xyPoints[0];
  250. pts[i*2+1] = xyPoints[1];
  251. if (i == 2) {
  252. moveLst.add(m);
  253. path2D.curveTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
  254. }
  255. }
  256. break;
  257. }
  258. case close:
  259. moveLst.add(of.createCTPath2DClose());
  260. path2D.closePath();
  261. isClosed = true;
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. EscherSimpleProperty shapePath = getShapeProp(opt, EscherProperties.GEOMETRY__SHAPEPATH);
  268. HSLFFreeformShape.ShapePath sp = HSLFFreeformShape.ShapePath.valueOf(shapePath == null ? 1 : shapePath.getPropertyValue());
  269. if ((sp == HSLFFreeformShape.ShapePath.LINES_CLOSED || sp == HSLFFreeformShape.ShapePath.CURVES_CLOSED) && !isClosed) {
  270. moveLst.add(of.createCTPath2DClose());
  271. path2D.closePath();
  272. }
  273. EscherSimpleProperty geoLeft = getShapeProp(opt, EscherProperties.GEOMETRY__LEFT);
  274. EscherSimpleProperty geoRight = getShapeProp(opt, EscherProperties.GEOMETRY__RIGHT);
  275. EscherSimpleProperty geoTop = getShapeProp(opt, EscherProperties.GEOMETRY__TOP);
  276. EscherSimpleProperty geoBottom = getShapeProp(opt, EscherProperties.GEOMETRY__BOTTOM);
  277. final Rectangle2D bounds;
  278. if (geoLeft != null && geoRight != null && geoTop != null && geoBottom != null) {
  279. bounds = new Rectangle2D.Double();
  280. bounds.setFrameFromDiagonal(
  281. new Point2D.Double(geoLeft.getPropertyValue(), geoTop.getPropertyValue()),
  282. new Point2D.Double(geoRight.getPropertyValue(), geoBottom.getPropertyValue())
  283. );
  284. } else {
  285. bounds = path2D.getBounds2D();
  286. }
  287. pathCT.setW((int)Math.rint(bounds.getWidth()));
  288. pathCT.setH((int)Math.rint(bounds.getHeight()));
  289. return new CustomGeometry(cusGeo);
  290. }
  291. private void handleEscapeInfo(CTPath2D pathCT, Path2D path2D, byte[] segElem, Iterator<byte[]> vertIter) {
  292. final ObjectFactory of = new ObjectFactory();
  293. HSLFFreeformShape.EscapeInfo ei = getEscapeInfo(segElem);
  294. switch (ei) {
  295. case EXTENSION:
  296. break;
  297. case ANGLE_ELLIPSE_TO:
  298. break;
  299. case ANGLE_ELLIPSE:
  300. break;
  301. case ARC_TO: {
  302. // The first two POINT values specify the bounding rectangle of the ellipse.
  303. // The second two POINT values specify the radial vectors for the ellipse.
  304. // The radial vectors are cast from the center of the bounding rectangle.
  305. // The path starts at the POINT where the first radial vector intersects the
  306. // bounding rectangle and goes to the POINT where the second radial vector
  307. // intersects the bounding rectangle. The drawing direction is always counterclockwise.
  308. // If the path has already been started, a line is drawn from the last POINT to
  309. // the starting POINT of the arc; otherwise, a new path is started.
  310. // The number of arc segments drawn equals the number of segments divided by four.
  311. int[] r1 = new int[2], r2 = new int[2], start = new int[2], end = new int[2];
  312. fillPoint(vertIter.next(), r1);
  313. fillPoint(vertIter.next(), r2);
  314. fillPoint(vertIter.next(), start);
  315. fillPoint(vertIter.next(), end);
  316. Arc2D arc2D = new Arc2D.Double();
  317. Rectangle2D.Double bounds = new Rectangle2D.Double();
  318. bounds.setFrameFromDiagonal(xy2p(r1), xy2p(r2));
  319. arc2D.setFrame(bounds);
  320. arc2D.setAngles(xy2p(start), xy2p(end));
  321. path2D.append(arc2D, true);
  322. CTPath2DArcTo arcTo = of.createCTPath2DArcTo();
  323. arcTo.setHR(d2s(bounds.getHeight()/2.0));
  324. arcTo.setWR(d2s(bounds.getWidth()/2.0));
  325. arcTo.setStAng(d2s(-arc2D.getAngleStart()*60000.));
  326. arcTo.setSwAng(d2s(-arc2D.getAngleExtent()*60000.));
  327. pathCT.getCloseOrMoveToOrLnTo().add(arcTo);
  328. break;
  329. }
  330. case ARC:
  331. break;
  332. case CLOCKWISE_ARC_TO:
  333. break;
  334. case CLOCKWISE_ARC:
  335. break;
  336. case ELLIPTICAL_QUADRANT_X:
  337. break;
  338. case ELLIPTICAL_QUADRANT_Y:
  339. break;
  340. case QUADRATIC_BEZIER:
  341. break;
  342. case NO_FILL:
  343. break;
  344. case NO_LINE:
  345. break;
  346. case AUTO_LINE:
  347. break;
  348. case AUTO_CURVE:
  349. break;
  350. case CORNER_LINE:
  351. break;
  352. case CORNER_CURVE:
  353. break;
  354. case SMOOTH_LINE:
  355. break;
  356. case SMOOTH_CURVE:
  357. break;
  358. case SYMMETRIC_LINE:
  359. break;
  360. case SYMMETRIC_CURVE:
  361. break;
  362. case FREEFORM:
  363. break;
  364. case FILL_COLOR:
  365. break;
  366. case LINE_COLOR:
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. private static String d2s(double d) {
  373. return Integer.toString((int)Math.rint(d));
  374. }
  375. private static Point2D xy2p(int[] xyPoints) {
  376. return new Point2D.Double(xyPoints[0],xyPoints[1]);
  377. }
  378. private static HSLFFreeformShape.PathInfo getPathInfo(byte[] elem) {
  379. int elemUS = LittleEndian.getUShort(elem, 0);
  380. int pathInfo = PATH_INFO.getValue(elemUS);
  381. return HSLFFreeformShape.PathInfo.valueOf(pathInfo);
  382. }
  383. private static HSLFFreeformShape.EscapeInfo getEscapeInfo(byte[] elem) {
  384. int elemUS = LittleEndian.getUShort(elem, 0);
  385. int escInfo = ESCAPE_INFO.getValue(elemUS);
  386. return HSLFFreeformShape.EscapeInfo.valueOf(escInfo);
  387. }
  388. private static <T extends EscherProperty> T getShapeProp(AbstractEscherOptRecord opt, int propId) {
  389. T prop = getEscherProperty(opt, (short)(propId + 0x4000));
  390. if (prop == null) {
  391. prop = getEscherProperty(opt, propId);
  392. }
  393. return prop;
  394. }
  395. private CTAdjPoint2D fillPoint(byte[] xyMaster, int[] xyPoints) {
  396. if (xyMaster == null || xyPoints == null) {
  397. LOG.log(POILogger.WARN, "Master bytes or points not set - ignore point");
  398. return null;
  399. }
  400. if ((xyMaster.length != 4 && xyMaster.length != 8) || xyPoints.length != 2) {
  401. LOG.log(POILogger.WARN, "Invalid number of master bytes for a single point - ignore point");
  402. return null;
  403. }
  404. int x, y;
  405. if (xyMaster.length == 4) {
  406. x = LittleEndian.getShort(xyMaster, 0);
  407. y = LittleEndian.getShort(xyMaster, 2);
  408. } else {
  409. x = LittleEndian.getInt(xyMaster, 0);
  410. y = LittleEndian.getInt(xyMaster, 4);
  411. }
  412. xyPoints[0] = x;
  413. xyPoints[1] = y;
  414. return toPoint(xyPoints);
  415. }
  416. private static CTAdjPoint2D toPoint(int[] xyPoints) {
  417. CTAdjPoint2D pt = new CTAdjPoint2D();
  418. pt.setX(Integer.toString(xyPoints[0]));
  419. pt.setY(Integer.toString(xyPoints[1]));
  420. return pt;
  421. }
  422. }