![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
Greedy word-wrap over pre-measured, styled items. More...
#include "nilorea/n_wrap.h"Go to the source code of this file.
Data Structures | |
| struct | wrap_hit |
| state for n_wrap_index_at's per-line callback More... | |
| struct | wrap_span |
| state for n_wrap_span_rects' per-line callback More... | |
Macros | |
| #define | WRAP_NO_SPACE ((size_t)-1) |
| no break point seen on the current line | |
Functions | |
| size_t | n_wrap_index_at (const n_wrap_item *items, size_t count, double max_width, double line_height, double x, double y) |
| The item boundary nearest a point: the inverse of n_wrap_layout(), for turning a click or a drag into a caret or a selection end. | |
| size_t | n_wrap_layout (const n_wrap_item *items, size_t count, double max_width, n_wrap_emit emit, void *user) |
Break items into lines no wider than max_width. | |
| size_t | n_wrap_span_rects (const n_wrap_item *items, size_t count, double max_width, size_t lo, size_t hi, n_wrap_rect_emit emit, void *user) |
The rectangles that cover the items [lo, hi): one per line the range touches, in order, for painting a selection highlight. | |
| static void | wrap_hit_line (size_t first, size_t last, size_t line, double width, void *user) |
| static void | wrap_span_line (size_t first, size_t last, size_t line, double width, void *user) |
| struct wrap_hit |
| Data Fields | ||
|---|---|---|
| int | found | |
| const n_wrap_item * | items | |
| size_t | result | |
| size_t | want | |
| double | x | |
| struct wrap_span |
| Data Fields | ||
|---|---|---|
| n_wrap_rect_emit | emit | |
| size_t | hi | |
| const n_wrap_item * | items | |
| size_t | lo | |
| size_t | n | |
| void * | user | |
| #define WRAP_NO_SPACE ((size_t)-1) |
no break point seen on the current line
Definition at line 30 of file n_wrap.c.
Referenced by n_wrap_layout().
| size_t n_wrap_index_at | ( | const n_wrap_item * | items, |
| size_t | count, | ||
| double | max_width, | ||
| double | line_height, | ||
| double | x, | ||
| double | y ) |
The item boundary nearest a point: the inverse of n_wrap_layout(), for turning a click or a drag into a caret or a selection end.
The point is in the laid-out text's own coordinates (0,0 = the first line's top-left), with every line line_height tall. The layout is the one n_wrap_layout() produces for the same items and max_width, so a hit can never disagree with what was drawn.
Within a line, a point on the left half of an item resolves to the boundary before it and on the right half to the boundary after it, so an atomic item (an emoji cell, an inline image) is selected whole or not at all. A point past a line's end resolves to that line's end; above the first line to 0; below the last line to count.
| items | the items given to n_wrap_layout(); may be NULL if count is 0 |
| count | number of items |
| max_width | the width they were wrapped to |
| line_height | height of one line; a value <= 0 resolves every point to the first line |
| x | point, from the left edge |
| y | point, from the top edge |
Definition at line 176 of file n_wrap.c.
References wrap_hit::found, wrap_hit::items, n_wrap_layout(), wrap_hit::result, wrap_hit::want, wrap_hit_line(), and wrap_hit::x.
Referenced by main().
| size_t n_wrap_layout | ( | const n_wrap_item * | items, |
| size_t | count, | ||
| double | max_width, | ||
| n_wrap_emit | emit, | ||
| void * | user ) |
Break items into lines no wider than max_width.
Greedy: each line takes as much as fits, breaking at the last N_WRAP_SPACE that fits or, when the line holds no space, immediately before the item that overflows. An item that cannot fit even alone is placed alone rather than looping.
| items | pre-measured items; may be NULL when count is 0 |
| count | number of items |
| max_width | the width to wrap to; a value <= 0 puts every non-empty line on its own item, since nothing but the first item of a line can ever fit |
| emit | called once per line, or NULL to only count lines (the measure pass, same breaks, no output) |
| user | cookie handed back to emit |
Definition at line 50 of file n_wrap.c.
References n_wrap_item::flags, N_WRAP_NEWLINE, N_WRAP_SPACE, n_wrap_item::width, and WRAP_NO_SPACE.
Referenced by _wrap_words(), main(), md_flow(), n_wrap_index_at(), n_wrap_span_rects(), and run().
| size_t n_wrap_span_rects | ( | const n_wrap_item * | items, |
| size_t | count, | ||
| double | max_width, | ||
| size_t | lo, | ||
| size_t | hi, | ||
| n_wrap_rect_emit | emit, | ||
| void * | user ) |
The rectangles that cover the items [lo, hi): one per line the range touches, in order, for painting a selection highlight.
Uses the same layout as n_wrap_layout(), so the rectangles sit exactly under the drawn items. A space dropped at a line break belongs to no line and so lies under no rectangle; a line whose covered part has zero width (only a hard break) emits nothing. lo and hi may come in either order.
| items | the items given to n_wrap_layout() |
| count | number of items |
| max_width | the width they were wrapped to |
| lo | first boundary of the span |
| hi | other boundary (clamped to count) |
| emit | called once per covered line; may be NULL to only count |
| user | cookie handed back to emit |
Definition at line 249 of file n_wrap.c.
References wrap_span::emit, wrap_span::hi, wrap_span::items, wrap_span::lo, wrap_span::n, n_wrap_layout(), wrap_span::user, and wrap_span_line().
Referenced by main().
|
static |
Definition at line 133 of file n_wrap.c.
References wrap_hit::found, wrap_hit::items, last, line(), wrap_hit::result, wrap_hit::want, n_wrap_item::width, and wrap_hit::x.
Referenced by n_wrap_index_at().
|
static |
Definition at line 210 of file n_wrap.c.
References wrap_span::emit, wrap_span::hi, wrap_span::items, last, line(), wrap_span::lo, wrap_span::n, wrap_span::user, and n_wrap_item::width.
Referenced by n_wrap_span_rects().