Creating and using fonts, ClanLib API overviewAbstract: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 fontsYou 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 wayTo 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 wayTo make a font in an image file in the old way, you have to use an 8-bit format and save your image as 256 index-coloured; the PCX format is perfect for this. 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 separates 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 fontsThe 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.,""(){}" ); Common options for the new and old method:
Options for the new method:
Options for the old method:
If you want more information about resources, have a look at the resource overview. Loading the fonts at runtimeTo load the font in your program, you can use the following C++ code: CL_ResourceManager *manager = new CL_ResourceManager("myresources.scr", true); CL_Font *font = new CL_Font("font", manager); Using TTF fonts with ClanTTF#include <ClanLib/ttf.h> main(...) { ... // Setting up ClanLib CL_TTFSetup::init(); ... CL_Font *font = new CL_Font("my_font", resources); ... // End of program, close down CL_TTFSetup::deinit(); } In the resource-file, type: myfont = font.ttf (type=TTF); Where myfont 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. ClanTTF is still under development, and you might experience some problems with it. This will be better in upcoming releases. |