SCHRIFT(3)
π SCHRIFT(3) β the libschrift era π
okay bestie, this is the Library Functions Manual page for SCHRIFT(3), aka libschrift β the lightweight TrueType font rendering library. grab a strawberry drink π because we're doing font math and it's lowkey iconic.
πΈ NAME πΈ
schrift, libschrift β Lightweight TrueType font rendering library
β¨ SYNOPSIS β¨
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);
π¦ DESCRIPTION π¦
So sft_version() is the lil function you call when you wanna know which version of schrift you're dealing with. schrift uses semantic versioning, so the returned string is literally of the form "MAJOR.MINOR.PATCH". slay.
π Loading fonts π
sft_loadfile() will load a font from a given filename β by mapping it into memory, bestie. Meanwhile sft_loadmem() can be handed an arbitrary memory address and size (in bytes). This is what lets you load fonts from ZIP file streams etc. π
Once a particular font is no longer needed, its memory should be freed again by calling sft_freefont(). Important caveat: if the font has been loaded with sft_loadmem(), the underlying memory region will have to be freed separately. don't forget, or it's giving leak era π
π the SFT structure π
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:
fontβ The font to render withxScaleβ The width of one em-square in pixelsyScaleβ The height of one em-square in pixelsxOffsetβ The horizontal offset to be applied before rendering to an image (Useful for subpixel-accurate positioning)yOffsetβ The vertical offset to be applied before rendering to an image (Useful for subpixel-accurate positioning)flagsβ A bitfield of binary flags
If the SFT_DOWNWARD_Y flag is set, the Y axis is interpreted to point downwards. Per default, it points upwards. so default vibe = Y points up; set SFT_DOWNWARD_Y and it's downward. got it? π«Ά
π Glyph ids π
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.
β Querying metrics β
sft_lmetrics() calculates the typographic metrics necessary for laying out multiple lines of text.
This function writes its output into the structure pointed to by metrics. The fields are as follows:
ascenderβ The distance from the baseline to the visual top of the textdescenderβ The distance from the baseline to the visual bottom of the textlineGapβ The default amount of horizontal padding between consecutive lines
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:
advanceWidthβ How much the pen position should advance to the right after rendering the glyphleftSideBearingβ The offset that should be applied along the X axis from the pen position to the edge of the glyph imageyOffsetβ An offset along the Y axis relative to the pen position that should be applied when displaying the glyphminWidthβ The minimum width that an image needs such that the glyph can be properly rendered into itminHeightβ The minimum height that an image needs such that the glyph can be properly rendered into it
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:
xShiftβ An amount that should be added to the pen's X position in-between the two glyphsyShiftβ An amount that should be added to the pen's Y position in-between the two glyphs
π§ Rendering glyphs π§
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:
pixelsβ A pointer to an array of bytes,width * heightin sizewidthβ The number of pixels in a row of the imageheightβ The number of pixels in a column of the image
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.
π₯ RETURN VALUES π₯
sft_loadmem()andsft_loadfile()returnNULLon error.sft_lmetrics(),sft_lookup(),sft_hmetrics(),sft_kerning(),sft_extents(), andsft_render()all return0on success and-1on error.
yes bestie, that list really does include sft_hmetrics() and sft_extents() β iykyk, we keep the details exactly as the page says. π
π EXAMPLES π
See the source code of the demo for a detailed example of real-world usage of schrift.
π AUTHORS π
Thomas Oltmann <thomas.oltmann.hhg@gmail.com>
π CAVEATS π
- 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.
kisses from Debian, dated January 30, 2021 β this has been SCHRIFT(3), mwah π periodt.
π¦ where this page comes from
- nixos 26.05: libschrift 0.10.2
- nixpkgs unstable: libschrift 0.10.2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms