From 54587d138acd98d27090dae2e63dca1f85a751c3 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 19 Apr 2008 11:17:37 +0000 Subject: [PATCH] misc improvements in slide rendering git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@649798 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hslf/model/ShapeGroup.java | 2 +- .../apache/poi/hslf/model/TextPainter.java | 61 +++++++++++-------- .../poi/hslf/usermodel/RichTextRun.java | 25 ++++++++ 3 files changed, 60 insertions(+), 28 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 b2eb353439..2c146817ed 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java @@ -273,7 +273,7 @@ public class ShapeGroup extends Shape{ AffineTransform at = graphics.getTransform(); if(!anchor.equals(coords)){ - graphics.scale(coords.getWidth()/anchor.getWidth(), coords.getHeight()/anchor.getHeight()); + graphics.scale(anchor.getWidth()/coords.getWidth(), anchor.getHeight()/coords.getHeight()); graphics.translate( anchor.getX()*coords.getWidth()/anchor.getWidth() - coords.getX(), 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 8f31e1f27c..c4eac3d066 100755 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java @@ -103,13 +103,12 @@ public class TextPainter { int startIndex = measurer.getPosition(); int nextBreak = text.indexOf('\n', measurer.getPosition() + 1); - int rtIdx = startIndex == 0 ? 0 : startIndex + 1; - if(startIndex == 0 || startIndex == text.length() - 1) rtIdx = startIndex; - else rtIdx = startIndex + 1; + boolean prStart = text.charAt(startIndex) == '\n'; + if(prStart) measurer.setPosition(startIndex++); - RichTextRun rt = getRichTextRunAt(rtIdx); + RichTextRun rt = getRichTextRunAt(startIndex); if(rt == null) { - logger.log(POILogger.WARN, "RichTextRun not found at pos" + (startIndex + 1) + "; text.length: " + text.length()); + logger.log(POILogger.WARN, "RichTextRun not found at pos" + startIndex + "; text.length: " + text.length()); break; } @@ -135,21 +134,24 @@ public class TextPainter { int endIndex = measurer.getPosition(); TextElement el = new TextElement(); + el.ascent = textLayout.getAscent(); el._startIndex = startIndex; el._endIndex = endIndex; el._align = rt.getAlignment(); el._text = textLayout; el._textOffset = rt.getTextOffset(); - boolean prStart = text.charAt(startIndex) == '\n' || startIndex == 0; - if (text.charAt(startIndex) == '\n'){ + textHeight += textLayout.getAscent(); + if (prStart || startIndex == 0){ int spaceBefore = rt.getSpaceBefore(); if (spaceBefore != 0) { - float val = (textLayout.getAscent() + textLayout.getDescent()) * spaceBefore/100; + float val = (float)(textLayout.getAscent() + textLayout.getDescent())* spaceBefore/100; textHeight += val; + el.ascent += val; } } - if(rt.isBullet() && prStart){ + + if(rt.isBullet() && (prStart || startIndex == 0)){ it.setIndex(startIndex); AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()), it.getAttributes()); @@ -166,13 +168,22 @@ public class TextPainter { } } - textHeight += textLayout.getAscent() + textLayout.getDescent(); + float descent = textLayout.getDescent(); int lineSpacing = rt.getLineSpacing(); - if(lineSpacing != 0) el._spacing = textLayout.getLeading()*lineSpacing/100; - else el._spacing = textLayout.getLeading(); - - textHeight += el._spacing; + if(lineSpacing != 0) descent += textLayout.getLeading()*lineSpacing/100; + else descent = textLayout.getLeading(); + textHeight += descent; + + el.descent = descent; + if (prStart){ + int spaceAfter = rt.getSpaceAfter(); + if (spaceAfter != 0) { + float val = (float)(textLayout.getAscent() + textLayout.getDescent())* spaceAfter/100; + textHeight += val; + el.descent += val; + } + } lines.add(el); } @@ -180,15 +191,15 @@ public class TextPainter { int valign = _shape.getVerticalAlignment(); double y0 = anchor.getY(); switch (valign){ - case TextBox.AnchorTopBaseline: - case TextBox.AnchorTop: + case TextShape.AnchorTopBaseline: + case TextShape.AnchorTop: y0 += _shape.getMarginTop(); break; - case TextBox.AnchorBottom: + case TextShape.AnchorBottom: y0 += anchor.getHeight() - textHeight - _shape.getMarginBottom(); break; default: - case TextBox.AnchorMiddle: + case TextShape.AnchorMiddle: float delta = (float)anchor.getHeight() - textHeight - _shape.getMarginTop() - _shape.getMarginBottom(); y0 += _shape.getMarginTop() + delta/2; break; @@ -197,11 +208,12 @@ public class TextPainter { //finally draw the text fragments for (int i = 0; i < lines.size(); i++) { TextElement elem = (TextElement)lines.get(i); - y0 += elem._text.getAscent(); + y0 += elem.ascent; Point2D.Double pen = new Point2D.Double(); pen.y = y0; switch (elem._align) { + default: case TextShape.AlignLeft: pen.x = anchor.getX() + _shape.getMarginLeft(); break; @@ -213,24 +225,18 @@ public class TextPainter { pen.x = anchor.getX() + _shape.getMarginLeft() + (anchor.getWidth() - elem._text.getAdvance() - _shape.getMarginLeft() - _shape.getMarginRight()); break; - default: - pen.x = anchor.getX() + _shape.getMarginLeft(); - break; } if(elem._bullet != null){ elem._bullet.draw(graphics, (float)(pen.x + elem._bulletOffset), (float)pen.y); } elem._text.draw(graphics, (float)(pen.x + elem._textOffset), (float)pen.y); - y0 += elem._text.getDescent(); - y0 += elem._text.getLeading(); - - y0 += elem._spacing; + y0 += elem.descent; } } - public static class TextElement { + static class TextElement { public TextLayout _text; public int _textOffset; public TextLayout _bullet; @@ -239,5 +245,6 @@ public class TextPainter { public int _startIndex; public int _endIndex; public float _spacing; + public float ascent, descent; } } 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 52d6f1fd07..c58979757e 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java @@ -716,6 +716,31 @@ public class RichTextRun { int val = getParaTextPropVal("spacebefore"); return val == -1 ? 0 : val; } + + /** + * Sets spacing after a paragraph. + *

+ * If spaceafter >= 0, then spaceafter is a percentage of normal line height. + * If spaceafter < 0, the absolute value of spaceafter is the spacing in master coordinates. + *

+ */ + public void setSpaceAfter(int val) { + setParaTextPropVal("spaceafter", val); + } + + /** + * Returns spacing after a paragraph + *

+ * If spaceafter >= 0, then spaceafter is a percentage of normal line height. + * If spaceafter < 0, the absolute value of spaceafter is the spacing in master coordinates. + *

+ * + * @return the spacing before a paragraph + */ + public int getSpaceAfter() { + int val = getParaTextPropVal("spaceafter"); + return val == -1 ? 0 : val; + } // --------------- Internal HSLF methods, not intended for end-user use! ------- /** -- 2.39.5