下面列出了java.awt.font.TextLayout#getCharacterCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void test() {
// construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
StringBuffer text = new StringBuffer(MIXED);
for (int i=0; i < NUM_WORDS; i++) {
text.append(SPACING);
text.append(WORD);
}
AttributedString attrString = new AttributedString(text.toString());
attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
DEFAULT_FRC);
// get width of a space-word sequence, in context
int sequenceLength = WORD.length()+SPACING.length();
measurer.setPosition(text.length() - sequenceLength);
TextLayout layout = measurer.nextLayout(10000.0f);
if (layout.getCharacterCount() != sequenceLength) {
throw new Error("layout length is incorrect!");
}
final float sequenceAdvance = layout.getVisibleAdvance();
float wrappingWidth = sequenceAdvance * 2;
// now run test with a variety of widths
while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
measurer.setPosition(0);
checkMeasurer(measurer,
wrappingWidth,
sequenceAdvance,
text.length());
wrappingWidth += sequenceAdvance / 5;
}
}
public TextBoxPaintable(String string, Font font, float maxWidth,
float insets, Paint paint) {
if (paint == null)
paint = SystemColor.textText;
this.insets = insets;
this.text = string;
this.paint = paint;
List<String> lines = new ArrayList<String>();
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
attributes.put(TextAttribute.FONT, font);
AttributedString attrString = new AttributedString(string, attributes);
LineBreakMeasurer lbm = new LineBreakMeasurer(attrString.getIterator(),
frc);
TextLayout tl = lbm.nextLayout(maxWidth);
float dy = 0;
GeneralPath path = new GeneralPath();
int startIndex = 0;
while (tl != null) {
path.append(
tl.getOutline(AffineTransform.getTranslateInstance(0, dy)),
false);
dy += tl.getAscent() + tl.getDescent() + tl.getLeading();
int charCount = tl.getCharacterCount();
lines.add(text.substring(startIndex, startIndex + charCount));
startIndex += charCount;
tl = lbm.nextLayout(maxWidth);
}
this.lines = lines.toArray(new String[lines.size()]);
untransformedShape = path;
Rectangle2D b = ShapeBounds.getBounds(untransformedShape);
b.setFrame(b.getX(), b.getY(), b.getWidth() + 2 * insets, b.getHeight()
+ 2 * insets);
untransformedBounds = b.getBounds();
}
public void test() {
// construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
StringBuffer text = new StringBuffer(MIXED);
for (int i=0; i < NUM_WORDS; i++) {
text.append(SPACING);
text.append(WORD);
}
AttributedString attrString = new AttributedString(text.toString());
attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
DEFAULT_FRC);
// get width of a space-word sequence, in context
int sequenceLength = WORD.length()+SPACING.length();
measurer.setPosition(text.length() - sequenceLength);
TextLayout layout = measurer.nextLayout(10000.0f);
if (layout.getCharacterCount() != sequenceLength) {
throw new Error("layout length is incorrect!");
}
final float sequenceAdvance = layout.getVisibleAdvance();
float wrappingWidth = sequenceAdvance * 2;
// now run test with a variety of widths
while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
measurer.setPosition(0);
checkMeasurer(measurer,
wrappingWidth,
sequenceAdvance,
text.length());
wrappingWidth += sequenceAdvance / 5;
}
}
public static String toStringShort(TextLayout textLayout) {
return "[" + textLayout.getCharacterCount() + "]W=" + textLayout.getAdvance(); // NOI18N
}
protected void drawComplexText( final RenderNode node ) {
final RenderableComplexText renderableComplexText = (RenderableComplexText) node;
// The text node that is printed will overlap with the ellipse we need to print.
if ( renderableComplexText.isNodeVisible( drawArea ) == false ) {
return;
}
if ( renderableComplexText.getRawText().length() == 0 ) {
// This text is empty.
return;
}
final String text;
TextLayout textLayout = renderableComplexText.getTextLayout();
String debugInfo = textLayout.toString();
String startPos =
debugInfo.substring( debugInfo.indexOf( "[start:" ), debugInfo.indexOf( ", len:" ) ).replace( "[start:", "" );
int startPosIntValue = -1;
try {
startPosIntValue = Integer.parseInt( startPos );
} catch ( NumberFormatException e ) {
// do nothing
}
// workaround for line breaking (since the text cannot be extracted directly from textLayout as stream or String)
// in order to avoid duplicates of same source raw text on multiple lines
if ( ( renderableComplexText.getRawText().length() > textLayout.getCharacterCount() ) && startPosIntValue >= 0 ) {
text =
renderableComplexText.getRawText().substring( startPosIntValue,
textLayout.getCharacterCount() + startPosIntValue );
} else {
text = renderableComplexText.getRawText();
}
final int x =
PlainTextPage.correctedDivisionFloor( ( renderableComplexText.getX() - drawArea.getX() ),
characterWidthInMicroPoint );
final int y =
PlainTextPage.correctedDivisionFloor( ( renderableComplexText.getY() - drawArea.getY() ),
characterHeightInMicroPoint );
int w = text.length();
// filter out results that do not belong to the current physical page
if ( x + w > plainTextPage.getWidth() ) {
w = Math.max( 0, plainTextPage.getWidth() - x );
}
if ( w == 0 ) {
return;
}
if ( y < 0 ) {
return;
}
if ( y >= plainTextPage.getHeight() ) {
return;
}
plainTextPage.addTextChunk( x, y, w, text, renderableComplexText.getStyleSheet() );
}