Textwidth() and font positioning.

BlitzMax Forums/BlitzMax Beginners Area/Textwidth() and font positioning.

Simon S(Posted 2005) [#1]
Heh, seems like I've constantly been on at you guys for advice recently. I'm glad this forum's such a useful place though.

I was writing a bit of code to some fancy string drawing effects (resize, fly in, fade in one at a time ect....), but hit a bit of a snag.

The textwidth command works great on a whole string, but doesn't get the font positions right doing it one character at a time. Some font's are a bit off, some are all over the place, but none work quite right. I know this is almost certainly down to the font files themselves.

But is there a way to access the font's positioning info? Can't remember the proper name for it, but basically the stuff that tells you where in pixels (or points or whatever it's measured in) each letter should follow on.

I could get around the problem with a laborous readpixel on each letter, but I'm hoping there's a solution that's a bit more direct.


Perturbatio(Posted 2005) [#2]
I think what you might be looking for is kerning.

I don't think blitz has a command for this, but you could probably do it through api calls.


Simon S(Posted 2005) [#3]
Kerining, that sound about right.

Never been near the api before, but that would tie it to one platform unless I wrote a specific version yeah?

I'll see what I can learn about api. I suppose the best solution is to write a bit of code to output a .txt of the font info I can distribute with my code to make things nice and easy for the main code.

Here's a peek at what I have so far.
http://www.zen65317.zen.co.uk/simon/Animfonts.zip

Try it with other font's and you'll see it's very off at points.
(change "font.fmain=LoadImageFont("alex.ttf",40)", and press space to start a string drawing.)

I'm hoping the finished result will be of some use to the community, but first I have to sort these odd positioning problems.

P.S. Bonus points for identifying the quote.


Simon S(Posted 2005) [#4]
Okay, I'm going to need someone to point me in the right direction. I found the command I need

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_5cz6.asp

But I'm at a bit of a loss as how to call it from Blitz. As fonts use the gdi32.dll (apparently) I tried to extern it into blitz, but it's not very happy with that.

ifirstchar and ilastchar are self explanitory. When it says lpbuffer I assume that's a bank or an int to return the value I want yeah?

I have no idea though what hdc (handle to DC) means. "Handle to the device context", does it mean the screen created with the graphics command, and if so what do I pass to it?

Very lost, never been anywhere near the system stuff before.


Yan(Posted 2005) [#5]
You can get the device context using something like this...
Import Pub.Win32

Extern "win32"
  Function GetActiveWindow()
End Extern


Graphics 800, 600, 0

hWnd:Int = GetActiveWindow()
hDC:Int = GetDC(hWnd)



Simon S(Posted 2005) [#6]
Well this is my attempt so far.

Extern "win32"
	Function GetActiveWindow()
	Function GetcharABCwidths(hdc,ifirstchr,ilastchr,lpbuffer:abc)
End Extern

Graphics 800, 600, 0

 
Type abc
	Field abcA:Int
	Field abcB:Int
	Field abcC:Int
End Type

hWnd:Int = GetActiveWindow()
hDC:Int = GetDC(hWnd)

result:abc=New abc
font:timagefont=LoadImageFont("steve.ttf",30)
SetImageFont(font)

num=GetCharABCwidths(hdc,32,32,result)


Which gives me this message
"C:/Work/My Programs/My Utilities/Animfonts/.bmx/getfontwidth.bmx.gui.debug.win32.o(code+0x19b): undefined reference to `GetcharABCwidths@16'"

The whole ABC concept I understand, A and C are the spaces before and after the font, B the actual width. Which I got from the MSDN page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_448i.asp

So close but no cigar. Still stumped by it. I understand what it has to do, but not how to express it in Bmax.


Simon S(Posted 2005) [#7]
Hey I see the update fixed TextHeight, it works perfectly now. Textwidth is better but still not giving me the results I expected.

I'm pretty damn stuck, wasn't able to get the above code working so I'm going to check the max module and see if I can get any clues from that.

Getting slowly closer!


GW(Posted 2005) [#8]
Does it work properly if you dont use a variable width font like Courier or the Blitz font?


Simon S(Posted 2005) [#9]
Yep, works perfectly.