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

0

I can not understand 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 is posString 198 and posChar 207?

    
asked by anonymous 08.06.2017 / 20:31

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 feed of a String is the distance along the baseline of the String. This distance is the width that should be used for centering or aligning to the right of the String.

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

For example, in Arabic text, the shape of a character may change to "connect" to other characters. In addition, in some scripts, certain strings may be represented by a single form, called a ligature. Character measurement alone is not responsible for these transformations.

Source:

FontMetrics Class - Oracle Docs

    
08.06.2017 / 21:33