Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_wrap.c File Reference

Greedy word-wrap over pre-measured, styled items. More...

#include "nilorea/n_wrap.h"
Include dependency graph for n_wrap.c:

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)

Detailed Description

Greedy word-wrap over pre-measured, styled items.

Author
Castagnier Mickael
Version
1.0

Definition in file n_wrap.c.


Data Structure Documentation

◆ wrap_hit

struct wrap_hit

state for n_wrap_index_at's per-line callback

Definition at line 125 of file n_wrap.c.

Collaboration diagram for wrap_hit:
Data Fields
int found
const n_wrap_item * items
size_t result
size_t want
double x

◆ wrap_span

struct wrap_span

state for n_wrap_span_rects' per-line callback

Definition at line 202 of file n_wrap.c.

Collaboration diagram for 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

Macro Definition Documentation

◆ WRAP_NO_SPACE

#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().

Function Documentation

◆ n_wrap_index_at()

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.

Parameters
itemsthe items given to n_wrap_layout(); may be NULL if count is 0
countnumber of items
max_widththe width they were wrapped to
line_heightheight of one line; a value <= 0 resolves every point to the first line
xpoint, from the left edge
ypoint, from the top edge
Returns
a boundary index in [0, count]: "before item i", count = after the last
Examples
ex_wrap.c.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ n_wrap_layout()

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.

Parameters
itemspre-measured items; may be NULL when count is 0
countnumber of items
max_widththe 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
emitcalled once per line, or NULL to only count lines (the measure pass, same breaks, no output)
usercookie handed back to emit
Returns
the number of lines, always >= 1
Examples
ex_wrap.c.

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

Here is the caller graph for this function:

◆ n_wrap_span_rects()

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.

Parameters
itemsthe items given to n_wrap_layout()
countnumber of items
max_widththe width they were wrapped to
lofirst boundary of the span
hiother boundary (clamped to count)
emitcalled once per covered line; may be NULL to only count
usercookie handed back to emit
Returns
the number of rectangles
Examples
ex_wrap.c.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wrap_hit_line()

void wrap_hit_line ( size_t first,
size_t last,
size_t line,
double width,
void * user )
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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wrap_span_line()

void wrap_span_line ( size_t first,
size_t last,
size_t line,
double width,
void * user )
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().

Here is the call graph for this function:
Here is the caller graph for this function: