Skip to main content

Module text_metrics

Module text_metrics 

Expand description

Shared text-metric arithmetic for widgets that paint a line of text themselves.

  1. 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.
  2. 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 / 2 too low. Measure with the style you draw with and hand the measured height to centered_text_top — or use crate::paint::PaintCtx::line_box.

Functions§

at_least_whole
The authored (or token) 1× minimum, or the measured line 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 at text_scale, snapped up to a whole pixel: the design exactly at 1× (a 28 px row stays 28, not 16.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_height in the band band_y0 ..= band_y0 + band_height.
line_growth
How much taller one line of size-pt text is at text_scale than at 1×; zero at or below 1×, so an authored design never shrinks.
line_height_px
Height of one line of size-pt text at text_scale without a renderer in hand — exactly what crate::text_render::TextRenderer::measure reports for a single line, size × text_scale × line_height of the legacy TextStyle.