Fixed TextShape.resizeToFitText() to properly resize TextShape. See patch#45140

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@664492 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-06-08 12:32:10 +00:00
parent d2445ee652
commit 4360c3607d

View File

@ -30,6 +30,7 @@ import java.awt.geom.AffineTransform;
import java.awt.font.FontRenderContext; import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout; import java.awt.font.TextLayout;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
/** /**
* A common superclass of all shapes that can hold text. * A common superclass of all shapes that can hold text.
@ -224,26 +225,24 @@ public abstract class TextShape extends SimpleShape {
String fntname = rt.getFontName(); String fntname = rt.getFontName();
Font font = new Font(fntname, style, size); Font font = new Font(fntname, style, size);
float width = 0, height = 0; float width = 0, height = 0, leading = 0;
String[] lines = txt.split("\r"); String[] lines = txt.split("\n");
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {
if(lines[i].length() == 0) continue; if(lines[i].length() == 0) continue;
TextLayout layout = new TextLayout(lines[i], font, _frc); TextLayout layout = new TextLayout(lines[i], font, _frc);
leading = Math.max(leading, layout.getLeading());
width = Math.max(width, layout.getAdvance()); width = Math.max(width, layout.getAdvance());
height = Math.max(height, (height + (layout.getDescent() + layout.getAscent())));
/**
* Even if top and bottom margins are set to 0 PowerPoint
* always sets extra space between the text and its bounding box.
*
* The approximation height = ascent*2 works good enough in most cases
*/
height = Math.max(height, 2*layout.getAscent());
} }
width += getMarginLeft() + getMarginRight(); // add one character to width
height += getMarginTop() + getMarginBottom(); Rectangle2D charBounds = font.getMaxCharBounds(_frc);
width += getMarginLeft() + getMarginRight() + charBounds.getWidth();
// add leading to height
height += getMarginTop() + getMarginBottom() + leading;
Rectangle2D anchor = getAnchor2D(); Rectangle2D anchor = getAnchor2D();
anchor.setRect(anchor.getX(), anchor.getY(), width, height); anchor.setRect(anchor.getX(), anchor.getY(), width, height);
@ -534,4 +533,13 @@ public abstract class TextShape extends SimpleShape {
graphics.setTransform(at); graphics.setTransform(at);
} }
/**
* Return <code>OEPlaceholderAtom</code>, the atom that describes a placeholder.
*
* @return <code>OEPlaceholderAtom</code> or <code>null</code> if not found
*/
public OEPlaceholderAtom getPlaceholderAtom(){
return (OEPlaceholderAtom)getClientDataRecord(RecordTypes.OEPlaceholderAtom.typeID);
}
} }