simvx.graphics.draw2d_text

MSDF text rendering for Draw2D.

Handles font loading, glyph layout, and text measurement using the shared TextRenderer and MSDF atlas infrastructure.

Module Contents

Classes

Draw2DTextMixin

Mixin providing MSDF text rendering and measurement for Draw2D.

Data

log

API

simvx.graphics.draw2d_text.log

‘getLogger(…)’

class simvx.graphics.draw2d_text.Draw2DTextMixin[source]

Mixin providing MSDF text rendering and measurement for Draw2D.

classmethod set_font(path: str | None = None, size: int = 48) None[source]

Load an MSDF font atlas via the shared TextRenderer.

classmethod draw_text(text, pos=None, *, colour=None, scale=1.0, rect=None, alignment='left', vertical_alignment='top', fit_to_width=False, min_scale=None)[source]

Draw text at pos or inside rect with optional alignment.

Two positioning modes:

  • pos=(x, y): text top-left at the given coordinate (legacy form).

  • rect=(x, y, w, h): text positioned by alignment (left/center/ right) and vertical_alignment (top/center/bottom) inside the rect. fit_to_width=True shrinks scale so the text fits rect.w, clamped to min_scale.

min_scale semantics:

  • None (default): the readable-pixel safety net (~10px) is applied automatically. Tiny scale values are silently bumped up so MSDF glyphs stay legible.

  • explicit value: the caller’s floor is honoured exactly. The readable safety net is bypassed: the caller has taken responsibility for legibility (e.g. shrinking labels to fit a cramped inventory cell).

classmethod text_height(text, scale=1.0)[source]

Height of text in pixels at scale.

Multi-line strings (containing \n) accumulate line heights using the font’s line-height metric. Returns 0 for empty input.

classmethod text_size(text, scale=1.0)[source]

Return (width, height) in pixels at scale.

classmethod fit_scale(text, max_width, *, base_scale=1.0, min_scale=None)[source]

Largest scale ≤ base_scale that fits text within max_width.

Returns base_scale when the text already fits or when inputs are degenerate. The returned value matches what :meth:draw_text would actually use, so callers can compute width metrics that align with what’s drawn.

min_scale semantics mirror :meth:draw_text:

  • None (default): clamps from below by the readable-pixel floor (~10px) so the returned scale never produces illegible MSDF output.

  • explicit value: the caller’s floor is honoured exactly, bypassing the readable safety net.

classmethod text_width(text, scale=1)[source]

Width of text in pixels at scale.

Multi-line strings (containing \n) return the widest line; newlines themselves contribute no horizontal advance.