Dimension of an individual character can be calculated using 
java.awt.Font.getStringBounds .
Example:

 FontRenderContext frc= new FontRenderContext(null, true, true);
 Font font = new Font("Arial", Font.ITALIC | Font.BOLD, 10);

 Rectangle2D bounds = font.getStringBounds("POI", frc);

If you have a text with multiple style runs then you will need to use 
java.text.AttributedString.
You can leverage TextPainter.getAttributedString, this method iterates over RichTextRuns in the TextRun and builds AttributedString for you.

Suppose you set the width of a text box to 200px and want to calculate the 
number of lines and total height of the text.
The code below demonstrates how you can do that:


        TextShape textShape = ...;

        TextPainter paineter = new TextPainter(textShape);
        AttributedString str = 
paineter.getAttributedString(textShape.getTextRun());

        AttributedCharacterIterator text = str.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(text, frc);
        float wrappingWidth = (float)textShape.getAnchor2D().getWidth();
        float textHeight = 0;
        while (measurer.getPosition() < text.getEndIndex()) {

            TextLayout layout = measurer.nextLayout(wrappingWidth);

            textHeight += layout.getAscent() + layout.getDescent() + 
layout.getLeading();
        }
        System.out.println(" wrapping width: " + wrappingWidth + ", text height: 
" + textHeight);

Note, the calculation above is very rough and doesn't take into account 
margins, line spacings, etc.
For a more complete example see how TextPainter draws text.

Yegor

Hi Yegor,

Could you pls explain me how to find out how to find out the width and
height of a character  (with some style, font, bold, underlined etc) in a
PPT ?

Regards,
Dinakara


dinshetty wrote:
Hi,

Pls explain why TextRun and RichTextRun are used what does it represent.




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to