Skip to main content

Module text

Module text 

Source
Expand description

Text-metric helpers for axis-label fit (B3-1): ggplot-rs owns the outer-label-overflow and check.overlap logic centrally, instead of a downstream consumer post-processing rendered SVG (the old approach had to re-parse <text> elements out of the markup).

Ported verbatim from the consumer’s ticks.rs em table and TickLabel::bbox, which is the only reason the measurements match what callers previously saw.

Functions§

text_width
Estimated width (px) of text set at font size size, in the house sans (Hanken Grotesk, or the system UI font it falls back to). This is a heuristic per-character average-width table, not real font metrics — good enough for layout decisions (fit, overlap, auto-wrap), not for pixel-perfect text extents.
truncate_to_width
Shorten label with a trailing ellipsis (“…”) until it (plus the ellipsis) fits within max_width px at font size (#159: a vertical multi-column legend grid that still overflows after exhausting column reduction falls back to truncating its labels). None means label already fits — no truncation needed, so the caller keeps the byte-identical untruncated path. A max_width too narrow for even a bare “…” still returns Some("…") — the best any truncation can do — rather than an empty string.
wrap_text
Word-wraps text to lines no wider than max_width px at font size, measured with text_width (ggtext / str_wrap behaviour: break at whitespace; a single word wider than max_width on its own is hard-split so it never overflows the box). Returns vec![String::new()] for empty input and otherwise always at least one line, so a caller can take lines.len() as the wrapped block’s line count without a special case.
wrapped_block_width
The widest wrapped line’s width (px) at font size — the natural width of a wrapped text block, e.g. for a GeomLabel background box sized to fit its wrapped text.