Font usage

Started by
0 comments, last by Aardvajk 17 years, 4 months ago
i am aware of the fact that there are enough Tutorials for it , but am looking for a Tutorial that explien from scratch how to use font from a bitmap file... for win32 programming :) - perheps writing the code for it real quick will help as well. thanks in advance :)
Advertisement
Do you mean having a font stored as a bitmap strip with each letter running along the bitmap?

If not, ignore the below.

I don't know of any tutorials on this subject, but the principle is quite simple. The easiest way to approach this is to use a non-proportional font (all letters are the same width).

All you would do is have one long bitmap with each letter arranged in a certain order along the bitmap. For simplicity, lets assume you just want capital letters and numbers.

So your bitmap would look like:

ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

Most graphics APIs provide a way to draw onto a destination surface from a source surface, specifying a source rectangle. Assuming you know how to do this, creating a font engine is just a question of being able to map an ASCII or UNICODE character code to a particular source rectangle.

I've just implemented a sort of bitmap font system for Direct3D. The way I handled the mapping was to create a 256 array of ints, and in each element of this array, put the index of the letter that corresponds to the bitmap representing the letter.

So in slot 65 (capital A in ASCII), I had index 0. In slot 66, index 1 and so on. I actually have this array stored as part of the font file but if you are using a simple bitmap for the letters you would need to have this either stored in a seperate file or generated by your program.

When you come to pass a normal string to your font drawing engine, for each character you just examine the slot like:

char C='A';

assert(C>=0 && C<256);
index I=FontIndicies[int(C)];

kind of thing. If your font is, say 8x8, you can compute its source rect from the index like:

SourceRect=Rect(I*8,0,(I*8)+8,8);

and then it is just a question of copying from the font bitmap to the destination surface, using the SourceRect as the source to copy from the font bitmap.

Clearly proportional fonts are slightly more complicated since as well as the index information, you also somehow need to store the width of each individual letter somewhere and use those values when stepping along your "cursor" as you print a string or to implement like a TextWidth("abc") function.

One method to reduce the work involved in this would be to arrange the proportionte font letters on a black bitmap, with a vertical red line between each letter (colours are arbitray of course). Your program can then load this bitmap and automatically search along the bitmap for red pixels in order to generate for itself the width of each letters. You still need a method like above to map a particular letter image to an ASCII or UNICODE value though if you want to be able to translate string literals into graphics.

Of course, I may have completely misunderstood what you mean, but I hope that was of some help.

This topic is closed to new replies.

Advertisement