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

bragi: UTF-8, measuring and drawing lines, wrapping, text boxes. More...

#include "bragi_internal.h"
#include <string.h>

Macros

#define REPLACEMENT   0xFFFDu
#define ELLIPSIS   0x2026u

Functions

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_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).
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.

Detailed Description

bragi: UTF-8, measuring and drawing lines, wrapping, text boxes.

Macro Definition Documentation

◆ REPLACEMENT

#define REPLACEMENT   0xFFFDu

◆ ELLIPSIS

#define ELLIPSIS   0x2026u

Function Documentation

◆ 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_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_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.