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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.sl.usermodel.ShapeContainer;
  21. import org.apache.poi.util.Beta;
  22. import org.apache.poi.util.Units;
  23. import org.apache.xmlbeans.XmlObject;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTAdjPoint2D;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTCustomGeometry2D;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTGeomRect;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DClose;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DCubicBezierTo;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DLineTo;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DMoveTo;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  35. import org.openxmlformats.schemas.presentationml.x2006.main.CTShapeNonVisual;
  36. import java.awt.geom.AffineTransform;
  37. import java.awt.geom.GeneralPath;
  38. import java.awt.geom.PathIterator;
  39. import java.awt.geom.Rectangle2D;
  40. /**
  41. * Represents a custom geometric shape.
  42. * This shape will consist of a series of lines and curves described within a creation path.
  43. *
  44. * @author Yegor Kozlov
  45. */
  46. @Beta
  47. public class XSLFFreeformShape extends XSLFAutoShape {
  48. /*package*/ XSLFFreeformShape(CTShape shape, XSLFSheet sheet) {
  49. super(shape, sheet);
  50. }
  51. /**
  52. * Set the shape path
  53. *
  54. * @param path shape outline
  55. * @return the number of points written
  56. */
  57. public int setPath(GeneralPath path) {
  58. CTPath2D ctPath = CTPath2D.Factory.newInstance();
  59. Rectangle2D bounds = path.getBounds2D();
  60. int x0 = Units.toEMU(bounds.getX());
  61. int y0 = Units.toEMU(bounds.getY());
  62. PathIterator it = path.getPathIterator(new AffineTransform());
  63. int numPoints = 0;
  64. ctPath.setH(Units.toEMU(bounds.getHeight()));
  65. ctPath.setW(Units.toEMU(bounds.getWidth()));
  66. while (!it.isDone()) {
  67. double[] vals = new double[6];
  68. int type = it.currentSegment(vals);
  69. switch (type) {
  70. case PathIterator.SEG_MOVETO:
  71. CTAdjPoint2D mv = ctPath.addNewMoveTo().addNewPt();
  72. mv.setX(Units.toEMU(vals[0]) - x0);
  73. mv.setY(Units.toEMU(vals[1]) - y0);
  74. numPoints++;
  75. break;
  76. case PathIterator.SEG_LINETO:
  77. CTAdjPoint2D ln = ctPath.addNewLnTo().addNewPt();
  78. ln.setX(Units.toEMU(vals[0]) - x0);
  79. ln.setY(Units.toEMU(vals[1]) - y0);
  80. numPoints++;
  81. break;
  82. case PathIterator.SEG_CUBICTO:
  83. CTPath2DCubicBezierTo bez = ctPath.addNewCubicBezTo();
  84. CTAdjPoint2D p1 = bez.addNewPt();
  85. p1.setX(Units.toEMU(vals[0]) - x0);
  86. p1.setY(Units.toEMU(vals[1]) - y0);
  87. CTAdjPoint2D p2 = bez.addNewPt();
  88. p2.setX(Units.toEMU(vals[2]) - x0);
  89. p2.setY(Units.toEMU(vals[3]) - y0);
  90. CTAdjPoint2D p3 = bez.addNewPt();
  91. p3.setX(Units.toEMU(vals[4]) - x0);
  92. p3.setY(Units.toEMU(vals[5]) - y0);
  93. numPoints += 3;
  94. break;
  95. case PathIterator.SEG_CLOSE:
  96. numPoints++;
  97. ctPath.addNewClose();
  98. break;
  99. }
  100. it.next();
  101. }
  102. getSpPr().getCustGeom().getPathLst().setPathArray(new CTPath2D[]{ctPath});
  103. setAnchor(bounds);
  104. return numPoints;
  105. }
  106. /**
  107. * Gets the shape path.
  108. * <p>
  109. * The path is translated in the shape's coordinate system, i.e.
  110. * freeform.getPath().getBounds2D() equals to freeform.getAnchor()
  111. * (small discrepancies are possible due to rounding errors)
  112. * </p>
  113. *
  114. * @return the path
  115. */
  116. public GeneralPath getPath() {
  117. GeneralPath path = new GeneralPath();
  118. Rectangle2D bounds = getAnchor();
  119. CTCustomGeometry2D geom = getSpPr().getCustGeom();
  120. for(CTPath2D spPath : geom.getPathLst().getPathList()){
  121. double scaleW = bounds.getWidth() / Units.toPoints(spPath.getW());
  122. double scaleH = bounds.getHeight() / Units.toPoints(spPath.getH());
  123. for(XmlObject ch : spPath.selectPath("*")){
  124. if(ch instanceof CTPath2DMoveTo){
  125. CTAdjPoint2D pt = ((CTPath2DMoveTo)ch).getPt();
  126. path.moveTo((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. );
  145. } else if (ch instanceof CTPath2DClose){
  146. path.closePath();
  147. }
  148. }
  149. }
  150. // the created path starts at (x=0, y=0).
  151. // The returned path should fit in the bounding rectangle
  152. AffineTransform at = new AffineTransform();
  153. at.translate(bounds.getX(), bounds.getY());
  154. return new GeneralPath(at.createTransformedShape(path));
  155. }
  156. /**
  157. * @param shapeId 1-based shapeId
  158. */
  159. static CTShape prototype(int shapeId) {
  160. CTShape ct = CTShape.Factory.newInstance();
  161. CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
  162. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  163. cnv.setName("Freeform " + shapeId);
  164. cnv.setId(shapeId + 1);
  165. nvSpPr.addNewCNvSpPr();
  166. nvSpPr.addNewNvPr();
  167. CTShapeProperties spPr = ct.addNewSpPr();
  168. CTCustomGeometry2D geom = spPr.addNewCustGeom();
  169. geom.addNewAvLst();
  170. geom.addNewGdLst();
  171. geom.addNewAhLst();
  172. geom.addNewCxnLst();
  173. CTGeomRect rect = geom.addNewRect();
  174. rect.setR("r");
  175. rect.setB("b");
  176. rect.setT("t");
  177. rect.setL("l");
  178. geom.addNewPathLst();
  179. return ct;
  180. }
  181. @Override
  182. protected java.awt.Shape getOutline(){
  183. return getPath();
  184. }
  185. }