Java Code Examples for java.awt.font.TextLayout#getCharacterCount()

The following examples show how to use java.awt.font.TextLayout#getCharacterCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestLineBreakWithFontSub.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
        }
    }
 
Example 2
Source File: TextBoxPaintable.java    From pumpernickel with MIT License 5 votes vote down vote up
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();
}
 
Example 3
Source File: TestLineBreakWithFontSub.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
        }
    }
 
Example 4
Source File: TextLayoutUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static String toStringShort(TextLayout textLayout) {
    return "[" + textLayout.getCharacterCount() + "]W=" + textLayout.getAdvance(); // NOI18N
}
 
Example 5
Source File: TextDocumentWriter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
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() );
}