int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);
boolean prStart = text.charAt(startIndex) == '\n';
if(prStart) measurer.setPosition(startIndex++);
RichTextRun rt = run.getRichTextRunAt(startIndex == text.length() ? (startIndex-1) : startIndex);
if(rt == null) {
logger.log(POILogger.WARN, "RichTextRun not found at pos" + startIndex + "; text.length: " + text.length());
break;
}
float wrappingWidth = (float)anchor.getWidth() - _shape.getMarginLeft() - _shape.getMarginRight();
wrappingWidth -= rt.getTextOffset();
if (_shape.getWordWrap() == TextShape.WrapNone) {
wrappingWidth = _shape.getSheet().getSlideShow().getPageSize().width;
}
TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1,
nextBreak == -1 ? paragraphEnd : nextBreak, true);
if (textLayout == null) {
textLayout = measurer.nextLayout((float)anchor.getWidth(),
nextBreak == -1 ? paragraphEnd : nextBreak, false);
}
if(textLayout == null){
logger.log(POILogger.WARN, "Failed to break text into lines: wrappingWidth: "+wrappingWidth+
"; text: " + rt.getText());
measurer.setPosition(rt.getEndIndex());
continue;
}
int endIndex = measurer.getPosition();
float lineHeight = (float)textLayout.getBounds().getHeight();
int linespacing = rt.getLineSpacing();
if(linespacing == 0) linespacing = 100;
TextElement el = new TextElement();
if(linespacing >= 0){
el.ascent = textLayout.getAscent()*linespacing/100;
} else {
el.ascent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI;
}
el._align = rt.getAlignment();
el._text = textLayout;
el._textOffset = rt.getTextOffset();
if (prStart){
int sp = rt.getSpaceBefore();
float spaceBefore;
if(sp >= 0){
spaceBefore = lineHeight * sp/100;
} else {
spaceBefore = -sp*Shape.POINT_DPI/Shape.MASTER_DPI;
}
el.ascent += spaceBefore;
}
float descent;
if(linespacing >= 0){
descent = (textLayout.getDescent() + textLayout.getLeading())*linespacing/100;
} else {
descent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI;
}
if (prStart){
int sp = rt.getSpaceAfter();
float spaceAfter;
if(sp >= 0){
spaceAfter = lineHeight * sp/100;
} else {
spaceAfter = -sp*Shape.POINT_DPI/Shape.MASTER_DPI;
}
el.ascent += spaceAfter;
}
el.descent = descent;
textHeight += el.ascent + el.descent;
if(rt.isBullet() && (prStart || startIndex == 0)){
it.setIndex(startIndex);
AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()));
Color clr = rt.getBulletColor();
if (clr != null) bat.addAttribute(TextAttribute.FOREGROUND, clr);
else bat.addAttribute(TextAttribute.FOREGROUND, it.getAttribute(TextAttribute.FOREGROUND));
bat.addAttribute(TextAttribute.FAMILY, it.getAttribute(TextAttribute.FAMILY));
bat.addAttribute(TextAttribute.SIZE, it.getAttribute(TextAttribute.SIZE));
TextLayout bulletLayout = new TextLayout(bat.getIterator(), graphics.getFontRenderContext());
if(text.substring(startIndex, endIndex).length() > 1){
el._bullet = bulletLayout;
el._bulletOffset = rt.getBulletOffset();
}
}
lines.add(el);
}