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

bragi: TrueType text for libgfx buffers. More...

#include <gfx.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  bragi_stats_t

Macros

#define BRAGI_CACHE_BUDGET   (1024 * 1024)

Typedefs

typedef struct bragi_font bragi_font_t
typedef struct bragi_face bragi_face_t

Enumerations

enum  bragi_align_t { BRAGI_ALIGN_LEFT , BRAGI_ALIGN_CENTER , BRAGI_ALIGN_RIGHT }

Functions

bragi_font_t * bragi_font_open (const char *path)
 load a TrueType file.
bragi_font_t * bragi_font_from_memory (const uint8_t *data, size_t size, bool take)
 a font from memory.
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_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).
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).
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.
uint32_t bragi_utf8_next (const char **text, const char *end)
 decode one character of UTF-8.
int32_t bragi_measure (bragi_face_t *face, const char *text, int32_t length)
 the width of a line of text in pixels (rounded up).
int32_t bragi_draw (gfx_buffer_t *buf, bragi_face_t *face, int32_t x, int32_t baseline, const char *text, int32_t length, gfx_color_t color)
 draw a line of text.
int32_t bragi_caret_x (bragi_face_t *face, const char *text, int32_t length, int32_t offset)
 where a caret before the byte at offset goes, in pixels from the start of the line (rounded to the nearest pixel).
int32_t bragi_offset_at (bragi_face_t *face, const char *text, int32_t length, int32_t x)
 the caret place nearest to x (pixels from the start of the line), for a click in a text field.
int32_t bragi_wrap (bragi_face_t *face, const char *text, int32_t width, const char **next)
 how many bytes of a line fit in a width, broken at a space if one is in the part that fits, else inside a word (at least one character).
int32_t bragi_draw_box (gfx_buffer_t *buf, bragi_face_t *face, gfx_rect_t box, const char *text, bragi_align_t align, gfx_color_t color)
 draw a paragraph in a box: wrapped to the box's width, aligned, and as many lines as fit in its height.
int32_t bragi_measure_box (bragi_face_t *face, const char *text, int32_t width)
 the height a paragraph needs at a width (all of it, nothing cut off).
void bragi_cell_size (bragi_face_t *face, int32_t *width, int32_t *height)
 the cell of a monospace face, in whole pixels: the advance of "M" and the line height.
void bragi_draw_cell (gfx_buffer_t *buf, bragi_face_t *face, int32_t x, int32_t y, uint32_t codepoint, gfx_color_t color)
 draw one character in a cell, its top left at (x, y).
void bragi_face_stats (const bragi_face_t *face, bragi_stats_t *stats)

Detailed Description

bragi: TrueType text for libgfx buffers.

a font is a TrueType file in memory. a face is a font at one size, with its own glyph cache. text is UTF-8; bad bytes show as U+FFFD.

glyphs are anti-aliased (8-bit coverage, from stb_truetype) and placed at quarter pixels, so the spacing of text stays even. the coverage is blended in sRGB, as browsers do, so text has the same weight as in the idunn reference.

a face can have a fallback face: a character the font does not have comes from the fallback (and from its fallback, and so on). with none, the font's own "missing glyph" box is drawn.

Macro Definition Documentation

◆ BRAGI_CACHE_BUDGET

#define BRAGI_CACHE_BUDGET   (1024 * 1024)

Typedef Documentation

◆ bragi_font_t

typedef struct bragi_font bragi_font_t

◆ bragi_face_t

typedef struct bragi_face bragi_face_t

Enumeration Type Documentation

◆ bragi_align_t

Enumerator
BRAGI_ALIGN_LEFT 
BRAGI_ALIGN_CENTER 
BRAGI_ALIGN_RIGHT 

Function Documentation

◆ 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_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_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_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_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_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_utf8_next()

uint32_t bragi_utf8_next ( const char ** text,
const char * end )

decode one character of UTF-8.

Parameters
textthe bytes; *text moves past the character.
endthe end of the bytes.
Returns
the code point; a bad sequence gives U+FFFD and moves one byte.

◆ bragi_measure()

int32_t bragi_measure ( bragi_face_t * face,
const char * text,
int32_t length )

the width of a line of text in pixels (rounded up).

a newline does not end it: use the layout calls for more than one line.

Parameters
lengthbytes of text, or -1 for up to the terminating zero.

◆ bragi_draw()

int32_t bragi_draw ( gfx_buffer_t * buf,
bragi_face_t * face,
int32_t x,
int32_t baseline,
const char * text,
int32_t length,
gfx_color_t color )

draw a line of text.

Parameters
xthe left end of the text.
baselinethe y of the baseline.
Returns
the advance width in pixels (where the next text would start).

◆ bragi_caret_x()

int32_t bragi_caret_x ( bragi_face_t * face,
const char * text,
int32_t length,
int32_t offset )

where a caret before the byte at offset goes, in pixels from the start of the line (rounded to the nearest pixel).

Parameters
offseta byte offset at the start of a character; it is cut to the text.

◆ bragi_offset_at()

int32_t bragi_offset_at ( bragi_face_t * face,
const char * text,
int32_t length,
int32_t x )

the caret place nearest to x (pixels from the start of the line), for a click in a text field.

Returns
a byte offset at the start of a character, or the length.

◆ bragi_wrap()

int32_t bragi_wrap ( bragi_face_t * face,
const char * text,
int32_t width,
const char ** next )

how many bytes of a line fit in a width, broken at a space if one is in the part that fits, else inside a word (at least one character).

Parameters
nextreceives where the following line starts (after the spaces and the newline the line ends with).
Returns
the bytes of this line (without trailing spaces; 0 for an empty line). the text is at its end when **next is '\0'.

◆ bragi_draw_box()

int32_t bragi_draw_box ( gfx_buffer_t * buf,
bragi_face_t * face,
gfx_rect_t box,
const char * text,
bragi_align_t align,
gfx_color_t color )

draw a paragraph in a box: wrapped to the box's width, aligned, and as many lines as fit in its height.

if not all the text fits, the last line ends with "..." (U+2026 when the font has it).

Returns
the height of the lines drawn.

◆ bragi_measure_box()

int32_t bragi_measure_box ( bragi_face_t * face,
const char * text,
int32_t width )

the height a paragraph needs at a width (all of it, nothing cut off).

◆ bragi_cell_size()

void bragi_cell_size ( bragi_face_t * face,
int32_t * width,
int32_t * height )

the cell of a monospace face, in whole pixels: the advance of "M" and the line height.

◆ bragi_draw_cell()

void bragi_draw_cell ( gfx_buffer_t * buf,
bragi_face_t * face,
int32_t x,
int32_t y,
uint32_t codepoint,
gfx_color_t color )

draw one character in a cell, its top left at (x, y).

the background is not drawn. the single and heavy box drawing lines and the block elements are drawn by code to fill the cell, so they join from cell to cell; the double lines and the diagonals come from the font.

◆ bragi_face_stats()

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