Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

dos-like

Engine for making things with a MS-DOS feel, but for modern platforms 路 By Mattias Gustavsson

What is the format used by fonts?

A topic by The Mojon Twins created Feb 15, 2024 Views: 145 Replies: 4
Viewing posts 1 to 4

I'd like to add my own, possibly write a visual editor to create more - I'd like to start by adding the 8x14 EGA Hi-Res font for 640x350, 24 line display, but I'm not having luck on deciphering the format. Could you give me some pointers?

This is a great lib, I love it to bits.

Developer (1 edit)

I am very happy that you enjoy the lib :)

The font format is a custom one, and it is handled by pixelfont.h in the libs folder. The lib has a set of "builder" functions here,  which are used to create a font in the format. Basically you call builder_create, then call builder_glyph for each character you want to add, and when you are done you call builder_font which gives you a pointer to a pixelfont structure. The first field in the structure is its size in bytes, and you can just save it as a binary blob, that can then be used in doslike by calling installuserfont. The same lib and builder is used in this other project, here, to build a pixelfont from ttf data using stb_truetype, but it is the same principle regardless what your input data is. What you probably want to do is make a standalone command line program that just includes pixelfont.h and makes your conversions.

let me know if it doesn't make sense, and feel free to ask followup questions!

and btw, I will clean up the readchar a bit at some point based on the issue logged in github, getting the full range should be easier, and besides, I probably want to look into unicode support as well, using UTF8.

Thanks for the prompt response! Great news about the UTF8 support. I'm doing conversions myself but this will surely come handy.

About the builder functions, I guess I have to call `pixelfont_builder_glyph` for every glyph I need to add - and pass a 32x32 bytes "bitmap" as `pixels` using 0 for BG and 1 for FG for each pixel in the glyph? top-left aligned?

Thanks again.

Nevermind, I got it working! 

Got the typeface as a bitmap array from https://github.com/susam/pcface/ , converted it to a C array of 256*14 bytes, and then used this to export it to a file:

int main(void) {
    int chr_width = 8; 
    int chr_height = 14; 
    int chr_baseline = 10; 
    PIXELFONT_U8 pixels [32 * 32]; 
    pixelfont_builder_t* builder = pixelfont_builder_create (
        chr_height, 
        chr_baseline,  
        chr_height,  
        NULL
    ); 
    unsigned char *ptr = PC_FACE_OLDSCHOOL_EGA_8X14_FONT_LIST;  
    for (int c = 0; c < 256; c ++) { 
        // Paint glyph 
        for (int y = 0; y < 14; y ++) { 
            unsigned char bitmap = *ptr ++; 
            for (int x = 0; x < 8; x ++) { 
                if (bitmap & (1 << (7 - x))) { 
                    pixels [x + chr_width * y] = 1; 
                } else { 
                    pixels [x + chr_width * y] = 0; 
                } 
            }
        }
        pixelfont_builder_glyph (builder, c, chr_width, pixels, chr_width, 0, chr_width);     
    } 
    pixelfont_t* pixelfont = pixelfont_builder_font (builder); 
    pixelfont_t* output = (pixelfont_t*) malloc (pixelfont->size_in_bytes); 
    memcpy (output, pixelfont, pixelfont->size_in_bytes); 
    FILE *pf = fopen ("ega14.fnt", "wb"); 
    fwrite (output, sizeof (unsigned char), pixelfont->size_in_bytes, pf); 
    fclose (pf); 
    pixelfont_builder_destroy (builder); 
}

Developer

That鈥檚 beautiful! 馃檪 I am glad you got it working!