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

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