Text Kerning / Character Spacing?
-
Hi all,
Is there control over the space between each character when using drawText in a paint routine?
Thanks!
-
@ericchesek Use
g.setFontWithSpacing()
-
@d-healey Excellent! Now if I want to get the width of that text, getStringWidth doesn't work anymore. Is there an alternative for that?
-
@ericchesek
g.getStringWidth()
? -
@d-healey Are you suggesting to keep using that, or asking for clarification? I am using g.getStringWidth("TEXT"); after setting the font using g.setFontWithSpacing(). The value of g.getStringWidth stays the same as if the spacing was set to 0.
-
@ericchesek I was asking for clarification because there is also
Engine.getStringWidth()
- don't know if that will work... -
Actually
g.getStringWidth()
works hereHiseSnippet 862.3ocsV01aSCCD1taAQKuHPhe.Q8SYRSUsvXLIDhw5KPEcqkkwFeC4k3lXMG6nXmsUglD+j4e.bNN8M1z1nRjOTk6dty9wmetKcTlLfpTxLDt5QSRoH7ic7mHzwsiILApeGD9oN6STZZlq00dSRIJEMDgwq8QiCb00QEO+586Q3DQ.ctKD5XIKfNfkvzy8NZ2Oy37djP5QrjEhdqc6GHEskbYNvm0bZhRIAmQhnGPLgUwAgeP2PlVl4qIZpBhYOY3D+X4EBa7GyTrS4TiQKjOrPV2n1wLd3nomUEBgWez7S9Z1S9Kb1mExl4edE3YE.tyyXwZ.txsQoV+CTBu.kV2Rom63GjwR0yQL74QN8EvExXBTpWjJ1XQU7q3zVBQHzMRHmQ6kAFyxva6lM2zE9Yi2VqFTtUZ2yIYtiHBJuk66bmlYDU2VljJEfgWcKbcSN1Wann5QPARenLWyDTuw4h.MSJ7h1n1OpU0rlDX4zwLkYsFHCH78j4hPkWwVWMpwXPE7AN2ydiqZbJGttAL.RY1ci2YfWDyzTCXIbOfmmvzw9fFgIh7pO7xIQTQ8McasyltMa7Z6BElQt3H5kvQ3ScGLXn6ICObPGHHhAFNqJIm1HMCNHdQFd5qg2iNgEpiWNkMr688Z2a0n4pr7WswamVEckhCjZ5PgWQ0r1U0b+anwiuQLy8WljyoY2HroiK61RzSjmbJMaSPUvyoyBDjjKqyev8SmGXkSKDnTzWvzCSok18j7Pi907906JPk5Q3su1uCQSLMJk9f3RoYZlgN3NzygIM11lpNcnpyzxzhXKEw.k0EnOYZSkQHiXvV+PGqnFc47gQ82cxLie9kcuvbkgvN3swHTLkEEqMV0wl4YWquElXHCy4D8xiQLyKKAfZ9R8tl9Sghomr37z+gYKMu0YK2WJ9bmQLcP7MywJ2.Ggai+Gbrbh7Sb5NdLMPOmfq6z6aq5326X6sCxh1m.snfNv4f7DeXxS.E1cAnNTl66JFEo0tow1TA7ohvBieCOkfsL13RvVSAQIjfL42CrcZlY9OrvCvIQwm3pBeqErcagJ59VrNm.eB56AAKuTWKwWtpI9pUMwsV0De8pl31qZhuYUSbm6NQy+P3C4ZYhssAg1eT2hwUXbWAATfEpUze.k7gnlA
-
@d-healey Oops. You are right. Something else must be wrong in my code. Thanks!
I'm off to debug. :) -
I see the source of my confusion.
Usingg.setFontWithSpacing(font, size, spacing);
results in a length ofspacing
at the end of the string.For example, if I write
g.getStringWidth("TEXT");
I expect TEXT's width and spacing (shown with underscores) to be calculated as T_E_X_T.
Instead, it is calculated as T_E_X_T_.So using
g.drawAlignedText("TEXT", [0, 0, this.getWidth(), this.getHeight()], "centred");
doesn't actually center the text due to that kerning offset. -
Might anyone know how to calculate that extra bit of kerning so I can account for it?
-
@ericchesek Perhaps calculate the width of 1 space separately and deduct it from the total width?
-
@d-healey Hmm this might work for monospace fonts, but I get different results based on getting the string width of a space or any character. I don't see why the kerning adds a space at the end of a string. I don't think this is common.
Maybe Christoph can provide some insight? @Christoph-Hart