The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
bragi_font.c File Reference

bragi: fonts, faces, glyph lookup and metrics (stb_truetype). More...

#include "bragi_internal.h"
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

Macros

#define STB_TRUETYPE_IMPLEMENTATION
#define FONT_MAX_SIZE   (32u * 1024 * 1024)

Functions

bragi_font_t * bragi_font_from_memory (const uint8_t *data, size_t size, bool take)
 a font from memory.
bragi_font_t * bragi_font_open (const char *path)
 load a TrueType file.
void bragi_font_close (bragi_font_t *font)
 close a font.
bragi_face_t * bragi_face_new (bragi_font_t *font, float pixel_size)
 the font at a size.
void bragi_face_set_tracking (bragi_face_t *face, float em)
 letter spacing, as CSS letter-spacing: added after every character, so measuring, wrapping and drawing all include it.
int32_t bragi_line_baseline (const bragi_face_t *face, int32_t line_height)
 where the baseline is in a line of text line_height pixels high, from its top: as CSS places text in a line box (the font's ascent and descent, with half of the rest above them).
void bragi_face_free (bragi_face_t *face)
void bragi_face_set_fallback (bragi_face_t *face, bragi_face_t *fallback)
 where characters the font does not have come from (NULL: none).
int32_t bragi_ascent (const bragi_face_t *face)
 the vertical metrics in whole pixels: from the baseline up to the top of the highest letters (ascent), down to the bottom of the lowest (descent, a positive number), and the distance from one baseline to the next (line height).
int32_t bragi_descent (const bragi_face_t *face)
int32_t bragi_line_height (const bragi_face_t *face)
bool bragi_has_char (const bragi_face_t *face, uint32_t codepoint)
 true if the font (not a fallback) has a glyph for the character.
bragi_face_t * bragi_find_glyph (bragi_face_t *face, uint32_t codepoint, int *glyph)
 find the face (this one or a fallback) and the glyph for a character.
int32_t bragi_advance (const bragi_face_t *face, int glyph)
 the advance of a glyph in 1/64 pixels.
int32_t bragi_kerning (const bragi_face_t *face, int left, int right)
 the kerning between two glyphs of the same face, in 1/64 pixels.
void bragi_face_stats (const bragi_face_t *face, bragi_stats_t *stats)

Detailed Description

bragi: fonts, faces, glyph lookup and metrics (stb_truetype).

stb_truetype is compiled here, and only bragi uses it, so it can be replaced later without changing the apps.

Macro Definition Documentation

◆ STB_TRUETYPE_IMPLEMENTATION

#define STB_TRUETYPE_IMPLEMENTATION

◆ FONT_MAX_SIZE

#define FONT_MAX_SIZE   (32u * 1024 * 1024)

Function Documentation

◆ bragi_font_from_memory()

bragi_font_t * bragi_font_from_memory ( const uint8_t * data,
size_t size,
bool take )

a font from memory.

with take, the font frees data when it is closed; else data must stay until then.

◆ bragi_font_open()

bragi_font_t * bragi_font_open ( const char * path)

load a TrueType file.

Returns
the font, or NULL (no file, or not a TrueType font).

◆ bragi_font_close()

void bragi_font_close ( bragi_font_t * font)

close a font.

close its faces first.

◆ bragi_face_new()

bragi_face_t * bragi_face_new ( bragi_font_t * font,
float pixel_size )

the font at a size.

Parameters
pixel_sizethe size of the em in pixels (what "16 px text" means); 4 to 512.
Returns
the face, or NULL.

◆ bragi_face_set_tracking()

void bragi_face_set_tracking ( bragi_face_t * face,
float em )

letter spacing, as CSS letter-spacing: added after every character, so measuring, wrapping and drawing all include it.

Parameters
emin ems (-0.006 for Inter body text in idunn).

◆ bragi_line_baseline()

int32_t bragi_line_baseline ( const bragi_face_t * face,
int32_t line_height )

where the baseline is in a line of text line_height pixels high, from its top: as CSS places text in a line box (the font's ascent and descent, with half of the rest above them).

◆ bragi_face_free()

void bragi_face_free ( bragi_face_t * face)

◆ bragi_face_set_fallback()

void bragi_face_set_fallback ( bragi_face_t * face,
bragi_face_t * fallback )

where characters the font does not have come from (NULL: none).

◆ bragi_ascent()

int32_t bragi_ascent ( const bragi_face_t * face)

the vertical metrics in whole pixels: from the baseline up to the top of the highest letters (ascent), down to the bottom of the lowest (descent, a positive number), and the distance from one baseline to the next (line height).

◆ bragi_descent()

int32_t bragi_descent ( const bragi_face_t * face)

◆ bragi_line_height()

int32_t bragi_line_height ( const bragi_face_t * face)

◆ bragi_has_char()

bool bragi_has_char ( const bragi_face_t * face,
uint32_t codepoint )

true if the font (not a fallback) has a glyph for the character.

◆ bragi_find_glyph()

bragi_face_t * bragi_find_glyph ( bragi_face_t * face,
uint32_t codepoint,
int * glyph )

find the face (this one or a fallback) and the glyph for a character.

with no face that has it, the face itself and glyph 0 (the "missing glyph" box).

◆ bragi_advance()

int32_t bragi_advance ( const bragi_face_t * face,
int glyph )

the advance of a glyph in 1/64 pixels.

◆ bragi_kerning()

int32_t bragi_kerning ( const bragi_face_t * face,
int left,
int right )

the kerning between two glyphs of the same face, in 1/64 pixels.

◆ bragi_face_stats()

void bragi_face_stats ( const bragi_face_t * face,
bragi_stats_t * stats )