Operator SDK
Build MIDI processing and UI modes for the Operator device
Loading...
Searching...
No Matches
op::sdk::text Namespace Reference

Text measurement and layout helpers for mode UIs. More...

Functions

void format_uint (uint32_t v, char *out, uint8_t out_size)
 Overwrite out with the decimal digits of v, NUL-terminated.
void format_int (int32_t v, char *out, uint8_t out_size, bool force_plus=false)
 Overwrite out with the signed decimal of v, NUL-terminated.
void append_uint (char *buf, uint8_t buf_size, uint32_t v)
 Append the decimal digits of v at the existing NUL of buf.
void append_int (char *buf, uint8_t buf_size, int32_t v, bool force_plus=false)
 Append the signed decimal of v at the existing NUL of buf.
void format_str (const char *s, char *out, uint8_t out_size)
 Overwrite out with a bounded copy of s (freestanding strlcpy).
void append_str (char *buf, uint8_t buf_size, const char *s)
 Append a bounded copy of s at the existing NUL of buf (freestanding strcat).
void append_enum (char *buf, uint8_t buf_size, uint32_t idx, const char *const *labels, uint32_t count, const char *fallback="?")
 Append labels[idx] at the existing NUL of buf, bounds-checked.
void append_fixed (char *buf, uint8_t buf_size, int32_t value, uint8_t decimals)
 Append value as a fixed-point decimal with decimals fractional digits at the existing NUL of buf.
uint16_t strlen_chars (const char *s)
 Count the characters in a NUL-terminated string.
uint16_t text_width (const char *s)
 Pixel width of a string rendered in the standard 5x7 font.
uint16_t large_text_width (const char *s)
 Pixel width of a string rendered in the large 8x12 font.
uint16_t align_center_x (const char *s, uint16_t total_w)
 X-offset that horizontally centers a standard-font string inside a box.
uint16_t large_align_center_x (const char *s, uint16_t total_w)
 X-offset that horizontally centers a large-font string inside a box.
void truncate_with_ellipsis (const char *s, uint16_t max_chars, char *out, uint16_t out_size)
 Copy s into out, appending "..." when it exceeds max_chars.
uint8_t word_wrap (const char *s, uint16_t width_px, const char **line_starts, uint8_t max_lines)
 Greedy word-wrap a NUL-terminated string into lines no wider than width_px pixels (standard font).

Variables

constexpr uint8_t kFontStdAdvance = 6
 Horizontal advance (pixels) of one standard-font glyph.
constexpr uint8_t kFontStdSpaceAdvance = 4
 Horizontal advance (pixels) of the standard-font space glyph.
constexpr uint8_t kFontStdHeight = 8
 Line height (pixels) of the standard font.
constexpr uint8_t kFontLargeAdvance = 8
 Horizontal advance (pixels) of one large-font glyph.
constexpr uint8_t kFontLargeSpaceAdvance = 6
 Horizontal advance (pixels) of the large-font space glyph.
constexpr uint8_t kFontLargeHeight = 13
 Line height (pixels) of the large font.

Detailed Description

Text measurement and layout helpers for mode UIs.

Function Documentation

◆ align_center_x()

uint16_t op::sdk::text::align_center_x ( const char * s,
uint16_t total_w )
inline

X-offset that horizontally centers a standard-font string inside a box.

Parameters
sText to measure.
total_wWidth of the containing box in pixels.
Returns
Offset; 0 if the text is wider than the box.

◆ append_enum()

void op::sdk::text::append_enum ( char * buf,
uint8_t buf_size,
uint32_t idx,
const char *const * labels,
uint32_t count,
const char * fallback = "?" )
inline

Append labels[idx] at the existing NUL of buf, bounds-checked.

Parameters
bufNUL-terminated buffer to extend in place. Non-null.
buf_sizeTotal capacity of buf in bytes. The appended label truncates to fit; buf stays NUL-terminated.
idxIndex into labels. If idx >= count the fallback string is appended instead (no out-of-range read).
labelsCaller-owned label table (count entries). This helper only indexes it safely. The mode owns the storage.
countNumber of entries in labels.
fallbackAppended when idx is out of range (default "?").

Composes e.g. char b[24] = "Rate: "; append_enum(b, sizeof b, r, kRateLabels, kRateCount); -> "Rate: 1/4". Implemented via append_str. Freestanding: no libc, no double.

◆ append_fixed()

void op::sdk::text::append_fixed ( char * buf,
uint8_t buf_size,
int32_t value,
uint8_t decimals )
inline

Append value as a fixed-point decimal with decimals fractional digits at the existing NUL of buf.

Parameters
bufNUL-terminated buffer to extend in place. Non-null.
buf_sizeTotal capacity of buf in bytes. truncates to fit; buf stays NUL-terminated.
valueInteger carrying decimals fractional digits, the same fixed-point convention as a Numeric param's decimal_places.
decimalsNumber of fractional digits. 0 appends a plain integer.

Composes e.g. append_fixed(buf, size, 1250, 2) -> "12.50" and append_fixed(buf, size, -5, 2) -> "-0.05". Freestanding: no libc, no double, 32-bit division only.

◆ append_int()

void op::sdk::text::append_int ( char * buf,
uint8_t buf_size,
int32_t v,
bool force_plus = false )
inline

Append the signed decimal of v at the existing NUL of buf.

Parameters
bufNUL-terminated buffer to extend in place. Non-null.
buf_sizeTotal capacity in bytes. Appended text truncates to fit; buf stays NUL-terminated.
vValue to append (INT32_MIN .. INT32_MAX); INT32_MIN-safe.
force_plusWhen true, non-negative v appends a leading '+'.

Freestanding: no libc, no double, 32-bit division only.

◆ append_str()

void op::sdk::text::append_str ( char * buf,
uint8_t buf_size,
const char * s )
inline

Append a bounded copy of s at the existing NUL of buf (freestanding strcat).

Parameters
bufNUL-terminated buffer to extend in place (compose "Label: " + value without a libc strcat). Non-null.
buf_sizeTotal capacity of buf in bytes. The appended text truncates to fit the remaining space; buf stays NUL-terminated.
sSource string to append (nullptr is treated as the empty string).

Freestanding: no libc, no double.

◆ append_uint()

void op::sdk::text::append_uint ( char * buf,
uint8_t buf_size,
uint32_t v )
inline

Append the decimal digits of v at the existing NUL of buf.

Parameters
bufNUL-terminated buffer to extend in place (compose "Label: " + number without a libc strcat). Non-null.
buf_sizeTotal capacity of buf in bytes. The appended digits truncate to fit the remaining space; buf stays NUL-terminated.
vValue to append (0 .. 4294967295).

Freestanding: no libc, no double, 32-bit division only.

◆ format_int()

void op::sdk::text::format_int ( int32_t v,
char * out,
uint8_t out_size,
bool force_plus = false )
inline

Overwrite out with the signed decimal of v, NUL-terminated.

Parameters
vValue to format (INT32_MIN .. INT32_MAX). INT32_MIN is handled correctly (magnitude taken via negation of an int64 temporary, then narrowed, no 64-bit divide).
outDestination buffer, overwritten and NUL-terminated. Non-null, out_size >= 1.
out_sizeCapacity in bytes. Result truncates to fit. Size for the widest output: 12 bytes holds "-2147483648" + NUL (or "+2147483647" with force_plus).
force_plusWhen true, non-negative values get a leading '+' (+5 / -3). When false, non-negative values are bare (5 / -3). Negative values always get '-'.

Freestanding: no libc, no double, 32-bit division only.

◆ format_str()

void op::sdk::text::format_str ( const char * s,
char * out,
uint8_t out_size )
inline

Overwrite out with a bounded copy of s (freestanding strlcpy).

Parameters
sSource string (nullptr is treated as the empty string "").
outDestination buffer; out[0] is overwritten, NUL-terminated on return. Non-null with out_size >= 1.
out_sizeCapacity of out in bytes. The copy truncates to fit (out_size - 1 characters max) and out is always NUL-terminated.

Freestanding: no libc, no double. Use for the runtime-string overwrite primitive (filenames, resolved labels).

◆ format_uint()

void op::sdk::text::format_uint ( uint32_t v,
char * out,
uint8_t out_size )
inline

Overwrite out with the decimal digits of v, NUL-terminated.

Parameters
vValue to format (0 .. 4294967295).
outDestination buffer. out[0] is overwritten, NUL-terminated on return. Must be non-null with out_size >= 1.
out_sizeCapacity of out in bytes. The result is truncated to fit (the least-significant digits are dropped if the buffer is too small), so size out for the widest value: 11 bytes holds any uint32 ("4294967295" + NUL).

Freestanding: no libc, no double, 32-bit division only. v == 0 yields "0".

◆ large_align_center_x()

uint16_t op::sdk::text::large_align_center_x ( const char * s,
uint16_t total_w )
inline

X-offset that horizontally centers a large-font string inside a box.

Parameters
sText to measure.
total_wWidth of the containing box in pixels.
Returns
Offset; 0 if the text is wider than the box.

◆ large_text_width()

uint16_t op::sdk::text::large_text_width ( const char * s)
inline

Pixel width of a string rendered in the large 8x12 font.

Parameters
sNUL-terminated string (nullptr is treated as empty).
Returns
Sum of per-glyph advances (space counts as kFontLargeSpaceAdvance, every other glyph as kFontLargeAdvance), matching the firmware renderer. A flat strlen*advance over-measures strings with spaces.

◆ strlen_chars()

uint16_t op::sdk::text::strlen_chars ( const char * s)
inline

Count the characters in a NUL-terminated string.

Parameters
sNUL-terminated string (may be nullptr, in which case 0 is returned).
Returns
Number of bytes preceding the terminator.

◆ text_width()

uint16_t op::sdk::text::text_width ( const char * s)
inline

Pixel width of a string rendered in the standard 5x7 font.

Parameters
sNUL-terminated string (nullptr is treated as empty).
Returns
Sum of per-glyph advances (space counts as kFontStdSpaceAdvance, every other glyph as kFontStdAdvance), matching the firmware renderer so callers (centering, underlines, right-anchoring) align with the actual pixels. A flat strlen*advance over-measures any string containing spaces.

◆ truncate_with_ellipsis()

void op::sdk::text::truncate_with_ellipsis ( const char * s,
uint16_t max_chars,
char * out,
uint16_t out_size )
inline

Copy s into out, appending "..." when it exceeds max_chars.

Parameters
sSource string (nullptr treated as empty).
max_charsMaximum number of visible characters (including the ellipsis). Values below 4 behave like a plain truncate (no ellipsis).
outDestination buffer; NUL-terminated on return.
out_sizeSize of out in bytes (must be >= 1).

◆ word_wrap()

uint8_t op::sdk::text::word_wrap ( const char * s,
uint16_t width_px,
const char ** line_starts,
uint8_t max_lines )
inline

Greedy word-wrap a NUL-terminated string into lines no wider than width_px pixels (standard font).

Parameters
sSource string.
width_pxMaximum pixel width of a single line.
line_startsOutput array. Entry i points to the first character of line i inside the original buffer.
max_linesCapacity of line_starts.
Returns
Number of lines populated (<= max_lines).

Note: the input string is not modified, so callers treat line_starts[i] as the start of a line whose length extends until the next line's start or end-of-string (space trimmed on line breaks). For destructive wrapping use your own routine. This implementation keeps the helper allocation-free.

Variable Documentation

◆ kFontLargeSpaceAdvance

uint8_t op::sdk::text::kFontLargeSpaceAdvance = 6
inlineconstexpr

Horizontal advance (pixels) of the large-font space glyph.

Space renders narrower (6px advance), matching the device's large-font renderer.

◆ kFontStdSpaceAdvance

uint8_t op::sdk::text::kFontStdSpaceAdvance = 4
inlineconstexpr

Horizontal advance (pixels) of the standard-font space glyph.

Space renders narrower than other glyphs (4px advance), matching the device's standard-font renderer.