oh my god... how to calculate text length?

BlitzMax Forums/BlitzMax Beginners Area/oh my god... how to calculate text length?

MrCredo(Posted 2005) [#1]
How can i calculate text length in __pixel__? I use Arial-font. I can't see how to do this??? Each char have different image width...

(copy arial.ttf to your prog directory)



Graphics 640,480,0

Local font:TImageFont=LoadImageFont("Arial.ttf",13)
If Not font End

SetImageFont font
SetBlend ALPHABLEND

SetClsColor 255,255,255
Cls

SetColor 0,0,0
DrawText "1234567890",10,10

gettextwidth("123",font)

Flip
WaitKey

Function gettextwidth(text$,font:TImageFont)
For i=0 To text.length-1
Local c=text[i]
Local glyph:TImageGlyph=font._glyphs[c]
Local image:TImage=glyph._image

Print glyph._x+","+glyph._y+","+glyph._w+","+glyph._h+","+glyph._advance '??????? or what
Next
EndFunction


Perturbatio(Posted 2005) [#2]
TextWidth(text$)


MrCredo(Posted 2005) [#3]
*headshot*


xlsior(Posted 2005) [#4]
One catch though:

TextWidth(text$) will return the size of the text *before scaling*. If you use any scaling other than 1,1, you will have to multiple the results of TextWidth with the scaling you applied, to obtain the true size in pixels at which something would be drawn.