I am using TextLayout to measure the bounds of a text string. In this measurement I need to include any trailing whitespace. However it seems that by default, TextLayout ignores any trailing whitespace when computing text bounds. Here’s an example:
import java.awt.geom.*;
import java.awt.font.*;
import java.text.*;
public class scratch
{
public static double measureTextWidth(String text)
{
AttributedString astring = new AttributedString(text);
AffineTransform at = new AffineTransform();
FontRenderContext frc = new FontRenderContext(at, true, true);
TextLayout layout = new TextLayout(astring.getIterator(), frc);
return layout.getBounds().getWidth();
}
public static void main(String args[])
{
System.out.println(measureTextWidth("sometext"));
System.out.println(measureTextWidth("sometext "));
}
}
This code returns the same value for both strings, even though the second one includes some trailing whitespace.
Is there any way to have TextLayout consider trailing whitespace when computing the width of the supplied text string?