use of org.apache.poi.sl.usermodel.TextParagraph.TextAlign in project poi by apache.
the class DrawTextParagraph method draw.
@Override
public void draw(Graphics2D graphics) {
if (lines.isEmpty()) {
return;
}
double penY = y;
boolean firstLine = true;
int indentLevel = paragraph.getIndentLevel();
Double leftMargin = paragraph.getLeftMargin();
if (leftMargin == null) {
// if the marL attribute is omitted, then a value of 347663 is implied
leftMargin = Units.toPoints(347663L * indentLevel);
}
Double indent = paragraph.getIndent();
if (indent == null) {
indent = Units.toPoints(347663L * indentLevel);
}
if (isHSLF()) {
// special handling for HSLF
indent -= leftMargin;
}
// Double rightMargin = paragraph.getRightMargin();
// if (rightMargin == null) {
// rightMargin = 0d;
// }
//The vertical line spacing
Double spacing = paragraph.getLineSpacing();
if (spacing == null) {
spacing = 100d;
}
for (DrawTextFragment line : lines) {
double penX;
if (firstLine) {
if (!isEmptyParagraph()) {
// TODO: find out character style for empty, but bulleted/numbered lines
bullet = getBullet(graphics, line.getAttributedString().getIterator());
}
if (bullet != null) {
bullet.setPosition(x + leftMargin + indent, penY);
bullet.draw(graphics);
// don't let text overlay the bullet and advance by the bullet width
double bulletWidth = bullet.getLayout().getAdvance() + 1;
penX = x + Math.max(leftMargin, leftMargin + indent + bulletWidth);
} else {
penX = x + leftMargin;
}
} else {
penX = x + leftMargin;
}
Rectangle2D anchor = DrawShape.getAnchor(graphics, paragraph.getParentShape());
// Insets are already applied on DrawTextShape.drawContent
// but (outer) anchor need to be adjusted
Insets2D insets = paragraph.getParentShape().getInsets();
double leftInset = insets.left;
double rightInset = insets.right;
TextAlign ta = paragraph.getTextAlign();
if (ta == null) {
ta = TextAlign.LEFT;
}
switch(ta) {
case CENTER:
penX += (anchor.getWidth() - line.getWidth() - leftInset - rightInset - leftMargin) / 2;
break;
case RIGHT:
penX += (anchor.getWidth() - line.getWidth() - leftInset - rightInset);
break;
default:
break;
}
line.setPosition(penX, penY);
line.draw(graphics);
if (spacing > 0) {
// If linespacing >= 0, then linespacing is a percentage of normal line height.
penY += spacing * 0.01 * line.getHeight();
} else {
// negative value means absolute spacing in points
penY += -spacing;
}
firstLine = false;
}
y = penY - y;
}
use of org.apache.poi.sl.usermodel.TextParagraph.TextAlign in project poi by apache.
the class DrawTextParagraph method breakText.
/**
* break text into lines, each representing a line of text that fits in the wrapping width
*
* @param graphics The drawing context for computing text-lengths.
*/
protected void breakText(Graphics2D graphics) {
lines.clear();
DrawFactory fact = DrawFactory.getInstance(graphics);
StringBuilder text = new StringBuilder();
AttributedString at = getAttributedString(graphics, text);
boolean emptyParagraph = ("".equals(text.toString().trim()));
AttributedCharacterIterator it = at.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext());
for (; ; ) {
int startIndex = measurer.getPosition();
// add a pixel to compensate rounding errors
double wrappingWidth = getWrappingWidth(lines.size() == 0, graphics) + 1;
// shape width can be smaller that the sum of insets (this was proved by a test file)
if (wrappingWidth < 0) {
wrappingWidth = 1;
}
int nextBreak = text.indexOf("\n", startIndex + 1);
if (nextBreak == -1) {
nextBreak = it.getEndIndex();
}
TextLayout layout = measurer.nextLayout((float) wrappingWidth, nextBreak, true);
if (layout == null) {
// layout can be null if the entire word at the current position
// does not fit within the wrapping width. Try with requireNextWord=false.
layout = measurer.nextLayout((float) wrappingWidth, nextBreak, false);
}
if (layout == null) {
// exit if can't break any more
break;
}
int endIndex = measurer.getPosition();
// skip over new line breaks (we paint 'clear' text runs not starting or ending with \n)
if (endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n') {
measurer.setPosition(endIndex + 1);
}
TextAlign hAlign = paragraph.getTextAlign();
if (hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) {
layout = layout.getJustifiedLayout((float) wrappingWidth);
}
AttributedString str = (emptyParagraph) ? // we will not paint empty paragraphs
null : new AttributedString(it, startIndex, endIndex);
DrawTextFragment line = fact.getTextFragment(layout, str);
lines.add(line);
maxLineHeight = Math.max(maxLineHeight, line.getHeight());
if (endIndex == it.getEndIndex()) {
break;
}
}
rawText = text.toString();
}
Aggregations