You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2008/04/18 16:57:10 UTC

svn commit: r649557 - in /poi/trunk/src/scratchpad/src/org/apache/poi/hslf: model/ShapeGroup.java model/TextPainter.java usermodel/RichTextRun.java

Author: yegor
Date: Fri Apr 18 07:57:07 2008
New Revision: 649557

URL: http://svn.apache.org/viewvc?rev=649557&view=rev
Log:
improved rendering of text

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

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java?rev=649557&r1=649556&r2=649557&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java Fri Apr 18 07:57:07 2008
@@ -24,6 +24,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.awt.geom.Rectangle2D;
+import java.awt.geom.AffineTransform;
 import java.awt.*;
 
 /**
@@ -115,6 +116,47 @@
     }
 
     /**
+     * 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
      */
     protected EscherContainerRecord createSpContainer(boolean isChild) {
@@ -191,14 +233,13 @@
      * @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 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);
     }
 }

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java?rev=649557&r1=649556&r2=649557&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java Fri Apr 18 07:57:07 2008
@@ -102,7 +102,12 @@
         while (measurer.getPosition() < paragraphEnd) {
             int startIndex = measurer.getPosition();
             int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);
-            RichTextRun rt = getRichTextRunAt(startIndex + 1);
+
+            int rtIdx = startIndex == 0 ? 0 : startIndex + 1;
+            if(startIndex == 0 || startIndex == text.length() - 1) rtIdx = startIndex;
+            else rtIdx = startIndex + 1;
+
+            RichTextRun rt = getRichTextRunAt(rtIdx);
             if(rt == null) {
                 logger.log(POILogger.WARN,  "RichTextRun not found at pos" + (startIndex + 1) + "; text.length: " + text.length());
                 break;
@@ -161,11 +166,11 @@
                 }
             }
 
-            textHeight += textLayout.getAscent() + textLayout.getLeading();
+            textHeight += textLayout.getAscent() + textLayout.getDescent();
 
             int lineSpacing = rt.getLineSpacing();
-            if(lineSpacing != 0) el._spacing = textLayout.getDescent()*lineSpacing/100;
-            else el._spacing = textLayout.getDescent();
+            if(lineSpacing != 0) el._spacing = textLayout.getLeading()*lineSpacing/100;
+            else el._spacing = textLayout.getLeading();
 
             textHeight += el._spacing;
 

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java?rev=649557&r1=649556&r2=649557&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java Fri Apr 18 07:57:07 2008
@@ -495,12 +495,12 @@
 	 */
 	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());
 	}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org