]> source.dussan.org Git - poi.git/commitdiff
more improvements in slide rendering
authorYegor Kozlov <yegor@apache.org>
Mon, 21 Apr 2008 12:51:47 +0000 (12:51 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 21 Apr 2008 12:51:47 +0000 (12:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@650129 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/examples/src/org/apache/poi/hslf/examples/PPT2PNG.java
src/scratchpad/src/org/apache/poi/hslf/model/AutoShape.java
src/scratchpad/src/org/apache/poi/hslf/model/AutoShapes.java
src/scratchpad/src/org/apache/poi/hslf/model/Line.java
src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java
src/scratchpad/src/org/apache/poi/hslf/model/ShapePainter.java
src/scratchpad/src/org/apache/poi/hslf/model/SimpleShape.java
src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java

index aa814505178772387963ca048e92a4e0027e4f5f..8a75297121ff641441da525b8230af7c745b98f8 100755 (executable)
@@ -83,6 +83,11 @@ public class PPT2PNG {
 \r
             BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);\r
             Graphics2D graphics = img.createGraphics();\r
+            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);\r
+            graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);\r
+            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);\r
+            graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);\r
+\r
             graphics.setPaint(Color.white);\r
             graphics.fill(new Rectangle2D.Float(0, 0, width, height));\r
 \r
index dc5f798cd86440a90152c7acc93bc63f338b2708..299fe55225d239f19373baa44130a2c470787697 100644 (file)
@@ -108,9 +108,9 @@ public class AutoShape extends TextShape {
 
     public java.awt.Shape getOutline(){
         ShapeOutline outline = AutoShapes.getShapeOutline(getShapeType());
-        Rectangle2D anchor = getAnchor2D();
+        Rectangle2D anchor = getLogicalAnchor2D();
         if(outline == null){
-            logger.log(POILogger.WARN, "getOutline() is not implemented for " + ShapeTypes.typeName(getShapeType()));
+            logger.log(POILogger.WARN, "Outline not found for " + ShapeTypes.typeName(getShapeType()));
             return anchor;
         } else {
             java.awt.Shape shape = outline.getOutline(this);
index 9d9f5e20436187745ef89cd1decab9b556ab5f6c..5d345e6de4b0d51689c60819dbe0a4d1a9cec64e 100755 (executable)
@@ -288,6 +288,86 @@ public class AutoShapes {
                 return path;\r
             }\r
         };\r
-    }\r
 \r
+        shapes[ShapeTypes.Can] = new ShapeOutline(){\r
+            public java.awt.Shape getOutline(Shape shape){\r
+                //m10800,qx0@1l0@2qy10800,21600,21600@2l21600@1qy10800,xem0@1qy10800@0,21600@1nfe\r
+                int adjval = shape.getEscherProperty(EscherProperties.GEOMETRY__ADJUSTVALUE, 5400);\r
+\r
+                GeneralPath path = new GeneralPath();\r
+\r
+                path.append(new Arc2D.Float(0, 0, 21600, adjval, 0, 180, Arc2D.OPEN), false);\r
+                path.moveTo(0, adjval/2);\r
+\r
+                path.lineTo(0, 21600 - adjval/2);\r
+                path.closePath();\r
+\r
+                path.append(new Arc2D.Float(0, 21600 - adjval, 21600, adjval, 180, 180, Arc2D.OPEN), false);\r
+                path.moveTo(21600, 21600 - adjval/2);\r
+\r
+                path.lineTo(21600, adjval/2);\r
+                path.append(new Arc2D.Float(0, 0, 21600, adjval, 180, 180, Arc2D.OPEN), false);\r
+                path.moveTo(0, adjval/2);\r
+                path.closePath();\r
+                return path;\r
+            }\r
+        };\r
+\r
+        shapes[ShapeTypes.LeftBrace] = new ShapeOutline(){\r
+            public java.awt.Shape getOutline(Shape shape){\r
+                //m21600,qx10800@0l10800@2qy0@11,10800@3l10800@1qy21600,21600e\r
+                int adjval = shape.getEscherProperty(EscherProperties.GEOMETRY__ADJUSTVALUE, 1800);\r
+                int adjval2 = shape.getEscherProperty(EscherProperties.GEOMETRY__ADJUST2VALUE, 10800);\r
+\r
+                GeneralPath path = new GeneralPath();\r
+                path.moveTo(21600, 0);\r
+\r
+                path.append(new Arc2D.Float(10800, 0, 21600, adjval*2, 90, 90, Arc2D.OPEN), false);\r
+                path.moveTo(10800, adjval);\r
+\r
+                path.lineTo(10800, adjval2 - adjval);\r
+\r
+                path.append(new Arc2D.Float(-10800, adjval2 - 2*adjval, 21600, adjval*2, 270, 90, Arc2D.OPEN), false);\r
+                path.moveTo(0, adjval2);\r
+\r
+                path.append(new Arc2D.Float(-10800, adjval2, 21600, adjval*2, 0, 90, Arc2D.OPEN), false);\r
+                path.moveTo(10800, adjval2 + adjval);\r
+\r
+                path.lineTo(10800, 21600 - adjval);\r
+\r
+                path.append(new Arc2D.Float(10800, 21600 - 2*adjval, 21600, adjval*2, 180, 90, Arc2D.OPEN), false);\r
+\r
+                return path;\r
+            }\r
+        };\r
+\r
+        shapes[ShapeTypes.RightBrace] = new ShapeOutline(){\r
+            public java.awt.Shape getOutline(Shape shape){\r
+                //m,qx10800@0 l10800@2qy21600@11,10800@3l10800@1qy,21600e\r
+                int adjval = shape.getEscherProperty(EscherProperties.GEOMETRY__ADJUSTVALUE, 1800);\r
+                int adjval2 = shape.getEscherProperty(EscherProperties.GEOMETRY__ADJUST2VALUE, 10800);\r
+\r
+                GeneralPath path = new GeneralPath();\r
+                path.moveTo(0, 0);\r
+\r
+                path.append(new Arc2D.Float(-10800, 0, 21600, adjval*2, 0, 90, Arc2D.OPEN), false);\r
+                path.moveTo(10800, adjval);\r
+\r
+                path.lineTo(10800, adjval2 - adjval);\r
+\r
+                path.append(new Arc2D.Float(10800, adjval2 - 2*adjval, 21600, adjval*2, 180, 90, Arc2D.OPEN), false);\r
+                path.moveTo(21600, adjval2);\r
+\r
+                path.append(new Arc2D.Float(10800, adjval2, 21600, adjval*2, 90, 90, Arc2D.OPEN), false);\r
+                path.moveTo(10800, adjval2 + adjval);\r
+\r
+                path.lineTo(10800, 21600 - adjval);\r
+\r
+                path.append(new Arc2D.Float(-10800, 21600 - 2*adjval, 21600, adjval*2, 270, 90, Arc2D.OPEN), false);\r
+\r
+                return path;\r
+            }\r
+        };\r
+\r
+    }\r
 }\r
index 073efa39519e26e0ac2b87eaab9632ae1539b715..370da96d6b17b7da763b080ff232f308973bcf07 100644 (file)
@@ -130,7 +130,7 @@ public class Line extends SimpleShape {
     }
 
     public java.awt.Shape getOutline(){
-        Rectangle2D anchor = getAnchor2D();
+        Rectangle2D anchor = getLogicalAnchor2D();
         return new Line2D.Double(anchor.getX(), anchor.getY(), anchor.getX() + anchor.getWidth(), anchor.getY() + anchor.getHeight());
     }
 }
index d4975df429b6764a2cc78561c9596e3855ea2a4b..e96e349002434786e473a7e9b3f79cfdc5e6d24d 100644 (file)
@@ -198,6 +198,10 @@ public abstract class Shape {
         return anchor;
     }
 
+    public Rectangle2D getLogicalAnchor2D(){
+        return getAnchor2D();
+    }
+
     /**
      * Sets the anchor (the bounding box rectangle) of this shape.
      * All coordinates should be expressed in points (72 dpi).
@@ -466,6 +470,6 @@ public abstract class Shape {
      * @return the shape outline
      */
     public java.awt.Shape getOutline(){
-        return getAnchor2D();
+        return getLogicalAnchor2D();
     }
 }
index a0fbb114dc57a276cde1847fc71c44d6165f2c89..b57a6e2336d6ba75527d622386877040aa238007 100644 (file)
@@ -237,7 +237,7 @@ public class ShapeGroup extends Shape{
         EscherClientAnchorRecord clientAnchor = (EscherClientAnchorRecord)getEscherChild(spContainer, EscherClientAnchorRecord.RECORD_ID);
         Rectangle2D.Float anchor = new Rectangle2D.Float();
         if(clientAnchor == null){
-            logger.log(POILogger.WARN, "EscherClientAnchorRecord was not found for the shape group");
+            logger.log(POILogger.INFO, "EscherClientAnchorRecord was not found for shape group. Searching for EscherChildAnchorRecord.");
             EscherChildAnchorRecord rec = (EscherChildAnchorRecord)getEscherChild(spContainer, EscherChildAnchorRecord.RECORD_ID);
             anchor = new Rectangle2D.Float(
                 (float)rec.getDx1()*POINT_DPI/MASTER_DPI,
@@ -282,7 +282,7 @@ public class ShapeGroup extends Shape{
 
         //transform coordinates
         AffineTransform at = graphics.getTransform();
-
+        /*
         if(!anchor.equals(coords)){
             graphics.scale(anchor.getWidth()/coords.getWidth(), anchor.getHeight()/coords.getHeight());
 
@@ -290,7 +290,7 @@ public class ShapeGroup extends Shape{
                     anchor.getX()*coords.getWidth()/anchor.getWidth() - coords.getX(),
                     anchor.getY()*coords.getHeight()/anchor.getHeight() - coords.getY());
         }
-
+        */
         Shape[] sh = getShapes();
         for (int i = 0; i < sh.length; i++) {
             sh[i].draw(graphics);
index b50b4f7caf6d1b0a6ec4fda47a0ccb067a75d140..fa947ddfed69b49bcb74b832c3841a8420094933 100755 (executable)
@@ -32,7 +32,7 @@ public class ShapePainter {
     protected static POILogger logger = POILogFactory.getLogger(ShapePainter.class);\r
 \r
     public static void paint(SimpleShape shape, Graphics2D graphics){\r
-        Rectangle2D anchor = shape.getAnchor2D();\r
+        Rectangle2D anchor = shape.getLogicalAnchor2D();\r
         java.awt.Shape outline = shape.getOutline();\r
 \r
         //flip vertical\r
index 0155ca3f5cdcdb9938548824f658e64b94f3bd85..ea0719a7f15a7e5635c0064584a058b16c4fad39 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.poi.hslf.record.ColorSchemeAtom;
 
 import java.awt.*;
 import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
 
 /**
  *  An abstract simple (non-group) shape.
@@ -232,6 +233,52 @@ public class SimpleShape extends Shape {
         return angle;
     }
 
+    public Rectangle2D getLogicalAnchor2D(){
+        Rectangle2D anchor = getAnchor2D();
+
+        //if it is a groupped shape see if we need to transform the coordinates
+        if (_parent != null){
+            Shape top = _parent;
+            while(top.getParent() != null) top = top.getParent();
+
+            Rectangle2D clientAnchor = top.getAnchor2D();
+            Rectangle2D spgrAnchor = ((ShapeGroup)top).getCoordinates();
+
+            double scalex = (double)spgrAnchor.getWidth()/clientAnchor.getWidth();
+            double scaley = (double)spgrAnchor.getHeight()/clientAnchor.getHeight();
+
+            double x = clientAnchor.getX() + (anchor.getX() - spgrAnchor.getX())/scalex;
+            double y = clientAnchor.getY() + (anchor.getY() - spgrAnchor.getY())/scaley;
+            double width = anchor.getWidth()/scalex;
+            double height = anchor.getHeight()/scaley;
+
+            anchor = new Rectangle2D.Double(x, y, width, height);
+
+        }
+
+        int angle = getRotation();
+        if(angle != 0){
+            double centerX = anchor.getX() + anchor.getWidth()/2;
+            double centerY = anchor.getY() + anchor.getHeight()/2;
+
+            AffineTransform trans = new AffineTransform();
+            trans.translate(centerX, centerY);
+            trans.rotate(Math.toRadians(angle));
+            trans.translate(-centerX, -centerY);
+
+            Rectangle2D rect = trans.createTransformedShape(anchor).getBounds2D();
+            if((anchor.getWidth() < anchor.getHeight() && rect.getWidth() > rect.getHeight()) ||
+                (anchor.getWidth() > anchor.getHeight() && rect.getWidth() < rect.getHeight())    ){
+                trans = new AffineTransform();
+                trans.translate(centerX, centerY);
+                trans.rotate(Math.PI/2);
+                trans.translate(-centerX, -centerY);
+                anchor = trans.createTransformedShape(anchor).getBounds2D();
+            }
+        }
+        return anchor;
+    }
+
     public void draw(Graphics2D graphics){
         AffineTransform at = graphics.getTransform();
         ShapePainter.paint(this, graphics);
index 7d23c7cfb8ee6310d3a3d6cd2043fe4970b092fd..c1eadc336c6494af6998d28978484ac9037c4154 100755 (executable)
@@ -54,7 +54,10 @@ public class TextPainter {
         for (int i = 0; i < rt.length; i++) {\r
             int start = rt[i].getStartIndex();\r
             int end = rt[i].getEndIndex();\r
-            if(start == end) continue;\r
+            if(start == end) {\r
+                logger.log(POILogger.INFO,  "Skipping RichTextRun with zero length");\r
+                continue;\r
+            }\r
 \r
             at.addAttribute(TextAttribute.FAMILY, rt[i].getFontName(), start, end);\r
             at.addAttribute(TextAttribute.SIZE, new Float(rt[i].getFontSize()), start, end);\r
@@ -86,7 +89,7 @@ public class TextPainter {
         int paragraphStart = it.getBeginIndex();\r
         int paragraphEnd = it.getEndIndex();\r
 \r
-        Rectangle2D anchor = _shape.getAnchor2D();\r
+        Rectangle2D anchor = _shape.getLogicalAnchor2D();\r
 \r
         float textHeight = 0;\r
         ArrayList lines = new ArrayList();\r
@@ -115,7 +118,7 @@ public class TextPainter {
             TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1,\r
                     nextBreak == -1 ? paragraphEnd : nextBreak, true);\r
             if (textLayout == null) {\r
-                textLayout = measurer.nextLayout(wrappingWidth,\r
+                textLayout = measurer.nextLayout((float)anchor.getWidth(),\r
                     nextBreak == -1 ? paragraphEnd : nextBreak, false);\r
             }\r
             if(textLayout == null){\r
@@ -175,9 +178,12 @@ public class TextPainter {
             if(rt.isBullet() && (prStart || startIndex == 0)){\r
                 it.setIndex(startIndex);\r
 \r
-                AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()), it.getAttributes());\r
+                AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()));\r
                 Color clr = rt.getBulletColor();\r
                 if (clr != null) bat.addAttribute(TextAttribute.FOREGROUND, clr);\r
+                else bat.addAttribute(TextAttribute.FOREGROUND, it.getAttribute(TextAttribute.FOREGROUND));\r
+                bat.addAttribute(TextAttribute.FAMILY, it.getAttribute(TextAttribute.FAMILY));\r
+                bat.addAttribute(TextAttribute.SIZE, it.getAttribute(TextAttribute.SIZE));\r
 \r
                 TextLayout bulletLayout = new TextLayout(bat.getIterator(), graphics.getFontRenderContext());\r
                 if(text.substring(startIndex, endIndex).length() > 1){\r