\r
import java.awt.Graphics2D;\r
import java.awt.font.TextLayout;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
import java.text.AttributedString;\r
import java.util.HashMap;\r
import java.util.Map;\r
return new DrawPaint(shape);\r
}\r
\r
+ /**\r
+ * Convenience method for drawing single shapes.\r
+ * For drawing whole slides, use {@link Slide#draw(Graphics2D)}\r
+ *\r
+ * @param graphics the graphics context to draw to\r
+ * @param shape the shape\r
+ * @param bounds the bounds within the graphics context to draw to \r
+ */\r
+ public void drawShape(Graphics2D graphics, Shape<?,?> shape, Rectangle2D bounds) {\r
+ Rectangle2D shapeBounds = shape.getAnchor();\r
+ if (shapeBounds.isEmpty() || (bounds != null && bounds.isEmpty())) {\r
+ return;\r
+ }\r
\r
+ AffineTransform txg = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);\r
+ AffineTransform tx = new AffineTransform();\r
+ try {\r
+ if (bounds != null) {\r
+ double scaleX = bounds.getWidth()/shapeBounds.getWidth();\r
+ double scaleY = bounds.getHeight()/shapeBounds.getHeight();\r
+ tx.translate(bounds.getCenterX(), bounds.getCenterY());\r
+ tx.scale(scaleX, scaleY);\r
+ tx.translate(-shapeBounds.getCenterX(), -shapeBounds.getCenterY());\r
+ }\r
+ graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, tx);\r
+ \r
+ Drawable d = getDrawable(shape);\r
+ d.applyTransform(graphics);\r
+ d.draw(graphics);\r
+ } finally {\r
+ graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, txg);\r
+ }\r
+ }\r
+ \r
+ \r
/**\r
* Replace font families for Windows JVM 6, which contains a font rendering error.\r
* This is likely to be removed, when POI upgrades to JDK 7\r
package org.apache.poi.sl.usermodel;
+import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
public interface Shape<
P extends TextParagraph<S,P,?>
> {
ShapeContainer<S,P> getParent();
-
+
/**
* @return the sheet this shape belongs to
*/
Sheet<S,P> getSheet();
-
+
/**
* Returns the anchor (the bounding box rectangle) of this shape.
* All coordinates are expressed in points (72 dpi).
*
* @return the anchor of this shape
*/
- Rectangle2D getAnchor();
+ Rectangle2D getAnchor();
+
+ /**
+ * Convenience method to draw a single shape
+ *
+ * @param graphics the graphics context
+ * @param bounds the rectangle to fit the shape to.
+ * if null, the bounds of the shape are used.
+ */
+ void draw(Graphics2D graphics, Rectangle2D bounds);
}
\r
package org.apache.poi.xslf.usermodel;\r
\r
+import java.awt.Graphics2D;\r
+import java.awt.geom.Rectangle2D;\r
import java.io.IOException;\r
import java.io.InputStream;\r
import java.util.Arrays;\r
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;\r
import org.apache.poi.openxml4j.opc.PackagePart;\r
import org.apache.poi.openxml4j.opc.PackageRelationship;\r
+import org.apache.poi.sl.draw.DrawFactory;\r
import org.apache.poi.sl.draw.DrawPaint;\r
import org.apache.poi.sl.usermodel.ColorStyle;\r
import org.apache.poi.sl.usermodel.PaintStyle;\r
}\r
return (fillProps == null) ? null : selectPaint(fillProps, phClr, theme.getPackagePart());\r
}\r
+ \r
+ @Override\r
+ public void draw(Graphics2D graphics, Rectangle2D bounds) {\r
+ DrawFactory.getInstance(graphics).drawShape(graphics, this, bounds);\r
+ }\r
}
\ No newline at end of file
import org.apache.poi.hslf.record.HSLFEscherClientDataRecord;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordTypes;
+import org.apache.poi.sl.draw.DrawFactory;
import org.apache.poi.sl.usermodel.FillStyle;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.ShapeContainer;
return getFill().getFillStyle();
}
- public void draw(Graphics2D graphics){
- logger.log(POILogger.INFO, "Rendering " + getShapeName());
+ @Override
+ public void draw(Graphics2D graphics, Rectangle2D bounds){
+ DrawFactory.getInstance(graphics).drawShape(graphics, this, bounds);
}
public AbstractEscherOptRecord getEscherOptRecord() {
import static org.junit.Assert.assertTrue;
import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.hssf.usermodel.DummyGraphics2d;
+import org.apache.poi.sl.draw.DrawFactory;
import org.apache.poi.sl.draw.DrawPaint;
import org.apache.poi.sl.draw.DrawTextParagraph;
import org.apache.poi.sl.usermodel.PaintStyle;
ppt.close();
}
+
+ @Test
+ public void bug59056() throws IOException {
+ HSLFSlideShow ppt = open("54541_cropped_bitmap.ppt");
+
+ for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) {
+ BufferedImage img = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D graphics = img.createGraphics();
+ Rectangle2D box = new Rectangle2D.Double(50,50,300,100);
+ graphics.setPaint(Color.red);
+ graphics.fill(box);
+ box = new Rectangle2D.Double(box.getX()+1,box.getY()+1,box.getWidth()-2,box.getHeight()-2);
+ DrawFactory.getInstance(graphics).drawShape(graphics, shape, box);
+ graphics.dispose();
+ // ImageIO.write(img, "png", new File("bla"+shape.getShapeId()+".png"));
+ }
+
+ ppt.close();
+
+ }
private static HSLFSlideShow open(String fileName) throws IOException {
File sample = HSLFTestDataSamples.getSampleFile(fileName);
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
+import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
HSLFPictureData pd = HSLFPictureData.create(PictureType.PNG);
HSLFPictureShape pict = new HSLFPictureShape(pd); //index to non-existing picture data
+ pict.setAnchor(new Rectangle2D.Double(50,50,100,100));
pict.setSheet(slide);
HSLFPictureData data = pict.getPictureData();
assertNull(data);
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
- pict.draw(graphics);
+ pict.draw(graphics, null);
ppt.close();
}