Using non-installed fonts with MaxGUI

BlitzMax Forums/BlitzMax Programming/Using non-installed fonts with MaxGUI

WendellM(Posted 2006) [#1]
I'm finally able to do something that I've been wanting for some time: use fonts that aren't installed in Windows with BlitzMax GUI gadgets. It turned out to be simple once the magic win32 commands were working:

Strict

Extern "win32"
	Function AddFontResourceA:Int (lpFileName$z)
	Function RemoveFontResourceA:Int (lpFileName$z)
End Extern

Local window:TGadget = CreateWindow( "Temp Font Test", 10,30, 200,200, Null,..
                                     WINDOW_TITLEBAR |WINDOW_CLIENTCOORDS )
Local txt:TGadget = CreateTextArea( 0,0, 200,200, window, TEXTAREA_WORDWRAP )

Local path$ = ""
Local font$ = "example"
AddFontResourceA( path + font + ".ttf" )
Local fontHandle:TGuiFont = LoadGuiFont( font, 18 )
Print fonthandle.name
Assert Lower(fontHandle.name) = Lower(font), "Font not loaded"

SetGadgetFont txt, fontHandle
SetTextAreaText txt, "Pack my box with five dozen liquor jugs."

Repeat
	WaitEvent
Until EventID() = EVENT_WINDOWCLOSE

RemoveFontResourceA( path + font + ".ttf" )
Replace "example" with the name of the non-installed font that you want. If it needs a path/folder prefix, set path$. For simplicity, I always rename the .ttf file to the actual font name (which is what's returned by fontHandle.name) and use that in the Assert check.

I check what LoadFont returns rather than what's returned by AddFontResourceA since LoadFont will sometimes load a font other than the one specified if it can't find it. That is, 'LoadFont( "NoSuchFontqwerty", 12 )' will quietly load something else (like Arial or MS Sans Serif) and return it rather than returning Null. So, I've gotten into the habit of checking the returned font's name.

Anyway, I'm sure that everyone has their own preferred ways of handling the details. But the main thing is that it's not necessary to install fonts before using them with MaxGUI. It doesn't seem to work with Incbin-ed fonts, but it does work with MoleBox.