The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
bragi.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file bragi.h
5 * @brief bragi: TrueType text for libgfx buffers.
6 *
7 * a font is a TrueType file in memory. a face is a font at one size, with
8 * its own glyph cache. text is UTF-8; bad bytes show as U+FFFD.
9 *
10 * glyphs are anti-aliased (8-bit coverage, from stb_truetype) and placed
11 * at quarter pixels, so the spacing of text stays even. the coverage is
12 * blended in sRGB, as browsers do, so text has the same weight as in the
13 * idunn reference.
14 *
15 * a face can have a fallback face: a character the font does not have
16 * comes from the fallback (and from its fallback, and so on). with none,
17 * the font's own "missing glyph" box is drawn.
18 */
19
20#include <gfx.h>
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25typedef struct bragi_font bragi_font_t;
26typedef struct bragi_face bragi_face_t;
27
28// the default memory for one face's glyph cache
29#define BRAGI_CACHE_BUDGET (1024 * 1024)
30
31/**
32 * load a TrueType file.
33 * @return the font, or NULL (no file, or not a TrueType font).
34 */
35bragi_font_t *bragi_font_open(const char *path);
36
37/**
38 * a font from memory. with take, the font frees data when it is closed;
39 * else data must stay until then.
40 */
41bragi_font_t *bragi_font_from_memory(const uint8_t *data, size_t size, bool take);
42
43/**
44 * close a font. close its faces first.
45 */
47
48/**
49 * the font at a size.
50 * @param pixel_size the size of the em in pixels (what "16 px text"
51 * means); 4 to 512.
52 * @return the face, or NULL.
53 */
54bragi_face_t *bragi_face_new(bragi_font_t *font, float pixel_size);
55
57
58/**
59 * where characters the font does not have come from (NULL: none).
60 */
62
63/**
64 * letter spacing, as CSS letter-spacing: added after every character, so
65 * measuring, wrapping and drawing all include it.
66 * @param em in ems (-0.006 for Inter body text in idunn).
67 */
68void bragi_face_set_tracking(bragi_face_t *face, float em);
69
70/**
71 * where the baseline is in a line of text line_height pixels high, from
72 * its top: as CSS places text in a line box (the font's ascent and
73 * descent, with half of the rest above them).
74 */
76
77/**
78 * the vertical metrics in whole pixels: from the baseline up to the top
79 * of the highest letters (ascent), down to the bottom of the lowest
80 * (descent, a positive number), and the distance from one baseline to
81 * the next (line height).
82 */
86
87/**
88 * true if the font (not a fallback) has a glyph for the character.
89 */
90bool bragi_has_char(const bragi_face_t *face, uint32_t codepoint);
91
92/**
93 * decode one character of UTF-8.
94 * @param text the bytes; *text moves past the character.
95 * @param end the end of the bytes.
96 * @return the code point; a bad sequence gives U+FFFD and moves one byte.
97 */
98uint32_t bragi_utf8_next(const char **text, const char *end);
99
100/**
101 * the width of a line of text in pixels (rounded up). a newline does not
102 * end it: use the layout calls for more than one line.
103 * @param length bytes of text, or -1 for up to the terminating zero.
104 */
105int32_t bragi_measure(bragi_face_t *face, const char *text, int32_t length);
106
107/**
108 * draw a line of text.
109 * @param x the left end of the text.
110 * @param baseline the y of the baseline.
111 * @return the advance width in pixels (where the next text would start).
112 */
114 const char *text, int32_t length, gfx_color_t color);
115
116/**
117 * where a caret before the byte at offset goes, in pixels from the start
118 * of the line (rounded to the nearest pixel).
119 * @param offset a byte offset at the start of a character; it is cut to
120 * the text.
121 */
122int32_t bragi_caret_x(bragi_face_t *face, const char *text, int32_t length, int32_t offset);
123
124/**
125 * the caret place nearest to x (pixels from the start of the line), for
126 * a click in a text field.
127 * @return a byte offset at the start of a character, or the length.
128 */
129int32_t bragi_offset_at(bragi_face_t *face, const char *text, int32_t length, int32_t x);
130
137
138/**
139 * how many bytes of a line fit in a width, broken at a space if one is in
140 * the part that fits, else inside a word (at least one character).
141 * @param next receives where the following line starts (after the spaces
142 * and the newline the line ends with).
143 * @return the bytes of this line (without trailing spaces; 0 for an empty
144 * line). the text is at its end when **next is '\0'.
145 */
146int32_t bragi_wrap(bragi_face_t *face, const char *text, int32_t width, const char **next);
147
148/**
149 * draw a paragraph in a box: wrapped to the box's width, aligned, and as
150 * many lines as fit in its height. if not all the text fits, the last
151 * line ends with "..." (U+2026 when the font has it).
152 * @return the height of the lines drawn.
153 */
154int32_t bragi_draw_box(gfx_buffer_t *buf, bragi_face_t *face, gfx_rect_t box, const char *text,
155 bragi_align_t align, gfx_color_t color);
156
157/**
158 * the height a paragraph needs at a width (all of it, nothing cut off).
159 */
160int32_t bragi_measure_box(bragi_face_t *face, const char *text, int32_t width);
161
162/**
163 * the cell of a monospace face, in whole pixels: the advance of "M" and
164 * the line height.
165 */
166void bragi_cell_size(bragi_face_t *face, int32_t *width, int32_t *height);
167
168/**
169 * draw one character in a cell, its top left at (x, y). the background
170 * is not drawn. the single and heavy box drawing lines and the block
171 * elements are drawn by code to fill the cell, so they join from cell to
172 * cell; the double lines and the diagonals come from the font.
173 */
175 uint32_t codepoint, gfx_color_t color);
176
177typedef struct
178{
179 uint32_t hits; // glyphs found in the cache
180 uint32_t misses; // glyphs made
181 uint32_t evicted; // glyphs dropped to stay in the budget
182 uint32_t bytes; // memory the cached glyphs use now
184
struct bragi_face bragi_face_t
Definition bragi.h:26
struct bragi_font bragi_font_t
Definition bragi.h:25
bragi_align_t
Definition bragi.h:132
@ BRAGI_ALIGN_LEFT
Definition bragi.h:133
@ BRAGI_ALIGN_CENTER
Definition bragi.h:134
@ BRAGI_ALIGN_RIGHT
Definition bragi.h:135
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.
Definition bragi_cell.c:55
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).
Definition bragi_cell.c:162
bragi_face_t * bragi_face_new(bragi_font_t *font, float pixel_size)
the font at a size.
Definition bragi_font.c:85
void bragi_face_free(bragi_face_t *face)
Definition bragi_font.c:127
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 drawin...
Definition bragi_font.c:107
void bragi_font_close(bragi_font_t *font)
close a font.
Definition bragi_font.c:76
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 ...
Definition bragi_font.c:112
bragi_font_t * bragi_font_open(const char *path)
load a TrueType file.
Definition bragi_font.c:43
int32_t bragi_descent(const bragi_face_t *face)
Definition bragi_font.c:152
void bragi_face_stats(const bragi_face_t *face, bragi_stats_t *stats)
Definition bragi_font.c:198
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.
Definition bragi_font.c:162
int32_t bragi_line_height(const bragi_face_t *face)
Definition bragi_font.c:157
void bragi_face_set_fallback(bragi_face_t *face, bragi_face_t *fallback)
where characters the font does not have come from (NULL: none).
Definition bragi_font.c:142
bragi_font_t * bragi_font_from_memory(const uint8_t *data, size_t size, bool take)
a font from memory.
Definition bragi_font.c:27
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)...
Definition bragi_font.c:147
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 ne...
Definition bragi_text.c:153
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.
Definition bragi_text.c:163
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).
Definition bragi_text.c:141
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).
Definition bragi_text.c:233
uint32_t bragi_utf8_next(const char **text, const char *end)
decode one character of UTF-8.
Definition bragi_text.c:13
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,...
Definition bragi_text.c:181
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.
Definition bragi_text.c:146
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 heigh...
Definition bragi_text.c:275
libgfx: drawing into pixel buffers in memory.
uint32_t gfx_color_t
a premultiplied ARGB colour, 0xAARRGGBB.
Definition gfx.h:30
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
NULL, size_t, ptrdiff_t, offsetof.
integer types of fixed width.
int int32_t
Definition stdint.h:14
bool, true and false.
Definition bragi_internal.h:45
float size
Definition bragi_internal.h:49
bragi_font_t * font
Definition bragi_internal.h:46
int32_t line_height
Definition bragi_internal.h:53
bragi_stats_t stats
Definition bragi_internal.h:59
bragi_face_t * fallback
Definition bragi_internal.h:47
Definition bragi_internal.h:22
Definition bragi.h:178
uint32_t hits
Definition bragi.h:179
uint32_t misses
Definition bragi.h:180
uint32_t evicted
Definition bragi.h:181
uint32_t bytes
Definition bragi.h:182
Definition gfx.h:41
Definition gfx.h:33