Getting font string char length

Started by RJ, November 27, 2015, 08:35:20

Previous topic - Next topic

RJ

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.

spasi

After each call to stbtt_GetPackedQuad, the xb argument's value is increased by the character advance. So, simply return xb.get(0);