RMGui 0.15.0
C11 retained-mode UI library: layout kernel, style schema, widget painters, fonts, software framebuffer
Loading...
Searching...
No Matches
richtext.h File Reference

Caller-owned, read-only styled text. More...

#include "font.h"
#include "text.h"
#include "text_shape.h"
#include "input.h"
#include "draw_target.h"
#include "host.h"
#include "style_ext.h"
#include "a11y_types.h"
#include "ui_context.h"

Data Structures

struct  rmgui_richtext_span_t
 rmgui_richtext_span_t.font_px More...
struct  rmgui_richtext_paragraph_t
 rmgui_richtext_paragraph_t.start, rmgui_richtext_paragraph_t.end More...
struct  rmgui_richtext_document_t
struct  rmgui_richtext_fonts_t
struct  rmgui_richtext_style_t
struct  rmgui_richtext_line_t
struct  rmgui_richtext_layout_t
struct  rmgui_richtext_t
 rmgui_richtext_t.host More...
struct  rmgui_richtext_view_t
 Rich text state with an optional borrowed inline painter and its user data. More...
struct  rmgui_richtext_hit_t
struct  rmgui_richtext_markdown_t
 Tiny deterministic Markdown (not CommonMark): bold, italic, mono, [text](target), #..###### headings, '- '/'* ' bullets. More...
struct  rmgui_richtext_markdown_run_t
 One half-open source range and its corresponding display range, in UTF-8 bytes. More...
struct  rmgui_richtext_markdown_map_t
struct  rmgui_richtext_markdown_options_t
 Opt-in grammar for rmgui_richtext_markdown_mapped_ex. More...

Macros

#define RMGUI_RICHTEXT_BOLD   1u
#define RMGUI_RICHTEXT_ITALIC   2u
#define RMGUI_RICHTEXT_MONO   4u
#define RMGUI_RICHTEXT_UNDERLINE   8u
#define RMGUI_RICHTEXT_STRIKE   16u
#define RMGUI_RICHTEXT_COLOR   32u
#define RMGUI_RICHTEXT_BACKGROUND   64u
#define RMGUI_RICHTEXT_COPY   1
 Copies selected UTF-8 plus NUL, writes byte length.
#define RMGUI_RICHTEXT_MARKDOWN_STRICT_LINKS   1u
 Strict link candidates.
#define RMGUI_RICHTEXT_MARKDOWN_LABEL_STYLES   2u
 Emphasis, bold, mono and backslash escapes are recognized inside a valid link label, with the same delimiter rules as elsewhere, confined to the label.
#define RMGUI_RICHTEXT_MARKDOWN_LITERAL_BULLETS   4u
 A line that starts with - or * emits the four bytes U+2022 followed by one ASCII space into the display text, with no paragraph bullet decoration (bullet stays zero and no indent column is reserved), so layout, search, selection and copy all see the same bytes.
#define RMGUI_RICHTEXT_MARKDOWN_HEADING_TEXT   8u
 A heading needs at least one byte after its marker and ASCII space: # alone or # alone on a line stays literal.
#define RMGUI_RICHTEXT_MARKDOWN_CHAT_INLINE   16u
 The inline grammar of the Automugen Chat message module, replacing the default inline rules; STRICT_LINKS and LABEL_STYLES then have no further effect.
#define RMGUI_RICHTEXT_MARKDOWN_MARKER_TEXT   32u
 A heading marker or a bullet marker (- or * ) needs at least one byte after its ASCII space that is not a carriage return or line feed.
#define RMGUI_RICHTEXT_MARKDOWN_FLAGS   63u
 All defined RMGUI_RICHTEXT_MARKDOWN_* flag bits.

Typedefs

typedef void(*) rmgui_richtext_inline_fn(void *, rmgui_target_t *, rmgui_rect_t box, rmgui_rect_t clip, unsigned glyph)
 Inline spans contain exactly one UTF-8 codepoint (typically U+FFFC).

Enumerations

enum  rmgui_richtext_align_t { RMGUI_RICHTEXT_LEFT , RMGUI_RICHTEXT_CENTER , RMGUI_RICHTEXT_RIGHT }

Functions

int rmgui_richtext_style (const rmgui_style_ext_resolved_t *, const rmgui_richtext_style_t *defaults, rmgui_richtext_style_t *out)
 Optional resolved extension names: font-size, line-gap, text, background, selection, link.
int rmgui_richtext_prepare (const rmgui_ui_t *, const rmgui_richtext_view_t *)
 Stage the current layout's text before recording.
int rmgui_richtext_draw (const rmgui_ui_t *, const rmgui_richtext_view_t *, rmgui_rect_t)
 Draw with the current layout's fonts and style and the optional inline callback.
int rmgui_richtext_layout (const rmgui_richtext_t *, const rmgui_richtext_document_t *, const rmgui_richtext_fonts_t *, const rmgui_richtext_style_t *, int width, rmgui_richtext_layout_t *cache)
 A document's spans exactly partition text; paragraphs do too, end at a newline or document end.
int rmgui_richtext_init (rmgui_richtext_t *, const rmgui_richtext_layout_t *)
 Bind resets selection/capture/scroll; no cache/text copies.
int rmgui_richtext_hit (const rmgui_richtext_layout_t *, int x, int y, rmgui_richtext_hit_t *out)
 Coordinates relative to the content origin, including scroll already.
int rmgui_richtext_select (rmgui_richtext_t *, size_t anchor, size_t caret)
int rmgui_richtext_copy (const rmgui_richtext_t *, char *, size_t capacity, size_t *length)
rmgui_input_result_t rmgui_richtext_input (rmgui_richtext_t *, rmgui_rect_t, const rmgui_input_t *)
 Pointer drag selection, link click activation (value=link_id), hover cursor; focus-gated arrows/home/end/up/down/page keys, Shift extension, Ctrl/Meta A/C.
int rmgui_richtext_prepare_to (const rmgui_richtext_t *, rmgui_target_t *)
void rmgui_richtext_paint_to (rmgui_target_t *, const rmgui_richtext_t *, rmgui_rect_t rect, rmgui_rect_t clip, rmgui_richtext_inline_fn, void *)
 Binary-searches first visible cached line, then visits only visible lines and their spans.
void rmgui_richtext_paint (rmgui_fb_t *, const rmgui_richtext_t *, rmgui_rect_t rect, rmgui_rect_t clip, rmgui_richtext_inline_fn, void *)
int rmgui_a11y_from_richtext (const rmgui_richtext_t *, size_t span, const char *name, rmgui_a11y_record_t *out)
 span=SIZE_MAX builds TEXT; a link span builds LINK.
int rmgui_a11y_richtext_selection (const rmgui_richtext_t *, size_t start, size_t end, rmgui_rect_t *rects, size_t capacity, size_t *count)
int rmgui_richtext_a11y_action (rmgui_richtext_t *, size_t span, rmgui_input_result_t *out)
 Accessibility PRESS on a link span; other host actions use input/select.
int rmgui_richtext_markdown (const char *source, size_t length, int base_px, rmgui_richtext_markdown_t *out)
int rmgui_richtext_markdown_mapped (const char *source, size_t length, int base_px, rmgui_richtext_markdown_t *out, rmgui_richtext_markdown_map_t *map)
 Like rmgui_richtext_markdown, with an optional map of ordered, coalesced runs covering the entire source.
int rmgui_richtext_markdown_mapped_ex (const char *source, size_t length, int base_px, const rmgui_richtext_markdown_options_t *options, rmgui_richtext_markdown_t *out, rmgui_richtext_markdown_map_t *map)
 Like rmgui_richtext_markdown_mapped, parsed with options (NULL is the default grammar).

Detailed Description

Caller-owned, read-only styled text.

No widget operation allocates. All offsets are UTF-8 bytes. Borrowed text/fonts/arrays stay immutable from layout through hit/input/paint. Re-layout after changing any of them. Status functions return 0 or -1; refusal leaves outputs/storage unchanged. NULL required pointers refuse (void painters simply do nothing). Capacities count elements, text capacities count bytes including the terminating NUL. Integer geometry saturates at INT_MAX; alignment division truncates toward zero. Fonts are selected, not synthesized; absent variants use regular. Font CPU glyph caches retain their normal font.h preparation contract.

Macro Definition Documentation

◆ RMGUI_RICHTEXT_BOLD

#define RMGUI_RICHTEXT_BOLD   1u

◆ RMGUI_RICHTEXT_ITALIC

#define RMGUI_RICHTEXT_ITALIC   2u

◆ RMGUI_RICHTEXT_MONO

#define RMGUI_RICHTEXT_MONO   4u

◆ RMGUI_RICHTEXT_UNDERLINE

#define RMGUI_RICHTEXT_UNDERLINE   8u

◆ RMGUI_RICHTEXT_STRIKE

#define RMGUI_RICHTEXT_STRIKE   16u

◆ RMGUI_RICHTEXT_COLOR

#define RMGUI_RICHTEXT_COLOR   32u

◆ RMGUI_RICHTEXT_BACKGROUND

#define RMGUI_RICHTEXT_BACKGROUND   64u

◆ RMGUI_RICHTEXT_COPY

#define RMGUI_RICHTEXT_COPY   1

Copies selected UTF-8 plus NUL, writes byte length.

No clipboard API calls. Host handles ACTION value RMGUI_RICHTEXT_COPY by invoking this function.

◆ RMGUI_RICHTEXT_MARKDOWN_STRICT_LINKS

#define RMGUI_RICHTEXT_MARKDOWN_STRICT_LINKS   1u

Strict link candidates.

The label and the destination must each be nonempty. The destination ends at the first ) that balances the opening (: a ( inside it opens a level, a backslash-escaped parenthesis does not count, and the escaped byte stays in the target. Any byte of ASCII 32 or below in the destination, escaped or not, rejects the candidate, and a carriage return ends the candidate before it can close. A rejected candidate is literal text: the [ is kept as an ordinary byte and scanning continues after it, so nothing of the label syntax is hidden. The link target is the borrowed source bytes between the opening and balancing parentheses.

◆ RMGUI_RICHTEXT_MARKDOWN_LABEL_STYLES

#define RMGUI_RICHTEXT_MARKDOWN_LABEL_STYLES   2u

Emphasis, bold, mono and backslash escapes are recognized inside a valid link label, with the same delimiter rules as elsewhere, confined to the label.

Each styled piece carries the link id and target of its link and keeps its style flags together with RMGUI_RICHTEXT_UNDERLINE. A [ inside a label is literal: links never nest. Delimiters and escapes are hidden runs in the map. Without this flag the label is emitted verbatim as one underlined span.

◆ RMGUI_RICHTEXT_MARKDOWN_LITERAL_BULLETS

#define RMGUI_RICHTEXT_MARKDOWN_LITERAL_BULLETS   4u

A line that starts with - or * emits the four bytes U+2022 followed by one ASCII space into the display text, with no paragraph bullet decoration (bullet stays zero and no indent column is reserved), so layout, search, selection and copy all see the same bytes.

The two source marker bytes are a hidden run; the four generated display bytes belong to no run and sit between that hidden run's display_start and the display_start of the next run, so every later visible run maps by its own display_start.

◆ RMGUI_RICHTEXT_MARKDOWN_HEADING_TEXT

#define RMGUI_RICHTEXT_MARKDOWN_HEADING_TEXT   8u

A heading needs at least one byte after its marker and ASCII space: # alone or # alone on a line stays literal.

Without the flag such a line hides its marker.

◆ RMGUI_RICHTEXT_MARKDOWN_CHAT_INLINE

#define RMGUI_RICHTEXT_MARKDOWN_CHAT_INLINE   16u

The inline grammar of the Automugen Chat message module, replacing the default inline rules; STRICT_LINKS and LABEL_STYLES then have no further effect.

Block rules (headings, bullets) are unchanged and come from the other flags. Inline scans work within one line. A carriage return ends every scan (code, label, destination) and drops all open emphasis frames, which become literal; a link label is its own scope for emphasis.

  • Escapes. A backslash escapes exactly these nine ASCII bytes and nothing else: *, ` , [, ], \\endiskip, #, -, ( and ). Before any other byte, including a space, an underscore or a non-ASCII byte, it is an ordinary byte.
  • Code. A backtick run of exactly one byte that has another single-byte backtick run later on the line delimits code: the content is opaque (no escapes, emphasis or links) and carries RMGUI_RICHTEXT_MONO. A single backtick with no partner is literal together with everything after it up to the carriage return, line feed or end of text, with no inner markup. A run of two or more backticks is literal, and runs are never split.
  • Emphasis. A run of one to three stars is bold (two), italic (one) or both (three); a run of four or more is literal. Only when the byte before the run is neither absent nor of ASCII 32 or below, a run first closes frames: the topmost open frame above the floor whose style bits the run covers closes, every frame above it is dropped (literal), and this repeats for the bits the run still covers. Any leftover opens a frame when the next byte exists, is above ASCII 32 and the leftover shares no style bit with any open frame, including frames outside the current link label; otherwise the leftover stars are literal. The first [ that has no closing ] on the line raises the floor to the frames then open, so later runs cannot close those until a carriage return; later unmatched [ on the line change nothing. A frame that nothing closes is literal: its stars stay in the display text and its content is unstyled, while pieces closed inside it keep their styles. Frames nest, so ***é***, **a *b*** and **x** carry the union of their styles, and *a**a* is a**a in italic. Punctuation has no special effect.
  • Links. A [ outside a label looks for the first unescaped ] on the line that is not inside a code fence. With none, the [ is an ordinary byte. If the next byte is not (, the bytes from [ through that ] are emitted verbatim, with no styling and nothing hidden, and parsing resumes after them. Otherwise the destination is scanned to the ) that balances the (; only the nine escapable bytes can be escaped, a byte of ASCII 32 or below makes the candidate invalid but the scan goes on, and a carriage return, line feed or the end of the text ends the scan before that byte. A nonempty label with a valid nonempty destination is a link; the target is the borrowed source bytes between the parentheses. Every other candidate is emitted verbatim from [ to where the scan stopped, just past the ) or before the byte that ended it. Inside a label an unescaped [ is ordinary text, and the label's emphasis, code and escapes are scoped to the label. Link pieces keep their style flags and RMGUI_RICHTEXT_UNDERLINE, carry the link id and target, and take the styles of the frames around the link.

Delimiters, escapes and link syntax are hidden runs in the map.

◆ RMGUI_RICHTEXT_MARKDOWN_MARKER_TEXT

#define RMGUI_RICHTEXT_MARKDOWN_MARKER_TEXT   32u

A heading marker or a bullet marker (- or * ) needs at least one byte after its ASCII space that is not a carriage return or line feed.

- , * before a line feed, # and # before a carriage return stay literal, while - (one more space) is a bullet whose text is one space. It applies to layout and literal bullets and to headings, with or without HEADING_TEXT. A heading's size applies up to the first carriage return on its line; the rest of the line, carriage return included, is parsed as ordinary text at the base size.

◆ RMGUI_RICHTEXT_MARKDOWN_FLAGS

#define RMGUI_RICHTEXT_MARKDOWN_FLAGS   63u

All defined RMGUI_RICHTEXT_MARKDOWN_* flag bits.

Typedef Documentation

◆ rmgui_richtext_inline_fn

typedef void(*) rmgui_richtext_inline_fn(void *, rmgui_target_t *, rmgui_rect_t box, rmgui_rect_t clip, unsigned glyph)

Inline spans contain exactly one UTF-8 codepoint (typically U+FFFC).

The hook paints the supplied box through the supplied target and clip; it is called only for visible lines and must not allocate. NULL hook leaves its reserved box empty. Copy preserves the replacement codepoint.

Enumeration Type Documentation

◆ rmgui_richtext_align_t

Enumerator
RMGUI_RICHTEXT_LEFT 
RMGUI_RICHTEXT_CENTER 
RMGUI_RICHTEXT_RIGHT 

Function Documentation

◆ rmgui_richtext_style()

int rmgui_richtext_style ( const rmgui_style_ext_resolved_t * ,
const rmgui_richtext_style_t * defaults,
rmgui_richtext_style_t * out )

Optional resolved extension names: font-size, line-gap, text, background, selection, link.

Missing properties retain defaults; wrong kind refuses.

◆ rmgui_richtext_prepare()

int rmgui_richtext_prepare ( const rmgui_ui_t * ,
const rmgui_richtext_view_t *  )

Stage the current layout's text before recording.

CPU targets need no staging. Inline callbacks are not invoked; their resources are the host's responsibility. Returns 0 on success or -1 on error.

◆ rmgui_richtext_draw()

int rmgui_richtext_draw ( const rmgui_ui_t * ,
const rmgui_richtext_view_t * ,
rmgui_rect_t  )

Draw with the current layout's fonts and style and the optional inline callback.

Returns -1 for invalid arguments, otherwise 0; paint failures latch on the target.

◆ rmgui_richtext_layout()

int rmgui_richtext_layout ( const rmgui_richtext_t * ,
const rmgui_richtext_document_t * ,
const rmgui_richtext_fonts_t * ,
const rmgui_richtext_style_t * ,
int width,
rmgui_richtext_layout_t * cache )

A document's spans exactly partition text; paragraphs do too, end at a newline or document end.

Empty documents may use NULL arrays/count zero. width > 0. Two-pass transactional layout uses the existing text breaker. Caller cache holds ALL lines; insufficient capacity refuses unchanged.

◆ rmgui_richtext_init()

int rmgui_richtext_init ( rmgui_richtext_t * ,
const rmgui_richtext_layout_t *  )

Bind resets selection/capture/scroll; no cache/text copies.

◆ rmgui_richtext_hit()

int rmgui_richtext_hit ( const rmgui_richtext_layout_t * ,
int x,
int y,
rmgui_richtext_hit_t * out )

Coordinates relative to the content origin, including scroll already.

Outside positions clamp to the nearest row and UTF-8 caret boundary. Exact midpoint ties choose the following boundary.

◆ rmgui_richtext_select()

int rmgui_richtext_select ( rmgui_richtext_t * ,
size_t anchor,
size_t caret )

◆ rmgui_richtext_copy()

int rmgui_richtext_copy ( const rmgui_richtext_t * ,
char * ,
size_t capacity,
size_t * length )

◆ rmgui_richtext_input()

rmgui_input_result_t rmgui_richtext_input ( rmgui_richtext_t * ,
rmgui_rect_t ,
const rmgui_input_t *  )

Pointer drag selection, link click activation (value=link_id), hover cursor; focus-gated arrows/home/end/up/down/page keys, Shift extension, Ctrl/Meta A/C.

Optional host.clipboard_set receives selected bytes directly on COPY; callback refusal reports REFUSED without changing selection. Cursor requests also call host.request_cursor when supplied. Do not reenter from callbacks. Wheel and keyboard caret movement clamp/adjust scroll_y. The host may also set scroll_y directly (painters clamp it). Disabled press returns REFUSED.

◆ rmgui_richtext_prepare_to()

int rmgui_richtext_prepare_to ( const rmgui_richtext_t * ,
rmgui_target_t *  )

◆ rmgui_richtext_paint_to()

void rmgui_richtext_paint_to ( rmgui_target_t * ,
const rmgui_richtext_t * ,
rmgui_rect_t rect,
rmgui_rect_t clip,
rmgui_richtext_inline_fn ,
void *  )

Binary-searches first visible cached line, then visits only visible lines and their spans.

Paint is O(log lines + visible bytes/spans), not total text. RECORD requires caller-prepared glyphs for every used font/size, including bullet U+2022. Inline hooks follow the same target resource contract.

◆ rmgui_richtext_paint()

void rmgui_richtext_paint ( rmgui_fb_t * ,
const rmgui_richtext_t * ,
rmgui_rect_t rect,
rmgui_rect_t clip,
rmgui_richtext_inline_fn ,
void *  )

◆ rmgui_a11y_from_richtext()

int rmgui_a11y_from_richtext ( const rmgui_richtext_t * ,
size_t span,
const char * name,
rmgui_a11y_record_t * out )

span=SIZE_MAX builds TEXT; a link span builds LINK.

Name is borrowed.

◆ rmgui_a11y_richtext_selection()

int rmgui_a11y_richtext_selection ( const rmgui_richtext_t * ,
size_t start,
size_t end,
rmgui_rect_t * rects,
size_t capacity,
size_t * count )

◆ rmgui_richtext_a11y_action()

int rmgui_richtext_a11y_action ( rmgui_richtext_t * ,
size_t span,
rmgui_input_result_t * out )

Accessibility PRESS on a link span; other host actions use input/select.

◆ rmgui_richtext_markdown()

int rmgui_richtext_markdown ( const char * source,
size_t length,
int base_px,
rmgui_richtext_markdown_t * out )

◆ rmgui_richtext_markdown_mapped()

int rmgui_richtext_markdown_mapped ( const char * source,
size_t length,
int base_px,
rmgui_richtext_markdown_t * out,
rmgui_richtext_markdown_map_t * map )

Like rmgui_richtext_markdown, with an optional map of ordered, coalesced runs covering the entire source.

Set runs and capacity; count is written on success. NULL runs is valid for an empty map; NULL map skips mapping. All buffers must be disjoint. On failure, neither output is modified.

Pass a caller-owned rmgui_richtext_markdown_map_t with runs and capacity to collect source-to-display ranges while parsing. Pass NULL for the same behavior as rmgui_richtext_markdown. Success sets count; failure leaves the document, map, and all buffers unchanged. A capacity of length runs always suffices: each run contains at least one source byte. Empty input needs no runs.

Runs cover the source in order, with no gaps or overlap. Adjacent ranges of the same kind are coalesced even if their styles differ. Visible ranges copy bytes verbatim: for a source byte p inside a visible run, its display byte is display_start + (p - source_start). Hidden ranges have display_length == 0; their display_start is the insertion boundary, not a displayed byte. Escapes hide the backslash, links hide the brackets, parentheses and destination, and formatting, heading and list markers are hidden when recognized. Literal unmatched syntax keeps its original bytes. The generated list bullet is a layout decoration, not a display byte.

To highlight a source range, intersect it with the visible runs and project only those intersections. In [a](https://host/b)b, the b inside the destination is hidden and the final b maps to byte 1 of ab.

◆ rmgui_richtext_markdown_mapped_ex()

int rmgui_richtext_markdown_mapped_ex ( const char * source,
size_t length,
int base_px,
const rmgui_richtext_markdown_options_t * options,
rmgui_richtext_markdown_t * out,
rmgui_richtext_markdown_map_t * map )

Like rmgui_richtext_markdown_mapped, parsed with options (NULL is the default grammar).

All inputs, outputs and the refusal contract are those of rmgui_richtext_markdown_mapped, and invalid options refuse before any output is written. Escapes, UTF-8 validation, the nonempty and no-leading-or-trailing space delimiter rules, one-based link ids and the source map invariants (runs cover the source in order, with no gaps or overlap; visible runs are verbatim; hidden runs have zero display length) are unchanged. Hidden delimiters and destinations never reach the display text, so selection and copy cannot expose them.