Module text_metrics
Expand description
Shared text-metric arithmetic for widgets that paint a line of text themselves.
- Scale once. Take the style from the theme
(
crate::theme::Theme::text): it is already resolved (crate::text_render::TextScaling::Resolved) and the renderer paints it as given. A size-based renderer API (draw_text,measure, …) treats its size as a 1× base and multiplies it by the renderer’s text scale, so never hand it a theme size. - Centre on the measured line box, never on the font size.
cosmic-text lays a line out as
size × line_height, so centring on the bare size leaves the glyphs(line_height − 1) × size / 2too low. Measure with the style you draw with and hand the measured height tocentered_text_top— or usecrate::paint::PaintCtx::line_box.
Functions§
- at_
least_ whole - The authored (or token) 1×
minimum, or themeasuredline plus its pad rounded up to a whole pixel once scaled type needs more — never a fraction short of what the painter lays down, and a whole pixel at every scale. - authored_
h - An authored 1× height grown by exactly the extra room its text lines
(
sizes) need attext_scale, snapped up to a whole pixel: the design exactly at 1× (a 28 px row stays 28, not16.25 + 12 = 28.25), and at 2× grown by the lines’ growth alone. - centered_
text_ top - Top edge that centres a measured text box of
measured_line_heightin the bandband_y0 ..= band_y0 + band_height. - line_
growth - How much taller one line of
size-pt text is attext_scalethan at 1×; zero at or below 1×, so an authored design never shrinks. - line_
height_ px - Height of one line of
size-pt text attext_scalewithout a renderer in hand — exactly whatcrate::text_render::TextRenderer::measurereports for a single line,size × text_scale × line_heightof the legacyTextStyle.