]> source.dussan.org Git - poi.git/commitdiff
support for adding Picture to ShapeGroup in HSLF. See Bug 43323 for details.
authorYegor Kozlov <yegor@apache.org>
Sun, 9 Sep 2007 07:38:35 +0000 (07:38 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 9 Sep 2007 07:38:35 +0000 (07:38 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@573955 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/model/Picture.java
src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java

index d6f4887e2733a16a58f925cece7ea683584a1773..0740e23bce0002f07319e90854495ac1f63eca95 100644 (file)
@@ -75,8 +75,18 @@ public class Picture extends SimpleShape {
     * @param idx the index of the picture
      */
     public Picture(int idx){
-        super(null, null);
-        _escherContainer = createSpContainer(idx);
+        this(idx, null);
+    }
+
+    /**
+     * Create a new <code>Picture</code>
+     *
+     * @param idx the index of the picture
+     * @param parent the parent shape
+     */
+    public Picture(int idx, Shape parent) {
+        super(null, parent);
+        _escherContainer = createSpContainer(idx, parent instanceof ShapeGroup);
     }
 
     /**
@@ -109,8 +119,8 @@ public class Picture extends SimpleShape {
      * @param idx the index of the picture which referes to <code>EscherBSE</code> container.
      * @return the create Picture object
      */
-    protected EscherContainerRecord createSpContainer(int idx) {
-        EscherContainerRecord spContainer = super.createSpContainer(false);
+    protected EscherContainerRecord createSpContainer(int idx, boolean isChild) {
+        EscherContainerRecord spContainer = super.createSpContainer(isChild);
         spContainer.setOptions((short)15);
 
         EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
index ffe89e03df173109adea2cd97369f6d4ae9fbde2..56d77764e9256d21648bfc839f1b7884b3c0407b 100644 (file)
@@ -151,8 +151,8 @@ public abstract class Shape {
             anchor = new java.awt.Rectangle();
             anchor.x = rec.getDx1()*POINT_DPI/MASTER_DPI;
             anchor.y = rec.getDy1()*POINT_DPI/MASTER_DPI;
-            anchor.width = (rec.getDx2() - anchor.x)*POINT_DPI/MASTER_DPI;
-            anchor.height = (rec.getDy2() - anchor.y)*POINT_DPI/MASTER_DPI;
+            anchor.width = rec.getDx2()*POINT_DPI/MASTER_DPI - anchor.x;
+            anchor.height = rec.getDy2()*POINT_DPI/MASTER_DPI - anchor.y;
         }
         else {
             EscherClientAnchorRecord rec = (EscherClientAnchorRecord)getEscherChild(_escherContainer, EscherClientAnchorRecord.RECORD_ID);
index fd637774c08af023cb4d0984a72bd54b4eac8dba..6dd5d9be1700827f7178d769efa97087bc0a5ff4 100644 (file)
@@ -25,6 +25,7 @@ import java.awt.*;
 import java.awt.Rectangle;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
+import java.io.File;
 import java.util.ArrayList;
 
 /**
@@ -225,4 +226,57 @@ public class TestShapes extends TestCase {
             assertTrue(lst2.containsAll(lst1));
         }
     }
+
+    /**
+     * Test adding shapes to <code>ShapeGroup</code>
+     */
+    public void testShapeGroup() throws Exception {
+        String cwd = System.getProperty("HSLF.testdata.path");
+        SlideShow ppt = new SlideShow();
+
+        Slide slide = ppt.createSlide();
+        Dimension pgsize = ppt.getPageSize();
+
+        ShapeGroup group = new ShapeGroup();
+
+        group.setAnchor(new Rectangle(0, 0, (int)pgsize.getWidth(), (int)pgsize.getHeight()));
+        slide.addShape(group);
+
+        File img = new File(cwd, "clock.jpg");
+        int idx = ppt.addPicture(img, Picture.JPEG);
+        Picture pict = new Picture(idx, group);
+        pict.setAnchor(new Rectangle(0, 0, 200, 200));
+        group.addShape(pict);
+
+        Line line = new Line(group);
+        line.setAnchor(new Rectangle(300, 300, 500, 0));
+        group.addShape(line);
+
+        //serialize and read again.
+        ByteArrayOutputStream  out = new ByteArrayOutputStream();
+        ppt.write(out);
+        out.close();
+
+        ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
+        ppt = new SlideShow(is);
+        is.close();
+
+        slide = ppt.getSlides()[0];
+
+        Shape[] shape = slide.getShapes();
+        assertEquals(1, shape.length);
+        assertTrue(shape[0] instanceof ShapeGroup);
+
+        group = (ShapeGroup)shape[0];
+        Shape[] grshape = group.getShapes();
+        assertEquals(2, grshape.length);
+        assertTrue(grshape[0] instanceof Picture);
+        assertTrue(grshape[1] instanceof Line);
+
+        pict = (Picture)grshape[0];
+        assertEquals(new Rectangle(0, 0, 200, 200), pict.getAnchor());
+
+        line = (Line)grshape[1];
+        assertEquals(new Rectangle(300, 300, 500, 0), line.getAnchor());
+    }
 }