Difference in string size with Fontmetrics.charWidth and Fontmetrics.stringWidth

Asked

Viewed 44 times

0

I’m not getting the following code:

String text = "C JEA NAV/NAP GRV NAV/ELT NAV SOLA PVC EXP PRETO ";    
int posString = FONT_METRICS.stringWidth(text);
int posChar = 0;
for (int i = 0; i < text.length(); i++) {
     Character character = text.charAt(i);
     posChar += FONT_METRICS.charWidth(character);
}
System.out.println("String: " + posString + " Char: " + posChar);

Why posString is 198 and posChar is 207?

1 answer

0


charWidth(char ch)
Returns the advance width of the specified character in this Font.

stringWidth(String str)
Returns the total advance width for showing the specified String in this Font.

The advance of a String is the distance along the String’s baseline. This distance is the width that should be used for centering or alignment to the right of the string.

Note that the advance of a String is not necessarily the sum of the advances of its individually measured characters because the width of a character may vary according to its context.

For example, in Arabic text, the form of a character can change to "connect" to other characters. Also, in some scripts, certain strings of characters can be represented by a single form, called a ligature. Individual character measurement is not responsible for these transformations.

FontMetrics

Source:

Class Fontmetrics - Docs Oracle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.