Portable Type Engine

A simple C library to draw text

Download now »

Introduction

The Portable Type Engine is a library to include in your own projects to enable high quality anti-aliased font rendering on systems using as small a footprint as necessary.

Using a single embedded font file, text of any size can be rendered (up to a limited determine by the embedded font - see below for more details).

The engine is just a single C file that needs to be included in your project. You need to implement the draw pixel function and include the font generated by the Font Tool.


Features


Example of rendered text


License

The Portable Text Engine is available under the BSD (3 Clause) open source license.

The Font Tool is licensed under the GPL license. This means that you can only include it in other GPL projects.

I do not anticipate any problems with using the Portable Text Engine in any projects either open source or commercial.


How to Use

Step 1 - Select a font

Pick the font that you want to use in your project. If you want a different font to the ones you have now try looking at Google Fonts.

Step 2 - Convert the font to a C file

The Font Tool is provided to convert the chosen font in to a C file that can be added to your project. It is written using QT so it can be run on Windows, Mac or Linux. Install the font in to your operating system and then start the font tool.

Pick the correct font in the font tool. Then pick the "code page" that you want the font to be converted to. If you don't know what a code page is then leave this as the default option.

If you don't want the entire code page worth of font data then you can use the options to pick a sub-set of font data. The default is to include the entire code page.

Finally pick the size of the output text. This should be 4 times bigger than the largest text you want to render (in pixels).

You can now save the C file by using the "Save" option.

Step 3 - Include the files in your project

The C file with the font data, plus the pte.c and pte.h files must be added to your project. They are standard C files so they can be used with either a C project or a C++ project. There is no library to include - the files are simply compiled in to your project.

Step 4 - Write the draw pixel function

The output of the Portable Text Engine is multiple calls to the hw_blendPixel( int x, int y, int blend, int col ). The parameters are the X and Y co-ordinates of the pixel to be set. The amount to blend (a value between 0 and 256, 0 entirely background and 256 means entirely colour, and 127 means half way between background and the text's colour). Col is simply passed through and can be anything. You can treat it as a palette index or you can treat it as a 24 bit rgb value or you can simply ignore it.

You must provide this funtion. Here is an example of how to implement it:

        void hw_blendPixel( int x, int y, int a, int col )
        {
            int b = 256 - a;
            unsigned pixel = g_image.pixel(x,y);
            unsigned int c[3];
            unsigned int p[3];
            c[0] = g_canvas.m_palette[col].blue;
            c[1] = g_canvas.m_palette[col].green;
            c[2] = g_canvas.m_palette[col].red;
        
            p[0] = pixel & 0xff;
            p[1] = (pixel>>8) & 0xff;
            p[2] = (pixel>>16) & 0xff;
        
        
            p[0] = ((p[0]*b) >> 8) + ((c[2]*a) >> 8);
            p[1] = ((p[1]*b) >> 8) + ((c[1]*a) >> 8);
            p[2] = ((p[2]*b) >> 8) + ((c[0]*a) >> 8);
        
            pixel = (255<<24)
                | (p[2] << 16)
                | (p[1] << 8)
                | (p[0]);
        
            g_image.setPixel(x,y,pixel);
        }
    

Step 5 - Call the text drawing functions

The drawing functions can now be called to render text. The functions are self explainitory. If you need more information, then have a look at the example project.


How it works

Unlike other engines, this engine does not render fonts using the vector information in the font file. In fact the engine works by resizing a bitmap version of the font.

The bitmap version needs to be at least 4 times the size of the largest text you wish to render. When it is scaled down to the target size, it is done using grayscale pixels. This resized version is is accurately placed using sub-pixel alignment, which means the spacing between the characters is preserved from the full size bitmap. The grayscale pixels produce a good looking anti-aliasing.


Download


© 2015 Matt Pyne