misc improvements in slide rendering

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@649798 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-04-19 11:17:37 +00:00
parent 4ff1e84247
commit 6e96462b0e
3 changed files with 59 additions and 27 deletions

View File

@ -273,7 +273,7 @@ public class ShapeGroup extends Shape{
AffineTransform at = graphics.getTransform(); AffineTransform at = graphics.getTransform();
if(!anchor.equals(coords)){ 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( graphics.translate(
anchor.getX()*coords.getWidth()/anchor.getWidth() - coords.getX(), anchor.getX()*coords.getWidth()/anchor.getWidth() - coords.getX(),

View File

@ -103,13 +103,12 @@ public class TextPainter {
int startIndex = measurer.getPosition(); int startIndex = measurer.getPosition();
int nextBreak = text.indexOf('\n', measurer.getPosition() + 1); int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);
int rtIdx = startIndex == 0 ? 0 : startIndex + 1; boolean prStart = text.charAt(startIndex) == '\n';
if(startIndex == 0 || startIndex == text.length() - 1) rtIdx = startIndex; if(prStart) measurer.setPosition(startIndex++);
else rtIdx = startIndex + 1;
RichTextRun rt = getRichTextRunAt(rtIdx); RichTextRun rt = getRichTextRunAt(startIndex);
if(rt == null) { 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; break;
} }
@ -135,21 +134,24 @@ public class TextPainter {
int endIndex = measurer.getPosition(); int endIndex = measurer.getPosition();
TextElement el = new TextElement(); TextElement el = new TextElement();
el.ascent = textLayout.getAscent();
el._startIndex = startIndex; el._startIndex = startIndex;
el._endIndex = endIndex; el._endIndex = endIndex;
el._align = rt.getAlignment(); el._align = rt.getAlignment();
el._text = textLayout; el._text = textLayout;
el._textOffset = rt.getTextOffset(); el._textOffset = rt.getTextOffset();
boolean prStart = text.charAt(startIndex) == '\n' || startIndex == 0; textHeight += textLayout.getAscent();
if (text.charAt(startIndex) == '\n'){ if (prStart || startIndex == 0){
int spaceBefore = rt.getSpaceBefore(); int spaceBefore = rt.getSpaceBefore();
if (spaceBefore != 0) { if (spaceBefore != 0) {
float val = (textLayout.getAscent() + textLayout.getDescent()) * spaceBefore/100; float val = (float)(textLayout.getAscent() + textLayout.getDescent())* spaceBefore/100;
textHeight += val; textHeight += val;
el.ascent += val;
} }
} }
if(rt.isBullet() && prStart){
if(rt.isBullet() && (prStart || startIndex == 0)){
it.setIndex(startIndex); it.setIndex(startIndex);
AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()), it.getAttributes()); 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(); int lineSpacing = rt.getLineSpacing();
if(lineSpacing != 0) el._spacing = textLayout.getLeading()*lineSpacing/100; if(lineSpacing != 0) descent += textLayout.getLeading()*lineSpacing/100;
else el._spacing = textLayout.getLeading(); else descent = textLayout.getLeading();
textHeight += descent;
textHeight += el._spacing; 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); lines.add(el);
} }
@ -180,15 +191,15 @@ public class TextPainter {
int valign = _shape.getVerticalAlignment(); int valign = _shape.getVerticalAlignment();
double y0 = anchor.getY(); double y0 = anchor.getY();
switch (valign){ switch (valign){
case TextBox.AnchorTopBaseline: case TextShape.AnchorTopBaseline:
case TextBox.AnchorTop: case TextShape.AnchorTop:
y0 += _shape.getMarginTop(); y0 += _shape.getMarginTop();
break; break;
case TextBox.AnchorBottom: case TextShape.AnchorBottom:
y0 += anchor.getHeight() - textHeight - _shape.getMarginBottom(); y0 += anchor.getHeight() - textHeight - _shape.getMarginBottom();
break; break;
default: default:
case TextBox.AnchorMiddle: case TextShape.AnchorMiddle:
float delta = (float)anchor.getHeight() - textHeight - _shape.getMarginTop() - _shape.getMarginBottom(); float delta = (float)anchor.getHeight() - textHeight - _shape.getMarginTop() - _shape.getMarginBottom();
y0 += _shape.getMarginTop() + delta/2; y0 += _shape.getMarginTop() + delta/2;
break; break;
@ -197,11 +208,12 @@ public class TextPainter {
//finally draw the text fragments //finally draw the text fragments
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
TextElement elem = (TextElement)lines.get(i); TextElement elem = (TextElement)lines.get(i);
y0 += elem._text.getAscent(); y0 += elem.ascent;
Point2D.Double pen = new Point2D.Double(); Point2D.Double pen = new Point2D.Double();
pen.y = y0; pen.y = y0;
switch (elem._align) { switch (elem._align) {
default:
case TextShape.AlignLeft: case TextShape.AlignLeft:
pen.x = anchor.getX() + _shape.getMarginLeft(); pen.x = anchor.getX() + _shape.getMarginLeft();
break; break;
@ -213,24 +225,18 @@ public class TextPainter {
pen.x = anchor.getX() + _shape.getMarginLeft() + pen.x = anchor.getX() + _shape.getMarginLeft() +
(anchor.getWidth() - elem._text.getAdvance() - _shape.getMarginLeft() - _shape.getMarginRight()); (anchor.getWidth() - elem._text.getAdvance() - _shape.getMarginLeft() - _shape.getMarginRight());
break; break;
default:
pen.x = anchor.getX() + _shape.getMarginLeft();
break;
} }
if(elem._bullet != null){ if(elem._bullet != null){
elem._bullet.draw(graphics, (float)(pen.x + elem._bulletOffset), (float)pen.y); elem._bullet.draw(graphics, (float)(pen.x + elem._bulletOffset), (float)pen.y);
} }
elem._text.draw(graphics, (float)(pen.x + elem._textOffset), (float)pen.y); elem._text.draw(graphics, (float)(pen.x + elem._textOffset), (float)pen.y);
y0 += elem._text.getDescent(); y0 += elem.descent;
y0 += elem._text.getLeading();
y0 += elem._spacing;
} }
} }
public static class TextElement { static class TextElement {
public TextLayout _text; public TextLayout _text;
public int _textOffset; public int _textOffset;
public TextLayout _bullet; public TextLayout _bullet;
@ -239,5 +245,6 @@ public class TextPainter {
public int _startIndex; public int _startIndex;
public int _endIndex; public int _endIndex;
public float _spacing; public float _spacing;
public float ascent, descent;
} }
} }

View File

@ -716,6 +716,31 @@ public class RichTextRun {
int val = getParaTextPropVal("spacebefore"); int val = getParaTextPropVal("spacebefore");
return val == -1 ? 0 : val; return val == -1 ? 0 : val;
} }
/**
* Sets spacing after a paragraph.
* <p>
* 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.
* </p>
*/
public void setSpaceAfter(int val) {
setParaTextPropVal("spaceafter", val);
}
/**
* Returns spacing after a paragraph
* <p>
* 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.
* </p>
*
* @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! ------- // --------------- Internal HSLF methods, not intended for end-user use! -------
/** /**