]> source.dussan.org Git - poi.git/commitdiff
improved rendering of text
authorYegor Kozlov <yegor@apache.org>
Fri, 18 Apr 2008 14:57:07 +0000 (14:57 +0000)
committerYegor Kozlov <yegor@apache.org>
Fri, 18 Apr 2008 14:57:07 +0000 (14:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@649557 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java
src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java

index 837d76b61ce8af02ee955a60ca9b91858cac0ce7..b2eb353439500129644fdfe387ec158a70bd40c8 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.poi.hslf.record.EscherTextboxWrapper;
 import java.util.ArrayList;
 import java.util.List;
 import java.awt.geom.Rectangle2D;
+import java.awt.geom.AffineTransform;
 import java.awt.*;
 
 /**
@@ -114,6 +115,47 @@ public class ShapeGroup extends Shape{
         spgr.setRectY2((anchor.y + anchor.height)*MASTER_DPI/POINT_DPI);
     }
 
+    /**
+     * Sets the coordinate space of this group.  All children are constrained
+     * to these coordinates.
+     *
+     * @param anchor the coordinate space of this group
+     */
+    public void setCoordinates(Rectangle2D anchor){
+        EscherContainerRecord spContainer = (EscherContainerRecord)_escherContainer.getChildRecords().get(0);
+        EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(spContainer, EscherSpgrRecord.RECORD_ID);
+
+        int x1 = (int)Math.round(anchor.getX()*MASTER_DPI/POINT_DPI);
+        int y1 = (int)Math.round(anchor.getY()*MASTER_DPI/POINT_DPI);
+        int x2 = (int)Math.round((anchor.getX() + anchor.getWidth())*MASTER_DPI/POINT_DPI);
+        int y2 = (int)Math.round((anchor.getY() + anchor.getHeight())*MASTER_DPI/POINT_DPI);
+
+        spgr.setRectX1(x1);
+        spgr.setRectY1(y1);
+        spgr.setRectX2(x2);
+        spgr.setRectY2(y2);
+
+    }
+
+    /**
+     * Gets the coordinate space of this group.  All children are constrained
+     * to these coordinates.
+     *
+     * @return the coordinate space of this group
+     */
+    public Rectangle2D getCoordinates(){
+        EscherContainerRecord spContainer = (EscherContainerRecord)_escherContainer.getChildRecords().get(0);
+        EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(spContainer, EscherSpgrRecord.RECORD_ID);
+
+        Rectangle2D.Float anchor = new Rectangle2D.Float();
+        anchor.x = (float)spgr.getRectX1()*POINT_DPI/MASTER_DPI;
+        anchor.y = (float)spgr.getRectY1()*POINT_DPI/MASTER_DPI;
+        anchor.width = (float)(spgr.getRectX2() - spgr.getRectX1())*POINT_DPI/MASTER_DPI;
+        anchor.height = (float)(spgr.getRectY2() - spgr.getRectY1())*POINT_DPI/MASTER_DPI;
+
+        return anchor;
+    }
+
     /**
      * Create a new ShapeGroup and create an instance of <code>EscherSpgrContainer</code> which represents a group of shapes
      */
@@ -191,14 +233,13 @@ public class ShapeGroup extends Shape{
      * @return the anchor of this shape group
      */
     public Rectangle2D getAnchor2D(){
-        EscherContainerRecord groupInfoContainer = (EscherContainerRecord)_escherContainer.getChild(0);
-        EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(groupInfoContainer, EscherSpgrRecord.RECORD_ID);
-        Rectangle2D anchor = new Rectangle2D.Float(
-            (float)spgr.getRectX1()*POINT_DPI/MASTER_DPI,
-            (float)spgr.getRectY1()*POINT_DPI/MASTER_DPI,
-            (float)(spgr.getRectX2() - spgr.getRectX1())*POINT_DPI/MASTER_DPI,
-            (float)(spgr.getRectY2() - spgr.getRectY1())*POINT_DPI/MASTER_DPI
-        );
+        EscherContainerRecord spContainer = (EscherContainerRecord)_escherContainer.getChildRecords().get(0);
+        EscherClientAnchorRecord clientAnchor = (EscherClientAnchorRecord)getEscherChild(spContainer, EscherClientAnchorRecord.RECORD_ID);
+        Rectangle2D.Float anchor = new Rectangle2D.Float();
+        anchor.x = (float)clientAnchor.getCol1()*POINT_DPI/MASTER_DPI;
+        anchor.y = (float)clientAnchor.getFlag()*POINT_DPI/MASTER_DPI;
+        anchor.width = (float)(clientAnchor.getDx1() - clientAnchor.getCol1())*POINT_DPI/MASTER_DPI ;
+        anchor.height = (float)(clientAnchor.getRow1() - clientAnchor.getFlag())*POINT_DPI/MASTER_DPI;
 
         return anchor;
     }
@@ -225,9 +266,25 @@ public class ShapeGroup extends Shape{
     }
 
     public void draw(Graphics2D graphics){
+        Rectangle2D anchor = getAnchor2D();
+        Rectangle2D coords = getCoordinates();
+
+        //transform coordinates
+        AffineTransform at = graphics.getTransform();
+
+        if(!anchor.equals(coords)){
+            graphics.scale(coords.getWidth()/anchor.getWidth(), coords.getHeight()/anchor.getHeight());
+
+            graphics.translate(
+                    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);
         }
+
+        graphics.setTransform(at);
     }
 }
index cfd98bdca9be4bff25053a88a0fb5337f3e08f04..8f31e1f27cb4d83c49076c63168f1cee8f370290 100755 (executable)
@@ -102,7 +102,12 @@ public class TextPainter {
         while (measurer.getPosition() < paragraphEnd) {\r
             int startIndex = measurer.getPosition();\r
             int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);\r
-            RichTextRun rt = getRichTextRunAt(startIndex + 1);\r
+\r
+            int rtIdx = startIndex == 0 ? 0 : startIndex + 1;\r
+            if(startIndex == 0 || startIndex == text.length() - 1) rtIdx = startIndex;\r
+            else rtIdx = startIndex + 1;\r
+\r
+            RichTextRun rt = getRichTextRunAt(rtIdx);\r
             if(rt == null) {\r
                 logger.log(POILogger.WARN,  "RichTextRun not found at pos" + (startIndex + 1) + "; text.length: " + text.length());\r
                 break;\r
@@ -161,11 +166,11 @@ public class TextPainter {
                 }\r
             }\r
 \r
-            textHeight += textLayout.getAscent() + textLayout.getLeading();\r
+            textHeight += textLayout.getAscent() + textLayout.getDescent();\r
 \r
             int lineSpacing = rt.getLineSpacing();\r
-            if(lineSpacing != 0) el._spacing = textLayout.getDescent()*lineSpacing/100;\r
-            else el._spacing = textLayout.getDescent();\r
+            if(lineSpacing != 0) el._spacing = textLayout.getLeading()*lineSpacing/100;\r
+            else el._spacing = textLayout.getLeading();\r
 \r
             textHeight += el._spacing;\r
 \r
index 54cf60c48f7788aa179a548a6e73abbf16fd7068..52d6f1fd0773e4efdf1b45b7b22dd6f051404418 100644 (file)
@@ -495,12 +495,12 @@ public class RichTextRun {
         */
        public Color getFontColor() {
         int rgb = getCharTextPropVal("font.color");
-        if (rgb >= 0x8000000) {
-            int idx = rgb % 0x8000000;
+
+        int cidx = rgb >> 24;
+        if (rgb % 0x1000000 == 0){
             ColorSchemeAtom ca = parentRun.getSheet().getColorScheme();
-            if(idx >= 0 && idx <= 7) rgb = ca.getColor(idx);
+            if(cidx >= 0 && cidx <= 7) rgb = ca.getColor(cidx);
         }
-
         Color tmp = new Color(rgb, true);
         return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
        }