PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pptx/src/org/apache/poi/xslf/usermodel/XSLFGroupShape.java

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