single.php Progress (part I) – Fonts from bitmaps

BennuGD has seen a lot of improvements lately. As writing about all of them in a single post would result in a boringly long one, I’m going to write a series of articles highlighting some improvements I find interesting.

Late last year (28th December) Splinter added two new functionalities to fnt_new(). They’re used with the following syntax:
FNT_NEW (CHARSET, DEPTH);
FNT_NEW (FILE, GRAPH, CHARSET, WITDH, HEIGHT, FIRST, LAST, FLAGS);

Read on for more details.

As you can see, there’s now a new CHARSET parameter which can be one of these:
CHARSET_ISO8859
CHARSET_CP850

This way, you can control the charset used in your new font.

Not only that, but there’s a new FLAGS parameter, which can be one of:
NFB_VARIABLEWIDTH
NFB_FIXEDWIDTH

This way, you can specify if your new font will be monospaced or not.
With this new functionality, you’ll be able to create a new font in memory from a bitmap that’s already been loaded. Splinter posted the following code showing the effect.

import "mod_video";
import "mod_wm";
import "mod_text";
import "mod_map";
import "mod_key";

private
    a,b,c;
    g;
    font, font2;

begin
    set_mode(640,480);
    g = png_load("font.png");
    font = fnt_new(0,g,CHARSET_ISO8859,16,16,0,255,0);
    write_int(font,0,0,0,&a);
    write_int(font,0,20,0,&b);
    write_int(font,0,40,0,&c);
    write(font,0,60,0,"hello world!");

    write_int(0,320,0,0,&a);
    write_int(0,320,20,0,&b);
    write_int(0,320,40,0,&c);
    write(0,320,60,0,"hello world!");

    font2 = fnt_new(0,g,CHARSET_ISO8859,16,16,0,255,1);
    write_int(font2,0,100,0,&a);
    write_int(font2,0,120,0,&b);
    write_int(font2,0,140,0,&c);
    write(font2,0,160,0,"hello world!");

    while( !key( _ESC ) && !exit_status )
        a++;
        b--;
        c*=2;
        if ( c == 0 ) c = 1; end
        frame;
    end
end

The code will take a bitmap such as this one:

And use it to first create a font in memory (that you can later save with fnt_save()) and write some text to the screeen, both in fixed-width and variable width forms:

Some text rendered with the previous font

Hope you find it useful!

PS: This new functionality still needs to be documented, would you like to do it? Go the the fnt_new() page in the wiki and add the relevant info!