I use the Java2D class TextLayoutwith LineBreakMeasurerand AttributedCharacterIteratorto draw a piece of text in a field. The text is wrapped.
Profiling shows me that the code is very slow. Most of the time is lost in the method TextLayout.draw(..).
Does anyone have a suggestion for improving speed?
AttributedCharacterIterator iterator = attribText.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, context);
int i = 1;
while (measurer.getPosition() < iterator.getEndIndex()) {
TextLayout textLayout = measurer.nextLayout(w);
float ascent = textLayout.getAscent();
float descent = textLayout.getDescent();
float leading = textLayout.getLeading();
float size = ascent + descent;
if( i == 1 ) {
if( coverType == CoverType.SPINE ) {
y = (box.height-size)/2;
y -= (size+leading)*(lines-1)/2;
} else if( vAlign == Alignment.Center ) {
y += (h-size)/2-(size+leading)*(lines-1)/2;
} else if( vAlign == Alignment.Bottom ) {
y += (h-size) - (size+leading)*(lines-1);
}
}
y += ascent;
float paintX = x;
switch( hAlign ) {
case Right: {
paintX = x + w - textLayout.getVisibleAdvance();
break;
}
case Center: {
paintX = x + (w - textLayout.getVisibleAdvance())/2;
break;
}
}
textLayout.draw(g2d, paintX, y);
y += descent + leading;
i++;
}
The corresponding code snippet is shown above. attribText- This AttributtedStringis the previously installed. contextis g2d.getFontRenderContext().
source
share