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.

XSLFFreeformShape.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import org.apache.poi.util.Beta;
  21. import org.apache.poi.util.Units;
  22. import org.apache.xmlbeans.XmlObject;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTAdjPoint2D;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTCustomGeometry2D;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTGeomRect;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DClose;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DCubicBezierTo;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DLineTo;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DMoveTo;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTShapeNonVisual;
  35. import java.awt.geom.AffineTransform;
  36. import java.awt.geom.GeneralPath;
  37. import java.awt.geom.PathIterator;
  38. import java.awt.geom.Rectangle2D;
  39. /**
  40. * Represents a custom geometric shape.
  41. * This shape will consist of a series of lines and curves described within a creation path.
  42. *
  43. * @author Yegor Kozlov
  44. */
  45. @Beta
  46. public class XSLFFreeformShape extends XSLFAutoShape {
  47. /*package*/ XSLFFreeformShape(CTShape shape, XSLFSheet sheet) {
  48. super(shape, sheet);
  49. }
  50. /**
  51. * Set the shape path
  52. *
  53. * @param path shape outline
  54. * @return the number of points written
  55. */
  56. public int setPath(GeneralPath path) {
  57. CTPath2D ctPath = CTPath2D.Factory.newInstance();
  58. Rectangle2D bounds = path.getBounds2D();
  59. int x0 = Units.toEMU(bounds.getX());
  60. int y0 = Units.toEMU(bounds.getY());
  61. PathIterator it = path.getPathIterator(new AffineTransform());
  62. int numPoints = 0;
  63. ctPath.setH(Units.toEMU(bounds.getHeight()));
  64. ctPath.setW(Units.toEMU(bounds.getWidth()));
  65. while (!it.isDone()) {
  66. double[] vals = new double[6];
  67. int type = it.currentSegment(vals);
  68. switch (type) {
  69. case PathIterator.SEG_MOVETO:
  70. CTAdjPoint2D mv = ctPath.addNewMoveTo().addNewPt();
  71. mv.setX(Units.toEMU(vals[0]) - x0);
  72. mv.setY(Units.toEMU(vals[1]) - y0);
  73. numPoints++;
  74. break;
  75. case PathIterator.SEG_LINETO:
  76. CTAdjPoint2D ln = ctPath.addNewLnTo().addNewPt();
  77. ln.setX(Units.toEMU(vals[0]) - x0);
  78. ln.setY(Units.toEMU(vals[1]) - y0);
  79. numPoints++;
  80. break;
  81. case PathIterator.SEG_CUBICTO:
  82. CTPath2DCubicBezierTo bez = ctPath.addNewCubicBezTo();
  83. CTAdjPoint2D p1 = bez.addNewPt();
  84. p1.setX(Units.toEMU(vals[0]) - x0);
  85. p1.setY(Units.toEMU(vals[1]) - y0);
  86. CTAdjPoint2D p2 = bez.addNewPt();
  87. p2.setX(Units.toEMU(vals[2]) - x0);
  88. p2.setY(Units.toEMU(vals[3]) - y0);
  89. CTAdjPoint2D p3 = bez.addNewPt();
  90. p3.setX(Units.toEMU(vals[4]) - x0);
  91. p3.setY(Units.toEMU(vals[5]) - y0);
  92. numPoints += 3;
  93. break;
  94. case PathIterator.SEG_CLOSE:
  95. numPoints++;
  96. ctPath.addNewClose();
  97. break;
  98. }
  99. it.next();
  100. }
  101. getSpPr().getCustGeom().getPathLst().setPathArray(new CTPath2D[]{ctPath});
  102. setAnchor(bounds);
  103. return numPoints;
  104. }
  105. /**
  106. * Gets the shape path.
  107. * <p>
  108. * The path is translated in the shape's coordinate system, i.e.
  109. * freeform.getPath().getBounds2D() equals to freeform.getAnchor()
  110. * (small discrepancies are possible due to rounding errors)
  111. * </p>
  112. *
  113. * @return the path
  114. */
  115. public GeneralPath getPath() {
  116. GeneralPath path = new GeneralPath();
  117. Rectangle2D bounds = getAnchor();
  118. CTCustomGeometry2D geom = getSpPr().getCustGeom();
  119. for(CTPath2D spPath : geom.getPathLst().getPathList()){
  120. double scaleW = bounds.getWidth() / Units.toPoints(spPath.getW());
  121. double scaleH = bounds.getHeight() / Units.toPoints(spPath.getH());
  122. for(XmlObject ch : spPath.selectPath("*")){
  123. if(ch instanceof CTPath2DMoveTo){
  124. CTAdjPoint2D pt = ((CTPath2DMoveTo)ch).getPt();
  125. path.moveTo(
  126. (float) (Units.toPoints((Long) pt.getX()) * scaleW),
  127. (float) (Units.toPoints((Long) pt.getY()) * scaleH));
  128. } else if (ch instanceof CTPath2DLineTo){
  129. CTAdjPoint2D pt = ((CTPath2DLineTo)ch).getPt();
  130. path.lineTo((float)Units.toPoints((Long)pt.getX()),
  131. (float)Units.toPoints((Long)pt.getY()));
  132. } else if (ch instanceof CTPath2DCubicBezierTo){
  133. CTPath2DCubicBezierTo bez = ((CTPath2DCubicBezierTo)ch);
  134. CTAdjPoint2D pt1 = bez.getPtArray(0);
  135. CTAdjPoint2D pt2 = bez.getPtArray(1);
  136. CTAdjPoint2D pt3 = bez.getPtArray(2);
  137. path.curveTo(
  138. (float) (Units.toPoints((Long) pt1.getX()) * scaleW),
  139. (float) (Units.toPoints((Long) pt1.getY()) * scaleH),
  140. (float) (Units.toPoints((Long) pt2.getX()) * scaleW),
  141. (float) (Units.toPoints((Long) pt2.getY()) * scaleH),
  142. (float) (Units.toPoints((Long) pt3.getX()) * scaleW),
  143. (float) (Units.toPoints((Long) pt3.getY()) * scaleH) );
  144. } else if (ch instanceof CTPath2DClose){
  145. path.closePath();
  146. }
  147. }
  148. }
  149. // the created path starts at (x=0, y=0).
  150. // The returned path should fit in the bounding rectangle
  151. AffineTransform at = new AffineTransform();
  152. at.translate(bounds.getX(), bounds.getY());
  153. return new GeneralPath(at.createTransformedShape(path));
  154. }
  155. /**
  156. * @param shapeId 1-based shapeId
  157. */
  158. static CTShape prototype(int shapeId) {
  159. CTShape ct = CTShape.Factory.newInstance();
  160. CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
  161. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  162. cnv.setName("Freeform " + shapeId);
  163. cnv.setId(shapeId + 1);
  164. nvSpPr.addNewCNvSpPr();
  165. nvSpPr.addNewNvPr();
  166. CTShapeProperties spPr = ct.addNewSpPr();
  167. CTCustomGeometry2D geom = spPr.addNewCustGeom();
  168. geom.addNewAvLst();
  169. geom.addNewGdLst();
  170. geom.addNewAhLst();
  171. geom.addNewCxnLst();
  172. CTGeomRect rect = geom.addNewRect();
  173. rect.setR("r");
  174. rect.setB("b");
  175. rect.setT("t");
  176. rect.setL("l");
  177. geom.addNewPathLst();
  178. return ct;
  179. }
  180. }