From b3ad61e1e9b510326740f16a47ed4ceb7b4cc30c Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Fri, 18 Apr 2008 14:57:07 +0000 Subject: [PATCH] improved rendering of text git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@649557 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hslf/model/ShapeGroup.java | 73 +++++++++++++++++-- .../apache/poi/hslf/model/TextPainter.java | 13 +++- .../poi/hslf/usermodel/RichTextRun.java | 8 +- 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java b/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java index 837d76b61c..b2eb353439 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java @@ -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 EscherSpgrContainer 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); } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java index cfd98bdca9..8f31e1f27c 100755 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java @@ -102,7 +102,12 @@ public class TextPainter { 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 @@ public class TextPainter { } } - 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; diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java index 54cf60c48f..52d6f1fd07 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java @@ -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()); } -- 2.39.5