From c6fa344c54b9d820596609d038754671aa4fa427 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Wed, 24 Feb 2016 22:43:51 +0000 Subject: [PATCH] Regression fixes for H/XSLF and HWMF see http://apache-poi.1045710.n5.nabble.com/3-14-beta-2-3-14-final-tt5721829.html git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1732236 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/stress/SlideShowHandler.java | 28 +- .../apache/poi/ddf/EscherArrayProperty.java | 11 +- .../apache/poi/sl/draw/DrawBackground.java | 3 + .../org/apache/poi/sl/draw/DrawFactory.java | 36 ++- .../org/apache/poi/sl/draw/DrawPaint.java | 29 +- .../apache/poi/sl/draw/DrawPictureShape.java | 3 +- .../org/apache/poi/sl/draw/DrawShape.java | 20 +- .../apache/poi/sl/draw/DrawSimpleShape.java | 26 +- .../apache/poi/sl/draw/DrawTextParagraph.java | 4 +- .../org/apache/poi/sl/draw/DrawTextShape.java | 34 +-- .../apache/poi/sl/draw/PathGradientPaint.java | 8 +- .../org/apache/poi/sl/draw/SLGraphics.java | 25 +- .../apache/poi/sl/draw/geom/ArcToCommand.java | 8 +- .../poi/sl/draw/geom/ClosePathCommand.java | 4 +- .../poi/sl/draw/geom/CurveToCommand.java | 8 +- .../poi/sl/draw/geom/LineToCommand.java | 8 +- .../poi/sl/draw/geom/MoveToCommand.java | 8 +- .../org/apache/poi/sl/draw/geom/Path.java | 18 +- .../apache/poi/sl/draw/geom/PathCommand.java | 4 +- .../poi/sl/draw/geom/QuadToCommand.java | 8 +- .../poi/sl/usermodel/FreeformShape.java | 6 +- .../poi/sl/usermodel/PlaceableShape.java | 5 + .../org/apache/poi/sl/usermodel/Shape.java | 3 +- .../poi/xslf/usermodel/XSLFFreeformShape.java | 10 +- .../poi/xslf/usermodel/XSLFHyperlink.java | 4 +- .../poi/xslf/usermodel/XSLFTextRun.java | 11 +- .../org/apache/poi/xslf/util/PPTX2PNG.java | 123 ++++---- .../poi/xslf/usermodel/TestPPTX2PNG.java | 17 +- .../xslf/usermodel/TestXSLFFreeformShape.java | 11 +- .../apache/poi/hslf/model/PPGraphics2D.java | 5 +- .../poi/hslf/record/InteractiveInfo.java | 12 +- .../org/apache/poi/hslf/record/Record.java | 2 +- .../apache/poi/hslf/usermodel/HSLFFill.java | 129 +++++++-- .../poi/hslf/usermodel/HSLFFreeformShape.java | 265 ++++++++++++++---- .../poi/hslf/usermodel/HSLFHyperlink.java | 3 + .../poi/hslf/usermodel/HSLFPictureShape.java | 7 + .../apache/poi/hslf/usermodel/HSLFShape.java | 15 +- .../poi/hslf/usermodel/HSLFShapeFactory.java | 11 +- .../poi/hslf/usermodel/HSLFSimpleShape.java | 9 +- .../poi/hslf/usermodel/HSLFTextParagraph.java | 20 +- .../poi/hwmf/draw/HwmfDrawProperties.java | 3 + .../apache/poi/hwmf/draw/HwmfGraphics.java | 102 +++++-- .../poi/hwmf/record/HwmfBinaryRasterOp.java | 112 ++++++++ .../apache/poi/hwmf/record/HwmfBitmapDib.java | 29 +- .../org/apache/poi/hwmf/record/HwmfDraw.java | 41 ++- .../apache/poi/hwmf/record/HwmfEscape.java | 146 +++++++++- .../org/apache/poi/hwmf/record/HwmfFill.java | 45 ++- .../org/apache/poi/hwmf/record/HwmfFont.java | 19 +- .../apache/poi/hwmf/record/HwmfMapMode.java | 2 +- .../org/apache/poi/hwmf/record/HwmfMisc.java | 22 +- .../apache/poi/hwmf/record/HwmfPalette.java | 23 +- .../poi/hwmf/record/HwmfTernaryRasterOp.java | 72 ++++- .../org/apache/poi/hwmf/record/HwmfText.java | 118 ++++---- .../apache/poi/hwmf/record/HwmfWindowing.java | 24 +- .../poi/hwmf/usermodel/HwmfPicture.java | 10 +- .../apache/poi/hslf/model/TestFreeform.java | 15 +- .../poi/hslf/usermodel/TestPicture.java | 36 ++- .../org/apache/poi/hwmf/TestHwmfParsing.java | 10 +- .../sl/draw/geom/TestPresetGeometries.java | 4 +- 59 files changed, 1336 insertions(+), 458 deletions(-) create mode 100644 src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBinaryRasterOp.java diff --git a/src/integrationtest/org/apache/poi/stress/SlideShowHandler.java b/src/integrationtest/org/apache/poi/stress/SlideShowHandler.java index 9d6d1b3dcd..e798b35339 100644 --- a/src/integrationtest/org/apache/poi/stress/SlideShowHandler.java +++ b/src/integrationtest/org/apache/poi/stress/SlideShowHandler.java @@ -26,10 +26,8 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import org.apache.poi.sl.draw.Drawable; +import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.sl.usermodel.PictureData; import org.apache.poi.sl.usermodel.Shape; import org.apache.poi.sl.usermodel.ShapeContainer; @@ -39,7 +37,6 @@ import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.sl.usermodel.TextRun; import org.apache.poi.sl.usermodel.TextShape; -import org.apache.poi.util.JvmBugs; public abstract class SlideShowHandler extends POIFSFileHandler { public void handleSlideShow(SlideShow ss) throws IOException { @@ -55,10 +52,12 @@ public abstract class SlideShowHandler extends POIFSFileHandler { // read in the writen file SlideShow read = SlideShowFactory.create(new ByteArrayInputStream(out.toByteArray())); - assertNotNull(read); - - readContent(read); - + try { + assertNotNull(read); + readContent(read); + } finally { + read.close(); + } } private ByteArrayOutputStream writeToArray(SlideShow ss) throws IOException { @@ -109,7 +108,7 @@ public abstract class SlideShowHandler extends POIFSFileHandler { for (Slide s : ss.getSlides()) { BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = img.createGraphics(); - fixFonts(graphics); + DrawFactory.getInstance(graphics).fixFonts(graphics); // default rendering options graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); @@ -124,15 +123,4 @@ public abstract class SlideShowHandler extends POIFSFileHandler { img.flush(); } } - - @SuppressWarnings("unchecked") - private static void fixFonts(Graphics2D graphics) { - if (!JvmBugs.hasLineBreakMeasurerBug()) return; - Map fontMap = (Map)graphics.getRenderingHint(Drawable.FONT_MAP); - if (fontMap == null) fontMap = new HashMap(); - fontMap.put("Calibri", "Lucida Sans"); - fontMap.put("Cambria", "Lucida Bright"); - graphics.setRenderingHint(Drawable.FONT_MAP, fontMap); - } - } \ No newline at end of file diff --git a/src/java/org/apache/poi/ddf/EscherArrayProperty.java b/src/java/org/apache/poi/ddf/EscherArrayProperty.java index 880057e0ff..bec5f24d00 100644 --- a/src/java/org/apache/poi/ddf/EscherArrayProperty.java +++ b/src/java/org/apache/poi/ddf/EscherArrayProperty.java @@ -43,7 +43,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements private boolean sizeIncludesHeaderSize = true; /** - * When reading a property from data stream remeber if the complex part is empty and set this flag. + * When reading a property from data stream remember if the complex part is empty and set this flag. */ private boolean emptyComplexPart = false; @@ -65,10 +65,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements } public int getNumberOfElementsInArray() { - if (emptyComplexPart){ - return 0; - } - return LittleEndian.getUShort(_complexData, 0); + return (emptyComplexPart) ? 0 : LittleEndian.getUShort(_complexData, 0); } public void setNumberOfElementsInArray(int numberOfElements) { @@ -82,7 +79,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements } public int getNumberOfElementsInMemory() { - return LittleEndian.getUShort(_complexData, 2); + return (emptyComplexPart) ? 0 : LittleEndian.getUShort(_complexData, 2); } public void setNumberOfElementsInMemory(int numberOfElements) { @@ -96,7 +93,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements } public short getSizeOfElements() { - return LittleEndian.getShort( _complexData, 4 ); + return (emptyComplexPart) ? 0 : LittleEndian.getShort( _complexData, 4 ); } public void setSizeOfElements(int sizeOfElements) { diff --git a/src/java/org/apache/poi/sl/draw/DrawBackground.java b/src/java/org/apache/poi/sl/draw/DrawBackground.java index b09bc98589..aa12b470cb 100644 --- a/src/java/org/apache/poi/sl/draw/DrawBackground.java +++ b/src/java/org/apache/poi/sl/draw/DrawBackground.java @@ -25,6 +25,7 @@ import java.awt.geom.Rectangle2D; import org.apache.poi.sl.usermodel.Background; import org.apache.poi.sl.usermodel.PlaceableShape; import org.apache.poi.sl.usermodel.ShapeContainer; +import org.apache.poi.sl.usermodel.Sheet; public class DrawBackground extends DrawShape { @@ -47,6 +48,7 @@ public class DrawBackground extends DrawShape { public void setFlipVertical(boolean flip) {} public boolean getFlipHorizontal() { return false; } public boolean getFlipVertical() { return false; } + public Sheet getSheet() { return shape.getSheet(); } }; DrawFactory drawFact = DrawFactory.getInstance(graphics); @@ -55,6 +57,7 @@ public class DrawBackground extends DrawShape { Rectangle2D anchor2 = getAnchor(graphics, anchor); if(fill != null) { + graphics.setRenderingHint(Drawable.GRADIENT_SHAPE, anchor); graphics.setPaint(fill); graphics.fill(anchor2); } diff --git a/src/java/org/apache/poi/sl/draw/DrawFactory.java b/src/java/org/apache/poi/sl/draw/DrawFactory.java index 971feae185..a7edeafbb0 100644 --- a/src/java/org/apache/poi/sl/draw/DrawFactory.java +++ b/src/java/org/apache/poi/sl/draw/DrawFactory.java @@ -17,11 +17,11 @@ package org.apache.poi.sl.draw; -import static org.apache.poi.sl.draw.Drawable.DRAW_FACTORY; - import java.awt.Graphics2D; import java.awt.font.TextLayout; import java.text.AttributedString; +import java.util.HashMap; +import java.util.Map; import org.apache.poi.sl.usermodel.Background; import org.apache.poi.sl.usermodel.ConnectorShape; @@ -38,6 +38,7 @@ import org.apache.poi.sl.usermodel.TableShape; import org.apache.poi.sl.usermodel.TextBox; import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.sl.usermodel.TextShape; +import org.apache.poi.util.JvmBugs; public class DrawFactory { protected static final ThreadLocal defaultFactory = new ThreadLocal(); @@ -58,7 +59,7 @@ public class DrawFactory { DrawFactory factory = null; boolean isHint = false; if (graphics != null) { - factory = (DrawFactory)graphics.getRenderingHint(DRAW_FACTORY); + factory = (DrawFactory)graphics.getRenderingHint(Drawable.DRAW_FACTORY); isHint = (factory != null); } // secondly try the thread local default @@ -70,7 +71,7 @@ public class DrawFactory { factory = new DrawFactory(); } if (graphics != null && !isHint) { - graphics.setRenderingHint(DRAW_FACTORY, factory); + graphics.setRenderingHint(Drawable.DRAW_FACTORY, factory); } return factory; } @@ -166,4 +167,29 @@ public class DrawFactory { public DrawPaint getPaint(PlaceableShape shape) { return new DrawPaint(shape); } -} + + + /** + * Replace font families for Windows JVM 6, which contains a font rendering error. + * This is likely to be removed, when POI upgrades to JDK 7 + * + * @param graphics the graphics context which will contain the font mapping + */ + public void fixFonts(Graphics2D graphics) { + if (!JvmBugs.hasLineBreakMeasurerBug()) return; + @SuppressWarnings("unchecked") + Map fontMap = (Map)graphics.getRenderingHint(Drawable.FONT_MAP); + if (fontMap == null) { + fontMap = new HashMap(); + graphics.setRenderingHint(Drawable.FONT_MAP, fontMap); + } + + String fonts[][] = { { "Calibri", "Lucida Sans" }, { "Cambria", "Lucida Bright" } }; + + for (String f[] : fonts) { + if (!fontMap.containsKey(f[0])) { + fontMap.put(f[0], f[1]); + } + } + } +} \ No newline at end of file diff --git a/src/java/org/apache/poi/sl/draw/DrawPaint.java b/src/java/org/apache/poi/sl/draw/DrawPaint.java index 8d5648f852..d4da431366 100644 --- a/src/java/org/apache/poi/sl/draw/DrawPaint.java +++ b/src/java/org/apache/poi/sl/draw/DrawPaint.java @@ -134,8 +134,11 @@ public class DrawPaint { ImageRenderer renderer = DrawPictureShape.getImageRenderer(graphics, fill.getContentType()); try { - renderer.loadImage(is, fill.getContentType()); - is.close(); + try { + renderer.loadImage(is, fill.getContentType()); + } finally { + is.close(); + } } catch (IOException e) { LOG.log(POILogger.ERROR, "Can't load image data - using transparent color", e); return null; @@ -274,15 +277,35 @@ public class DrawPaint { Point2D p2 = new Point2D.Double(anchor.getX() + anchor.getWidth(), anchor.getY() + anchor.getHeight() / 2); p2 = at.transform(p2, null); - + snapToAnchor(p1, anchor); snapToAnchor(p2, anchor); + if (p1.equals(p2)) { + // gradient paint on the same point throws an exception ... and doesn't make sense + return null; + } + float[] fractions = fill.getGradientFractions(); Color[] colors = new Color[fractions.length]; int i = 0; for (ColorStyle fc : fill.getGradientColors()) { + if (fc == null) { + // get color of background + fc = new ColorStyle() { + public int getTint() { return -1; } + public int getShade() { return -1; } + public int getSatOff() { return -1; } + public int getSatMod() { return -1; } + public int getLumOff() { return -1; } + public int getLumMod() { return -1; } + public int getHueOff() { return -1; } + public int getHueMod() { return -1; } + public Color getColor() { return Color.white; } + public int getAlpha() { return 0; } + }; + } colors[i++] = applyColorTransform(fc); } diff --git a/src/java/org/apache/poi/sl/draw/DrawPictureShape.java b/src/java/org/apache/poi/sl/draw/DrawPictureShape.java index beadc1f988..bdc5ab68c4 100644 --- a/src/java/org/apache/poi/sl/draw/DrawPictureShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawPictureShape.java @@ -52,8 +52,7 @@ public class DrawPictureShape extends DrawSimpleShape { renderer.loadImage(data.getData(), data.getContentType()); renderer.drawImage(graphics, anchor, insets); } catch (IOException e) { - // TODO: draw specific runtime exception? - throw new RuntimeException(e); + LOG.log(POILogger.ERROR, "image can't be loaded/rendered.", e); } } diff --git a/src/java/org/apache/poi/sl/draw/DrawShape.java b/src/java/org/apache/poi/sl/draw/DrawShape.java index d036e52733..0c465bc52a 100644 --- a/src/java/org/apache/poi/sl/draw/DrawShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawShape.java @@ -90,17 +90,23 @@ public class DrawShape implements Drawable { Rectangle2D anchor2 = txs.createTransformedShape(ps.getAnchor()).getBounds2D(); - scaleX = anchor.getWidth() == 0. ? 1.0 : anchor.getWidth() / anchor2.getWidth(); - scaleY = anchor.getHeight() == 0. ? 1.0 : anchor.getHeight() / anchor2.getHeight(); + scaleX = safeScale(anchor.getWidth(), anchor2.getWidth()); + scaleY = safeScale(anchor.getHeight(), anchor2.getHeight()); } else { quadrant = 0; } // transformation is applied reversed ... graphics.translate(centerX, centerY); - graphics.rotate(Math.toRadians(rotation-quadrant*90.)); + double rot = Math.toRadians(rotation-quadrant*90.); + if (rot != 0) { + graphics.rotate(rot); + } graphics.scale(scaleX, scaleY); - graphics.rotate(Math.toRadians(quadrant*90)); + rot = Math.toRadians(quadrant*90); + if (rot != 0) { + graphics.rotate(rot); + } graphics.translate(-centerX, -centerY); } @@ -119,6 +125,12 @@ public class DrawShape implements Drawable { } } + private static double safeScale(double dim1, double dim2) { + if (dim1 == 0.) { + return 1; + } + return (dim2 == 0.) ? 1 : dim1/dim2; + } public void draw(Graphics2D graphics) { } diff --git a/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java b/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java index aa03f8a53b..7206996660 100644 --- a/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java @@ -23,7 +23,7 @@ import java.awt.Graphics2D; import java.awt.Paint; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.io.IOException; import java.io.InputStream; @@ -179,20 +179,20 @@ public class DrawSimpleShape extends DrawShape { case STEALTH: case ARROW: p = new Path(false, true); - GeneralPath arrow = new GeneralPath(); - arrow.moveTo((float) (-lineWidth * scaleX), (float) (-lineWidth * scaleY / 2)); + Path2D.Double arrow = new Path2D.Double(); + arrow.moveTo((-lineWidth * scaleX), (-lineWidth * scaleY / 2)); arrow.lineTo(0, 0); - arrow.lineTo((float) (-lineWidth * scaleX), (float) (lineWidth * scaleY / 2)); + arrow.lineTo((-lineWidth * scaleX), (lineWidth * scaleY / 2)); tailShape = arrow; at.translate(x2, y2); at.rotate(alpha); break; case TRIANGLE: p = new Path(); - GeneralPath triangle = new GeneralPath(); - triangle.moveTo((float) (-lineWidth * scaleX), (float) (-lineWidth * scaleY / 2)); + Path2D.Double triangle = new Path2D.Double(); + triangle.moveTo((-lineWidth * scaleX), (-lineWidth * scaleY / 2)); triangle.lineTo(0, 0); - triangle.lineTo((float) (-lineWidth * scaleX), (float) (lineWidth * scaleY / 2)); + triangle.lineTo((-lineWidth * scaleX), (lineWidth * scaleY / 2)); triangle.closePath(); tailShape = triangle; at.translate(x2, y2); @@ -252,20 +252,20 @@ public class DrawSimpleShape extends DrawShape { case STEALTH: case ARROW: p = new Path(false, true); - GeneralPath arrow = new GeneralPath(); - arrow.moveTo((float) (lineWidth * scaleX), (float) (-lineWidth * scaleY / 2)); + Path2D.Double arrow = new Path2D.Double(); + arrow.moveTo((lineWidth * scaleX), (-lineWidth * scaleY / 2)); arrow.lineTo(0, 0); - arrow.lineTo((float) (lineWidth * scaleX), (float) (lineWidth * scaleY / 2)); + arrow.lineTo((lineWidth * scaleX), (lineWidth * scaleY / 2)); headShape = arrow; at.translate(x1, y1); at.rotate(alpha); break; case TRIANGLE: p = new Path(); - GeneralPath triangle = new GeneralPath(); - triangle.moveTo((float) (lineWidth * scaleX), (float) (-lineWidth * scaleY / 2)); + Path2D.Double triangle = new Path2D.Double(); + triangle.moveTo((lineWidth * scaleX), (-lineWidth * scaleY / 2)); triangle.lineTo(0, 0); - triangle.lineTo((float) (lineWidth * scaleX), (float) (lineWidth * scaleY / 2)); + triangle.lineTo((lineWidth * scaleX), (lineWidth * scaleY / 2)); triangle.closePath(); headShape = triangle; at.translate(x1, y1); diff --git a/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java b/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java index e8a7b0b216..7b28bde582 100644 --- a/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java +++ b/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java @@ -38,6 +38,7 @@ import org.apache.poi.sl.usermodel.Insets2D; import org.apache.poi.sl.usermodel.PaintStyle; import org.apache.poi.sl.usermodel.PlaceableShape; import org.apache.poi.sl.usermodel.ShapeContainer; +import org.apache.poi.sl.usermodel.Sheet; import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.sl.usermodel.TextParagraph.BulletStyle; import org.apache.poi.sl.usermodel.TextParagraph.TextAlign; @@ -465,6 +466,7 @@ public class DrawTextParagraph implements Drawable { public void setFlipVertical(boolean flip) {} public boolean getFlipHorizontal() { return false; } public boolean getFlipVertical() { return false; } + public Sheet getSheet() { return paragraph.getParentShape().getSheet(); } }; return ps; } @@ -530,7 +532,7 @@ public class DrawTextParagraph implements Drawable { attList.add(new AttributedStringData(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, beginIndex, endIndex)); } - Hyperlink hl = run.getHyperlink(); + Hyperlink hl = run.getHyperlink(); if (hl != null) { attList.add(new AttributedStringData(HYPERLINK_HREF, hl.getAddress(), beginIndex, endIndex)); attList.add(new AttributedStringData(HYPERLINK_LABEL, hl.getLabel(), beginIndex, endIndex)); diff --git a/src/java/org/apache/poi/sl/draw/DrawTextShape.java b/src/java/org/apache/poi/sl/draw/DrawTextShape.java index 06b57dab26..648dd48cec 100644 --- a/src/java/org/apache/poi/sl/draw/DrawTextShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawTextShape.java @@ -21,11 +21,15 @@ import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; -import java.util.*; +import java.util.Iterator; -import org.apache.poi.sl.usermodel.*; +import org.apache.poi.sl.usermodel.Insets2D; +import org.apache.poi.sl.usermodel.PlaceableShape; +import org.apache.poi.sl.usermodel.ShapeContainer; +import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.sl.usermodel.TextParagraph.BulletStyle; -import org.apache.poi.util.JvmBugs; +import org.apache.poi.sl.usermodel.TextRun; +import org.apache.poi.sl.usermodel.TextShape; public class DrawTextShape extends DrawSimpleShape { @@ -35,7 +39,7 @@ public class DrawTextShape extends DrawSimpleShape { @Override public void drawContent(Graphics2D graphics) { - fixFonts(graphics); + DrawFactory.getInstance(graphics).fixFonts(graphics); TextShape s = getShape(); @@ -71,7 +75,7 @@ public class DrawTextShape extends DrawSimpleShape { } Double textRot = s.getTextRotation(); - if (textRot != null) { + if (textRot != null && textRot != 0) { graphics.translate(anchor.getCenterX(), anchor.getCenterY()); graphics.rotate(Math.toRadians(textRot)); graphics.translate(-anchor.getCenterX(), -anchor.getCenterY()); @@ -110,8 +114,9 @@ public class DrawTextShape extends DrawSimpleShape { double y0 = y; //noinspection RedundantCast - Iterator> paragraphs = (Iterator>) getShape().iterator(); - + Iterator> paragraphs = + (Iterator>) getShape().iterator(); + boolean isFirstLine = true; for (int autoNbrIdx=0; paragraphs.hasNext(); autoNbrIdx++){ TextParagraph p = paragraphs.next(); @@ -170,23 +175,10 @@ public class DrawTextShape extends DrawSimpleShape { // dry-run in a 1x1 image and return the vertical advance BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); - fixFonts(graphics); + DrawFactory.getInstance(graphics).fixFonts(graphics); return drawParagraphs(graphics, 0, 0); } - @SuppressWarnings("unchecked") - private static void fixFonts(Graphics2D graphics) { - if (!JvmBugs.hasLineBreakMeasurerBug()) return; - Map fontMap = (Map)graphics.getRenderingHint(Drawable.FONT_MAP); - if (fontMap == null) { - fontMap = new HashMap(); - graphics.setRenderingHint(Drawable.FONT_MAP, fontMap); - } - - if (!fontMap.containsKey("Calibri")) fontMap.put("Calibri", "Lucida Sans"); - if (!fontMap.containsKey("Cambria")) fontMap.put("Cambria", "Lucida Bright"); - } - @Override protected TextShape getShape() { return (TextShape)shape; diff --git a/src/java/org/apache/poi/sl/draw/PathGradientPaint.java b/src/java/org/apache/poi/sl/draw/PathGradientPaint.java index c5ad799f4c..292ee3a7e7 100644 --- a/src/java/org/apache/poi/sl/draw/PathGradientPaint.java +++ b/src/java/org/apache/poi/sl/draw/PathGradientPaint.java @@ -87,7 +87,7 @@ class PathGradientPaint implements Paint { ) { shape = (Shape)hints.get(Drawable.GRADIENT_SHAPE); if (shape == null) { - throw new IllegalPathStateException("PathGradientPaint needs a shape to be set via the rendering hint PathGradientPaint.GRADIANT_SHAPE."); + throw new IllegalPathStateException("PathGradientPaint needs a shape to be set via the rendering hint Drawable.GRADIANT_SHAPE."); } this.deviceBounds = deviceBounds; @@ -137,14 +137,14 @@ class PathGradientPaint implements Paint { return childRaster; } - protected int getGradientSteps(Shape shape) { - Rectangle rect = shape.getBounds(); + protected int getGradientSteps(Shape gradientShape) { + Rectangle rect = gradientShape.getBounds(); int lower = 1; int upper = (int)(Math.max(rect.getWidth(),rect.getHeight())/2.0); while (lower < upper-1) { int mid = lower + (upper - lower) / 2; BasicStroke bs = new BasicStroke(mid, capStyle, joinStyle); - Area area = new Area(bs.createStrokedShape(shape)); + Area area = new Area(bs.createStrokedShape(gradientShape)); if (area.isSingular()) { upper = mid; } else { diff --git a/src/java/org/apache/poi/sl/draw/SLGraphics.java b/src/java/org/apache/poi/sl/draw/SLGraphics.java index 2a6d884ca1..b4bc14ae96 100644 --- a/src/java/org/apache/poi/sl/draw/SLGraphics.java +++ b/src/java/org/apache/poi/sl/draw/SLGraphics.java @@ -42,6 +42,7 @@ import java.awt.geom.Arc2D; import java.awt.geom.Ellipse2D; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; +import java.awt.geom.Path2D; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageOp; @@ -243,7 +244,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * @see #setComposite */ public void draw(Shape shape){ - GeneralPath path = new GeneralPath(_transform.createTransformedShape(shape)); + Path2D.Double path = new Path2D.Double(_transform.createTransformedShape(shape)); FreeformShape p = _group.createFreeform(); p.setPath(path); p.setFillColor(null); @@ -339,7 +340,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * @see #setClip */ public void fill(Shape shape){ - GeneralPath path = new GeneralPath(_transform.createTransformedShape(shape)); + Path2D.Double path = new Path2D.Double(_transform.createTransformedShape(shape)); FreeformShape p = _group.createFreeform(); p.setPath(path); applyPaint(p); @@ -450,7 +451,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { */ public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){ - RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight); + RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight); draw(rect); } @@ -481,7 +482,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * @see java.awt.Graphics#drawOval */ public void fillOval(int x, int y, int width, int height){ - Ellipse2D oval = new Ellipse2D.Float(x, y, width, height); + Ellipse2D oval = new Ellipse2D.Double(x, y, width, height); fill(oval); } @@ -504,7 +505,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){ - RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight); + RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight); fill(rect); } @@ -544,9 +545,8 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * relative to the start angle. * @see java.awt.Graphics#drawArc */ - public void fillArc(int x, int y, int width, int height, - int startAngle, int arcAngle){ - Arc2D arc = new Arc2D.Float(x, y, width, height, startAngle, arcAngle, Arc2D.PIE); + public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle){ + Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle, Arc2D.PIE); fill(arc); } @@ -587,9 +587,8 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * relative to the start angle. * @see java.awt.Graphics#fillArc */ - public void drawArc(int x, int y, int width, int height, - int startAngle, int arcAngle) { - Arc2D arc = new Arc2D.Float(x, y, width, height, startAngle, arcAngle, Arc2D.OPEN); + public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) { + Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle, Arc2D.OPEN); draw(arc); } @@ -636,7 +635,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * @see java.awt.Graphics#fillOval */ public void drawOval(int x, int y, int width, int height){ - Ellipse2D oval = new Ellipse2D.Float(x, y, width, height); + Ellipse2D oval = new Ellipse2D.Double(x, y, width, height); draw(oval); } @@ -932,7 +931,7 @@ public final class SLGraphics extends Graphics2D implements Cloneable { * @param y2 the second point's y coordinate. */ public void drawLine(int x1, int y1, int x2, int y2){ - Line2D line = new Line2D.Float(x1, y1, x2, y2); + Line2D line = new Line2D.Double(x1, y1, x2, y2); draw(line); } diff --git a/src/java/org/apache/poi/sl/draw/geom/ArcToCommand.java b/src/java/org/apache/poi/sl/draw/geom/ArcToCommand.java index 0382d7f997..18531d7ed9 100644 --- a/src/java/org/apache/poi/sl/draw/geom/ArcToCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/ArcToCommand.java @@ -19,12 +19,12 @@ package org.apache.poi.sl.draw.geom; -import org.apache.poi.sl.draw.binding.CTPath2DArcTo; - import java.awt.geom.Arc2D; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; import java.awt.geom.Point2D; +import org.apache.poi.sl.draw.binding.CTPath2DArcTo; + /** * ArcTo command within a shape path in DrawingML: * @@ -48,7 +48,7 @@ public class ArcToCommand implements PathCommand { swAng = arc.getSwAng().toString(); } - public void execute(GeneralPath path, Context ctx){ + public void execute(Path2D.Double path, Context ctx){ double rx = ctx.getValue(wr); double ry = ctx.getValue(hr); double start = ctx.getValue(stAng) / 60000; diff --git a/src/java/org/apache/poi/sl/draw/geom/ClosePathCommand.java b/src/java/org/apache/poi/sl/draw/geom/ClosePathCommand.java index 9d29062806..66b35ed585 100644 --- a/src/java/org/apache/poi/sl/draw/geom/ClosePathCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/ClosePathCommand.java @@ -19,7 +19,7 @@ package org.apache.poi.sl.draw.geom; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; /** * Date: 10/25/11 @@ -31,7 +31,7 @@ public class ClosePathCommand implements PathCommand { ClosePathCommand(){ } - public void execute(GeneralPath path, Context ctx){ + public void execute(Path2D.Double path, Context ctx){ path.closePath(); } } diff --git a/src/java/org/apache/poi/sl/draw/geom/CurveToCommand.java b/src/java/org/apache/poi/sl/draw/geom/CurveToCommand.java index 02eeb2953e..6c6361aed9 100644 --- a/src/java/org/apache/poi/sl/draw/geom/CurveToCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/CurveToCommand.java @@ -19,9 +19,9 @@ package org.apache.poi.sl.draw.geom; -import org.apache.poi.sl.draw.binding.CTAdjPoint2D; +import java.awt.geom.Path2D; -import java.awt.geom.GeneralPath; +import org.apache.poi.sl.draw.binding.CTAdjPoint2D; /** * Date: 10/25/11 @@ -40,13 +40,13 @@ public class CurveToCommand implements PathCommand { arg6 = pt3.getY().toString(); } - public void execute(GeneralPath path, Context ctx){ + public void execute(Path2D.Double path, Context ctx){ double x1 = ctx.getValue(arg1); double y1 = ctx.getValue(arg2); double x2 = ctx.getValue(arg3); double y2 = ctx.getValue(arg4); double x3 = ctx.getValue(arg5); double y3 = ctx.getValue(arg6); - path.curveTo((float)x1, (float)y1, (float)x2, (float)y2, (float)x3, (float)y3); + path.curveTo(x1, y1, x2, y2, x3, y3); } } diff --git a/src/java/org/apache/poi/sl/draw/geom/LineToCommand.java b/src/java/org/apache/poi/sl/draw/geom/LineToCommand.java index 99c5a6b20c..7f6e13c542 100644 --- a/src/java/org/apache/poi/sl/draw/geom/LineToCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/LineToCommand.java @@ -19,9 +19,9 @@ package org.apache.poi.sl.draw.geom; -import org.apache.poi.sl.draw.binding.CTAdjPoint2D; +import java.awt.geom.Path2D; -import java.awt.geom.GeneralPath; +import org.apache.poi.sl.draw.binding.CTAdjPoint2D; /** * Date: 10/25/11 @@ -41,9 +41,9 @@ public class LineToCommand implements PathCommand { arg2 = s2; } - public void execute(GeneralPath path, Context ctx){ + public void execute(Path2D.Double path, Context ctx){ double x = ctx.getValue(arg1); double y = ctx.getValue(arg2); - path.lineTo((float)x, (float)y); + path.lineTo(x, y); } } diff --git a/src/java/org/apache/poi/sl/draw/geom/MoveToCommand.java b/src/java/org/apache/poi/sl/draw/geom/MoveToCommand.java index 22ccd54092..59c7a8adf2 100644 --- a/src/java/org/apache/poi/sl/draw/geom/MoveToCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/MoveToCommand.java @@ -19,9 +19,9 @@ package org.apache.poi.sl.draw.geom; -import org.apache.poi.sl.draw.binding.CTAdjPoint2D; +import java.awt.geom.Path2D; -import java.awt.geom.GeneralPath; +import org.apache.poi.sl.draw.binding.CTAdjPoint2D; /** * Date: 10/25/11 @@ -41,9 +41,9 @@ public class MoveToCommand implements PathCommand { arg2 = s2; } - public void execute(GeneralPath path, Context ctx){ + public void execute(Path2D.Double path, Context ctx){ double x = ctx.getValue(arg1); double y = ctx.getValue(arg2); - path.moveTo((float)x, (float)y); + path.moveTo(x, y); } } diff --git a/src/java/org/apache/poi/sl/draw/geom/Path.java b/src/java/org/apache/poi/sl/draw/geom/Path.java index b496e9fc96..78590faf06 100644 --- a/src/java/org/apache/poi/sl/draw/geom/Path.java +++ b/src/java/org/apache/poi/sl/draw/geom/Path.java @@ -19,11 +19,19 @@ package org.apache.poi.sl.draw.geom; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; import java.util.ArrayList; import java.util.List; -import org.apache.poi.sl.draw.binding.*; +import org.apache.poi.sl.draw.binding.CTAdjPoint2D; +import org.apache.poi.sl.draw.binding.CTPath2D; +import org.apache.poi.sl.draw.binding.CTPath2DArcTo; +import org.apache.poi.sl.draw.binding.CTPath2DClose; +import org.apache.poi.sl.draw.binding.CTPath2DCubicBezierTo; +import org.apache.poi.sl.draw.binding.CTPath2DLineTo; +import org.apache.poi.sl.draw.binding.CTPath2DMoveTo; +import org.apache.poi.sl.draw.binding.CTPath2DQuadBezierTo; +import org.apache.poi.sl.draw.binding.STPathFillMode; /** * Specifies a creation path consisting of a series of moves, lines and curves @@ -90,10 +98,10 @@ public class Path { } /** - * Convert the internal represenation to java.awt.GeneralPath + * Convert the internal represenation to java.awt.geom.Path2D */ - public GeneralPath getPath(Context ctx) { - GeneralPath path = new GeneralPath(); + public Path2D.Double getPath(Context ctx) { + Path2D.Double path = new Path2D.Double(); for(PathCommand cmd : commands) cmd.execute(path, ctx); return path; diff --git a/src/java/org/apache/poi/sl/draw/geom/PathCommand.java b/src/java/org/apache/poi/sl/draw/geom/PathCommand.java index 3063ab81bc..41fa21a546 100644 --- a/src/java/org/apache/poi/sl/draw/geom/PathCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/PathCommand.java @@ -19,7 +19,7 @@ package org.apache.poi.sl.draw.geom; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; /** * A path command in DrawingML. One of: @@ -41,5 +41,5 @@ public interface PathCommand { * @param path the path to append the result to * @param ctx the context to lookup variables */ - void execute(GeneralPath path, Context ctx); + void execute(Path2D.Double path, Context ctx); } diff --git a/src/java/org/apache/poi/sl/draw/geom/QuadToCommand.java b/src/java/org/apache/poi/sl/draw/geom/QuadToCommand.java index e9a9364b2d..d5e848b5a9 100644 --- a/src/java/org/apache/poi/sl/draw/geom/QuadToCommand.java +++ b/src/java/org/apache/poi/sl/draw/geom/QuadToCommand.java @@ -19,9 +19,9 @@ package org.apache.poi.sl.draw.geom; -import org.apache.poi.sl.draw.binding.CTAdjPoint2D; +import java.awt.geom.Path2D; -import java.awt.geom.GeneralPath; +import org.apache.poi.sl.draw.binding.CTAdjPoint2D; /** * Date: 10/25/11 @@ -38,11 +38,11 @@ public class QuadToCommand implements PathCommand { arg4 = pt2.getY().toString(); } - public void execute(GeneralPath path, Context ctx){ + public void execute(Path2D.Double path, Context ctx){ double x1 = ctx.getValue(arg1); double y1 = ctx.getValue(arg2); double x2 = ctx.getValue(arg3); double y2 = ctx.getValue(arg4); - path.quadTo((float)x1, (float)y1, (float)x2, (float)y2); + path.quadTo(x1, y1, x2, y2); } } diff --git a/src/java/org/apache/poi/sl/usermodel/FreeformShape.java b/src/java/org/apache/poi/sl/usermodel/FreeformShape.java index ca9a8d30f1..9536065ef0 100644 --- a/src/java/org/apache/poi/sl/usermodel/FreeformShape.java +++ b/src/java/org/apache/poi/sl/usermodel/FreeformShape.java @@ -17,7 +17,7 @@ package org.apache.poi.sl.usermodel; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; public interface FreeformShape< S extends Shape, @@ -33,7 +33,7 @@ public interface FreeformShape< * * @return the path */ - GeneralPath getPath(); + Path2D.Double getPath(); /** * Set the shape path @@ -41,5 +41,5 @@ public interface FreeformShape< * @param path shape outline * @return the number of points written */ - int setPath(GeneralPath path); + int setPath(Path2D.Double path); } diff --git a/src/java/org/apache/poi/sl/usermodel/PlaceableShape.java b/src/java/org/apache/poi/sl/usermodel/PlaceableShape.java index 191bad65f6..9fdcf91f6b 100644 --- a/src/java/org/apache/poi/sl/usermodel/PlaceableShape.java +++ b/src/java/org/apache/poi/sl/usermodel/PlaceableShape.java @@ -25,6 +25,11 @@ public interface PlaceableShape< > { ShapeContainer getParent(); + /** + * @return the sheet this shape belongs to + */ + Sheet getSheet(); + /** * @return the position of this shape within the drawing canvas. * The coordinates are expressed in points diff --git a/src/java/org/apache/poi/sl/usermodel/Shape.java b/src/java/org/apache/poi/sl/usermodel/Shape.java index 36f8379c58..4d053df6fe 100644 --- a/src/java/org/apache/poi/sl/usermodel/Shape.java +++ b/src/java/org/apache/poi/sl/usermodel/Shape.java @@ -25,8 +25,7 @@ public interface Shape< > { ShapeContainer getParent(); - /** - * + /** * @return the sheet this shape belongs to */ Sheet getSheet(); diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFFreeformShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFFreeformShape.java index f75ffa8ed4..77e99d2d39 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFFreeformShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFFreeformShape.java @@ -20,7 +20,7 @@ package org.apache.poi.xslf.usermodel; import java.awt.geom.AffineTransform; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; import java.awt.geom.PathIterator; import java.awt.geom.Rectangle2D; @@ -57,7 +57,7 @@ public class XSLFFreeformShape extends XSLFAutoShape } @Override - public int setPath(GeneralPath path) { + public int setPath(Path2D.Double path) { CTPath2D ctPath = CTPath2D.Factory.newInstance(); Rectangle2D bounds = path.getBounds2D(); @@ -119,8 +119,8 @@ public class XSLFFreeformShape extends XSLFAutoShape } @Override - public GeneralPath getPath() { - GeneralPath path = new GeneralPath(); + public Path2D.Double getPath() { + Path2D.Double path = new Path2D.Double(); Rectangle2D bounds = getAnchor(); CTCustomGeometry2D geom = getSpPr().getCustGeom(); @@ -168,7 +168,7 @@ public class XSLFFreeformShape extends XSLFAutoShape // The returned path should fit in the bounding rectangle AffineTransform at = new AffineTransform(); at.translate(bounds.getX(), bounds.getY()); - return new GeneralPath(at.createTransformedShape(path)); + return new Path2D.Double(at.createTransformedShape(path)); } /** * @param shapeId 1-based shapeId diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java index 6f4a5228d0..fd66d1f7e5 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java @@ -48,11 +48,11 @@ public class XSLFHyperlink implements Hyperlink { @Override public String getAddress() { - if (!_link.isSetId()) { + String id = _link.getId(); + if (id == null || "".equals(id)) { return _link.getAction(); } - String id = _link.getId(); URI targetURI = _sheet.getPackagePart().getRelationship(id).getTargetURI(); return targetURI.toASCIIString(); diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java index 049de50261..08804b0a8e 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java @@ -455,8 +455,15 @@ public class XSLFTextRun implements TextRun { @Override public XSLFHyperlink getHyperlink(){ - if(!_r.getRPr().isSetHlinkClick()) return null; - return new XSLFHyperlink(_r.getRPr().getHlinkClick(), _p.getParentShape().getSheet()); + CTTextCharacterProperties rPr = _r.getRPr(); + if (rPr == null) { + return null; + } + CTHyperlink hl = rPr.getHlinkClick(); + if (hl == null) { + return null; + } + return new XSLFHyperlink(hl, _p.getParentShape().getSheet()); } private boolean fetchCharacterProperty(CharacterPropertyFetcher fetcher){ diff --git a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java index 2ec85f5ac7..7f2f039688 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java @@ -24,18 +24,17 @@ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; -import java.util.HashMap; import java.util.List; import java.util.Locale; -import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import javax.imageio.ImageIO; -import org.apache.poi.sl.draw.Drawable; +import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.sl.usermodel.Slide; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; -import org.apache.poi.util.JvmBugs; /** * An utility to convert slides of a .pptx slide show to a PNG image @@ -65,7 +64,7 @@ public class PPTX2PNG { return; } - int slidenum = -1; + String slidenumStr = "-1"; float scale = 1; File file = null; String format = "png"; @@ -77,7 +76,7 @@ public class PPTX2PNG { if ("-scale".equals(args[i])) { scale = Float.parseFloat(args[++i]); } else if ("-slide".equals(args[i])) { - slidenum = Integer.parseInt(args[++i]); + slidenumStr = args[++i]; } else if ("-format".equals(args[i])) { format = args[++i]; } else if ("-outdir".equals(args[i])) { @@ -120,9 +119,11 @@ public class PPTX2PNG { SlideShow ss = SlideShowFactory.create(file, null, true); List> slides = ss.getSlides(); + Set slidenum = slideIndexes(slides.size(), slidenumStr); - if (slidenum < -1 || slidenum == 0 || slidenum > slides.size()) { + if (slidenum.isEmpty()) { usage("slidenum must be either -1 (for all) or within range: [1.."+slides.size()+"] for "+file); + ss.close(); return; } @@ -130,39 +131,36 @@ public class PPTX2PNG { int width = (int) (pgsize.width * scale); int height = (int) (pgsize.height * scale); - int slideNo=1; - for(Slide slide : slides) { - if (slidenum == -1 || slideNo == slidenum) { - String title = slide.getTitle(); - if (!quiet) { - System.out.println("Rendering slide " + slideNo + (title == null ? "" : ": " + title)); - } + for(Integer slideNo : slidenum) { + Slide slide = slides.get(slideNo); + String title = slide.getTitle(); + if (!quiet) { + System.out.println("Rendering slide " + slideNo + (title == null ? "" : ": " + title)); + } - BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - Graphics2D graphics = img.createGraphics(); - fixFonts(graphics); - - // default rendering options - graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); - graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); - graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - - graphics.scale(scale, scale); - - // draw stuff - slide.draw(graphics); - - // save the result - if (!"null".equals(format)) { - String outname = file.getName().replaceFirst(".pptx?", ""); - outname = String.format(Locale.ROOT, "%1$s-%2$04d.%3$s", outname, slideNo, format); - File outfile = new File(outdir, outname); - ImageIO.write(img, format, outfile); - } - } - slideNo++; - } + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics2D graphics = img.createGraphics(); + DrawFactory.getInstance(graphics).fixFonts(graphics); + + // default rendering options + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); + graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); + + graphics.scale(scale, scale); + + // draw stuff + slide.draw(graphics); + + // save the result + if (!"null".equals(format)) { + String outname = file.getName().replaceFirst(".pptx?", ""); + outname = String.format(Locale.ROOT, "%1$s-%2$04d.%3$s", outname, slideNo, format); + File outfile = new File(outdir, outname); + ImageIO.write(img, format, outfile); + } + } if (!quiet) { System.out.println("Done"); @@ -170,14 +168,43 @@ public class PPTX2PNG { ss.close(); } - - @SuppressWarnings("unchecked") - private static void fixFonts(Graphics2D graphics) { - if (!JvmBugs.hasLineBreakMeasurerBug()) return; - Map fontMap = (Map)graphics.getRenderingHint(Drawable.FONT_MAP); - if (fontMap == null) fontMap = new HashMap(); - fontMap.put("Calibri", "Lucida Sans"); - fontMap.put("Cambria", "Lucida Bright"); - graphics.setRenderingHint(Drawable.FONT_MAP, fontMap); + + private static Set slideIndexes(final int slideCount, String range) { + Set slideIdx = new TreeSet(); + if ("-1".equals(range)) { + for (int i=0; iFILL_* constants defined in this class. @@ -172,6 +251,7 @@ public final class HSLFFill { } } + @SuppressWarnings("resource") protected EscherBSERecord getEscherBSERecord(int idx){ HSLFSheet sheet = shape.getSheet(); if(sheet == null) { @@ -258,6 +338,7 @@ public final class HSLFFill { /** * PictureData object used in a texture, pattern of picture fill. */ + @SuppressWarnings("resource") public HSLFPictureData getPictureData(){ AbstractEscherOptRecord opt = shape.getEscherOptRecord(); EscherSimpleProperty p = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__PATTERNTEXTURE); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFreeformShape.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFreeformShape.java index dde778ffcc..8be86f0995 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFreeformShape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFreeformShape.java @@ -18,22 +18,25 @@ package org.apache.poi.hslf.usermodel; import java.awt.geom.AffineTransform; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; import java.awt.geom.PathIterator; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Iterator; import java.util.List; import org.apache.poi.ddf.AbstractEscherOptRecord; import org.apache.poi.ddf.EscherArrayProperty; import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherProperties; +import org.apache.poi.ddf.EscherProperty; import org.apache.poi.ddf.EscherSimpleProperty; import org.apache.poi.sl.usermodel.FreeformShape; import org.apache.poi.sl.usermodel.ShapeContainer; import org.apache.poi.sl.usermodel.ShapeType; +import org.apache.poi.util.BitField; +import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; import org.apache.poi.util.Units; @@ -57,6 +60,85 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh public static final byte[] SEGMENTINFO_CLOSE = new byte[]{0x01, (byte)0x60}; public static final byte[] SEGMENTINFO_END = new byte[]{0x00, (byte)0x80}; + private static BitField PATH_INFO = BitFieldFactory.getInstance(0xE000); + private static BitField ESCAPE_INFO = BitFieldFactory.getInstance(0x1F00); + + enum PathInfo { + lineTo(0),curveTo(1),moveTo(2),close(3),end(4),escape(5),clientEscape(6); + int flag; + PathInfo(int flag) { + this.flag = flag; + } + static PathInfo valueOf(int flag) { + for (PathInfo v : values()) { + if (v.flag == flag) { + return v; + } + } + return null; + } + } + + enum EscapeInfo { + EXTENSION(0x0000), + ANGLE_ELLIPSE_TO(0x0001), + ANGLE_ELLIPSE(0x0002), + ARC_TO(0x0003), + ARC(0x0004), + CLOCKWISE_ARC_TO(0x0005), + CLOCKWISE_ARC(0x0006), + ELLIPTICAL_QUADRANT_X(0x0007), + ELLIPTICAL_QUADRANT_Y(0x0008), + QUADRATIC_BEZIER(0x0009), + NO_FILL(0X000A), + NO_LINE(0X000B), + AUTO_LINE(0X000C), + AUTO_CURVE(0X000D), + CORNER_LINE(0X000E), + CORNER_CURVE(0X000F), + SMOOTH_LINE(0X0010), + SMOOTH_CURVE(0X0011), + SYMMETRIC_LINE(0X0012), + SYMMETRIC_CURVE(0X0013), + FREEFORM(0X0014), + FILL_COLOR(0X0015), + LINE_COLOR(0X0016); + + int flag; + EscapeInfo(int flag) { + this.flag = flag; + } + static EscapeInfo valueOf(int flag) { + for (EscapeInfo v : values()) { + if (v.flag == flag) { + return v; + } + } + return null; + } + } + + enum ShapePath { + LINES(0), + LINES_CLOSED(1), + CURVES(2), + CURVES_CLOSED(3), + COMPLEX(4); + + int flag; + ShapePath(int flag) { + this.flag = flag; + } + static ShapePath valueOf(int flag) { + for (ShapePath v : values()) { + if (v.flag == flag) { + return v; + } + } + return null; + } + } + /** * Create a Freeform object and initialize it from the supplied Record container. * @@ -88,7 +170,7 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh } @Override - public int setPath(GeneralPath path) { + public int setPath(Path2D.Double path) { Rectangle2D bounds = path.getBounds2D(); PathIterator it = path.getPathIterator(new AffineTransform()); @@ -174,23 +256,19 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh opt.sortProperties(); setAnchor(bounds); - + return numPoints; } @Override - public GeneralPath getPath(){ + public Path2D.Double getPath(){ AbstractEscherOptRecord opt = getEscherOptRecord(); - opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__SHAPEPATH, 0x4)); - EscherArrayProperty verticesProp = getEscherProperty(opt, (short)(EscherProperties.GEOMETRY__VERTICES + 0x4000)); - if(verticesProp == null) verticesProp = getEscherProperty(opt, EscherProperties.GEOMETRY__VERTICES); - - EscherArrayProperty segmentsProp = getEscherProperty(opt, (short)(EscherProperties.GEOMETRY__SEGMENTINFO + 0x4000)); - if(segmentsProp == null) segmentsProp = getEscherProperty(opt, EscherProperties.GEOMETRY__SEGMENTINFO); + EscherArrayProperty verticesProp = getShapeProp(opt, EscherProperties.GEOMETRY__VERTICES); + EscherArrayProperty segmentsProp = getShapeProp(opt, EscherProperties.GEOMETRY__SEGMENTINFO); // return empty path if either GEOMETRY__VERTICES or GEOMETRY__SEGMENTINFO is missing, see Bugzilla 54188 - GeneralPath path = new GeneralPath(); + Path2D.Double path = new Path2D.Double(); //sanity check if(verticesProp == null) { @@ -202,46 +280,60 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh return path; } - int numPoints = verticesProp.getNumberOfElementsInArray(); - int numSegments = segmentsProp.getNumberOfElementsInArray(); - for (int i = 0, j = 0; i < numSegments && j < numPoints; i++) { - byte[] elem = segmentsProp.getElement(i); - if(Arrays.equals(elem, SEGMENTINFO_MOVETO)){ - byte[] p = verticesProp.getElement(j++); - short x = LittleEndian.getShort(p, 0); - short y = LittleEndian.getShort(p, 2); - path.moveTo(Units.masterToPoints(x), Units.masterToPoints(y)); - } else if (Arrays.equals(elem, SEGMENTINFO_CUBICTO) || Arrays.equals(elem, SEGMENTINFO_CUBICTO2)){ - i++; - byte[] p1 = verticesProp.getElement(j++); - short x1 = LittleEndian.getShort(p1, 0); - short y1 = LittleEndian.getShort(p1, 2); - byte[] p2 = verticesProp.getElement(j++); - short x2 = LittleEndian.getShort(p2, 0); - short y2 = LittleEndian.getShort(p2, 2); - byte[] p3 = verticesProp.getElement(j++); - short x3 = LittleEndian.getShort(p3, 0); - short y3 = LittleEndian.getShort(p3, 2); - path.curveTo( - Units.masterToPoints(x1), Units.masterToPoints(y1), - Units.masterToPoints(x2), Units.masterToPoints(y2), - Units.masterToPoints(x3), Units.masterToPoints(y3)); - - } else if (Arrays.equals(elem, SEGMENTINFO_LINETO)){ - i++; - byte[] pnext = segmentsProp.getElement(i); - if(Arrays.equals(pnext, SEGMENTINFO_ESCAPE)){ - if(j + 1 < numPoints){ - byte[] p = verticesProp.getElement(j++); - short x = LittleEndian.getShort(p, 0); - short y = LittleEndian.getShort(p, 2); - path.lineTo(Units.masterToPoints(x), Units.masterToPoints(y)); + Iterator vertIter = verticesProp.iterator(); + Iterator segIter = segmentsProp.iterator(); + + byte segPushBack[] = null; + while (vertIter.hasNext() && segIter.hasNext()) { + byte[] segElem = (segPushBack != null) ? segPushBack : segIter.next(); + segPushBack = null; + PathInfo pi = getPathInfo(segElem); + switch (pi) { + case escape: { + handleEscapeInfo(path, segElem, vertIter); + break; + } + case moveTo: { + byte[] p = vertIter.next(); + double x = Units.masterToPoints(LittleEndian.getShort(p, 0)); + double y = Units.masterToPoints(LittleEndian.getShort(p, 2)); + path.moveTo(x,y); + break; + } + case curveTo: { + byte[] p1 = vertIter.next(); + double x1 = Units.masterToPoints(LittleEndian.getShort(p1, 0)); + double y1 = Units.masterToPoints(LittleEndian.getShort(p1, 2)); + byte[] p2 = vertIter.next(); + double x2 = Units.masterToPoints(LittleEndian.getShort(p2, 0)); + double y2 = Units.masterToPoints(LittleEndian.getShort(p2, 2)); + byte[] p3 = vertIter.next(); + double x3 = Units.masterToPoints(LittleEndian.getShort(p3, 0)); + double y3 = Units.masterToPoints(LittleEndian.getShort(p3, 2)); + path.curveTo(x1,y1,x2,y2,x3,y3); + break; + } + case lineTo: + if (vertIter.hasNext()) { + byte[] p = vertIter.next(); + double x = Units.masterToPoints(LittleEndian.getShort(p, 0)); + double y = Units.masterToPoints(LittleEndian.getShort(p, 2)); + path.lineTo(x,y); } - } else if (Arrays.equals(pnext, SEGMENTINFO_CLOSE)){ + break; + case close: path.closePath(); - } + break; + default: + break; } } + + EscherSimpleProperty shapePath = getShapeProp(opt, EscherProperties.GEOMETRY__SHAPEPATH); + ShapePath sp = ShapePath.valueOf(shapePath == null ? 1 : shapePath.getPropertyValue()); + if (sp == ShapePath.LINES_CLOSED || sp == ShapePath.CURVES_CLOSED) { + path.closePath(); + } Rectangle2D anchor = getAnchor(); Rectangle2D bounds = path.getBounds2D(); @@ -251,6 +343,81 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh anchor.getWidth()/bounds.getWidth(), anchor.getHeight()/bounds.getHeight() ); - return new GeneralPath(at.createTransformedShape(path)); + return new Path2D.Double(at.createTransformedShape(path)); + } + + private static T getShapeProp(AbstractEscherOptRecord opt, int propId) { + T prop = getEscherProperty(opt, (short)(propId + 0x4000)); + if (prop == null) { + prop = getEscherProperty(opt, propId); + } + return prop; + } + + private void handleEscapeInfo(Path2D path, byte segElem[], Iterator vertIter) { + EscapeInfo ei = getEscapeInfo(segElem); + switch (ei) { + case EXTENSION: + break; + case ANGLE_ELLIPSE_TO: + break; + case ANGLE_ELLIPSE: + break; + case ARC_TO: + break; + case ARC: + break; + case CLOCKWISE_ARC_TO: + break; + case CLOCKWISE_ARC: + break; + case ELLIPTICAL_QUADRANT_X: + break; + case ELLIPTICAL_QUADRANT_Y: + break; + case QUADRATIC_BEZIER: + break; + case NO_FILL: + break; + case NO_LINE: + break; + case AUTO_LINE: + break; + case AUTO_CURVE: + break; + case CORNER_LINE: + break; + case CORNER_CURVE: + break; + case SMOOTH_LINE: + break; + case SMOOTH_CURVE: + break; + case SYMMETRIC_LINE: + break; + case SYMMETRIC_CURVE: + break; + case FREEFORM: + break; + case FILL_COLOR: + break; + case LINE_COLOR: + break; + default: + break; + } + } + + + private static PathInfo getPathInfo(byte elem[]) { + int elemUS = LittleEndian.getUShort(elem, 0); + int pathInfo = PATH_INFO.getValue(elemUS); + return PathInfo.valueOf(pathInfo); + } + + private static EscapeInfo getEscapeInfo(byte elem[]) { + int elemUS = LittleEndian.getUShort(elem, 0); + int escInfo = ESCAPE_INFO.getValue(elemUS); + return EscapeInfo.valueOf(escInfo); } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java index 74fe3cb183..949a1230dd 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java @@ -363,6 +363,9 @@ public final class HSLFHyperlink implements Hyperlink { int val = (p == null) ? defaultColor : p.getPropertyValue(); EscherColorRef ecr = new EscherColorRef(val); - + Color col = getColor(ecr); + + double alpha = getAlpha(opacityProperty); + return new Color(col.getRed(), col.getGreen(), col.getBlue(), (int)(alpha*255.0)); + } + + Color getColor(EscherColorRef ecr) { boolean fPaletteIndex = ecr.hasPaletteIndexFlag(); boolean fPaletteRGB = ecr.hasPaletteRGBFlag(); boolean fSystemRGB = ecr.hasSystemRGBFlag(); @@ -373,11 +379,10 @@ public abstract class HSLFShape implements Shape { } else if (fSysIndex){ //TODO } - - double alpha = getAlpha(opacityProperty); - return new Color(rgb[0], rgb[1], rgb[2], (int)(alpha*255.0)); + + return new Color(rgb[0], rgb[1], rgb[2]); } - + double getAlpha(short opacityProperty) { AbstractEscherOptRecord opt = getEscherOptRecord(); EscherSimpleProperty op = getEscherProperty(opt, opacityProperty); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java index 1971713536..4797ff5f57 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java @@ -111,8 +111,15 @@ public final class HSLFShapeFactory { shape = createNonPrimitive(spContainer, parent); break; default: - EscherTextboxRecord etr = spContainer.getChildById(EscherTextboxRecord.RECORD_ID); - if (parent instanceof HSLFTable && etr != null) { + if (parent instanceof HSLFTable) { + EscherTextboxRecord etr = spContainer.getChildById(EscherTextboxRecord.RECORD_ID); + if (etr == null) { + logger.log(POILogger.WARN, "invalid ppt - add EscherTextboxRecord to cell"); + etr = new EscherTextboxRecord(); + etr.setRecordId(EscherTextboxRecord.RECORD_ID); + etr.setOptions((short)15); + spContainer.addChildRecord(etr); + } shape = new HSLFTableCell(spContainer, (HSLFTable)parent); } else { shape = new HSLFAutoShape(spContainer, parent); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java index 1326de19a0..0a13a62d57 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSimpleShape.java @@ -275,7 +275,8 @@ public abstract class HSLFSimpleShape extends HSLFShape implements SimpleShape fontMap = (Map)graphicsCtx.getRenderingHint(Drawable.FONT_MAP); + if (fontMap != null && fontMap.containsKey(font.getFacename())) { + fontFamily = fontMap.get(font.getFacename()); + } + if (fontHandler != null) { + fontFamily = fontHandler.getRendererableFont(font.getFacename(), font.getPitchAndFamily()); + } + if (fontFamily == null) { + fontFamily = font.getFacename(); + } + + as.addAttribute(TextAttribute.FAMILY, fontFamily); + as.addAttribute(TextAttribute.SIZE, getFontHeight(font)); + as.addAttribute(TextAttribute.STRIKETHROUGH, font.isStrikeOut()); + if (font.isUnderline()) { + as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); + } + if (font.isItalic()) { + as.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); + } + as.addAttribute(TextAttribute.WEIGHT, font.getWeight()); + } + + private double getFontHeight(HwmfFont font) { + // see HwmfFont#height for details + double fontHeight = font.getHeight(); + if (fontHeight == 0) { + return 12; + } else if (fontHeight < 0) { + return -fontHeight; + } else { + // TODO: fix font height calculation + // the height is given as font size + ascent + descent + // as an approximation we reduce the height by a static factor + return fontHeight*3/4; + } + } } diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBinaryRasterOp.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBinaryRasterOp.java new file mode 100644 index 0000000000..31ddcd76e8 --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBinaryRasterOp.java @@ -0,0 +1,112 @@ +/* ==================================================================== + 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.hwmf.record; + +/** + * The BinaryRasterOperation Enumeration section lists the binary raster-operation codes. + * Rasteroperation codes define how metafile processing combines the bits from the selected + * pen with the bits in the destination bitmap. + * + * Each raster-operation code represents a Boolean operation in which the values of the pixels in the + * selected pen and the destination bitmap are combined. Following are the two operands used in these + * operations. + * + * + * + * + * + *
OperandMeaning
PSelected pen
DDestination bitmap
+ * + * Following are the Boolean operators used in these operations. + * + * + * + * + * + * + *
OperandMeaning
aBitwise AND
nBitwise NOT (inverse)
oBitwise OR
xBitwise exclusive OR (XOR)
+ * + * All Boolean operations are presented in reverse Polish notation. For example, the following + * operation replaces the values of the pixels in the destination bitmap with a combination of the pixel + * values of the pen and the selected brush: DPo. + * + * Each raster-operation code is a 32-bit integer whose high-order word is a Boolean operation index and + * whose low-order word is the operation code. The 16-bit operation index is a zero-extended, 8-bit + * value that represents all possible outcomes resulting from the Boolean operation on two parameters + * (in this case, the pen and destination values). For example, the operation indexes for the DPo and + * DPan operations are shown in the following list. + * + * + * + * + * + * + * + *
PDDPoDPan
0001
0111
1011
1110
+ * + */ +public enum HwmfBinaryRasterOp { + /** 0, Pixel is always 0 */ + R2_BLACK(0x0001), + /** DPon, Pixel is the inverse of the R2_MERGEPEN color. */ + R2_NOTMERGEPEN(0x0002), + /** DPna, Pixel is a combination of the screen color and the inverse of the pen color. */ + R2_MASKNOTPEN(0x0003), + /** Pn, Pixel is the inverse of the pen color. */ + R2_NOTCOPYPEN(0x0004), + /** PDna, Pixel is a combination of the colors common to both the pen and the inverse of the screen. */ + R2_MASKPENNOT(0x0005), + /** Dn, Pixel is the inverse of the screen color. */ + R2_NOT(0x0006), + /** DPx, Pixel is a combination of the colors in the pen or in the screen, but not in both. */ + R2_XORPEN(0x0007), + /** DPan, Pixel is the inverse of the R2_MASKPEN color. */ + R2_NOTMASKPEN(0x0008), + /** DPa, Pixel is a combination of the colors common to both the pen and the screen. */ + R2_MASKPEN(0x0009), + /** DPxn, Pixel is the inverse of the R2_XORPEN color. */ + R2_NOTXORPEN(0x000A), + /** D, Pixel remains unchanged. */ + R2_NOP(0x000B), + /** DPno, Pixel is a combination of the colors common to both the screen and the inverse of the pen. */ + R2_MERGENOTPEN(0x000C), + /** P, Pixel is the pen color. */ + R2_COPYPEN(0x000D), + /** PDno, Pixel is a combination of the pen color and the inverse of the screen color.*/ + R2_MERGEPENNOT(0x000E), + /** DPo, Pixel is a combination of the pen color and the screen color. */ + R2_MERGEPEN(0x000F), + /** 1, Pixel is always 1 */ + R2_WHITE(0x0010); + + int opIndex; + + HwmfBinaryRasterOp(int opIndex) { + this.opIndex=opIndex; + } + + public static HwmfBinaryRasterOp valueOf(int opIndex) { + for (HwmfBinaryRasterOp bb : HwmfBinaryRasterOp.values()) { + if (bb.opIndex == opIndex) { + return bb; + } + } + return null; + } + +} diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java index 0f720239f3..ae7f5cb307 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java @@ -18,13 +18,11 @@ package org.apache.poi.hwmf.record; import java.awt.Color; +import java.awt.Graphics2D; import java.awt.image.BufferedImage; -import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; import javax.imageio.ImageIO; @@ -32,6 +30,8 @@ import org.apache.poi.hssf.record.RecordFormatException; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInputStream; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; /** * The DeviceIndependentBitmap Object defines an image in device-independent bitmap (DIB) format. @@ -188,6 +188,7 @@ public class HwmfBitmapDib { } } + private static POILogger logger = POILogFactory.getLogger(HwmfBitmapDib.class); private static final int BMP_HEADER_SIZE = 14; private int headerSize; @@ -204,8 +205,6 @@ public class HwmfBitmapDib { private long headerColorUsed = -1; @SuppressWarnings("unused") private long headerColorImportant = -1; - - @SuppressWarnings("unused") private Color colorTable[]; @SuppressWarnings("unused") private int colorMaskR=0,colorMaskG=0,colorMaskB=0; @@ -360,22 +359,24 @@ public class HwmfBitmapDib { protected int readRGBQuad(LittleEndianInputStream leis, int count) throws IOException { int size = 0; - List colorList = new ArrayList(); + colorTable = new Color[count]; for (int i=0; i>> 6) & 3); + pitchAndFamily = leis.readUByte(); byte buf[] = new byte[32], b, readBytes = 0; do { @@ -546,12 +551,16 @@ public class HwmfFont { return quality; } + public int getPitchAndFamily() { + return pitchAndFamily; + } + public WmfFontFamilyClass getFamily() { - return family; + return WmfFontFamilyClass.valueOf(pitchAndFamily & 0xF); } public WmfFontPitch getPitch() { - return pitch; + return WmfFontPitch.valueOf((pitchAndFamily >>> 6) & 3); } public String getFacename() { diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMapMode.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMapMode.java index 28a26a88f6..cae700e6dc 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMapMode.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMapMode.java @@ -109,6 +109,6 @@ public enum HwmfMapMode { for (HwmfMapMode mm : values()) { if (mm.flag == flag) return mm; } - return null; + return MM_ISOTROPIC; } } \ No newline at end of file diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMisc.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMisc.java index 2d6ab4e77b..c597b1d964 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMisc.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfMisc.java @@ -265,25 +265,9 @@ public class HwmfMisc { /** * A 16-bit unsigned integer that defines the foreground binary raster - * operation mixing mode. This MUST be one of the values: - * R2_BLACK = 0x0001, - * R2_NOTMERGEPEN = 0x0002, - * R2_MASKNOTPEN = 0x0003, - * R2_NOTCOPYPEN = 0x0004, - * R2_MASKPENNOT = 0x0005, - * R2_NOT = 0x0006, - * R2_XORPEN = 0x0007, - * R2_NOTMASKPEN = 0x0008, - * R2_MASKPEN = 0x0009, - * R2_NOTXORPEN = 0x000A, - * R2_NOP = 0x000B, - * R2_MERGENOTPEN = 0x000C, - * R2_COPYPEN = 0x000D, - * R2_MERGEPENNOT = 0x000E, - * R2_MERGEPEN = 0x000F, - * R2_WHITE = 0x0010 + * operation mixing mode */ - private int drawMode; + private HwmfBinaryRasterOp drawMode; @Override public HwmfRecordType getRecordType() { @@ -292,7 +276,7 @@ public class HwmfMisc { @Override public int init(LittleEndianInputStream leis, long recordSize, int recordFunction) throws IOException { - drawMode = leis.readUShort(); + drawMode = HwmfBinaryRasterOp.valueOf(leis.readUShort()); return LittleEndianConsts.SHORT_SIZE; } diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPalette.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPalette.java index 42e661b17e..eb6139c217 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPalette.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPalette.java @@ -93,7 +93,7 @@ public class HwmfPalette { } } - public static abstract class WmfPaletteParent implements HwmfRecord { + public static abstract class WmfPaletteParent implements HwmfRecord, HwmfObjectTableEntry { /** * Start (2 bytes): A 16-bit unsigned integer that defines the offset into the Palette Object when @@ -121,6 +121,11 @@ public class HwmfPalette { return size; } + @Override + public final void draw(HwmfGraphics ctx) { + ctx.addObjectTableEntry(this); + } + protected List getPaletteCopy() { List newPalette = new ArrayList(); for (PaletteEntry et : palette) { @@ -143,11 +148,6 @@ public class HwmfPalette { return HwmfRecordType.createPalette; } - @Override - public void draw(HwmfGraphics ctx) { - ctx.addObjectTableEntry(this); - } - @Override public void applyObject(HwmfGraphics ctx) { ctx.getProperties().setPalette(getPaletteCopy()); @@ -165,7 +165,7 @@ public class HwmfPalette { } @Override - public void draw(HwmfGraphics ctx) { + public void applyObject(HwmfGraphics ctx) { HwmfDrawProperties props = ctx.getProperties(); List palette = props.getPalette(); if (palette == null) { @@ -192,7 +192,7 @@ public class HwmfPalette { * The META_RESIZEPALETTE record redefines the size of the logical palette that is defined in the * playback device context. */ - public static class WmfResizePalette implements HwmfRecord { + public static class WmfResizePalette implements HwmfRecord, HwmfObjectTableEntry { /** * A 16-bit unsigned integer that defines the number of entries in * the logical palette. @@ -212,6 +212,11 @@ public class HwmfPalette { @Override public void draw(HwmfGraphics ctx) { + ctx.addObjectTableEntry(this); + } + + @Override + public void applyObject(HwmfGraphics ctx) { HwmfDrawProperties props = ctx.getProperties(); List palette = props.getPalette(); if (palette == null) { @@ -292,7 +297,7 @@ public class HwmfPalette { } @Override - public void draw(HwmfGraphics ctx) { + public void applyObject(HwmfGraphics ctx) { HwmfDrawProperties props = ctx.getProperties(); List dest = props.getPalette(); List src = getPaletteCopy(); diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfTernaryRasterOp.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfTernaryRasterOp.java index 5ee3100fc0..9cd9b1e81c 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfTernaryRasterOp.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfTernaryRasterOp.java @@ -17,6 +17,68 @@ package org.apache.poi.hwmf.record; +/** + * Each ternary raster operation code represents a Boolean operation in which the values of the pixels in + * the source, the selected brush, and the destination are combined. Following are the three operands + * used in these operations. + * + * + * + * + * + * + *
OperandMeaning
DDestination bitmap
PSelected brush (also called pattern)
SSource bitmap
+ * + * Following are the Boolean operators used in these operations. + * + * + * + * + * + * + *
OperandMeaning
aBitwise AND
nBitwise NOT (inverse)
oBitwise OR
xBitwise exclusive OR (XOR)
+ * + * All Boolean operations are presented in reverse Polish notation. For example, the following operation + * replaces the values of the pixels in the destination bitmap with a combination of the pixel values of the + * source and brush: PSo. + * + * The following operation combines the values of the pixels in the source and brush with the pixel values + * of the destination bitmap: DPSoo (there are alternative spellings of some functions, so although a + * particular spelling MAY NOT be listed in the enumeration, an equivalent form SHOULD be). + * + * Each raster operation code is a 32-bit integer whose high-order word is a Boolean operation index and + * whose low-order word is the operation code. The 16-bit operation index is a zero-extended, 8-bit + * value that represents the result of the Boolean operation on predefined brush, source, and destination + * values. For example, the operation indexes for the PSo and DPSoo operations are shown in the + * following list. + * + * + * + * + * + * + * + * + * + * + * + *
PSDDPoDPan
00000
00101
01011
01111
10011
10111
11011
11111
+ * + * The operation indexes are determined by reading the binary values in a column of the table from the + * bottom up. For example, in the PSo column, the binary value is 11111100, which is equivalent to 00FC + * (hexadecimal is implicit for these values), which is the operation index for PSo. + * + * Using this method, DPSoo can be seen to have the operation index 00FE. Operation indexes define the + * locations of corresponding raster operation codes in the preceding enumeration. The PSo operation is + * in line 252 (0x00FC) of the enumeration; DPSoo is in line 254 (0x00FE). + * + * The most commonly used raster operations have been given explicit enumeration names, which + * SHOULD be used; examples are PATCOPY and WHITENESS. + * + * When the source and destination bitmaps are monochrome, a bit value of 0 represents a black pixel + * and a bit value of 1 represents a white pixel. When the source and the destination bitmaps are color, + * those colors are represented with red green blue (RGB) values. + */ public enum HwmfTernaryRasterOp { BLACKNESS(0x0000,0x0042,"0"), DPSOON(0x0001,0x0289,"DPSoon"), @@ -274,17 +336,17 @@ public enum HwmfTernaryRasterOp { PSDNOO(0x00FD,0x0A0A,"PSDnoo"), DPSOO(0x00FE,0x02A9,"DPSoo"), WHITENESS(0x00FF,0x0062,"1"); - + int opIndex; int opCode; String opCmd; - + HwmfTernaryRasterOp(int opIndex, int opCode, String opCmd) { this.opIndex=opIndex; this.opCode=opCode; this.opCmd=opCmd; } - + public static HwmfTernaryRasterOp valueOf(int opIndex) { for (HwmfTernaryRasterOp bb : HwmfTernaryRasterOp.values()) { if (bb.opIndex == opIndex) { @@ -293,11 +355,11 @@ public enum HwmfTernaryRasterOp { } return null; } - + public String describeCmd() { String stack[] = new String[10]; int stackPnt = 0; - + for (char c : opCmd.toCharArray()) { switch (c) { case 'S': diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfText.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfText.java index 8c6c41549b..e4e4ecb4d8 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfText.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfText.java @@ -19,7 +19,6 @@ package org.apache.poi.hwmf.record; import java.awt.geom.Rectangle2D; import java.io.IOException; -import java.text.AttributedString; import org.apache.poi.hwmf.draw.HwmfDrawProperties; import org.apache.poi.hwmf.draw.HwmfGraphics; @@ -29,10 +28,13 @@ import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInputStream; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.util.RecordFormatException; public class HwmfText { - + private static final POILogger logger = POILogFactory.getLogger(HwmfText.class); + /** * The META_SETTEXTCHAREXTRA record defines inter-character spacing for text justification in the * playback device context. Spacing is added to the white space between each character, including @@ -185,6 +187,48 @@ public class HwmfText { */ public static class WmfExtTextOut implements HwmfRecord { + /** + * Indicates that the background color that is defined in the playback device context + * SHOULD be used to fill the rectangle. + */ + private static final BitField ETO_OPAQUE = BitFieldFactory.getInstance(0x0002); + + /** + * Indicates that the text SHOULD be clipped to the rectangle. + */ + private static final BitField ETO_CLIPPED = BitFieldFactory.getInstance(0x0004); + + /** + * Indicates that the string to be output SHOULD NOT require further processing + * with respect to the placement of the characters, and an array of character + * placement values SHOULD be provided. This character placement process is + * useful for fonts in which diacritical characters affect character spacing. + */ + private static final BitField ETO_GLYPH_INDEX = BitFieldFactory.getInstance(0x0010); + + /** + * Indicates that the text MUST be laid out in right-to-left reading order, instead of + * the default left-to-right order. This SHOULD be applied only when the font that is + * defined in the playback device context is either Hebrew or Arabic. + */ + private static final BitField ETO_RTLREADING = BitFieldFactory.getInstance(0x0080); + + /** + * Indicates that to display numbers, digits appropriate to the locale SHOULD be used. + */ + private static final BitField ETO_NUMERICSLOCAL = BitFieldFactory.getInstance(0x0400); + + /** + * Indicates that to display numbers, European digits SHOULD be used. + */ + private static final BitField ETO_NUMERICSLATIN = BitFieldFactory.getInstance(0x0800); + + /** + * Indicates that both horizontal and vertical character displacement values + * SHOULD be provided. + */ + private static final BitField ETO_PDY = BitFieldFactory.getInstance(0x2000); + /** * A 16-bit signed integer that defines the y-coordinate, in logical units, where the text string is to be located. @@ -199,40 +243,12 @@ public class HwmfText { * A 16-bit signed integer that defines the length of the string. */ private int stringLength; - /** - * A 16-bit unsigned integer that defines the use of the application-defined - * rectangle. This member can be a combination of one or more values in the - * ExtTextOutOptions Flags: - * - * ETO_OPAQUE (0x0002): - * Indicates that the background color that is defined in the playback device context - * SHOULD be used to fill the rectangle. - * - * ETO_CLIPPED (0x0004): - * Indicates that the text SHOULD be clipped to the rectangle. - * - * ETO_GLYPH_INDEX (0x0010): - * Indicates that the string to be output SHOULD NOT require further processing - * with respect to the placement of the characters, and an array of character - * placement values SHOULD be provided. This character placement process is - * useful for fonts in which diacritical characters affect character spacing. - * - * ETO_RTLREADING (0x0080): - * Indicates that the text MUST be laid out in right-to-left reading order, instead of - * the default left-to-right order. This SHOULD be applied only when the font that is - * defined in the playback device context is either Hebrew or Arabic. <37> - * - * ETO_NUMERICSLOCAL (0x0400): - * Indicates that to display numbers, digits appropriate to the locale SHOULD be - * used. - * - * ETO_NUMERICSLATIN (0x0800): - * Indicates that to display numbers, European digits SHOULD be used. <39> - * - * ETO_PDY (0x2000): - * Indicates that both horizontal and vertical character displacement values - * SHOULD be provided. - */ + + /** + * A 16-bit unsigned integer that defines the use of the application-defined + * rectangle. This member can be a combination of one or more values in the + * ExtTextOutOptions Flags (ETO_*) + */ private int fwOpts; /** * An optional 8-byte Rect Object (section 2.2.2.18) that defines the @@ -275,7 +291,8 @@ public class HwmfText { int size = 4*LittleEndianConsts.SHORT_SIZE; - if (fwOpts != 0 && size+8<=remainingRecordSize) { + // Check if we have a rectangle + if ((ETO_OPAQUE.isSet(fwOpts) || ETO_CLIPPED.isSet(fwOpts)) && size+8<=remainingRecordSize) { // the bounding rectangle is optional and only read when fwOpts are given left = leis.readShort(); top = leis.readShort(); @@ -289,16 +306,20 @@ public class HwmfText { text = new String(buf, 0, stringLength, LocaleUtil.CHARSET_1252); size += buf.length; - if (size < remainingRecordSize) { - if (size + stringLength*LittleEndianConsts.SHORT_SIZE < remainingRecordSize) { - throw new RecordFormatException("can't read Dx array - given recordSize doesn't contain enough values for string length "+stringLength); - } - - dx = new int[stringLength]; - for (int i=0; i= remainingRecordSize) { + logger.log(POILogger.INFO, "META_EXTTEXTOUT doesn't contain character tracking info"); + return size; + } + + int dxLen = Math.min(stringLength, (remainingRecordSize-size)/LittleEndianConsts.SHORT_SIZE); + if (dxLen < stringLength) { + logger.log(POILogger.WARN, "META_EXTTEXTOUT tracking info doesn't cover all characters"); + } + + dx = new int[stringLength]; + for (int i=0; i records = new ArrayList(); final HwmfPlaceableHeader placeableHeader; final HwmfHeader header; @@ -52,6 +54,10 @@ public class HwmfPicture { header = new HwmfHeader(leis); for (;;) { + if (leis.available() < 6) { + logger.log(POILogger.ERROR, "unexpected eof - wmf file was truncated"); + break; + } // recordSize in DWORDs long recordSize = leis.readUInt()*2; int recordFunction = leis.readShort(); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestFreeform.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestFreeform.java index 7b3b785444..91d23664d8 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestFreeform.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestFreeform.java @@ -20,7 +20,10 @@ package org.apache.poi.hslf.model; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.awt.geom.*; +import java.awt.geom.Area; +import java.awt.geom.Line2D; +import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; import org.apache.poi.hslf.usermodel.HSLFFreeformShape; import org.junit.Test; @@ -38,7 +41,7 @@ public final class TestFreeform { @Test public void testClosedPath() { - GeneralPath path1 = new GeneralPath(); + Path2D.Double path1 = new Path2D.Double(); path1.moveTo(100, 100); path1.lineTo(200, 100); path1.lineTo(200, 200); @@ -55,7 +58,7 @@ public final class TestFreeform { @Test public void testLine() { - GeneralPath path1 = new GeneralPath(new Line2D.Double(100, 100, 200, 100)); + Path2D.Double path1 = new Path2D.Double(new Line2D.Double(100, 100, 200, 100)); HSLFFreeformShape p = new HSLFFreeformShape(); p.setPath(path1); @@ -67,7 +70,7 @@ public final class TestFreeform { @Test public void testRectangle() { - GeneralPath path1 = new GeneralPath(new Rectangle2D.Double(100, 100, 200, 50)); + Path2D.Double path1 = new Path2D.Double(new Rectangle2D.Double(100, 100, 200, 50)); HSLFFreeformShape p = new HSLFFreeformShape(); p.setPath(path1); @@ -84,8 +87,8 @@ public final class TestFreeform { public void test54188() { HSLFFreeformShape p = new HSLFFreeformShape(); - GeneralPath path = p.getPath(); - GeneralPath emptyPath = new GeneralPath(); + Path2D.Double path = p.getPath(); + Path2D.Double emptyPath = new Path2D.Double(); assertEquals(emptyPath.getBounds2D(), path.getBounds2D()); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPicture.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPicture.java index 7dc57f772c..08d6c64211 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPicture.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPicture.java @@ -30,23 +30,22 @@ import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.BitSet; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.imageio.ImageIO; import org.apache.poi.POIDataSamples; import org.apache.poi.ddf.EscherBSERecord; import org.apache.poi.hssf.usermodel.DummyGraphics2d; -import org.apache.poi.sl.draw.Drawable; +import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.sl.usermodel.Slide; import org.apache.poi.sl.usermodel.SlideShow; -import org.apache.poi.util.JvmBugs; import org.junit.Ignore; import org.junit.Test; @@ -64,7 +63,7 @@ public final class TestPicture { * */ @Test - public void multiplePictures() throws Exception { + public void multiplePictures() throws IOException { HSLFSlideShow ppt = new HSLFSlideShow(); HSLFSlide s = ppt.createSlide(); @@ -92,6 +91,8 @@ public final class TestPicture { EscherBSERecord bse3 = pict.getEscherBSERecord(); assertSame(bse2, bse3); assertEquals(3, bse1.getRef()); + + ppt.close(); } /** @@ -99,7 +100,7 @@ public final class TestPicture { * was not found. The correct behaviour is to return null. */ @Test - public void bug46122() { + public void bug46122() throws IOException { HSLFSlideShow ppt = new HSLFSlideShow(); HSLFSlide slide = ppt.createSlide(); HSLFPictureData pd = HSLFPictureData.create(PictureType.PNG); @@ -112,10 +113,12 @@ public final class TestPicture { BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); pict.draw(graphics); + + ppt.close(); } @Test - public void macImages() throws Exception { + public void macImages() throws IOException { HSLFSlideShowImpl hss = new HSLFSlideShowImpl(_slTests.openResourceAsStream("53446.ppt")); List pictures = hss.getPictureData(); @@ -154,11 +157,14 @@ public final class TestPicture { break; } } + + hss.close(); } @Test @Ignore("Just for visual validation - antialiasing is different on various systems") - public void bug54541() throws Exception { + public void bug54541() + throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { String files[] = { // "sample_pptx_grouping_issues.pptx", // "54542_cropped_bitmap.pptx", @@ -196,7 +202,7 @@ public final class TestPicture { } else { BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = img.createGraphics(); - fixFonts(graphics); + DrawFactory.getInstance(graphics).fixFonts(graphics); slide.draw(graphics); graphics.setColor(Color.BLACK); graphics.setStroke(new BasicStroke(1)); @@ -204,16 +210,8 @@ public final class TestPicture { ImageIO.write(img, "PNG", new File(file.replaceFirst(".pptx?", "-")+slideNo+".png")); } } + + ss.close(); } } - - @SuppressWarnings("unchecked") - private void fixFonts(Graphics2D graphics) { - if (!JvmBugs.hasLineBreakMeasurerBug()) return; - Map fontMap = (Map)graphics.getRenderingHint(Drawable.FONT_MAP); - if (fontMap == null) fontMap = new HashMap(); - fontMap.put("Calibri", "Lucida Sans"); - fontMap.put("Cambria", "Lucida Bright"); - graphics.setRenderingHint(Drawable.FONT_MAP, fontMap); - } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java b/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java index 7ddacf475a..78e6534569 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java +++ b/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.RenderingHints; +import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileFilter; @@ -64,7 +65,7 @@ public class TestHwmfParsing { @Ignore("This is work-in-progress and not a real unit test ...") public void paint() throws IOException { File f = POIDataSamples.getSlideShowInstance().getFile("santa.wmf"); -// File f = new File("E:\\project\\poi\\misc\\govdocs-ppt", "000133-0001.wmf"); + // File f = new File("bla.wmf"); FileInputStream fis = new FileInputStream(f); HwmfPicture wmf = new HwmfPicture(fis); fis.close(); @@ -73,6 +74,11 @@ public class TestHwmfParsing { int width = Units.pointsToPixel(dim.getWidth()); // keep aspect ratio for height int height = Units.pointsToPixel(dim.getHeight()); + double max = Math.max(width, height); + if (max > 1500) { + width *= 1500/max; + height *= 1500/max; + } BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bufImg.createGraphics(); @@ -81,7 +87,7 @@ public class TestHwmfParsing { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - wmf.draw(g); + wmf.draw(g, new Rectangle2D.Double(0,0,width,height)); g.dispose(); diff --git a/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java b/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java index 49338e0b68..24c6f7f91b 100644 --- a/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java +++ b/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java @@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.net.URL; import java.util.Enumeration; @@ -44,7 +44,7 @@ public class TestPresetGeometries { } }); for(Path p : geom){ - GeneralPath path = p.getPath(ctx); + Path2D path = p.getPath(ctx); assertNotNull(path); } } -- 2.39.5