In preparation for table rendering, added table row heights and column widths to common sl.

To have better results in rendering switch anchor from java.awt.Rectangle to Rectangle2D.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1715540 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-11-21 15:54:01 +00:00
parent 2bc397b665
commit 7458fdc277
41 changed files with 459 additions and 287 deletions

View File

@ -22,6 +22,7 @@ import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -183,8 +184,8 @@ public final class ApacheconEU08 {
table1.setRowHeight(1, 80); table1.setRowHeight(1, 80);
Dimension dim = ppt.getPageSize(); Dimension dim = ppt.getPageSize();
Rectangle oldAnchor = table1.getAnchor(); Rectangle2D oldAnchor = table1.getAnchor();
table1.setAnchor(new Rectangle((dim.width-450)/2, 100, oldAnchor.width, oldAnchor.height)); table1.setAnchor(new Rectangle2D.Double((dim.width-450)/2, 100, oldAnchor.getWidth(), oldAnchor.getHeight()));
TextBox<?,?> box1 = slide.createTextBox(); TextBox<?,?> box1 = slide.createTextBox();
box1.setHorizontalCentered(true); box1.setHorizontalCentered(true);

View File

@ -78,7 +78,7 @@ public final class TableDemo {
table1.setColumnWidth(1, 150); table1.setColumnWidth(1, 150);
int pgWidth = ppt.getPageSize().width; int pgWidth = ppt.getPageSize().width;
table1.moveTo((pgWidth - table1.getAnchor().width)/2, 100); table1.moveTo((pgWidth - table1.getAnchor().getWidth())/2., 100.);
//test data for the second taable //test data for the second taable
String[][] txt2 = { String[][] txt2 = {
@ -125,5 +125,6 @@ public final class TableDemo {
ppt.write(out); ppt.write(out);
out.close(); out.close();
ppt.close();
} }
} }

View File

@ -20,7 +20,6 @@ package org.apache.poi.sl.draw;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Paint; import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import org.apache.poi.sl.usermodel.Background; import org.apache.poi.sl.usermodel.Background;
@ -36,12 +35,12 @@ public class DrawBackground extends DrawShape {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void draw(Graphics2D graphics) { public void draw(Graphics2D graphics) {
Dimension pg = shape.getSheet().getSlideShow().getPageSize(); Dimension pg = shape.getSheet().getSlideShow().getPageSize();
final Rectangle anchor = new Rectangle(0, 0, (int)pg.getWidth(), (int)pg.getHeight()); final Rectangle2D anchor = new Rectangle2D.Double(0, 0, pg.getWidth(), pg.getHeight());
PlaceableShape<?,?> ps = new PlaceableShape(){ PlaceableShape<?,?> ps = new PlaceableShape(){
public ShapeContainer<?,?> getParent() { return null; } public ShapeContainer<?,?> getParent() { return null; }
public Rectangle getAnchor() { return anchor; } public Rectangle2D getAnchor() { return anchor; }
public void setAnchor(Rectangle newAnchor) {} public void setAnchor(Rectangle2D newAnchor) {}
public double getRotation() { return 0; } public double getRotation() { return 0; }
public void setRotation(double theta) {} public void setRotation(double theta) {}
public void setFlipHorizontal(boolean flip) {} public void setFlipHorizontal(boolean flip) {}

View File

@ -20,7 +20,6 @@ package org.apache.poi.sl.draw;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.io.IOException; import java.io.IOException;
@ -70,12 +69,12 @@ public class DrawPictureShape extends DrawSimpleShape {
PictureShape<?,?> ps = getShape(); PictureShape<?,?> ps = getShape();
Dimension dim = ps.getPictureData().getImageDimension(); Dimension dim = ps.getPictureData().getImageDimension();
Rectangle origRect = ps.getAnchor(); Rectangle2D origRect = ps.getAnchor();
int x = (int)origRect.getX(); double x = origRect.getX();
int y = (int)origRect.getY(); double y = origRect.getY();
int w = (int)dim.getWidth(); double w = dim.getWidth();
int h = (int)dim.getHeight(); double h = dim.getHeight();
ps.setAnchor(new Rectangle(x, y, w, h)); ps.setAnchor(new Rectangle2D.Double(x, y, w, h));
} }
@ -85,7 +84,7 @@ public class DrawPictureShape extends DrawSimpleShape {
* *
* @param target The target rectangle * @param target The target rectangle
*/ */
public void resize(Rectangle target) { public void resize(Rectangle2D target) {
resize(target, RectAlign.CENTER); resize(target, RectAlign.CENTER);
} }
@ -100,7 +99,7 @@ public class DrawPictureShape extends DrawSimpleShape {
* The alignment within the target rectangle when resizing. * The alignment within the target rectangle when resizing.
* A null value corresponds to RectAlign.CENTER * A null value corresponds to RectAlign.CENTER
*/ */
public void resize(Rectangle target, RectAlign align) { public void resize(Rectangle2D target, RectAlign align) {
PictureShape<?,?> ps = getShape(); PictureShape<?,?> ps = getShape();
Dimension dim = ps.getPictureData().getImageDimension(); Dimension dim = ps.getPictureData().getImageDimension();
if (dim.width <= 0 || dim.height <= 0) { if (dim.width <= 0 || dim.height <= 0) {
@ -170,6 +169,6 @@ public class DrawPictureShape extends DrawSimpleShape {
break; break;
} }
ps.setAnchor(new Rectangle((int)x, (int)y, (int)w, (int)h)); ps.setAnchor(new Rectangle2D.Double(x, y, w, h));
} }
} }

View File

@ -19,7 +19,6 @@ package org.apache.poi.sl.draw;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Paint; import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext; import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer; import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute; import java.awt.font.TextAttribute;
@ -427,8 +426,8 @@ public class DrawTextParagraph implements Drawable {
private PlaceableShape<?,?> getParagraphShape() { private PlaceableShape<?,?> getParagraphShape() {
PlaceableShape<?,?> ps = new PlaceableShape(){ PlaceableShape<?,?> ps = new PlaceableShape(){
public ShapeContainer<?,?> getParent() { return null; } public ShapeContainer<?,?> getParent() { return null; }
public Rectangle getAnchor() { return paragraph.getParentShape().getAnchor(); } public Rectangle2D getAnchor() { return paragraph.getParentShape().getAnchor(); }
public void setAnchor(Rectangle anchor) {} public void setAnchor(Rectangle2D anchor) {}
public double getRotation() { return 0; } public double getRotation() { return 0; }
public void setRotation(double theta) {} public void setRotation(double theta) {}
public void setFlipHorizontal(boolean flip) {} public void setFlipHorizontal(boolean flip) {}

View File

@ -17,7 +17,7 @@
package org.apache.poi.sl.usermodel; package org.apache.poi.sl.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
public interface GroupShape< public interface GroupShape<
S extends Shape<S,P>, S extends Shape<S,P>,
@ -30,7 +30,7 @@ public interface GroupShape<
* *
* @return the coordinate space of this group * @return the coordinate space of this group
*/ */
Rectangle getInteriorAnchor(); Rectangle2D getInteriorAnchor();
/** /**
* Sets the coordinate space of this group. All children are constrained * Sets the coordinate space of this group. All children are constrained
@ -38,5 +38,5 @@ public interface GroupShape<
* *
* @param anchor the coordinate space of this group * @param anchor the coordinate space of this group
*/ */
void setInteriorAnchor(Rectangle anchor); void setInteriorAnchor(Rectangle2D anchor);
} }

View File

@ -17,7 +17,7 @@
package org.apache.poi.sl.usermodel; package org.apache.poi.sl.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
public interface PlaceableShape< public interface PlaceableShape<
S extends Shape<S,P>, S extends Shape<S,P>,
@ -29,13 +29,13 @@ public interface PlaceableShape<
* @return the position of this shape within the drawing canvas. * @return the position of this shape within the drawing canvas.
* The coordinates are expressed in points * The coordinates are expressed in points
*/ */
Rectangle getAnchor(); Rectangle2D getAnchor();
/** /**
* @param anchor the position of this shape within the drawing canvas. * @param anchor the position of this shape within the drawing canvas.
* The coordinates are expressed in points * The coordinates are expressed in points
*/ */
void setAnchor(Rectangle anchor); void setAnchor(Rectangle2D anchor);
/** /**
* Rotation angle in degrees * Rotation angle in degrees

View File

@ -17,7 +17,7 @@
package org.apache.poi.sl.usermodel; package org.apache.poi.sl.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
public interface Shape< public interface Shape<
S extends Shape<S,P>, S extends Shape<S,P>,
@ -37,5 +37,5 @@ public interface Shape<
* *
* @return the anchor of this shape * @return the anchor of this shape
*/ */
Rectangle getAnchor(); Rectangle2D getAnchor();
} }

View File

@ -27,6 +27,14 @@ public interface TableShape<
TableCell<S,P> getCell(int row, int col); TableCell<S,P> getCell(int row, int col);
/**
* Gets the width (in points) of the n-th column
*
* @param idx the column index (0-based)
* @return the width (in points)
*/
double getColumnWidth(int idx);
/** /**
* Sets the width (in points) of the n-th column * Sets the width (in points) of the n-th column
* *
@ -35,6 +43,14 @@ public interface TableShape<
*/ */
void setColumnWidth(int idx, double width); void setColumnWidth(int idx, double width);
/**
* Gets the row height
*
* @param row the row index (0-based)
* @return the height (in points)
*/
double getRowHeight(int row);
/** /**
* Sets the row height. * Sets the row height.
* *

View File

@ -51,7 +51,7 @@ public class Units {
* @return emus * @return emus
*/ */
public static int toEMU(double points){ public static int toEMU(double points){
return (int)Math.round(EMU_PER_POINT*points); return (int)Math.rint(EMU_PER_POINT*points);
} }
/** /**
@ -103,13 +103,13 @@ public class Units {
public static int pointsToMaster(double points) { public static int pointsToMaster(double points) {
points *= MASTER_DPI; points *= MASTER_DPI;
points /= POINT_DPI; points /= POINT_DPI;
return (int)points; return (int)Math.rint(points);
} }
public static int pointsToPixel(double points) { public static int pointsToPixel(double points) {
points *= PIXEL_DPI; points *= PIXEL_DPI;
points /= POINT_DPI; points /= POINT_DPI;
return (int)points; return (int)Math.rint(points);
} }
public static double pixelToPoints(int pixel) { public static double pixelToPoints(int pixel) {

View File

@ -19,7 +19,7 @@ package org.apache.poi.xslf.usermodel;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import org.apache.poi.sl.draw.DrawPaint; import org.apache.poi.sl.draw.DrawPaint;
import org.apache.poi.sl.usermodel.Background; import org.apache.poi.sl.usermodel.Background;
@ -43,9 +43,9 @@ public class XSLFBackground extends XSLFSimpleShape
} }
@Override @Override
public Rectangle getAnchor(){ public Rectangle2D getAnchor(){
Dimension pg = getSheet().getSlideShow().getPageSize(); Dimension pg = getSheet().getSlideShow().getPageSize();
return new Rectangle(0, 0, (int)pg.getWidth(), (int)pg.getHeight()); return new Rectangle2D.Double(0, 0, pg.getWidth(), pg.getHeight());
} }
@Override @Override

View File

@ -16,6 +16,9 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
@ -25,9 +28,6 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture; import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import java.awt.Color;
import java.awt.Rectangle;
/** /**
* @author Yegor Kozlov * @author Yegor Kozlov
@ -57,7 +57,7 @@ public class XSLFDrawing {
CTShape sp = _spTree.addNewSp(); CTShape sp = _spTree.addNewSp();
sp.set(XSLFAutoShape.prototype(_shapeId++)); sp.set(XSLFAutoShape.prototype(_shapeId++));
XSLFAutoShape shape = new XSLFAutoShape(sp, _sheet); XSLFAutoShape shape = new XSLFAutoShape(sp, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
return shape; return shape;
} }
@ -65,7 +65,7 @@ public class XSLFDrawing {
CTShape sp = _spTree.addNewSp(); CTShape sp = _spTree.addNewSp();
sp.set(XSLFFreeformShape.prototype(_shapeId++)); sp.set(XSLFFreeformShape.prototype(_shapeId++));
XSLFFreeformShape shape = new XSLFFreeformShape(sp, _sheet); XSLFFreeformShape shape = new XSLFFreeformShape(sp, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
return shape; return shape;
} }
@ -73,7 +73,7 @@ public class XSLFDrawing {
CTShape sp = _spTree.addNewSp(); CTShape sp = _spTree.addNewSp();
sp.set(XSLFTextBox.prototype(_shapeId++)); sp.set(XSLFTextBox.prototype(_shapeId++));
XSLFTextBox shape = new XSLFTextBox(sp, _sheet); XSLFTextBox shape = new XSLFTextBox(sp, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
return shape; return shape;
} }
@ -81,7 +81,7 @@ public class XSLFDrawing {
CTConnector sp = _spTree.addNewCxnSp(); CTConnector sp = _spTree.addNewCxnSp();
sp.set(XSLFConnectorShape.prototype(_shapeId++)); sp.set(XSLFConnectorShape.prototype(_shapeId++));
XSLFConnectorShape shape = new XSLFConnectorShape(sp, _sheet); XSLFConnectorShape shape = new XSLFConnectorShape(sp, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
shape.setLineColor(Color.black); shape.setLineColor(Color.black);
shape.setLineWidth(0.75); shape.setLineWidth(0.75);
return shape; return shape;
@ -91,7 +91,7 @@ public class XSLFDrawing {
CTGroupShape obj = _spTree.addNewGrpSp(); CTGroupShape obj = _spTree.addNewGrpSp();
obj.set(XSLFGroupShape.prototype(_shapeId++)); obj.set(XSLFGroupShape.prototype(_shapeId++));
XSLFGroupShape shape = new XSLFGroupShape(obj, _sheet); XSLFGroupShape shape = new XSLFGroupShape(obj, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
return shape; return shape;
} }
@ -99,7 +99,7 @@ public class XSLFDrawing {
CTPicture obj = _spTree.addNewPic(); CTPicture obj = _spTree.addNewPic();
obj.set(XSLFPictureShape.prototype(_shapeId++, rel)); obj.set(XSLFPictureShape.prototype(_shapeId++, rel));
XSLFPictureShape shape = new XSLFPictureShape(obj, _sheet); XSLFPictureShape shape = new XSLFPictureShape(obj, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
return shape; return shape;
} }
@ -107,7 +107,7 @@ public class XSLFDrawing {
CTGraphicalObjectFrame obj = _spTree.addNewGraphicFrame(); CTGraphicalObjectFrame obj = _spTree.addNewGraphicFrame();
obj.set(XSLFTable.prototype(_shapeId++)); obj.set(XSLFTable.prototype(_shapeId++));
XSLFTable shape = new XSLFTable(obj, _sheet); XSLFTable shape = new XSLFTable(obj, _sheet);
shape.setAnchor(new Rectangle()); shape.setAnchor(new Rectangle2D.Double());
return shape; return shape;
} }
} }

View File

@ -19,7 +19,6 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator; import java.awt.geom.PathIterator;
@ -61,7 +60,7 @@ public class XSLFFreeformShape extends XSLFAutoShape
public int setPath(GeneralPath path) { public int setPath(GeneralPath path) {
CTPath2D ctPath = CTPath2D.Factory.newInstance(); CTPath2D ctPath = CTPath2D.Factory.newInstance();
Rectangle bounds = path.getBounds(); Rectangle2D bounds = path.getBounds2D();
int x0 = Units.toEMU(bounds.getX()); int x0 = Units.toEMU(bounds.getX());
int y0 = Units.toEMU(bounds.getY()); int y0 = Units.toEMU(bounds.getY());
PathIterator it = path.getPathIterator(new AffineTransform()); PathIterator it = path.getPathIterator(new AffineTransform());

View File

@ -19,7 +19,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
@ -28,6 +28,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.sl.draw.DrawNotImplemented; import org.apache.poi.sl.draw.DrawNotImplemented;
import org.apache.poi.sl.usermodel.PlaceableShape;
import org.apache.poi.sl.usermodel.ShapeType; import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
@ -44,7 +45,7 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFra
*/ */
@Beta @Beta
@DrawNotImplemented @DrawNotImplemented
public class XSLFGraphicFrame extends XSLFShape { public class XSLFGraphicFrame extends XSLFShape implements PlaceableShape<XSLFShape, XSLFTextParagraph> {
/*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){ /*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
super(shape,sheet); super(shape,sheet);
} }
@ -54,18 +55,19 @@ public class XSLFGraphicFrame extends XSLFShape {
} }
@Override @Override
public Rectangle getAnchor(){ public Rectangle2D getAnchor(){
CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm(); CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
CTPoint2D off = xfrm.getOff(); CTPoint2D off = xfrm.getOff();
int x = (int)Units.toPoints(off.getX()); double x = Units.toPoints(off.getX());
int y = (int)Units.toPoints(off.getY()); double y = Units.toPoints(off.getY());
CTPositiveSize2D ext = xfrm.getExt(); CTPositiveSize2D ext = xfrm.getExt();
int cx = (int)Units.toPoints(ext.getCx()); double cx = Units.toPoints(ext.getCx());
int cy = (int)Units.toPoints(ext.getCy()); double cy = Units.toPoints(ext.getCy());
return new Rectangle(x, y, cx, cy); return new Rectangle2D.Double(x, y, cx, cy);
} }
public void setAnchor(Rectangle anchor){ @Override
public void setAnchor(Rectangle2D anchor){
CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm(); CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff(); CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
long x = Units.toEMU(anchor.getX()); long x = Units.toEMU(anchor.getX());

View File

@ -19,7 +19,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -79,19 +79,19 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
} }
@Override @Override
public Rectangle getAnchor(){ public Rectangle2D getAnchor(){
CTGroupTransform2D xfrm = getXfrm(); CTGroupTransform2D xfrm = getXfrm();
CTPoint2D off = xfrm.getOff(); CTPoint2D off = xfrm.getOff();
int x = (int)Units.toPoints(off.getX()); double x = Units.toPoints(off.getX());
int y = (int)Units.toPoints(off.getY()); double y = Units.toPoints(off.getY());
CTPositiveSize2D ext = xfrm.getExt(); CTPositiveSize2D ext = xfrm.getExt();
int cx = (int)Units.toPoints(ext.getCx()); double cx = Units.toPoints(ext.getCx());
int cy = (int)Units.toPoints(ext.getCy()); double cy = Units.toPoints(ext.getCy());
return new Rectangle(x,y,cx,cy); return new Rectangle2D.Double(x,y,cx,cy);
} }
@Override @Override
public void setAnchor(Rectangle anchor){ public void setAnchor(Rectangle2D anchor){
CTGroupTransform2D xfrm = getSafeXfrm(); CTGroupTransform2D xfrm = getSafeXfrm();
CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff(); CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
long x = Units.toEMU(anchor.getX()); long x = Units.toEMU(anchor.getX());
@ -111,15 +111,16 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
* used for calculations of grouping, scaling, and rotation * used for calculations of grouping, scaling, and rotation
* behavior of shapes placed within a group. * behavior of shapes placed within a group.
*/ */
public Rectangle getInteriorAnchor(){ @Override
public Rectangle2D getInteriorAnchor(){
CTGroupTransform2D xfrm = getXfrm(); CTGroupTransform2D xfrm = getXfrm();
CTPoint2D off = xfrm.getChOff(); CTPoint2D off = xfrm.getChOff();
int x = (int)Units.toPoints(off.getX()); double x = Units.toPoints(off.getX());
int y = (int)Units.toPoints(off.getY()); double y = Units.toPoints(off.getY());
CTPositiveSize2D ext = xfrm.getChExt(); CTPositiveSize2D ext = xfrm.getChExt();
int cx = (int)Units.toPoints(ext.getCx()); double cx = Units.toPoints(ext.getCx());
int cy = (int)Units.toPoints(ext.getCy()); double cy = Units.toPoints(ext.getCy());
return new Rectangle(x, y, cx, cy); return new Rectangle2D.Double(x, y, cx, cy);
} }
/** /**
@ -128,7 +129,8 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
* used for calculations of grouping, scaling, and rotation * used for calculations of grouping, scaling, and rotation
* behavior of shapes placed within a group. * behavior of shapes placed within a group.
*/ */
public void setInteriorAnchor(Rectangle anchor) { @Override
public void setInteriorAnchor(Rectangle2D anchor) {
CTGroupTransform2D xfrm = getSafeXfrm(); CTGroupTransform2D xfrm = getSafeXfrm();
CTPoint2D off = xfrm.isSetChOff() ? xfrm.getChOff() : xfrm.addNewChOff(); CTPoint2D off = xfrm.isSetChOff() ? xfrm.getChOff() : xfrm.addNewChOff();
long x = Units.toEMU(anchor.getX()); long x = Units.toEMU(anchor.getX());

View File

@ -18,7 +18,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import org.apache.poi.sl.draw.DrawPaint; import org.apache.poi.sl.draw.DrawPaint;
import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint; import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
@ -48,11 +48,11 @@ public class XSLFShadow extends XSLFShape implements Shadow<XSLFShape,XSLFTextPa
} }
@Override @Override
public Rectangle getAnchor(){ public Rectangle2D getAnchor(){
return _parent.getAnchor(); return _parent.getAnchor();
} }
public void setAnchor(Rectangle anchor){ public void setAnchor(Rectangle2D anchor){
throw new IllegalStateException("You can't set anchor of a shadow"); throw new IllegalStateException("You can't set anchor of a shadow");
} }

View File

@ -20,7 +20,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
@ -121,21 +121,21 @@ public abstract class XSLFSimpleShape extends XSLFShape
} }
@Override @Override
public Rectangle getAnchor() { public Rectangle2D getAnchor() {
CTTransform2D xfrm = getXfrm(); CTTransform2D xfrm = getXfrm();
CTPoint2D off = xfrm.getOff(); CTPoint2D off = xfrm.getOff();
int x = (int)Units.toPoints(off.getX()); double x = Units.toPoints(off.getX());
int y = (int)Units.toPoints(off.getY()); double y = Units.toPoints(off.getY());
CTPositiveSize2D ext = xfrm.getExt(); CTPositiveSize2D ext = xfrm.getExt();
int cx = (int)Units.toPoints(ext.getCx()); double cx = Units.toPoints(ext.getCx());
int cy = (int)Units.toPoints(ext.getCy()); double cy = Units.toPoints(ext.getCy());
return new Rectangle(x, y, cx, cy); return new Rectangle2D.Double(x, y, cx, cy);
} }
@Override @Override
public void setAnchor(Rectangle anchor) { public void setAnchor(Rectangle2D anchor) {
CTTransform2D xfrm = getSafeXfrm(); CTTransform2D xfrm = getSafeXfrm();
CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff(); CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
long x = Units.toEMU(anchor.getX()); long x = Units.toEMU(anchor.getX());

View File

@ -100,6 +100,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
return _table.sizeOfTrArray(); return _table.sizeOfTrArray();
} }
@Override
public double getColumnWidth(int idx){ public double getColumnWidth(int idx){
return Units.toPoints( return Units.toPoints(
_table.getTblGrid().getGridColArray(idx).getW()); _table.getTblGrid().getGridColArray(idx).getW());
@ -110,6 +111,11 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
_table.getTblGrid().getGridColArray(idx).setW(Units.toEMU(width)); _table.getTblGrid().getGridColArray(idx).setW(Units.toEMU(width));
} }
@Override
public double getRowHeight(int row) {
return Units.toPoints(_table.getTrArray(row).getH());
}
@Override @Override
public void setRowHeight(int row, double height) { public void setRowHeight(int row, double height) {
_table.getTrArray(row).setH(Units.toEMU(height)); _table.getTrArray(row).setH(Units.toEMU(height));

View File

@ -19,7 +19,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -518,8 +518,8 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
* *
* @return a <code>Rectangle2D</code> that is the bounds of this shape. * @return a <code>Rectangle2D</code> that is the bounds of this shape.
*/ */
public Rectangle resizeToFitText(){ public Rectangle2D resizeToFitText(){
Rectangle anchor = getAnchor(); Rectangle2D anchor = getAnchor();
if(anchor.getWidth() == 0.) throw new POIXMLException( if(anchor.getWidth() == 0.) throw new POIXMLException(
"Anchor of the shape was not set."); "Anchor of the shape was not set.");
double height = getTextHeight(); double height = getTextHeight();

View File

@ -0,0 +1,64 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.sl;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.apache.poi.POIDataSamples;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.sl.usermodel.TableShape;
import org.junit.Test;
public class TestTable {
private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
@Test
public void testColWidthRowHeight() throws IOException {
// Test of table dimensions of same slideshow saved as ppt/x
// to check if both return similar (points) value
SlideShow<?,?> ppt = SlideShowFactory.create(_slTests.getFile("table_test.ppt"));
TableShape<?,?> ts = (TableShape<?,?>)ppt.getSlides().get(0).getShapes().get(0);
int cols = ts.getNumberOfColumns();
int rows = ts.getNumberOfRows();
SlideShow<?,?> pptx = SlideShowFactory.create(_slTests.getFile("table_test.pptx"));
TableShape<?,?> tsx = (TableShape<?,?>)pptx.getSlides().get(0).getShapes().get(0);
int colsx = tsx.getNumberOfColumns();
int rowsx = tsx.getNumberOfRows();
assertEquals(cols, colsx);
assertEquals(rows, rowsx);
for (int i=0; i<cols; i++) {
assertEquals(ts.getColumnWidth(i), tsx.getColumnWidth(i), 0.2);
}
for (int i=0; i<rows; i++) {
assertEquals(ts.getRowHeight(i), tsx.getRowHeight(i), 0.3);
}
pptx.close();
ppt.close();
}
}

View File

@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.sl.usermodel.PictureData; import org.apache.poi.sl.usermodel.PictureData;
@ -32,6 +32,7 @@ import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.Slide; import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.util.Units;
import org.junit.Test; import org.junit.Test;
public class TestDrawPictureShape { public class TestDrawPictureShape {
@ -54,18 +55,32 @@ public class TestDrawPictureShape {
PictureData pd = picShape.getPictureData(); PictureData pd = picShape.getPictureData();
Dimension dimPd = pd.getImageDimension(); Dimension dimPd = pd.getImageDimension();
new DrawPictureShape(picShape).resize(); new DrawPictureShape(picShape).resize();
Dimension dimShape = picShape.getAnchor().getSize(); Dimension dimShape = new Dimension(
(int)picShape.getAnchor().getWidth(),
(int)picShape.getAnchor().getHeight()
);
assertEquals(dimPd, dimShape); assertEquals(dimPd, dimShape);
int newWidth = (int)(dimPd.getWidth()*(100d/dimPd.getHeight())); double newWidth = (dimPd.getWidth()*(100d/dimPd.getHeight()));
// ... -1 is a rounding error // ... -1 is a rounding error
Rectangle expRect = new Rectangle(50+300-newWidth-1, 50, newWidth, 100); Rectangle2D expRect = new Rectangle2D.Double(rbf(50+300-newWidth, picShape), 50, rbf(newWidth, picShape), 100);
Rectangle target = new Rectangle(50,50,300,100); Rectangle2D target = new Rectangle2D.Double(50,50,300,100);
new DrawPictureShape(picShape).resize(target, RectAlign.BOTTOM_RIGHT); new DrawPictureShape(picShape).resize(target, RectAlign.BOTTOM_RIGHT);
Rectangle actRect = picShape.getAnchor(); Rectangle2D actRect = picShape.getAnchor();
assertEquals(expRect, actRect); assertEquals(expRect.getX(), actRect.getX(), .0001);
assertEquals(expRect.getY(), actRect.getY(), .0001);
assertEquals(expRect.getWidth(), actRect.getWidth(), .0001);
assertEquals(expRect.getHeight(), actRect.getHeight(), .0001);
ss.close();
} }
} }
// round back and forth - points -> master -> points
static double rbf(double val, PictureShape<?,?> picShape) {
if (picShape.getClass().getName().contains("HSLF")) {
return Units.masterToPoints(Units.pointsToMaster(val));
} else {
return val;
}
}
} }

View File

@ -20,13 +20,18 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import org.apache.poi.sl.usermodel.LineDecoration.DecorationShape; import org.apache.poi.sl.usermodel.LineDecoration.DecorationShape;
import org.apache.poi.sl.usermodel.LineDecoration.DecorationSize; import org.apache.poi.sl.usermodel.LineDecoration.DecorationSize;
import org.apache.poi.sl.usermodel.*; import org.apache.poi.sl.usermodel.ShapeType;
import org.junit.Test; import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.*; import org.openxmlformats.schemas.drawingml.x2006.main.CTConnection;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualConnectorProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector; import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
/** /**
@ -122,17 +127,17 @@ public class TestXSLFConnectorShape {
XSLFAutoShape rect1 = slide.createAutoShape(); XSLFAutoShape rect1 = slide.createAutoShape();
rect1.setShapeType(ShapeType.RECT); rect1.setShapeType(ShapeType.RECT);
rect1.setAnchor(new Rectangle(100, 100, 100, 100)); rect1.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
rect1.setFillColor(Color.blue); rect1.setFillColor(Color.blue);
XSLFAutoShape rect2 = slide.createAutoShape(); XSLFAutoShape rect2 = slide.createAutoShape();
rect2.setShapeType(ShapeType.RECT); rect2.setShapeType(ShapeType.RECT);
rect2.setAnchor(new Rectangle(300, 300, 100, 100)); rect2.setAnchor(new Rectangle2D.Double(300, 300, 100, 100));
rect2.setFillColor(Color.red); rect2.setFillColor(Color.red);
XSLFConnectorShape connector1 = slide.createConnector(); XSLFConnectorShape connector1 = slide.createConnector();
connector1.setAnchor(new Rectangle(200, 150, 100, 200)); connector1.setAnchor(new Rectangle2D.Double(200, 150, 100, 200));
CTConnector ctConnector = (CTConnector)connector1.getXmlObject(); CTConnector ctConnector = (CTConnector)connector1.getXmlObject();
ctConnector.getSpPr().getPrstGeom().setPrst(STShapeType.BENT_CONNECTOR_3); ctConnector.getSpPr().getPrstGeom().setPrst(STShapeType.BENT_CONNECTOR_3);

View File

@ -16,11 +16,11 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D; import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import org.junit.Test; import org.junit.Test;
@ -35,7 +35,7 @@ public class TestXSLFFreeformShape {
XSLFSlide slide = ppt.createSlide(); XSLFSlide slide = ppt.createSlide();
XSLFFreeformShape shape1 = slide.createFreeform(); XSLFFreeformShape shape1 = slide.createFreeform();
// comples path consisting of a rectangle and an ellipse inside it // comples path consisting of a rectangle and an ellipse inside it
GeneralPath path1 = new GeneralPath(new Rectangle(150, 150, 300, 300)); GeneralPath path1 = new GeneralPath(new Rectangle2D.Double(150, 150, 300, 300));
path1.append(new Ellipse2D.Double(200, 200, 100, 50), false); path1.append(new Ellipse2D.Double(200, 200, 100, 50), false);
shape1.setPath(path1); shape1.setPath(path1);

View File

@ -21,7 +21,7 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import org.junit.Test; import org.junit.Test;
@ -40,11 +40,11 @@ public class TestXSLFGroupShape {
XSLFGroupShape group = slide.createGroup(); XSLFGroupShape group = slide.createGroup();
assertEquals(1, slide.getShapes().size()); assertEquals(1, slide.getShapes().size());
Rectangle interior = new Rectangle(-10, -10, 20, 20); Rectangle2D interior = new Rectangle2D.Double(-10, -10, 20, 20);
group.setInteriorAnchor(interior); group.setInteriorAnchor(interior);
assertEquals(interior, group.getInteriorAnchor()); assertEquals(interior, group.getInteriorAnchor());
Rectangle anchor = new Rectangle(0, 0, 792, 612); Rectangle2D anchor = new Rectangle2D.Double(0, 0, 792, 612);
group.setAnchor(anchor); group.setAnchor(anchor);
assertEquals(anchor, group.getAnchor()); assertEquals(anchor, group.getAnchor());

View File

@ -24,7 +24,7 @@ import static org.junit.Assert.assertTrue;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -74,7 +74,7 @@ public class TestXSLFTextParagraph {
"of text within a shape. Properties here apply to all text " + "of text within a shape. Properties here apply to all text " +
"residing within the corresponding paragraph."); "residing within the corresponding paragraph.");
Rectangle anchor = new Rectangle(50, 50, 300, 200); Rectangle2D anchor = new Rectangle2D.Double(50, 50, 300, 200);
sh.setAnchor(anchor); sh.setAnchor(anchor);
DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p); DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
@ -172,7 +172,7 @@ public class TestXSLFTextParagraph {
"of text within a shape. Properties here apply to all text " + "of text within a shape. Properties here apply to all text " +
"residing within the corresponding paragraph."); "residing within the corresponding paragraph.");
sh.setAnchor(new Rectangle(50, 50, 300, 200)); sh.setAnchor(new Rectangle2D.Double(50, 50, 300, 200));
DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p); DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
@ -184,13 +184,13 @@ public class TestXSLFTextParagraph {
assertEquals(4, lines.size()); assertEquals(4, lines.size());
// decrease the shape width from 300 pt to 100 pt // decrease the shape width from 300 pt to 100 pt
sh.setAnchor(new Rectangle(50, 50, 100, 200)); sh.setAnchor(new Rectangle2D.Double(50, 50, 100, 200));
dtp.breakText(graphics); dtp.breakText(graphics);
lines = dtp.getLines(); lines = dtp.getLines();
assertEquals(12, lines.size()); assertEquals(12, lines.size());
// decrease the shape width from 300 pt to 100 pt // decrease the shape width from 300 pt to 100 pt
sh.setAnchor(new Rectangle(50, 50, 600, 200)); sh.setAnchor(new Rectangle2D.Double(50, 50, 600, 200));
dtp.breakText(graphics); dtp.breakText(graphics);
lines = dtp.getLines(); lines = dtp.getLines();
assertEquals(2, lines.size()); assertEquals(2, lines.size());
@ -224,7 +224,7 @@ public class TestXSLFTextParagraph {
assertEquals("POI", lines.get(1).getString()); assertEquals("POI", lines.get(1).getString());
XSLFAutoShape sh2 = slide.createAutoShape(); XSLFAutoShape sh2 = slide.createAutoShape();
sh2.setAnchor(new Rectangle(50, 50, 300, 200)); sh2.setAnchor(new Rectangle2D.Double(50, 50, 300, 200));
XSLFTextParagraph p2 = sh2.addNewTextParagraph(); XSLFTextParagraph p2 = sh2.addNewTextParagraph();
XSLFTextRun r2 = p2.addNewTextRun(); XSLFTextRun r2 = p2.addNewTextRun();
r2.setFontFamily("serif"); // this should always be available r2.setFontFamily("serif"); // this should always be available

View File

@ -17,7 +17,6 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator; import java.awt.geom.PathIterator;
@ -90,7 +89,7 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh
@Override @Override
public int setPath(GeneralPath path) { public int setPath(GeneralPath path) {
Rectangle bounds = path.getBounds(); Rectangle2D bounds = path.getBounds2D();
PathIterator it = path.getPathIterator(new AffineTransform()); PathIterator it = path.getPathIterator(new AffineTransform());
List<byte[]> segInfo = new ArrayList<byte[]>(); List<byte[]> segInfo = new ArrayList<byte[]>();

View File

@ -17,7 +17,7 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -73,7 +73,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
} }
@Override @Override
public void setAnchor(Rectangle anchor) { public void setAnchor(Rectangle2D anchor) {
EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID); EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
boolean isInitialized = !(clientAnchor.getDx1() == 0 && clientAnchor.getRow1() == 0); boolean isInitialized = !(clientAnchor.getDx1() == 0 && clientAnchor.getRow1() == 0);
@ -85,7 +85,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
} }
@Override @Override
public void setInteriorAnchor(Rectangle anchor){ public void setInteriorAnchor(Rectangle2D anchor){
EscherSpgrRecord spgr = getEscherChild(EscherSpgrRecord.RECORD_ID); EscherSpgrRecord spgr = getEscherChild(EscherSpgrRecord.RECORD_ID);
int x1 = Units.pointsToMaster(anchor.getX()); int x1 = Units.pointsToMaster(anchor.getX());
@ -100,16 +100,16 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
} }
@Override @Override
public Rectangle getInteriorAnchor(){ public Rectangle2D getInteriorAnchor(){
EscherSpgrRecord rec = getEscherChild(EscherSpgrRecord.RECORD_ID); EscherSpgrRecord rec = getEscherChild(EscherSpgrRecord.RECORD_ID);
int x1 = (int)Units.masterToPoints(rec.getRectX1()); double x1 = Units.masterToPoints(rec.getRectX1());
int y1 = (int)Units.masterToPoints(rec.getRectY1()); double y1 = Units.masterToPoints(rec.getRectY1());
int x2 = (int)Units.masterToPoints(rec.getRectX2()); double x2 = Units.masterToPoints(rec.getRectX2());
int y2 = (int)Units.masterToPoints(rec.getRectY2()); double y2 = Units.masterToPoints(rec.getRectY2());
return new Rectangle(x1,y1,x2-x1,y2-y1); return new Rectangle2D.Double(x1,y1,x2-x1,y2-y1);
} }
protected void setExteriorAnchor(Rectangle anchor) { protected void setExteriorAnchor(Rectangle2D anchor) {
EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID); EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
//hack. internal variable EscherClientAnchorRecord.shortRecord can be //hack. internal variable EscherClientAnchorRecord.shortRecord can be
@ -121,10 +121,10 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
clientAnchor.fillFields(header, 0, null); clientAnchor.fillFields(header, 0, null);
// All coordinates need to be converted to Master units (576 dpi) // All coordinates need to be converted to Master units (576 dpi)
clientAnchor.setFlag((short)Units.pointsToMaster(anchor.y)); clientAnchor.setFlag((short)Units.pointsToMaster(anchor.getY()));
clientAnchor.setCol1((short)Units.pointsToMaster(anchor.x)); clientAnchor.setCol1((short)Units.pointsToMaster(anchor.getX()));
clientAnchor.setDx1((short)Units.pointsToMaster(anchor.width + anchor.x)); clientAnchor.setDx1((short)Units.pointsToMaster(anchor.getWidth() + anchor.getX()));
clientAnchor.setRow1((short)Units.pointsToMaster(anchor.height + anchor.y)); clientAnchor.setRow1((short)Units.pointsToMaster(anchor.getHeight() + anchor.getY()));
// TODO: does this make sense? // TODO: does this make sense?
setInteriorAnchor(anchor); setInteriorAnchor(anchor);
@ -177,20 +177,20 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
/** /**
* Moves and scales this <code>ShapeGroup</code> to the specified anchor. * Moves and scales this <code>ShapeGroup</code> to the specified anchor.
*/ */
protected void moveAndScale(Rectangle anchorDest){ protected void moveAndScale(Rectangle2D anchorDest){
Rectangle anchorSrc = getAnchor(); Rectangle2D anchorSrc = getAnchor();
double scaleX = (anchorSrc.width == 0) ? 0 : anchorDest.width / (double)anchorSrc.width; double scaleX = (anchorSrc.getWidth() == 0) ? 0 : anchorDest.getWidth() / anchorSrc.getWidth();
double scaleY = (anchorSrc.height == 0) ? 0 : anchorDest.height / (double)anchorSrc.height; double scaleY = (anchorSrc.getHeight() == 0) ? 0 : anchorDest.getHeight() / anchorSrc.getHeight();
setExteriorAnchor(anchorDest); setExteriorAnchor(anchorDest);
for (HSLFShape shape : getShapes()) { for (HSLFShape shape : getShapes()) {
Rectangle chanchor = shape.getAnchor(); Rectangle2D chanchor = shape.getAnchor();
int x = (int)Math.rint(anchorDest.x+(chanchor.x-anchorSrc.x)*scaleX); double x = anchorDest.getX()+(chanchor.getX()-anchorSrc.getX())*scaleX;
int y = (int)Math.rint(anchorDest.y+(chanchor.y-anchorSrc.y)*scaleY); double y = anchorDest.getY()+(chanchor.getY()-anchorSrc.getY())*scaleY;
int width = (int)Math.rint(chanchor.width*scaleX); double width = chanchor.getWidth()*scaleX;
int height = (int)Math.rint(chanchor.height*scaleY); double height = chanchor.getHeight()*scaleY;
shape.setAnchor(new Rectangle(x, y, width, height)); shape.setAnchor(new Rectangle2D.Double(x, y, width, height));
} }
} }
@ -200,7 +200,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
* *
* @return the anchor of this shape group * @return the anchor of this shape group
*/ */
public Rectangle getAnchor(){ public Rectangle2D getAnchor(){
EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID); EscherClientAnchorRecord clientAnchor = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
int x1,y1,x2,y2; int x1,y1,x2,y2;
if(clientAnchor == null){ if(clientAnchor == null){
@ -216,11 +216,11 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
x2 = clientAnchor.getDx1(); x2 = clientAnchor.getDx1();
y2 = clientAnchor.getRow1(); y2 = clientAnchor.getRow1();
} }
Rectangle anchor= new Rectangle( Rectangle2D anchor= new Rectangle2D.Double(
(int)(x1 == -1 ? -1 : Units.masterToPoints(x1)), (x1 == -1 ? -1 : Units.masterToPoints(x1)),
(int)(y1 == -1 ? -1 : Units.masterToPoints(y1)), (y1 == -1 ? -1 : Units.masterToPoints(y1)),
(int)(x2 == -1 ? -1 : Units.masterToPoints(x2-x1)), (x2 == -1 ? -1 : Units.masterToPoints(x2-x1)),
(int)(y2 == -1 ? -1 : Units.masterToPoints(y2-y1)) (y2 == -1 ? -1 : Units.masterToPoints(y2-y1))
); );
return anchor; return anchor;
@ -295,7 +295,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
public HSLFTextBox createTextBox() { public HSLFTextBox createTextBox() {
HSLFTextBox s = new HSLFTextBox(this); HSLFTextBox s = new HSLFTextBox(this);
s.setHorizontalCentered(true); s.setHorizontalCentered(true);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -304,7 +304,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
public HSLFAutoShape createAutoShape() { public HSLFAutoShape createAutoShape() {
HSLFAutoShape s = new HSLFAutoShape(ShapeType.RECT, this); HSLFAutoShape s = new HSLFAutoShape(ShapeType.RECT, this);
s.setHorizontalCentered(true); s.setHorizontalCentered(true);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -313,7 +313,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
public HSLFFreeformShape createFreeform() { public HSLFFreeformShape createFreeform() {
HSLFFreeformShape s = new HSLFFreeformShape(this); HSLFFreeformShape s = new HSLFFreeformShape(this);
s.setHorizontalCentered(true); s.setHorizontalCentered(true);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -321,7 +321,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
@Override @Override
public HSLFConnectorShape createConnector() { public HSLFConnectorShape createConnector() {
HSLFConnectorShape s = new HSLFConnectorShape(this); HSLFConnectorShape s = new HSLFConnectorShape(this);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -329,7 +329,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
@Override @Override
public HSLFGroupShape createGroup() { public HSLFGroupShape createGroup() {
HSLFGroupShape s = new HSLFGroupShape(this); HSLFGroupShape s = new HSLFGroupShape(this);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -340,7 +340,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
throw new IllegalArgumentException("pictureData needs to be of type HSLFPictureData"); throw new IllegalArgumentException("pictureData needs to be of type HSLFPictureData");
} }
HSLFPictureShape s = new HSLFPictureShape((HSLFPictureData)pictureData, this); HSLFPictureShape s = new HSLFPictureShape((HSLFPictureData)pictureData, this);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -351,7 +351,7 @@ implements HSLFShapeContainer, GroupShape<HSLFShape,HSLFTextParagraph> {
throw new IllegalArgumentException("numRows and numCols must be greater than 0"); throw new IllegalArgumentException("numRows and numCols must be greater than 0");
} }
HSLFTable s = new HSLFTable(numRows,numCols,this); HSLFTable s = new HSLFTable(numRows,numCols,this);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }

View File

@ -18,7 +18,7 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.List; import java.util.List;
import org.apache.poi.ddf.AbstractEscherOptRecord; import org.apache.poi.ddf.AbstractEscherOptRecord;
@ -113,6 +113,7 @@ public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape<HS
return _escherContainer; return _escherContainer;
} }
@SuppressWarnings("resource")
@Override @Override
public HSLFPictureData getPictureData(){ public HSLFPictureData getPictureData(){
HSLFSlideShow ppt = getSheet().getSlideShow(); HSLFSlideShow ppt = getSheet().getSlideShow();
@ -132,6 +133,7 @@ public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape<HS
return null; return null;
} }
@SuppressWarnings("resource")
protected EscherBSERecord getEscherBSERecord(){ protected EscherBSERecord getEscherBSERecord(){
HSLFSlideShow ppt = getSheet().getSlideShow(); HSLFSlideShow ppt = getSheet().getSlideShow();
Document doc = ppt.getDocumentRecord(); Document doc = ppt.getDocumentRecord();
@ -184,7 +186,7 @@ public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape<HS
EscherBSERecord bse = getEscherBSERecord(); EscherBSERecord bse = getEscherBSERecord();
bse.setRef(bse.getRef() + 1); bse.setRef(bse.getRef() + 1);
Rectangle anchor = getAnchor(); Rectangle2D anchor = getAnchor();
if (anchor.isEmpty()){ if (anchor.isEmpty()){
new DrawPictureShape(this).resize(); new DrawPictureShape(this).resize();
} }

View File

@ -19,7 +19,7 @@ package org.apache.poi.hslf.usermodel;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.ddf.AbstractEscherOptRecord; import org.apache.poi.ddf.AbstractEscherOptRecord;
@ -133,7 +133,7 @@ public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
* *
* @return the anchor of this shape * @return the anchor of this shape
*/ */
public Rectangle getAnchor() { public Rectangle2D getAnchor() {
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID); EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
int flags = spRecord.getFlags(); int flags = spRecord.getFlags();
int x1,y1,x2,y2; int x1,y1,x2,y2;
@ -156,11 +156,11 @@ public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
} }
// TODO: find out where this -1 value comes from at #57820 (link to ms docs?) // TODO: find out where this -1 value comes from at #57820 (link to ms docs?)
Rectangle anchor = new Rectangle( Rectangle2D anchor = new Rectangle2D.Double(
(int)(x1 == -1 ? -1 : Units.masterToPoints(x1)), (x1 == -1 ? -1 : Units.masterToPoints(x1)),
(int)(y1 == -1 ? -1 : Units.masterToPoints(y1)), (y1 == -1 ? -1 : Units.masterToPoints(y1)),
(int)(x2 == -1 ? -1 : Units.masterToPoints(x2-x1)), (x2 == -1 ? -1 : Units.masterToPoints(x2-x1)),
(int)(y2 == -1 ? -1 : Units.masterToPoints(y2-y1)) (y2 == -1 ? -1 : Units.masterToPoints(y2-y1))
); );
return anchor; return anchor;
@ -172,7 +172,7 @@ public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
* *
* @param anchor new anchor * @param anchor new anchor
*/ */
public void setAnchor(Rectangle anchor){ public void setAnchor(Rectangle2D anchor){
int x = Units.pointsToMaster(anchor.getX()); int x = Units.pointsToMaster(anchor.getX());
int y = Units.pointsToMaster(anchor.getY()); int y = Units.pointsToMaster(anchor.getY());
int w = Units.pointsToMaster(anchor.getWidth() + anchor.getX()); int w = Units.pointsToMaster(anchor.getWidth() + anchor.getX());
@ -201,10 +201,10 @@ public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
* @param x the x coordinate of the top left corner of the shape * @param x the x coordinate of the top left corner of the shape
* @param y the y coordinate of the top left corner of the shape * @param y the y coordinate of the top left corner of the shape
*/ */
public final void moveTo(float x, float y) { public final void moveTo(double x, double y) {
// This convenience method should be implemented via setAnchor in subclasses // This convenience method should be implemented via setAnchor in subclasses
// see HSLFGroupShape.setAnchor() for a reference // see HSLFGroupShape.setAnchor() for a reference
Rectangle anchor = getAnchor(); Rectangle2D anchor = getAnchor();
anchor.setRect(x, y, anchor.getWidth(), anchor.getHeight()); anchor.setRect(x, y, anchor.getWidth(), anchor.getHeight());
setAnchor(anchor); setAnchor(anchor);
} }

View File

@ -18,7 +18,7 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -402,7 +402,7 @@ public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,H
public HSLFTextBox createTextBox() { public HSLFTextBox createTextBox() {
HSLFTextBox s = new HSLFTextBox(); HSLFTextBox s = new HSLFTextBox();
s.setHorizontalCentered(true); s.setHorizontalCentered(true);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -411,7 +411,7 @@ public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,H
public HSLFAutoShape createAutoShape() { public HSLFAutoShape createAutoShape() {
HSLFAutoShape s = new HSLFAutoShape(ShapeType.RECT); HSLFAutoShape s = new HSLFAutoShape(ShapeType.RECT);
s.setHorizontalCentered(true); s.setHorizontalCentered(true);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -420,7 +420,7 @@ public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,H
public HSLFFreeformShape createFreeform() { public HSLFFreeformShape createFreeform() {
HSLFFreeformShape s = new HSLFFreeformShape(); HSLFFreeformShape s = new HSLFFreeformShape();
s.setHorizontalCentered(true); s.setHorizontalCentered(true);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -428,7 +428,7 @@ public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,H
@Override @Override
public HSLFConnectorShape createConnector() { public HSLFConnectorShape createConnector() {
HSLFConnectorShape s = new HSLFConnectorShape(); HSLFConnectorShape s = new HSLFConnectorShape();
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -436,7 +436,7 @@ public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,H
@Override @Override
public HSLFGroupShape createGroup() { public HSLFGroupShape createGroup() {
HSLFGroupShape s = new HSLFGroupShape(); HSLFGroupShape s = new HSLFGroupShape();
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }
@ -447,7 +447,7 @@ public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,H
throw new IllegalArgumentException("pictureData needs to be of type HSLFPictureData"); throw new IllegalArgumentException("pictureData needs to be of type HSLFPictureData");
} }
HSLFPictureShape s = new HSLFPictureShape((HSLFPictureData)pictureData); HSLFPictureShape s = new HSLFPictureShape((HSLFPictureData)pictureData);
s.setAnchor(new Rectangle(0, 0, 100, 100)); s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s); addShape(s);
return s; return s;
} }

View File

@ -17,7 +17,7 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -76,13 +76,13 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
if(numRows < 1) throw new IllegalArgumentException("The number of rows must be greater than 1"); if(numRows < 1) throw new IllegalArgumentException("The number of rows must be greater than 1");
if(numCols < 1) throw new IllegalArgumentException("The number of columns must be greater than 1"); if(numCols < 1) throw new IllegalArgumentException("The number of columns must be greater than 1");
int x=0, y=0, tblWidth=0, tblHeight=0; double x=0, y=0, tblWidth=0, tblHeight=0;
cells = new HSLFTableCell[numRows][numCols]; cells = new HSLFTableCell[numRows][numCols];
for (int i = 0; i < cells.length; i++) { for (int i = 0; i < cells.length; i++) {
x = 0; x = 0;
for (int j = 0; j < cells[i].length; j++) { for (int j = 0; j < cells[i].length; j++) {
cells[i][j] = new HSLFTableCell(this); cells[i][j] = new HSLFTableCell(this);
Rectangle anchor = new Rectangle(x, y, HSLFTableCell.DEFAULT_WIDTH, HSLFTableCell.DEFAULT_HEIGHT); Rectangle2D anchor = new Rectangle2D.Double(x, y, HSLFTableCell.DEFAULT_WIDTH, HSLFTableCell.DEFAULT_HEIGHT);
cells[i][j].setAnchor(anchor); cells[i][j].setAnchor(anchor);
x += HSLFTableCell.DEFAULT_WIDTH; x += HSLFTableCell.DEFAULT_WIDTH;
} }
@ -90,7 +90,7 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
} }
tblWidth = x; tblWidth = x;
tblHeight = y; tblHeight = y;
setExteriorAnchor(new Rectangle(0, 0, tblWidth, tblHeight)); setExteriorAnchor(new Rectangle2D.Double(0, 0, tblWidth, tblHeight));
EscherContainerRecord spCont = (EscherContainerRecord) getSpContainer().getChild(0); EscherContainerRecord spCont = (EscherContainerRecord) getSpContainer().getChild(0);
AbstractEscherOptRecord opt = new EscherOptRecord(); AbstractEscherOptRecord opt = new EscherOptRecord();
@ -159,13 +159,18 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
private static class TableCellComparator implements Comparator<HSLFShape> { private static class TableCellComparator implements Comparator<HSLFShape> {
public int compare( HSLFShape o1, HSLFShape o2 ) { public int compare( HSLFShape o1, HSLFShape o2 ) {
Rectangle anchor1 = o1.getAnchor(); Rectangle2D anchor1 = o1.getAnchor();
Rectangle anchor2 = o2.getAnchor(); Rectangle2D anchor2 = o2.getAnchor();
int delta = anchor1.y - anchor2.y; double delta = anchor1.getY() - anchor2.getY();
if (delta == 0) delta = anchor1.x - anchor2.x; if (delta == 0) {
delta = anchor1.getX() - anchor2.getX();
}
// descending size // descending size
if (delta == 0) delta = (anchor2.width*anchor2.height)-(anchor1.width*anchor1.height); if (delta == 0) {
return delta; delta = (anchor2.getWidth()*anchor2.getHeight())-(anchor1.getWidth()*anchor1.getHeight());
}
return (int)Math.signum(delta);
} }
} }
@ -186,12 +191,12 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
List<HSLFTableCell[]> lst = new ArrayList<HSLFTableCell[]>(); List<HSLFTableCell[]> lst = new ArrayList<HSLFTableCell[]>();
List<HSLFTableCell> row = new ArrayList<HSLFTableCell>(); List<HSLFTableCell> row = new ArrayList<HSLFTableCell>();
int y0 = htc.get(0).getAnchor().y; double y0 = htc.get(0).getAnchor().getY();
for (HSLFTableCell sh : htc) { for (HSLFTableCell sh : htc) {
Rectangle anchor = sh.getAnchor(); Rectangle2D anchor = sh.getAnchor();
boolean isNextRow = (anchor.y > y0); boolean isNextRow = (anchor.getY() > y0);
if (isNextRow) { if (isNextRow) {
y0 = anchor.y; y0 = anchor.getY();
lst.add(row.toArray(new HSLFTableCell[row.size()])); lst.add(row.toArray(new HSLFTableCell[row.size()]));
row.clear(); row.clear();
} }
@ -207,7 +212,7 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
final double lx1, lx2, ly1, ly2; final double lx1, lx2, ly1, ly2;
LineRect(HSLFLine l) { LineRect(HSLFLine l) {
this.l = l; this.l = l;
Rectangle r = l.getAnchor(); Rectangle2D r = l.getAnchor();
lx1 = r.getMinX(); lx1 = r.getMinX();
lx2 = r.getMaxX(); lx2 = r.getMaxX();
ly1 = r.getMinY(); ly1 = r.getMinY();
@ -240,7 +245,7 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
// TODO: this only works for non-rotated tables // TODO: this only works for non-rotated tables
for (HSLFTableCell[] tca : cells) { for (HSLFTableCell[] tca : cells) {
for (HSLFTableCell tc : tca) { for (HSLFTableCell tc : tca) {
final Rectangle cellAnchor = tc.getAnchor(); final Rectangle2D cellAnchor = tc.getAnchor();
/** /**
* x1/y1 --------+ * x1/y1 --------+
@ -321,29 +326,53 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
} }
} }
@Override
public double getRowHeight(int row) {
if (row < 0 || row >= cells.length) {
throw new IllegalArgumentException("Row index '"+row+"' is not within range [0-"+(cells.length-1)+"]");
}
return cells[row][0].getAnchor().getHeight();
}
@Override @Override
public void setRowHeight(int row, double height) { public void setRowHeight(int row, double height) {
if (row < 0 || row >= cells.length) {
throw new IllegalArgumentException("Row index '"+row+"' is not within range [0-"+(cells.length-1)+"]");
}
int pxHeight = Units.pointsToPixel(height); int pxHeight = Units.pointsToPixel(height);
int currentHeight = cells[row][0].getAnchor().height; double currentHeight = cells[row][0].getAnchor().getHeight();
int dy = pxHeight - currentHeight; double dy = pxHeight - currentHeight;
for (int i = row; i < cells.length; i++) { for (int i = row; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) { for (int j = 0; j < cells[i].length; j++) {
Rectangle anchor = cells[i][j].getAnchor(); Rectangle2D anchor = cells[i][j].getAnchor();
if(i == row) { if(i == row) {
anchor.height = pxHeight; anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), pxHeight);
} else { } else {
anchor.y += dy; anchor.setRect(anchor.getX(), anchor.getY()+dy, anchor.getWidth(), pxHeight);
} }
cells[i][j].setAnchor(anchor); cells[i][j].setAnchor(anchor);
} }
} }
Rectangle tblanchor = getAnchor(); Rectangle2D tblanchor = getAnchor();
tblanchor.height += dy; tblanchor.setRect(tblanchor.getX(), tblanchor.getY(), tblanchor.getWidth(), tblanchor.getHeight() + dy);
setExteriorAnchor(tblanchor); setExteriorAnchor(tblanchor);
} }
@Override
public double getColumnWidth(int col) {
if (col < 0 || col >= cells[0].length) {
throw new IllegalArgumentException("Column index '"+col+"' is not within range [0-"+(cells[0].length-1)+"]");
}
// TODO: check for merged cols
double width = cells[0][col].getAnchor().getWidth();
return width;
}
@Override @Override
public void setColumnWidth(int col, final double width){ public void setColumnWidth(int col, final double width){
if (col < 0 || col >= cells[0].length) { if (col < 0 || col >= cells[0].length) {
@ -352,20 +381,20 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
double currentWidth = cells[0][col].getAnchor().getWidth(); double currentWidth = cells[0][col].getAnchor().getWidth();
double dx = width - currentWidth; double dx = width - currentWidth;
for (HSLFTableCell cols[] : cells) { for (HSLFTableCell cols[] : cells) {
Rectangle anchor = cols[col].getAnchor(); Rectangle2D anchor = cols[col].getAnchor();
anchor.width = (int)Math.rint(width); anchor.setRect(anchor.getX(), anchor.getY(), width, anchor.getHeight());
cols[col].setAnchor(anchor); cols[col].setAnchor(anchor);
if (col < cols.length - 1) { if (col < cols.length - 1) {
for (int j = col+1; j < cols.length; j++) { for (int j = col+1; j < cols.length; j++) {
anchor = cols[j].getAnchor(); anchor = cols[j].getAnchor();
anchor.x += dx; anchor.setRect(anchor.getX()+dx, anchor.getY(), anchor.getWidth(), anchor.getHeight());
cols[j].setAnchor(anchor); cols[j].setAnchor(anchor);
} }
} }
} }
Rectangle tblanchor = getAnchor(); Rectangle2D tblanchor = getAnchor();
tblanchor.width += dx; tblanchor.setRect(tblanchor.getX(), tblanchor.getY(), tblanchor.getWidth() + dx, tblanchor.getHeight());
setExteriorAnchor(tblanchor); setExteriorAnchor(tblanchor);
} }
@ -393,7 +422,7 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
} }
@Override @Override
protected void moveAndScale(Rectangle anchorDest){ protected void moveAndScale(Rectangle2D anchorDest){
super.moveAndScale(anchorDest); super.moveAndScale(anchorDest);
updateRowHeightsProperty(); updateRowHeightsProperty();
} }
@ -403,7 +432,7 @@ implements HSLFShapeContainer, TableShape<HSLFShape,HSLFTextParagraph> {
EscherArrayProperty p = opt.lookup(EscherProperties.GROUPSHAPE__TABLEROWPROPERTIES); EscherArrayProperty p = opt.lookup(EscherProperties.GROUPSHAPE__TABLEROWPROPERTIES);
byte[] val = new byte[4]; byte[] val = new byte[4];
for (int rowIdx = 0; rowIdx < cells.length; rowIdx++) { for (int rowIdx = 0; rowIdx < cells.length; rowIdx++) {
int rowHeight = Units.pointsToMaster(cells[rowIdx][0].getAnchor().height); int rowHeight = Units.pointsToMaster(cells[rowIdx][0].getAnchor().getHeight());
LittleEndian.putInt(val, 0, rowHeight); LittleEndian.putInt(val, 0, rowHeight);
p.setElement(rowIdx, val); p.setElement(rowIdx, val);
} }

View File

@ -18,7 +18,7 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import org.apache.poi.ddf.AbstractEscherOptRecord; import org.apache.poi.ddf.AbstractEscherOptRecord;
import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherContainerRecord;
@ -85,40 +85,40 @@ public final class HSLFTableCell extends HSLFTextBox implements TableCell<HSLFSh
if (line == null) { if (line == null) {
return; return;
} }
Rectangle cellAnchor = getAnchor(); Rectangle2D cellAnchor = getAnchor();
Rectangle lineAnchor = new Rectangle(); double x,y,w,h;
switch(edge){ switch(edge){
case top: case top:
lineAnchor.x = cellAnchor.x; x = cellAnchor.getX();
lineAnchor.y = cellAnchor.y; y = cellAnchor.getY();
lineAnchor.width = cellAnchor.width; w = cellAnchor.getWidth();
lineAnchor.height = 0; h = 0;
break; break;
case right: case right:
lineAnchor.x = cellAnchor.x + cellAnchor.width; x = cellAnchor.getX() + cellAnchor.getWidth();
lineAnchor.y = cellAnchor.y; y = cellAnchor.getY();
lineAnchor.width = 0; w = 0;
lineAnchor.height = cellAnchor.height; h = cellAnchor.getHeight();
break; break;
case bottom: case bottom:
lineAnchor.x = cellAnchor.x; x = cellAnchor.getX();
lineAnchor.y = cellAnchor.y + cellAnchor.height; y = cellAnchor.getY() + cellAnchor.getHeight();
lineAnchor.width = cellAnchor.width; w = cellAnchor.getWidth();
lineAnchor.height = 0; h = 0;
break; break;
case left: case left:
lineAnchor.x = cellAnchor.x; x = cellAnchor.getX();
lineAnchor.y = cellAnchor.y; y = cellAnchor.getY();
lineAnchor.width = 0; w = 0;
lineAnchor.height = cellAnchor.height; h = cellAnchor.getHeight();
break; break;
default: default:
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
line.setAnchor(lineAnchor); line.setAnchor(new Rectangle2D.Double(x,y,w,h));
} }
public void setAnchor(Rectangle anchor){ public void setAnchor(Rectangle2D anchor){
super.setAnchor(anchor); super.setAnchor(anchor);
anchorBorder(BorderEdge.top, borderTop); anchorBorder(BorderEdge.top, borderTop);

View File

@ -20,7 +20,6 @@ package org.apache.poi.hslf.usermodel;
import static org.apache.poi.hslf.record.RecordTypes.OEPlaceholderAtom; import static org.apache.poi.hslf.record.RecordTypes.OEPlaceholderAtom;
import static org.apache.poi.hslf.record.RecordTypes.RoundTripHFPlaceholder12; import static org.apache.poi.hslf.record.RecordTypes.RoundTripHFPlaceholder12;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext; import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.io.IOException; import java.io.IOException;
@ -209,7 +208,11 @@ implements TextShape<HSLFShape,HSLFTextParagraph> {
} catch (IOException e){ } catch (IOException e){
throw new HSLFException(e); throw new HSLFException(e);
} }
if(getAnchor().equals(new Rectangle()) && !"".equals(getText())) resizeToFitText(); boolean isInitialAnchor = getAnchor().equals(new Rectangle2D.Double());
boolean isFilledTxt = !"".equals(getText());
if (isInitialAnchor && isFilledTxt) {
resizeToFitText();
}
} }
for (HSLFTextParagraph htp : _paragraphs) { for (HSLFTextParagraph htp : _paragraphs) {
htp.setShapeId(getShapeId()); htp.setShapeId(getShapeId());
@ -250,10 +253,10 @@ implements TextShape<HSLFShape,HSLFTextParagraph> {
* @return a <code>Rectangle2D</code> that is the bounds of this shape. * @return a <code>Rectangle2D</code> that is the bounds of this shape.
*/ */
public Rectangle2D resizeToFitText(){ public Rectangle2D resizeToFitText(){
Rectangle anchor = getAnchor(); Rectangle2D anchor = getAnchor();
if(anchor.getWidth() == 0.) { if(anchor.getWidth() == 0.) {
logger.log(POILogger.WARN, "Width of shape wasn't set. Defaulting to 200px"); logger.log(POILogger.WARN, "Width of shape wasn't set. Defaulting to 200px");
anchor.setSize(200, (int)anchor.getHeight()); anchor.setRect(anchor.getX(), anchor.getY(), 200., anchor.getHeight());
setAnchor(anchor); setAnchor(anchor);
} }
double height = getTextHeight(); double height = getTextHeight();

View File

@ -21,7 +21,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -50,7 +50,7 @@ public final class TestMovieShape {
HSLFPictureData thumbnailData = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG); HSLFPictureData thumbnailData = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
MovieShape shape = new MovieShape(movieIdx, thumbnailData); MovieShape shape = new MovieShape(movieIdx, thumbnailData);
shape.setAnchor(new Rectangle(300,225,120,90)); shape.setAnchor(new Rectangle2D.Double(300,225,120,90));
slide.addShape(shape); slide.addShape(shape);
assertEquals(path, shape.getPath()); assertEquals(path, shape.getPath());

View File

@ -20,10 +20,11 @@ package org.apache.poi.hslf.model;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
@ -50,23 +51,36 @@ public final class TestOleEmbedding {
* @throws Exception if an error occurs. * @throws Exception if an error occurs.
*/ */
@Test @Test
public void testOleEmbedding2003() throws Exception { public void testOleEmbedding2003() throws IOException {
HSLFSlideShowImpl slideShow = new HSLFSlideShowImpl(_slTests.openResourceAsStream("ole2-embedding-2003.ppt")); HSLFSlideShowImpl slideShow = new HSLFSlideShowImpl(_slTests.openResourceAsStream("ole2-embedding-2003.ppt"));
// Placeholder EMFs for clients that don't support the OLE components. // Placeholder EMFs for clients that don't support the OLE components.
List<HSLFPictureData> pictures = slideShow.getPictureData(); List<HSLFPictureData> pictures = slideShow.getPictureData();
assertEquals("Should be two pictures", 2, pictures.size()); assertEquals("Should be two pictures", 2, pictures.size());
//assertDigestEquals("Wrong data for picture 1", "8d1fbadf4814f321bb1ccdd056e3c788", pictures[0].getData());
//assertDigestEquals("Wrong data for picture 2", "987a698e83559cf3d38a0deeba1cc63b", pictures[1].getData()); long checkSums[] = { 0xD37A4204l, 0x26A62F68l, 0x82853169l, 0xE0E45D2Bl };
int checkId = 0;
// check for checksum to be uptodate
for (HSLFPictureData pd : pictures) {
long checkEMF = IOUtils.calculateChecksum(pd.getData());
assertEquals(checkSums[checkId++], checkEMF);
}
// Actual embedded objects. // Actual embedded objects.
HSLFObjectData[] objects = slideShow.getEmbeddedObjects(); HSLFObjectData[] objects = slideShow.getEmbeddedObjects();
assertEquals("Should be two objects", 2, objects.length); assertEquals("Should be two objects", 2, objects.length);
//assertDigestEquals("Wrong data for objecs 1", "0d1fcc61a83de5c4894dc0c88e9a019d", objects[0].getData()); for (HSLFObjectData od : objects) {
//assertDigestEquals("Wrong data for object 2", "b323604b2003a7299c77c2693b641495", objects[1].getData()); long checkEMF = IOUtils.calculateChecksum(od.getData());
assertEquals(checkSums[checkId++], checkEMF);
} }
slideShow.close();
}
@Test @Test
public void testOLEShape() throws Exception { public void testOLEShape() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("ole2-embedding-2003.ppt")); HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("ole2-embedding-2003.ppt"));
HSLFSlide slide = ppt.getSlides().get(0); HSLFSlide slide = ppt.getSlides().get(0);
@ -97,12 +111,12 @@ public final class TestOleEmbedding {
} }
assertEquals("Expected 2 OLE shapes", 2, cnt); assertEquals("Expected 2 OLE shapes", 2, cnt);
ppt.close();
} }
@Test @Test
public void testEmbedding() throws Exception { public void testEmbedding() throws IOException {
HSLFSlideShowImpl _hslfSlideShow = HSLFSlideShowImpl.create(); HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlideShow ppt = new HSLFSlideShow(_hslfSlideShow);
File pict = POIDataSamples.getSlideShowInstance().getFile("clock.jpg"); File pict = POIDataSamples.getSlideShowInstance().getFile("clock.jpg");
HSLFPictureData pictData = ppt.addPicture(pict, PictureType.JPEG); HSLFPictureData pictData = ppt.addPicture(pict, PictureType.JPEG);
@ -117,7 +131,7 @@ public final class TestOleEmbedding {
OLEShape oleShape1 = new OLEShape(pictData); OLEShape oleShape1 = new OLEShape(pictData);
oleShape1.setObjectID(oleObjectId1); oleShape1.setObjectID(oleObjectId1);
slide1.addShape(oleShape1); slide1.addShape(oleShape1);
oleShape1.setAnchor(new Rectangle(100,100,100,100)); oleShape1.setAnchor(new Rectangle2D.Double(100,100,100,100));
// add second slide with different order in object creation // add second slide with different order in object creation
HSLFSlide slide2 = ppt.createSlide(); HSLFSlide slide2 = ppt.createSlide();
@ -131,7 +145,7 @@ public final class TestOleEmbedding {
oleShape2.setObjectID(oleObjectId2); oleShape2.setObjectID(oleObjectId2);
slide2.addShape(oleShape2); slide2.addShape(oleShape2);
oleShape2.setAnchor(new Rectangle(100,100,100,100)); oleShape2.setAnchor(new Rectangle2D.Double(100,100,100,100));
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ppt.write(bos); ppt.write(bos);
@ -148,6 +162,8 @@ public final class TestOleEmbedding {
poiData1.close(); poiData1.close();
poiData2.close(); poiData2.close();
ppt.close();
}
} }
}

View File

@ -26,7 +26,7 @@ import static org.junit.Assert.assertTrue;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -49,7 +49,6 @@ import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSimpleShape; import org.apache.poi.hslf.usermodel.HSLFSimpleShape;
import org.apache.poi.hslf.usermodel.HSLFSlide; import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hslf.usermodel.HSLFTextBox; import org.apache.poi.hslf.usermodel.HSLFTextBox;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph; import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun; import org.apache.poi.hslf.usermodel.HSLFTextRun;
@ -84,7 +83,7 @@ public final class TestShapes {
} }
@Test @Test
public void graphics() throws Exception { public void graphics() throws IOException {
HSLFSlide slide = ppt.createSlide(); HSLFSlide slide = ppt.createSlide();
HSLFLine line = new HSLFLine(); HSLFLine line = new HSLFLine();
@ -96,7 +95,7 @@ public final class TestShapes {
slide.addShape(line); slide.addShape(line);
HSLFAutoShape ellipse = new HSLFAutoShape(ShapeType.ELLIPSE); HSLFAutoShape ellipse = new HSLFAutoShape(ShapeType.ELLIPSE);
java.awt.Rectangle ellipseAnchor = new Rectangle(320, 154, 55, 111); Rectangle2D ellipseAnchor = new Rectangle2D.Double(320, 154, 55, 111);
ellipse.setAnchor(ellipseAnchor); ellipse.setAnchor(ellipseAnchor);
ellipse.setLineWidth(2); ellipse.setLineWidth(2);
ellipse.setLineDash(LineDash.SOLID); ellipse.setLineDash(LineDash.SOLID);
@ -110,10 +109,10 @@ public final class TestShapes {
//read ppt from byte array //read ppt from byte array
ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray()))); HSLFSlideShow ppt2 = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
assertEquals(1, ppt.getSlides().size()); assertEquals(1, ppt2.getSlides().size());
slide = ppt.getSlides().get(0); slide = ppt2.getSlides().get(0);
List<HSLFShape> shape = slide.getShapes(); List<HSLFShape> shape = slide.getShapes();
assertEquals(2, shape.size()); assertEquals(2, shape.size());
@ -122,6 +121,8 @@ public final class TestShapes {
assertTrue(shape.get(1) instanceof HSLFAutoShape); //group shape assertTrue(shape.get(1) instanceof HSLFAutoShape); //group shape
assertEquals(ellipseAnchor, shape.get(1).getAnchor()); //group shape assertEquals(ellipseAnchor, shape.get(1).getAnchor()); //group shape
ppt2.close();
} }
/** /**
@ -129,7 +130,7 @@ public final class TestShapes {
* @throws Exception * @throws Exception
*/ */
@Test @Test
public void textBoxRead() throws Exception { public void textBoxRead() throws IOException {
ppt = new HSLFSlideShow(_slTests.openResourceAsStream("with_textbox.ppt")); ppt = new HSLFSlideShow(_slTests.openResourceAsStream("with_textbox.ppt"));
HSLFSlide sl = ppt.getSlides().get(0); HSLFSlide sl = ppt.getSlides().get(0);
for (HSLFShape sh : sl.getShapes()) { for (HSLFShape sh : sl.getShapes()) {
@ -161,7 +162,7 @@ public final class TestShapes {
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Test @Test
public void testParagraphs() throws Exception { public void testParagraphs() throws IOException {
HSLFSlideShow ss = new HSLFSlideShow(); HSLFSlideShow ss = new HSLFSlideShow();
HSLFSlide slide = ss.createSlide(); HSLFSlide slide = ss.createSlide();
HSLFTextBox shape = new HSLFTextBox(); HSLFTextBox shape = new HSLFTextBox();
@ -175,7 +176,7 @@ public final class TestShapes {
p2r2.setStrikethrough(true); p2r2.setStrikethrough(true);
// run 3 has same text properties as run 2 and will be merged when saving // run 3 has same text properties as run 2 and will be merged when saving
HSLFTextRun p2r3 = shape.appendText("para 2 run 3.", false); HSLFTextRun p2r3 = shape.appendText("para 2 run 3.", false);
shape.setAnchor(new Rectangle(100,100,100,10)); shape.setAnchor(new Rectangle2D.Double(100,100,100,10));
slide.addShape(shape); slide.addShape(shape);
shape.resizeToFitText(); shape.resizeToFitText();
@ -207,7 +208,7 @@ public final class TestShapes {
* and set some of the style attributes * and set some of the style attributes
*/ */
@Test @Test
public void textBoxWriteBytes() throws Exception { public void textBoxWriteBytes() throws IOException {
ppt = new HSLFSlideShow(); ppt = new HSLFSlideShow();
HSLFSlide sl = ppt.createSlide(); HSLFSlide sl = ppt.createSlide();
HSLFTextRun rt; HSLFTextRun rt;
@ -241,8 +242,8 @@ public final class TestShapes {
ppt.write(out); ppt.write(out);
out.close(); out.close();
ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray()))); HSLFSlideShow ppt2 = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
sl = ppt.getSlides().get(0); sl = ppt2.getSlides().get(0);
txtbox = (HSLFTextBox)sl.getShapes().get(0); txtbox = (HSLFTextBox)sl.getShapes().get(0);
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0); rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
@ -255,6 +256,8 @@ public final class TestShapes {
assertFalse(rt.isUnderlined()); assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily()); assertEquals("Arial", rt.getFontFamily());
assertTrue(sameColor(Color.red, rt.getFontColor())); assertTrue(sameColor(Color.red, rt.getFontColor()));
ppt2.close();
} }
/** /**
@ -276,7 +279,7 @@ public final class TestShapes {
* it must be the same as returned by Slide.getTextRuns(). * it must be the same as returned by Slide.getTextRuns().
*/ */
@Test @Test
public void textBoxSet() throws Exception { public void textBoxSet() throws IOException {
textBoxSet("with_textbox.ppt"); textBoxSet("with_textbox.ppt");
textBoxSet("basic_test_ppt_file.ppt"); textBoxSet("basic_test_ppt_file.ppt");
textBoxSet("next_test_ppt_file.ppt"); textBoxSet("next_test_ppt_file.ppt");
@ -285,7 +288,7 @@ public final class TestShapes {
textBoxSet("incorrect_slide_order.ppt"); textBoxSet("incorrect_slide_order.ppt");
} }
private void textBoxSet(String filename) throws Exception { private void textBoxSet(String filename) throws IOException {
HSLFSlideShow ss = new HSLFSlideShow(_slTests.openResourceAsStream(filename)); HSLFSlideShow ss = new HSLFSlideShow(_slTests.openResourceAsStream(filename));
for (HSLFSlide sld : ss.getSlides()) { for (HSLFSlide sld : ss.getSlides()) {
ArrayList<String> lst1 = new ArrayList<String>(); ArrayList<String> lst1 = new ArrayList<String>();
@ -311,13 +314,14 @@ public final class TestShapes {
assertTrue(lst1.containsAll(lst2)); assertTrue(lst1.containsAll(lst2));
assertTrue(lst2.containsAll(lst1)); assertTrue(lst2.containsAll(lst1));
} }
ss.close();
} }
/** /**
* Test adding shapes to <code>ShapeGroup</code> * Test adding shapes to <code>ShapeGroup</code>
*/ */
@Test @Test
public void shapeGroup() throws Exception { public void shapeGroup() throws IOException {
HSLFSlideShow ss = new HSLFSlideShow(); HSLFSlideShow ss = new HSLFSlideShow();
HSLFSlide slide = ss.createSlide(); HSLFSlide slide = ss.createSlide();
@ -325,22 +329,23 @@ public final class TestShapes {
HSLFGroupShape group = new HSLFGroupShape(); HSLFGroupShape group = new HSLFGroupShape();
group.setAnchor(new Rectangle(0, 0, (int)pgsize.getWidth(), (int)pgsize.getHeight())); group.setAnchor(new Rectangle2D.Double(0, 0, pgsize.getWidth(), pgsize.getHeight()));
slide.addShape(group); slide.addShape(group);
HSLFPictureData data = ss.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG); HSLFPictureData data = ss.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
HSLFPictureShape pict = new HSLFPictureShape(data, group); HSLFPictureShape pict = new HSLFPictureShape(data, group);
pict.setAnchor(new Rectangle(0, 0, 200, 200)); pict.setAnchor(new Rectangle2D.Double(0, 0, 200, 200));
group.addShape(pict); group.addShape(pict);
HSLFLine line = new HSLFLine(group); HSLFLine line = new HSLFLine(group);
line.setAnchor(new Rectangle(300, 300, 500, 0)); line.setAnchor(new Rectangle2D.Double(300, 300, 500, 0));
group.addShape(line); group.addShape(line);
//serialize and read again. //serialize and read again.
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ss.write(out); ss.write(out);
out.close(); out.close();
ss.close();
ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
ss = new HSLFSlideShow(is); ss = new HSLFSlideShow(is);
@ -359,10 +364,12 @@ public final class TestShapes {
assertTrue(grshape.get(1) instanceof HSLFLine); assertTrue(grshape.get(1) instanceof HSLFLine);
pict = (HSLFPictureShape)grshape.get(0); pict = (HSLFPictureShape)grshape.get(0);
assertEquals(new Rectangle(0, 0, 200, 200), pict.getAnchor()); assertEquals(new Rectangle2D.Double(0, 0, 200, 200), pict.getAnchor());
line = (HSLFLine)grshape.get(1); line = (HSLFLine)grshape.get(1);
assertEquals(new Rectangle(300, 300, 500, 0), line.getAnchor()); assertEquals(new Rectangle2D.Double(300, 300, 500, 0), line.getAnchor());
ss.close();
} }
/** /**
@ -387,10 +394,12 @@ public final class TestShapes {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ss.write(out); ss.write(out);
out.close(); out.close();
ss.close();
ss = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())); ss = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
sl = ss.getSlides().get(0); sl = ss.getSlides().get(0);
assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().size()); assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().size());
ss.close();
} }
@Test @Test
@ -409,7 +418,7 @@ public final class TestShapes {
} }
@Test @Test
public void shapeId() { public void shapeId() throws IOException {
HSLFSlideShow ss = new HSLFSlideShow(); HSLFSlideShow ss = new HSLFSlideShow();
HSLFSlide slide = ss.createSlide(); HSLFSlide slide = ss.createSlide();
HSLFShape shape = null; HSLFShape shape = null;
@ -456,6 +465,7 @@ public final class TestShapes {
slide.addShape(shape); slide.addShape(shape);
} }
assertEquals(numClusters + 1, dgg.getNumIdClusters()); assertEquals(numClusters + 1, dgg.getNumIdClusters());
ss.close();
} }
@Test @Test
@ -482,5 +492,7 @@ public final class TestShapes {
assertEquals("Border width is 5 pt", sh4.getText()); assertEquals("Border width is 5 pt", sh4.getText());
assertEquals(Color.black, sh4.getLineColor()); assertEquals(Color.black, sh4.getLineColor());
assertEquals(5.0, sh4.getLineWidth(), 0); assertEquals(5.0, sh4.getLineWidth(), 0);
ss.close();
} }
} }

View File

@ -25,6 +25,7 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List; import java.util.List;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
@ -42,8 +43,6 @@ import org.junit.Test;
/** /**
* Test <code>Table</code> object. * Test <code>Table</code> object.
*
* @author Yegor Kozlov
*/ */
public final class TestTable { public final class TestTable {
private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
@ -52,7 +51,7 @@ public final class TestTable {
* Test that ShapeFactory works properly and returns <code>Table</code> * Test that ShapeFactory works properly and returns <code>Table</code>
*/ */
@Test @Test
public void testShapeFactory() throws Exception { public void testShapeFactory() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(); HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide(); HSLFSlide slide = ppt.createSlide();
@ -72,6 +71,7 @@ public final class TestTable {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out); ppt.write(out);
out.close(); out.close();
ppt.close();
ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())); ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
slide = ppt.getSlides().get(0); slide = ppt.getSlides().get(0);
@ -79,13 +79,14 @@ public final class TestTable {
HSLFTable tbl3 = (HSLFTable)slide.getShapes().get(0); HSLFTable tbl3 = (HSLFTable)slide.getShapes().get(0);
assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns()); assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns());
assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows()); assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows());
ppt.close();
} }
/** /**
* Error constructing Table when rownum=1 * Error constructing Table when rownum=1
*/ */
@Test @Test
public void test45889(){ public void test45889() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(); HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide(); HSLFSlide slide = ppt.createSlide();
List<HSLFShape> shapes; List<HSLFShape> shapes;
@ -101,24 +102,25 @@ public final class TestTable {
assertEquals(tbl1.getNumberOfColumns(), tbl2.getNumberOfColumns()); assertEquals(tbl1.getNumberOfColumns(), tbl2.getNumberOfColumns());
assertEquals(tbl1.getNumberOfRows(), tbl2.getNumberOfRows()); assertEquals(tbl1.getNumberOfRows(), tbl2.getNumberOfRows());
ppt.close();
} }
@Test @Test(expected=IllegalArgumentException.class)
public void testIllegalCOnstruction(){ public void testIllegalRowCnstruction() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(); HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide(); HSLFSlide slide = ppt.createSlide();
try {
slide.createTable(0, 5); slide.createTable(0, 5);
fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1"); fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
} catch (IllegalArgumentException e){ ppt.close();
} }
try {
@Test(expected=IllegalArgumentException.class)
public void testIllegalColConstruction() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
slide.createTable(5, 0); slide.createTable(5, 0);
fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1"); fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
} catch (IllegalArgumentException e){ ppt.close();
}
} }
/** /**
@ -126,7 +128,7 @@ public final class TestTable {
* when the table is positioned with its top at -1 * when the table is positioned with its top at -1
*/ */
@Test @Test
public void test57820() throws Exception { public void test57820() throws IOException {
SlideShow<?,?> ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bug57820-initTableNullRefrenceException.ppt")); SlideShow<?,?> ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bug57820-initTableNullRefrenceException.ppt"));
List<? extends Slide<?,?>> slides = ppt.getSlides(); List<? extends Slide<?,?>> slides = ppt.getSlides();
@ -143,7 +145,8 @@ public final class TestTable {
} }
assertNotNull(tbl); assertNotNull(tbl);
assertEquals(-1, tbl.getAnchor().getY(), 0); assertEquals(-1, tbl.getAnchor().getY(), 0);
ppt.close();
} }
} }

View File

@ -25,7 +25,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle; import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -58,10 +58,10 @@ public class TestTable {
new DrawTableShape(table).setAllBorders(1.0, Color.black, StrokeStyle.LineDash.DASH_DOT); new DrawTableShape(table).setAllBorders(1.0, Color.black, StrokeStyle.LineDash.DASH_DOT);
table.setAnchor(new Rectangle(100, 100, 400, 400)); table.setAnchor(new Rectangle2D.Double(100, 100, 400, 400));
Rectangle rectExp = new Rectangle(420,367,80,133); Rectangle2D rectExp = new Rectangle2D.Double(420,366.625,80,133.375);
Rectangle rectAct = table.getCell(rows-1, cols-1).getAnchor(); Rectangle2D rectAct = table.getCell(rows-1, cols-1).getAnchor();
assertEquals(rectExp, rectAct); assertEquals(rectExp, rectAct);
ppt.close(); ppt.close();
} }

View File

@ -22,8 +22,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.awt.Rectangle;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.net.URL; import java.net.URL;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map; import java.util.Map;
@ -38,7 +38,7 @@ public class TestPresetGeometries {
for(String name : shapes.keySet()) { for(String name : shapes.keySet()) {
CustomGeometry geom = shapes.get(name); CustomGeometry geom = shapes.get(name);
Context ctx = new Context(geom, new Rectangle(0, 0, 100, 100), new IAdjustableShape() { Context ctx = new Context(geom, new Rectangle2D.Double(0, 0, 100, 100), new IAdjustableShape() {
public Guide getAdjustValue(String presetName) { public Guide getAdjustValue(String presetName) {
return null; return null;
} }

Binary file not shown.

Binary file not shown.