Scalable Screen Font

From OSDev.wiki
Jump to navigation Jump to search

Scalable Screen Font is a compressed font format which comes with a small, free, MIT licensed rendering library. It was designed specially for hobby OS developers, therefore it has minimal dependencies.

Supported Font Types

  • Bitmap - you can convert PC Screen Font with UNICODE table into SSFN directly. X11 Bitmap Distribution Format also supported.
  • Pixmap - to have colorful emoji icons, you can convert TGA files into SSFN fonts.
  • Vector - vector based fonts can be converted from OpenType and TrueType fonts.

Fonts are compressed, and in case of vector fonts, with a data-loss compression. You can set the compression ratio to balance between quality and font size. With the same quality as a typical OpenType file, the SSFN font usually takes only the half the file size.

Library

The SSFN package comes with a single ANSI C/C++ header file, which has two renderers. The header contains everything, no shared library nor static linking needed.

Simple Renderer

Designed specially for OS consoles, has only one function. It can render unscaled bitmap fonts directly to the framebuffer. Has absolutely no dependencies, and compiles to less than a kilobyte of code.

#define SSFN_NOIMPLEMENTATION               /* don't include the normal renderer implementation */
#define SSFN_CONSOLEBITMAP_HICOLOR          /* use the special renderer for hicolor packed pixels */
#include <ssfn.h>

/* set up context by global variables */
ssfn_font = &_binary_console_sfn_start;     /* the bitmap font to use */
ssfn_dst_ptr = 0xE0000000;                  /* framebuffer address and bytes per line */
ssfn_dst_pitch = 4096;
ssfn_fg = 0xFFFF;                           /* colors, white on black */
ssfn_bg = 0;
ssfn_x = 100;                               /* coordinates to draw to */
ssfn_y = 200;

/* render one glyph directly to the screen and then adjust ssfn_x and ssfn_y */
ssfn_putc(0x41);

Normal Renderer

There's another for user space applications. This one can render all three types of fonts, it can scale, antialias, autohint and kern glyphs. Has minimal libc dependencies (memset, memcmp, realloc, free) and compiles to about 21 kilobytes of code.

#include <ssfn.h>

ssfn_t ctx;                                         /* the renderer context */
ssfn_glyph_t *glyph;                                /* the returned rasterized bitmap */

/* you don't need to initialize the library, just make sure the context is zerod out */
memset(&ctx, 0, sizeof(ssfn_t));

/* add one or more fonts to the context. Fonts must be already in memory */
ssfn_load(&ctx, &_binary_times_sfn_start);          /* you can add different styles... */
ssfn_load(&ctx, &_binary_timesbold_sfn_start);
ssfn_load(&ctx, &_binary_timesitalic_sfn_start);
ssfn_load(&ctx, &_binary_emoji_sfn_start);          /* ...or different UNICODE ranges */
ssfn_load(&ctx, &_binary_cjk_sfn_start);

/* select the typeface to use */
ssfn_select(&ctx,
    SSFN_FAMILY_SERIF, NULL,                        /* family */
    SSFN_STYLE_REGULAR | SSFN_STYLE_UNDERLINE, 64,  /* style and size */
    SSFN_MODE_BITMAP                                /* rendering mode */
);

/* rasterize a glyph for the 0x41 UNICODE code point into a newly allocated bitmap */
glyph = ssfn_render(&ctx, 0x41);

/* display the bitmap on your screen */
my_draw_glyph(
    pen_x, pen_y - glyph->baseline,                 /* coordinates to draw to */
    glyph->w, glyph->h, glyph->pitch,               /* bitmap dimensions */
    &glyph->data                                    /* bitmap data */
);
pen_x += glyph->adv_x;                              /* adjust the cursor */
pen_y += glyph->adv_y;                              /* ssfn handles vertical fonts too */

/* free resources */
free(glyph);                                        /* no special treatment for freeing glyphs */
ssfn_free(&ctx);                                    /* free the renderer context's internal buffers */

Links

See Also

External Links