Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

XSLFFreeformShape.java 9.2KB

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