LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: RJ on November 27, 2015, 08:35:20

Title: Getting font string char length
Post by: RJ on November 27, 2015, 08:35:20
Does STBTrueType have a method that could return the width of the input string?

public int getDimensions(String text, int size) {
text.replace (" ", "  ");
float[] dimensions = new float[2];
xb.put(0, 0);
yb.put(0, 0);
chardata.position(size * 128);
for (int i = 0; i < text.length(); i++) {
stbtt_GetPackedQuad(chardata, BITMAP_W, BITMAP_H, text.charAt(i), xb, yb, stbttQuad, 0);
dimensions[i == 0 ? 0 : 1] = i == 0 ? stbttQuad.getX0() : stbttQuad.getX1();
}
return text.length () == 1 ? (int) (dimensions [1] - dimensions[0]) * 2 : (int) (dimensions [1] - dimensions[0]);
}


The above code doesn't return the correct length even if I add things like multiplying spaces, and string 2 or <. I've looked through the class and saw a few method for getting the horizontal metrics but it didn't work as well.
Title: Re: Getting font string char length
Post by: spasi on November 27, 2015, 12:22:25
After each call to stbtt_GetPackedQuad, the xb argument's value is increased by the character advance. So, simply return xb.get(0);