Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import java.awt.geom.Rectangle2D;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.apache.poi.ddf.EscherChildAnchorRecord;
  21. import org.apache.poi.ddf.EscherClientAnchorRecord;
  22. import org.apache.poi.ddf.EscherContainerRecord;
  23. import org.apache.poi.ddf.EscherRecord;
  24. import org.apache.poi.ddf.EscherSpRecord;
  25. import org.apache.poi.ddf.EscherSpgrRecord;
  26. import org.apache.poi.sl.usermodel.GroupShape;
  27. import org.apache.poi.sl.usermodel.PictureData;
  28. import org.apache.poi.sl.usermodel.ShapeContainer;
  29. import org.apache.poi.sl.usermodel.ShapeType;
  30. import org.apache.poi.util.LittleEndian;
  31. import org.apache.poi.util.POILogger;
  32. import org.apache.poi.util.Units;
  33. /**
  34. * Represents a group of shapes.
  35. *
  36. * @author Yegor Kozlov
  37. */
  38. public class HSLFGroupShape extends HSLFShape
  39. implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
  40. /**
  41. * Create a new ShapeGroup. This constructor is used when a new shape is created.
  42. *
  43. */
  44. public HSLFGroupShape(){
  45. this(null, null);
  46. _escherContainer = createSpContainer(false);
  47. }
  48. /**
  49. * Create a new ShapeGroup. This constructor is used when a new shape is created.
  50. *
  51. * @param parent the parent of the shape
  52. */
  53. public HSLFGroupShape(ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  54. this(null, parent);
  55. _escherContainer = createSpContainer(parent instanceof HSLFGroupShape);
  56. }
  57. /**
  58. * Create a ShapeGroup object and initialize it from the supplied Record container.
  59. *
  60. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  61. * @param parent the parent of the shape
  62. */
  63. protected HSLFGroupShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  64. super(escherRecord, parent);
  65. }
  66. @Override
  67. public void setAnchor(Rectangle2D anchor) {
  68. EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  69. boolean isInitialized = !(clientAnchor.getDx1() == 0 && clientAnchor.getRow1() == 0);
  70. if (isInitialized) {
  71. moveAndScale(anchor);
  72. } else {
  73. setExteriorAnchor(anchor);
  74. }
  75. }
  76. @Override
  77. public void setInteriorAnchor(Rectangle2D anchor){
  78. EscherSpgrRecord spgr = getEscherChild(EscherSpgrRecord.RECORD_ID);
  79. int x1 = Units.pointsToMaster(anchor.getX());
  80. int y1 = Units.pointsToMaster(anchor.getY());
  81. int x2 = Units.pointsToMaster(anchor.getX() + anchor.getWidth());
  82. int y2 = Units.pointsToMaster(anchor.getY() + anchor.getHeight());
  83. spgr.setRectX1(x1);
  84. spgr.setRectY1(y1);
  85. spgr.setRectX2(x2);
  86. spgr.setRectY2(y2);
  87. }
  88. @Override
  89. public Rectangle2D getInteriorAnchor(){
  90. EscherSpgrRecord rec = getEscherChild(EscherSpgrRecord.RECORD_ID);
  91. double x1 = Units.masterToPoints(rec.getRectX1());
  92. double y1 = Units.masterToPoints(rec.getRectY1());
  93. double x2 = Units.masterToPoints(rec.getRectX2());
  94. double y2 = Units.masterToPoints(rec.getRectY2());
  95. return new Rectangle2D.Double(x1,y1,x2-x1,y2-y1);
  96. }
  97. protected void setExteriorAnchor(Rectangle2D anchor) {
  98. EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  99. //hack. internal variable EscherClientAnchorRecord.shortRecord can be
  100. //initialized only in fillFields(). We need to set shortRecord=false;
  101. byte[] header = new byte[16];
  102. LittleEndian.putUShort(header, 0, 0);
  103. LittleEndian.putUShort(header, 2, 0);
  104. LittleEndian.putInt(header, 4, 8);
  105. clientAnchor.fillFields(header, 0, null);
  106. // All coordinates need to be converted to Master units (576 dpi)
  107. clientAnchor.setFlag((short)Units.pointsToMaster(anchor.getY()));
  108. clientAnchor.setCol1((short)Units.pointsToMaster(anchor.getX()));
  109. clientAnchor.setDx1((short)Units.pointsToMaster(anchor.getWidth() + anchor.getX()));
  110. clientAnchor.setRow1((short)Units.pointsToMaster(anchor.getHeight() + anchor.getY()));
  111. // TODO: does this make sense?
  112. setInteriorAnchor(anchor);
  113. }
  114. /**
  115. * Create a new ShapeGroup and create an instance of <code>EscherSpgrContainer</code> which represents a group of shapes
  116. */
  117. protected EscherContainerRecord createSpContainer(boolean isChild) {
  118. EscherContainerRecord spgr = new EscherContainerRecord();
  119. spgr.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  120. spgr.setOptions((short)15);
  121. //The group itself is a shape, and always appears as the first EscherSpContainer in the group container.
  122. EscherContainerRecord spcont = new EscherContainerRecord();
  123. spcont.setRecordId(EscherContainerRecord.SP_CONTAINER);
  124. spcont.setOptions((short)15);
  125. EscherSpgrRecord spg = new EscherSpgrRecord();
  126. spg.setOptions((short)1);
  127. spcont.addChildRecord(spg);
  128. EscherSpRecord sp = new EscherSpRecord();
  129. short type = (short)((ShapeType.NOT_PRIMITIVE.nativeId << 4) + 2);
  130. sp.setOptions(type);
  131. sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_GROUP);
  132. spcont.addChildRecord(sp);
  133. EscherClientAnchorRecord anchor = new EscherClientAnchorRecord();
  134. spcont.addChildRecord(anchor);
  135. spgr.addChildRecord(spcont);
  136. return spgr;
  137. }
  138. /**
  139. * Add a shape to this group.
  140. *
  141. * @param shape - the Shape to add
  142. */
  143. public void addShape(HSLFShape shape){
  144. _escherContainer.addChildRecord(shape.getSpContainer());
  145. HSLFSheet sheet = getSheet();
  146. shape.setSheet(sheet);
  147. shape.setShapeId(sheet.allocateShapeId());
  148. shape.afterInsert(sheet);
  149. }
  150. /**
  151. * Moves and scales this <code>ShapeGroup</code> to the specified anchor.
  152. */
  153. protected void moveAndScale(Rectangle2D anchorDest){
  154. Rectangle2D anchorSrc = getAnchor();
  155. double scaleX = (anchorSrc.getWidth() == 0) ? 0 : anchorDest.getWidth() / anchorSrc.getWidth();
  156. double scaleY = (anchorSrc.getHeight() == 0) ? 0 : anchorDest.getHeight() / anchorSrc.getHeight();
  157. setExteriorAnchor(anchorDest);
  158. for (HSLFShape shape : getShapes()) {
  159. Rectangle2D chanchor = shape.getAnchor();
  160. double x = anchorDest.getX()+(chanchor.getX()-anchorSrc.getX())*scaleX;
  161. double y = anchorDest.getY()+(chanchor.getY()-anchorSrc.getY())*scaleY;
  162. double width = chanchor.getWidth()*scaleX;
  163. double height = chanchor.getHeight()*scaleY;
  164. shape.setAnchor(new Rectangle2D.Double(x, y, width, height));
  165. }
  166. }
  167. /**
  168. * Returns the anchor (the bounding box rectangle) of this shape group.
  169. * All coordinates are expressed in points (72 dpi).
  170. *
  171. * @return the anchor of this shape group
  172. */
  173. public Rectangle2D getAnchor(){
  174. EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  175. int x1,y1,x2,y2;
  176. if(clientAnchor == null){
  177. logger.log(POILogger.INFO, "EscherClientAnchorRecord was not found for shape group. Searching for EscherChildAnchorRecord.");
  178. EscherChildAnchorRecord rec = getEscherChild(EscherChildAnchorRecord.RECORD_ID);
  179. x1 = rec.getDx1();
  180. y1 = rec.getDy1();
  181. x2 = rec.getDx2();
  182. y2 = rec.getDy2();
  183. } else {
  184. x1 = clientAnchor.getCol1();
  185. y1 = clientAnchor.getFlag();
  186. x2 = clientAnchor.getDx1();
  187. y2 = clientAnchor.getRow1();
  188. }
  189. Rectangle2D anchor= new Rectangle2D.Double(
  190. (x1 == -1 ? -1 : Units.masterToPoints(x1)),
  191. (y1 == -1 ? -1 : Units.masterToPoints(y1)),
  192. (x2 == -1 ? -1 : Units.masterToPoints(x2-x1)),
  193. (y2 == -1 ? -1 : Units.masterToPoints(y2-y1))
  194. );
  195. return anchor;
  196. }
  197. /**
  198. * Return type of the shape.
  199. * In most cases shape group type is {@link org.apache.poi.sl.usermodel.ShapeType#NOT_PRIMITIVE}
  200. *
  201. * @return type of the shape.
  202. */
  203. public ShapeType getShapeType(){
  204. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  205. int nativeId = spRecord.getOptions() >> 4;
  206. return ShapeType.forId(nativeId, false);
  207. }
  208. /**
  209. * Returns <code>null</code> - shape groups can't have hyperlinks
  210. *
  211. * @return <code>null</code>.
  212. */
  213. public HSLFHyperlink getHyperlink(){
  214. return null;
  215. }
  216. @Override
  217. public <T extends EscherRecord> T getEscherChild(int recordId){
  218. EscherContainerRecord groupInfoContainer = (EscherContainerRecord)_escherContainer.getChild(0);
  219. return groupInfoContainer.getChildById((short)recordId);
  220. }
  221. public Iterator<HSLFShape> iterator() {
  222. return getShapes().iterator();
  223. }
  224. public boolean removeShape(HSLFShape shape) {
  225. // TODO: implement!
  226. throw new UnsupportedOperationException();
  227. }
  228. @Override
  229. public List<HSLFShape> getShapes() {
  230. // Out escher container record should contain several
  231. // SpContainers, the first of which is the group shape itself
  232. Iterator<EscherRecord> iter = _escherContainer.getChildIterator();
  233. // Don't include the first SpContainer, it is always NotPrimitive
  234. if (iter.hasNext()) {
  235. iter.next();
  236. }
  237. List<HSLFShape> shapeList = new ArrayList<HSLFShape>();
  238. while (iter.hasNext()) {
  239. EscherRecord r = iter.next();
  240. if(r instanceof EscherContainerRecord) {
  241. // Create the Shape for it
  242. EscherContainerRecord container = (EscherContainerRecord)r;
  243. HSLFShape shape = HSLFShapeFactory.createShape(container, this);
  244. shape.setSheet(getSheet());
  245. shapeList.add( shape );
  246. } else {
  247. // Should we do anything special with these non
  248. // Container records?
  249. logger.log(POILogger.ERROR, "Shape contained non container escher record, was " + r.getClass().getName());
  250. }
  251. }
  252. return shapeList;
  253. }
  254. @Override
  255. public HSLFTextBox createTextBox() {
  256. HSLFTextBox s = new HSLFTextBox(this);
  257. s.setHorizontalCentered(true);
  258. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  259. addShape(s);
  260. return s;
  261. }
  262. @Override
  263. public HSLFAutoShape createAutoShape() {
  264. HSLFAutoShape s = new HSLFAutoShape(ShapeType.RECT, this);
  265. s.setHorizontalCentered(true);
  266. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  267. addShape(s);
  268. return s;
  269. }
  270. @Override
  271. public HSLFFreeformShape createFreeform() {
  272. HSLFFreeformShape s = new HSLFFreeformShape(this);
  273. s.setHorizontalCentered(true);
  274. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  275. addShape(s);
  276. return s;
  277. }
  278. @Override
  279. public HSLFConnectorShape createConnector() {
  280. HSLFConnectorShape s = new HSLFConnectorShape(this);
  281. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  282. addShape(s);
  283. return s;
  284. }
  285. @Override
  286. public HSLFGroupShape createGroup() {
  287. HSLFGroupShape s = new HSLFGroupShape(this);
  288. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  289. addShape(s);
  290. return s;
  291. }
  292. @Override
  293. public HSLFPictureShape createPicture(PictureData pictureData) {
  294. if (!(pictureData instanceof HSLFPictureData)) {
  295. throw new IllegalArgumentException("pictureData needs to be of type HSLFPictureData");
  296. }
  297. HSLFPictureShape s = new HSLFPictureShape((HSLFPictureData)pictureData, this);
  298. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  299. addShape(s);
  300. return s;
  301. }
  302. @Override
  303. public HSLFTable createTable(int numRows, int numCols) {
  304. if (numRows < 1 || numCols < 1) {
  305. throw new IllegalArgumentException("numRows and numCols must be greater than 0");
  306. }
  307. HSLFTable s = new HSLFTable(numRows,numCols,this);
  308. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  309. addShape(s);
  310. return s;
  311. }
  312. }