Fonts

I have a font which is loaded from a texture. Something like this:

Of course, different characters have different width. If I write them to the screen using squares and mapping each letter’s corresponding square in the texture on that square, then the text will not be equally spaced (the distance between the characters will not be the same).
For example, the distance between ‘Q’ and ‘|’ will be greater than the distance between ‘Q’ and ‘A’.
Is there a way around this “problem”?

either force the distances to be uniform. (each character takes up 16 pixels) or do what I did. create a file that stores the width’s of each character and use that. If you are using windows and the font is generated dynamically then you can use win32 specific calls to get font dimensions.

If you any have a only few works needed for your application you can make texture map’s of whole words like “Score” one texture map, “Start” etc…

You also could make a more complex font routine with data indicating the width of the letter.

a = 5
i = 3

int font_width = { 4, 5, 3, etc.

then just read each charactor based on that data.

Originally posted by Sektor:
[b]I have a font which is loaded from a texture. Something like this:

Of course, different characters have different width. If I write them to the screen using squares and mapping each letter’s corresponding square in the texture on that square, then the text will not be equally spaced (the distance between the characters will not be the same).
For example, the distance between ‘Q’ and ‘|’ will be greater than the distance between ‘Q’ and ‘A’.
Is there a way around this “problem”?[/b]

I thought about this before, but how can I get the width of the characters from the texture?

If you want to determine the width of the character from the texture, and the layout of the characters in the texture is a regular grid, then it is easy. For each cell, scan it looking for the minimum and maximum x positions of set pixels in it. Then the difference of those values would be width of the character. Though you will need to add in some extra padding space to that value to make the text not run too close together.

If you get the font dimensions from Win32, then it is possible to get perfectly formatted text with kerning and all that jazz.

I’d like to take the font dimensions from the texture because I have created some fonts using some effects (and the characters’ sizes changed).

You can do into any good pant program and get the number of pixels between charactors.
May require the use of a piece of paper and just start at the top of the bitmap and work your way down.

One other way would be since we know the size of each charactor box.
On say a 8 x 8 box, the center would be 4.
So say the charactor ‘i’ center would be charactors_index * 5 + 4.
Which gives us the center and if its width is 3 then it becomes center ± 1 on each side.

Hope that makes sense.

Originally posted by Sektor:
I’d like to take the font dimensions from the texture because I have created some fonts using some effects (and the characters’ sizes changed).

First consider using monospaced typefaces.They are simpler to use since you can use a constant advance between fonts.Other than that the only sure way to get things right is to get the metrics from the font itself.You can use freetype to do get the metrics and render the fonts to a texture (have a look at the docs if you’re interested).You can then also make the borders between the glyphs bigger to accomodate for special fx.Of course you’ll have to write your own tool(or find one that suits you,but I doubt you will).Freetype 2 is pretty easy to use though so you might want to give it a shot.