SCHRIFT(3) | Library Functions Manual | SCHRIFT(3) |
schrift
,
libschrift
— Lightweight
TrueType font rendering library
library “libschrift”
#include <schrift.h>
const char *
sft_version
(void);
SFT_Font *
sft_loadmem
(const
void *mem, unsigned long
size);
SFT_Font *
sft_loadfile
(const
char *filename);
void
sft_freefont
(SFT_Font
*font);
int
sft_lmetrics
(const
SFT *sft, SFT_LMetrics
*metrics);
int
sft_lookup
(const
SFT *sft, SFT_UChar
codepoint, SFT_Glyph
*glyph);
int
sft_gmetrics
(const
SFT *sft, SFT_Glyph
glyph, SFT_GMetrics
*metrics);
int
sft_kerning
(const
SFT *sft, SFT_Glyph
leftGlyph, SFT_Glyph
rightGlyph, SFT_Kerning
*kerning);
int
sft_render
(const
SFT *sft, SFT_Glyph
glyph, SFT_Image
image);
The function
sft_version
()
may be called to determine the version of schrift
.
Since schrift
uses semantic versioning, the returned
string is of the form "MAJOR.MINOR.PATCH".
sft_loadfile
()
will load a font from a given filename (by mapping it into memory), whereas
sft_loadmem
()
can be given an arbitrary memory address and size (in bytes). This allows
loading fonts from ZIP file streams etc.
Once a particular font is no longer needed, its memory should be
freed again by calling
sft_freefont
().
If the font has been loaded with sft_loadmem
(), the
underlying memory region will have to be freed separately.
Most functions take a SFT as their primary argument. This is a structure to be filled out by the caller. It bundles multiple commonly needed parameters. The fields are as follows:
If the SFT_DOWNWARD_Y
flag is set, the Y
axis is interpreted to point downwards. Per default, it points upwards.
schrift
operates in terms of glyph ids (of
type SFT_Glyph), which are font-specific identifiers
assigned to renderable symbols (glyphs). The way to obtain a glyph id is to
call
sft_lookup
().
This function takes a Unicode codepoint in codepoint
and writes its corresponding glyph id into the variable pointed to by
glyph.
sft_lmetrics
()
calculates the typographic metrics neccessary for laying out multiple lines
of text.
This function writes its output into the structure pointed to by metrics. The fields are as follows:
When displaying multiple glyphs in a line, you have to keep track
of the pen position.
sft_gmetrics
()
tells you where to draw the next glyph with respect to the pen position, and
how to update it after rendering the glyph.
This function writes its output into the structure pointed to by metrics. The fields are as follows:
Some sequences of glyphs may look awkward if they're layed out
naively. For example, the gap between the two characters "VA"
might look disproportionally large. Kerning is a way to combat this
artifact, by slightly moving the second character closer or further away by
a small amount.
sft_kerning
()
may be used to retrieve kerning information for a given pair of glyph
ids.
This function writes its output into the structure pointed to by kerning. The fields are as follows:
To actually render a glyph into a easily-displayable raster image,
use
sft_render
().
Other than most functions, sft_render
()
takes a structure in image that is to be filled out by
the caller. The fields are as follows:
The image will be rendered into the memory provided in
pixels. Each byte corresponds to one pixel, with rows
of the image being directly adjacent in memory without padding between them.
Glyphs are rendered "white on black", meaning the background has a
pixel value of 0, and the foreground goes up to a value of 255. Pixel values
follow a linear color ramp, unlike conventional computer screens. That is to
say, they are not
gamma-corrected. These properties make it very easy to use images
rendered with sft_render
() as alpha masks.
sft_loadmem
() and
sft_loadfile
() return NULL
on error.
sft_lmetrics
(),
sft_lookup
(),
sft_hmetrics
(),
sft_kerning
(),
sft_extents
(), and
sft_render
() all return 0 on success and -1 on
error.
See the source code of the
demo for a
detailed example of real-world usage of schrift
.
Thomas Oltmann <thomas.oltmann.hhg@gmail.com>
The only text encoding that schrift
understands is Unicode.
Similarly, the only kind of font file supported right now are TrueType (.ttf) fonts (Some OpenType fonts might work too, as OpenType is effectively a superset of TrueType).
As of v0.10.2, there is no support for right-to-left scripts yet.
schrift
currently does not implement font
hinting and probably never will.
January 30, 2021 | Void Linux |