Font Width?

BlitzMax Forums/BlitzMax Beginners Area/Font Width?

Grey Alien(Posted 2006) [#1]
Probably another no hoper, but is there any way to work out the width of a string when rendered in a certain font. Easy enough for a fixed width font if the char width is known but what about non-fixed width fonts?

At the moment I can't actually load any other bloody fonts yet anyway, so does anyone know the charwidth of the default Max font and if it's fixed width or not? Thanks.


Diablo(Posted 2006) [#2]
Do you mean like this:

Graphics 800, 600

Global test$ = "FTW, HeLlO WoRlD!!!"

While Not KeyDown(KEY_ESCAPE)

	Cls
	
	SetColor 0, 0, 175
	DrawRect 10, 10, TextWidth(test), GetImageFont().height() ' textheight(test)
	SetColor 255, 255, 255
	DrawText test, 10, 10
	
	
	Flip
	
Wend



Grey Alien(Posted 2006) [#3]
what a twat I am, I looked through the 2D help and just must have had a blindspot on that command. Thanks.

btw why use GetImageFont.Height instead of TextHeight(test)? Is it quicker?


Grey Alien(Posted 2006) [#4]
This fails:

	Local temp:TImageFont = New TImageFont
	temp = GetImageFont


with "unable to convert from TImageFont() to TImageFont" wtf! Is it trying to typecast?

Also If I put () on the end of GetImageFont I get "Types Int and Int() are unrelated" Big WTF is going on. help. thanks.


undomiel(Posted 2006) [#5]
Wouldn't you need to execute LoadImageFont first so that GetImageFont would have something to return?


tonyg(Posted 2006) [#6]
For a start you've assigned temp as a TImageFont and now are using it to hold the GetImageFont output.
Second GetImageFont is a function so you need GetImageFont()
In addition, you're creating a TImageFont without any parms.


Diablo(Posted 2006) [#7]
btw why use GetImageFont.Height instead of TextHeight(test)? Is it quicker?


I dont know if its quicker (tho it may be, i havent done any tests, i'm sure some body will have done tho) it just that i dont think textheight works correctly(I havent tried it from version 1.16 upwards), i just put it there so you knew about it.

Wouldn't you need to execute LoadImageFont first so that GetImageFont would have something to return?

I dont think u'd need to, defualt font is set in the graphics call i think.

@Grey Alien
Only thing I can suggest is try this code:
Graphics 800, 600

Local temp:TImageFont = New TImageFont
temp = GetImageFont()

Print temp.height()


which works for me. Make sure u call graphics(). As far as
"Types Int and Int() are unrelated"
- You've got me there.

In addition, you're creating a TImageFont without any parms.

This shouldnt matter as i think he wants to ref the current image font.

If you want to load an image font, simply LoadImageFont:TImageFont(url:Object,size,style=SMOOTHFONT) I dont think it will auto look in the font dir so u will need to copy the font to your apps dir (or were ever you want fonts to be) and load it like any other media i.e. [path][filename]

myfont:TImageFont = LoadImageFont([path][filename], etc.)
if myfont then SetImageFont(myfont) else print "Could not load the font"



Grey Alien(Posted 2006) [#8]
Thanks TonyG:
For a start you've assigned temp as a TImageFont and now are using it to hold the GetImageFont output.
I know, that's what I wanted to do as originally just testing the output of GetImageFont was failing, but this was because I left the brackets off. What I dislike about Max and PLus is sometimes you can leave the brackets off functions and other times not, I don't get when you can and can't. It would be simpler if functions ALWAYS required brackets.
Second GetImageFont is a function so you need GetImageFont()
When I tried this I got that Int error, but later when I tried it, it worked, I can't explain why!
In addition, you're creating a TImageFont without any parms.
just cos I wanted a valid structure in place to assign the return value of GetImageFont to. Originally I tried Local temp:TImageFont = GetImageFont but it was returning an error (turns out it need brackets, oh well).
Wouldn't you need to execute LoadImageFont first so that GetImageFont would have something to return?
From reading the docs, it implied there was already a default font loaded.
i just put it there so you knew about it.
OK, thanks, different options is useful.
Ok thanks diablo, your first code snippet works. Also the int error went away I, can't really explain why... Your second code snippet works if I put "c:\Windows\Fonts" as the path, obviously this isn't so good as it may not be the same on all systems and I should include the font with the data, then it'll work on Macs.

I've got to write more mini test routines to check out how this language works properly.

Thanks everyone for the help.


FlameDuck(Posted 2006) [#9]
sometimes you can leave the brackets off functions and other times not, I don't get when you can and can't.
You could just always have them you know. Anyway to answer your question, you can leave out the final parenthesis when you don't care about return values.

just cos I wanted a valid structure in place to assign the return value of GetImageFont to.
GetImageFont returns a NEW TImageFont object anyway.


Grey Alien(Posted 2006) [#10]
Anyway to answer your question, you can leave out the final parenthesis when you don't care about return values.

Thanks, that explains my problems too. I'll just always use them.
GetImageFont returns a NEW TImageFont object anyway.
yeah, I figured out I didn't need to create one to read the contents of the function into. I only went down that dumb road because I left the brackets off the function name, doh.


xlsior(Posted 2006) [#11]
One heads up: be aware that the results of the height and width commands do NOT take the current scale into consideration.

If you do any scaling, you have to multiply or divide your results by whatever scale you're working in to get an accurate result in pixels.


Grey Alien(Posted 2006) [#12]
again, worth knowing thanks.