Change font size of already loaded font

BlitzMax Forums/BlitzMax Beginners Area/Change font size of already loaded font

kronholm(Posted 2006) [#1]
Surely there must be a way. I've looked through the source for the font mods, but have been unable to find an answer.

Anyone know if and how it's possible to change the size of a font that's already loaded?


assari(Posted 2006) [#2]
Have you tried setscale?


kronholm(Posted 2006) [#3]
Yes, and it's not the same thing, I'm talking truetype fonts here, sorry if I didn't mentiond that. Using setscale on a 10-sized font and blowing it up 10 times results in obvious pixelation, whereas loading the same font again, with size 100 is super smooth. I just want to be able to change the font size, without having to re-load it.


wedoe(Posted 2006) [#4]
How about loading 100 and shrink it to 10 when needed ?


kronholm(Posted 2006) [#5]
I take it it's not possible to resize the font on the fly then.


GW(Posted 2006) [#6]
Bmax loads the fonts and creates static images from them.
Your not using fonts, but images.


WendellM(Posted 2006) [#7]
You mentioned not wanting to reload, but after experimentation I've found that gives me the best result - though Wedoe's approach of scaling down from a one-time-loaded, larger font is OK, too. When I need to have variable-sized fonts (like on a scaleable map), I reload the font with every scale change, and the results seem fast enough to me (at least for what I'm doing). Like:

If map.scale <> map.oldscale Then
	Local FontSz# = 11.0 * map.scale
	If FontSz < 1.0 Then FontSz = 1.0 ' lower than 1 crashes
	IFont = LoadImageFont( "graphics/veranda.ttf", FontSz, SMOOTHFONT )
	map.oldscale = map.scale
EndIf
(where map.scale, map.oldscale, and IFont are declared earlier)


kronholm(Posted 2006) [#8]
Wendel that's kind of what I've been doing up until now, but I discovered a memoryleak when doing that. The memory isn't freed when you just do ifont = loadimagefont, it just adds to it. I tried first setting ifont to null and doing gccollect(), but that didn't work either.


tonyg(Posted 2006) [#9]
Can't you use SMOOTHFONT to have mipmapped and filtered fonts? Can you draw with ALPHABLEND?
<edit> Sorry just tried it and even with smoothfont, alphablend and the syncmods for floating point drawtext scaled fonts are HORRIBLY blurred.
This?
Think the solution is a bitmap font module.


WendellM(Posted 2006) [#10]
Thanks for the memory leak mention, kronholm - I'd never have thought to look out for that in this case. However, the test that I just wrote to check it doesn't seem to show a leak:
Strict
AppTitle = "~q+~q for fast testing, ~q-~q for readable text, ~qEsc~q to quit"

Graphics 640,480
SetBlend ALPHABLEND

Local scale#, oldscale#
Local IFont:TImageFont
Local dly = 5
Local loads

Repeat
	Delay dly
	Cls
	scale = Rnd( 1, 4)
	If scale <> oldscale Then
		Local FontSz# = 11.0 * scale
		IFont = LoadImageFont( "veranda.ttf", FontSz, SMOOTHFONT ) ' your .ttf goes here
		If IFont = Null Then Notify( "IFont not loaded", True ) ; End
		loads :+ 1 
		SetImageFont IFont
		oldscale = scale
	EndIf
	DrawText "mem = " + GCMemAlloced() + ", loads = " + loads, 10,10
	Flip
	If KeyDown( KEY_EQUALS ) Then dly = 5 ' + for fast testing
	If KeyDown( KEY_MINUS ) Then dly = 500 ' - to allow reading
Until KeyDown( KEY_ESCAPE )
I left it with auto-garbage collection which I usually use. The memory usage bounces up and down a bit as normal with auto-GC, but it doesn't keep going up and up which would indicate a leak. Do you happen to have an example showing a runaway memory leak when repeatedly reloading fonts?

Update: Hmm, watching memory used in Task Manager tells a whole different story... Keeps going up and up - not good! Looks like there is indeed a leak - good catch. Will you be doing the bug reporting honors, or shall I? :)


kronholm(Posted 2006) [#11]
Here's a quick example.. Just bring up task manager to see it climb to the hundreds within seconds.
Notice I also uncommented gccollect in this case, no matter though, if you uncomment it it still happens..

Graphics 800,600,0
SetBlend ALPHABLEND
Incbin "fonts\amersn_.ttf"

Global font:tfont = New tfont

Type tfont
	Field font:timagefont
	Method size(fontsize=1)
		font = Null
'		GCCollect()
		font = LoadImageFont("incbin::fonts\amersn_.ttf",fontsize,SMOOTHFONT)
		SetImageFont font

	EndMethod
EndType

Repeat
	Cls
	font.size(Rand(20,100))			'random font size
	DrawText "This is a test.. ",0,0
	font.size(1)					'back to default font size for rest of application
	Flip
	'GCCollect()
Until KeyDown(key_escape)



WendellM(Posted 2006) [#12]
Yep, you're right, Dan. I tried uncommenting the GCCollect and adding a GCSetMode 2 at the top, but it still leaks either way. Fiddling around with a couple of other different approaches (like loading the font once outside the loop and reassigning it to itself within the loop) seems to confirm that it's LoadImageFont that has the leak and not some other aspect of font handling/displaying.

I checked both the Max Bugs forum and the Bug Bin, and there's nothing about this in recent history. So, I reported it as a bug, listing your first memleak post here as the starting point. Thanks for discovering this (given long enough runtime, it would have caused memory problems for the very game I'm working on right now), and hopefully BRL can fix it.


kronholm(Posted 2006) [#13]
I just thought it was my screwed up code heh, but it's good to know it's a bug. Thanks for reporting it so I don't have to :)


TomToad(Posted 2006) [#14]
DO you know what sizes the fonts will be in advance or could precalculate the sizes during initialization? You could just create an array of fonts and SetImageFont() whichever size you need.
Strict
Graphics 800,600,32

Local FontArray:TImageFont[10]

For Local t = 1 To 10
	FontArray[t-1] = LoadImageFont("c:\windows\fonts\arial.ttf",t*8)
Next

While Not KeyHit(KEY_ESCAPE)
	Local Line:Int = 0
	
	For Local t = 0 To 9
		SetImageFont(FontArray[t])
		DrawText "Hello World!",0,Line
		line :+ TextHeight("Hello World!") + 2
	Next
	Flip
Wend



kronholm(Posted 2006) [#15]
that is what I've been doing so far, and looks like I wil continue doing, just with the fonts being stored inside a list in my own tfont. But my question was if it was possible to resize them on the fly, and that's been answered; it's not :)


WendellM(Posted 2006) [#16]
( Just to tie things up in this thread, BRL has fixed this. :) http://blitzbasic.com/Community/posts.php?topic=60460#674306 )