ClanLib logo

Creating and using fonts, ClanLib API overview

Abstract:

ClanLib makes it possible to create your own font in an image file (like TGA, PNG or PCX) or use existing TTF fonts.

This document will demonstrate two different ways to create fonts in ClanLib, and show how to use those in your application. It will also briefly show how to use TTF fonts directly using ClanTTF.

Creating fonts

You can make your own font by drawing all the letters and numbers in an image file.

There are two types of font creation in ClanLib, the old (hard) and new (easy) way. In the old way you had to use specific palette colors to seperate the letters, while you can use the alpha/transparency in the new way.

The new way

To make a font in an image file in the new way, you have to use an image-format which supports alpha/transparency. We suggest that you use TGA or PNG. An image with alpha/transparency uses 32 bit to store each pixel. The 24 bit specifies the RGB color and the rest (8 bit) specifies the alpha/tranparency.

The letters in the image-file are separated by one or several vertical lines with full transparency (that is pixels with an alpha below the trans_limit). The font cutter will then use these transparent lines to cut out the letters, telling it where each letter starts and stops. Note that all letters must be placed beside eachother in one row; you can't place letters below others.

Read this for a tutorial on how to create a font using this method.

When a font is loaded, it is analyzed. If there is no alpha in the image, despite that it has an alpha channel, it stores it as a colorkey surface (palette-color seperated font). So basically, if you have a target image, where only alpha is either 100% on or off, the image will be just as fast as an image that never had an alpha channel. If you use variable alpha-values, the font will be blitted using these values (which is slower, but much nicer than a colorkey surface).

The old way

The letters are drawn on 3 lines, the first one being the uppercase letters, the second the lower case letters and the third line being the numbers.

The letters are seperated by straight lines, which are drawn with the last 3 colors of the 256 color-palette. Color number 254 seperates the letters vertically, number 255 seperates the lines and color 253 marks the end of the line.

Read this for a tutorial on how to create a font using this method.

Using fonts

The resource type can look like this: // New way: font = font.tga( type=font, trans_limit=0.05, x=0, y=0, subtract_width=0, spacelen=4, letters="!'#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_'abcdefghijklmnopqrstuvwxyz{|}~"); // Old way: font = font.pcx( type=font, x=1, y=1, tcol=0, spacelen=6, letters="abcABC12345.,""(){}" ); trans_limit means the amount of the alpha amount allowed where it still considers the pixel to be transparent. subtract_width is the kerning, if it is eg. 5, then 5 pixels will be subtracted from each letter's width. spacelen is the pixel width of a space.

If you want more information about resources, have a look at the resource overview.

Using TTF fonts with ClanTTF

Using TTF fonts with ClanTTF is very simple. In your program simply #include . Then call CL_TTFSetup::init() and use fonts as you would normally. A resource file for a TTF font is easy to do. In resources, type:

local_name = font.ttf (type=TTF);

Where local_name is the name you would use in the program and font.ttf is the file name of the font. For an example of how to do this checkout the Examples/TTF directory. Once you have finished using ClanTTF call CL_TTFSetup::deinit().

ClanTTF is still under development, and you might experience some problems with it. This will be better in upcoming releases.



Questions or comments, write to the ClanLib mailing list.