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.

XSLFGroupShape.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.openxml4j.opc.PackagePart;
  21. import org.apache.poi.openxml4j.opc.PackageRelationship;
  22. import org.apache.poi.openxml4j.opc.TargetMode;
  23. import org.apache.poi.util.Beta;
  24. import org.apache.poi.util.Units;
  25. import org.apache.xmlbeans.XmlObject;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  35. import java.awt.Graphics2D;
  36. import java.awt.geom.AffineTransform;
  37. import java.awt.geom.Rectangle2D;
  38. import java.util.List;
  39. import java.util.regex.Pattern;
  40. /**
  41. * Represents a group shape that consists of many shapes grouped together.
  42. *
  43. * @author Yegor Kozlov
  44. */
  45. @Beta
  46. public class XSLFGroupShape extends XSLFShape {
  47. private final CTGroupShape _shape;
  48. private final XSLFSheet _sheet;
  49. private final List<XSLFShape> _shapes;
  50. private final CTGroupShapeProperties _spPr;
  51. private XSLFDrawing _drawing;
  52. /*package*/ XSLFGroupShape(CTGroupShape shape, XSLFSheet sheet){
  53. _shape = shape;
  54. _sheet = sheet;
  55. _shapes = _sheet.buildShapes(_shape);
  56. _spPr = shape.getGrpSpPr();
  57. }
  58. @Override
  59. public CTGroupShape getXmlObject(){
  60. return _shape;
  61. }
  62. @Override
  63. public Rectangle2D getAnchor(){
  64. CTGroupTransform2D xfrm = _spPr.getXfrm();
  65. CTPoint2D off = xfrm.getOff();
  66. long x = off.getX();
  67. long y = off.getY();
  68. CTPositiveSize2D ext = xfrm.getExt();
  69. long cx = ext.getCx();
  70. long cy = ext.getCy();
  71. return new Rectangle2D.Double(
  72. Units.toPoints(x), Units.toPoints(y),
  73. Units.toPoints(cx), Units.toPoints(cy));
  74. }
  75. @Override
  76. public void setAnchor(Rectangle2D anchor){
  77. CTGroupTransform2D xfrm = _spPr.isSetXfrm() ? _spPr.getXfrm() : _spPr.addNewXfrm();
  78. CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
  79. long x = Units.toEMU(anchor.getX());
  80. long y = Units.toEMU(anchor.getY());
  81. off.setX(x);
  82. off.setY(y);
  83. CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm.addNewExt();
  84. long cx = Units.toEMU(anchor.getWidth());
  85. long cy = Units.toEMU(anchor.getHeight());
  86. ext.setCx(cx);
  87. ext.setCy(cy);
  88. }
  89. /**
  90. *
  91. * @return the coordinates of the child extents rectangle
  92. * used for calculations of grouping, scaling, and rotation
  93. * behavior of shapes placed within a group.
  94. */
  95. public Rectangle2D getInteriorAnchor(){
  96. CTGroupTransform2D xfrm = _spPr.getXfrm();
  97. CTPoint2D off = xfrm.getChOff();
  98. long x = off.getX();
  99. long y = off.getY();
  100. CTPositiveSize2D ext = xfrm.getChExt();
  101. long cx = ext.getCx();
  102. long cy = ext.getCy();
  103. return new Rectangle2D.Double(
  104. Units.toPoints(x), Units.toPoints(y),
  105. Units.toPoints(cx), Units.toPoints(cy));
  106. }
  107. /**
  108. *
  109. * @param anchor the coordinates of the child extents rectangle
  110. * used for calculations of grouping, scaling, and rotation
  111. * behavior of shapes placed within a group.
  112. */
  113. public void setInteriorAnchor(Rectangle2D anchor){
  114. CTGroupTransform2D xfrm = _spPr.isSetXfrm() ? _spPr.getXfrm() : _spPr.addNewXfrm();
  115. CTPoint2D off = xfrm.isSetChOff() ? xfrm.getChOff() : xfrm.addNewChOff();
  116. long x = Units.toEMU(anchor.getX());
  117. long y = Units.toEMU(anchor.getY());
  118. off.setX(x);
  119. off.setY(y);
  120. CTPositiveSize2D ext = xfrm.isSetChExt() ? xfrm.getChExt() : xfrm.addNewChExt();
  121. long cx = Units.toEMU(anchor.getWidth());
  122. long cy = Units.toEMU(anchor.getHeight());
  123. ext.setCx(cx);
  124. ext.setCy(cy);
  125. }
  126. /**
  127. *
  128. * @return child shapes contained witin this group
  129. */
  130. public XSLFShape[] getShapes(){
  131. return _shapes.toArray(new XSLFShape[_shapes.size()]);
  132. }
  133. /**
  134. * Remove the specified shape from this group
  135. */
  136. public boolean removeShape(XSLFShape xShape) {
  137. XmlObject obj = xShape.getXmlObject();
  138. if(obj instanceof CTShape){
  139. _shape.getSpList().remove(obj);
  140. } else if (obj instanceof CTGroupShape){
  141. _shape.getGrpSpList().remove(obj);
  142. } else if (obj instanceof CTConnector){
  143. _shape.getCxnSpList().remove(obj);
  144. } else {
  145. throw new IllegalArgumentException("Unsupported shape: " + xShape);
  146. }
  147. return _shapes.remove(xShape);
  148. }
  149. @Override
  150. public String getShapeName(){
  151. return _shape.getNvGrpSpPr().getCNvPr().getName();
  152. }
  153. @Override
  154. public int getShapeId(){
  155. return (int)_shape.getNvGrpSpPr().getCNvPr().getId();
  156. }
  157. /**
  158. * @param shapeId 1-based shapeId
  159. */
  160. static CTGroupShape prototype(int shapeId) {
  161. CTGroupShape ct = CTGroupShape.Factory.newInstance();
  162. CTGroupShapeNonVisual nvSpPr = ct.addNewNvGrpSpPr();
  163. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  164. cnv.setName("Group " + shapeId);
  165. cnv.setId(shapeId + 1);
  166. nvSpPr.addNewCNvGrpSpPr();
  167. nvSpPr.addNewNvPr();
  168. ct.addNewGrpSpPr();
  169. return ct;
  170. }
  171. // shape factory methods
  172. private XSLFDrawing getDrawing(){
  173. if(_drawing == null) {
  174. _drawing = new XSLFDrawing(_sheet, _shape);
  175. }
  176. return _drawing;
  177. }
  178. public XSLFAutoShape createAutoShape(){
  179. XSLFAutoShape sh = getDrawing().createAutoShape();
  180. _shapes.add(sh);
  181. return sh;
  182. }
  183. public XSLFFreeformShape createFreeform(){
  184. XSLFFreeformShape sh = getDrawing().createFreeform();
  185. _shapes.add(sh);
  186. return sh;
  187. }
  188. public XSLFTextBox createTextBox(){
  189. XSLFTextBox sh = getDrawing().createTextBox();
  190. _shapes.add(sh);
  191. return sh;
  192. }
  193. public XSLFConnectorShape createConnector(){
  194. XSLFConnectorShape sh = getDrawing().createConnector();
  195. _shapes.add(sh);
  196. return sh;
  197. }
  198. public XSLFGroupShape createGroup(){
  199. XSLFGroupShape sh = getDrawing().createGroup();
  200. _shapes.add(sh);
  201. return sh;
  202. }
  203. public XSLFPictureShape createPicture(int pictureIndex){
  204. List<PackagePart> pics = _sheet.getPackagePart().getPackage()
  205. .getPartsByName(Pattern.compile("/ppt/media/.*?"));
  206. PackagePart pic = pics.get(pictureIndex);
  207. PackageRelationship rel = _sheet.getPackagePart().addRelationship(
  208. pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
  209. XSLFPictureShape sh = getDrawing().createPicture(rel.getId());
  210. sh.resize();
  211. _shapes.add(sh);
  212. return sh;
  213. }
  214. @Override
  215. public void setFlipHorizontal(boolean flip){
  216. _spPr.getXfrm().setFlipH(flip);
  217. }
  218. @Override
  219. public void setFlipVertical(boolean flip){
  220. _spPr.getXfrm().setFlipV(flip);
  221. }
  222. @Override
  223. public boolean getFlipHorizontal(){
  224. return _spPr.getXfrm().getFlipH();
  225. }
  226. @Override
  227. public boolean getFlipVertical(){
  228. return _spPr.getXfrm().getFlipV();
  229. }
  230. @Override
  231. public void setRotation(double theta){
  232. _spPr.getXfrm().setRot((int)(theta*60000));
  233. }
  234. @Override
  235. public double getRotation(){
  236. return (double)_spPr.getXfrm().getRot()/60000;
  237. }
  238. @Override
  239. public void draw(Graphics2D graphics){
  240. // the coordinate system of this group of shape
  241. Rectangle2D interior = getInteriorAnchor();
  242. // anchor of this group relative to the parent shape
  243. Rectangle2D exterior = getAnchor();
  244. graphics.translate(exterior.getX(), exterior.getY());
  245. double scaleX = exterior.getWidth() / interior.getWidth();
  246. double scaleY = exterior.getHeight() / interior.getHeight();
  247. graphics.scale(scaleX, scaleY);
  248. graphics.translate(-interior.getX(), -interior.getY());
  249. for (XSLFShape shape : getShapes()) {
  250. // remember the initial transform and restore it after we are done with the drawing
  251. AffineTransform at = graphics.getTransform();
  252. graphics.setRenderingHint(XSLFRenderingHint.GSAVE, true);
  253. shape.applyTransform(graphics);
  254. shape.draw(graphics);
  255. // restore the coordinate system
  256. graphics.setTransform(at);
  257. graphics.setRenderingHint(XSLFRenderingHint.GRESTORE, true);
  258. }
  259. }
  260. }