From: Yegor Kozlov Date: Sun, 9 Sep 2007 07:38:35 +0000 (+0000) Subject: support for adding Picture to ShapeGroup in HSLF. See Bug 43323 for details. X-Git-Tag: REL_3_0_2_BETA1~55 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=33b693fd08b486d29c6fb6c9b0a8fbffba7916cf;p=poi.git support for adding Picture to ShapeGroup in HSLF. See Bug 43323 for details. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@573955 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java b/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java index d6f4887e27..0740e23bce 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java @@ -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 Picture + * + * @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 EscherBSE 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); diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java b/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java index ffe89e03df..56d77764e9 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java @@ -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); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java index fd637774c0..6dd5d9be17 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java @@ -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 ShapeGroup + */ + 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()); + } }