Scalable Screen Font: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(3 intermediate revisions by 2 users not shown)
Line 17:
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 two kilobytes of code.
 
<sourcesyntaxhighlight lang="c">
#define SSFN_CONSOLEBITMAP_HICOLOR SSFN_CONSOLEBITMAP_TRUECOLOR /* use the special renderer for hicolor32 bit truecolor packed pixels */
#include <ssfn.h>
 
Line 25:
ssfn_dst.ptr = 0xE0000000; /* framebuffer address and bytes per line */
ssfn_dst.p = 4096;
ssfn_dst.fg = 0xFFFF0xFFFFFFFF; /* colors, white on black */
ssfn_dst.bg = 0;
ssfn_dst.x = 100; /* coordinates to draw to */
ssfn_dst.y = 200;
 
/* render one glyphtext directly to the screen and then adjust ssfn_dst.x and ssfn_dst.y */
ssfn_putc(0x41'H');
ssfn_putc('e');
</source>
ssfn_putc('l');
ssfn_putc('l');
ssfn_putc('o');
</syntaxhighlight>
 
== Normal Renderer ==
Line 38 ⟶ 42:
There's another for user space applications. This one can render all three types of fonts, supports gzip compressed fonts, it can scale, anti-alias and kern glyphs. Has minimal libc dependencies (memset, memcmp, realloc, free) and compiles to about 28 kilobytes of code. (Just for completeness, you can compile it in total dependency-free mode if you define SSFN_MAXLINES, but then there'll be no internal glyph cache and you must provide inflated fonts only to ssfn_load().)
 
<sourcesyntaxhighlight lang="c">
#define SSFN_IMPLEMENTATION /* use the normal renderer implementation */
#include <ssfn.h>
Line 70 ⟶ 74:
buf.fg = 0xFF808080; /* foreground color */
 
/* rasterize the first glyph in an UTF-8 string into a 32 bit packed pixel buffer. Returns how many bytes were consumed from the string */
ssfn_render(&ctx, &buf, "AHello");
ssfn_render(&ctx, &buf, "ello");
ssfn_render(&ctx, &buf, "llo"); /* assuming there's a ligature for "ll" in the font */
ssfn_render(&ctx, &buf, "o");
 
/* free resources */
ssfn_free(&ctx); /* free the renderer context's internal buffers */
</syntaxhighlight>
</source>
The renderer takes care of the font direction, it uses horizontal or vertical alignment automatically. It cannot determine right-to-left though, for that you'll need
a minimal BiDi state machine too. That has to be implemented in the text renderer (or text shaping library) that's built on top of the low level rasterizer. The [http://www.unicode.org/reports/tr9/ algorithm to properly display bidirectional texts] is specified by UNICODE. But once you have decided that you need to draw a glyph in right-to-left direction, just pass SSFN_STYLE_RTL to ssfn_select().
Line 91 ⟶ 98:
 
[[Category:Graphical UI]]
[[Category:Fonts]]