wolfhece.opengl.glyph_atlas
MSDF glyph atlas for GPU text rendering.
Renders TrueType glyphs at high resolution via PIL, computes a Multi-channel Signed Distance Field (MSDF), and packs them into a 3-channel (RGB) OpenGL texture atlas. The MSDF preserves sharp corners that a single-channel SDF rounds off, giving noticeably crisper text at small pixel sizes.
Usage:
atlas = GlyphAtlas.get("arial.ttf") # cached singleton per font
atlas.bind(0) # bind to texture unit 0
m = atlas.get_metrics('A') # query glyph layout info
Author: HECE - University of Liege, Pierre Archambeau Date: 2026
Copyright (c) 2026 University of Liege. All rights reserved.
Module Contents
- class wolfhece.opengl.glyph_atlas.GlyphMetrics[source]
Layout metrics for a single glyph in the MSDF atlas.
All spatial values are normalised so that 1.0 equals one em (i.e. the
RENDER_SIZEused during atlas construction).
- class wolfhece.opengl.glyph_atlas.GlyphAtlas(font_name: str = 'arial.ttf', charset: list[str] | None = None)[source]
MSDF glyph atlas backed by a 3-channel RGB OpenGL texture.
Build once per font; the atlas is cached and reused for all rendering. Each channel encodes a signed distance field for a different subset of contour edges (grouped by normal direction). The fragment shader reconstructs sharp corners via
median(r, g, b).Encoding: 0.5 = edge, >0.5 = inside, <0.5 = outside (per channel).
- Parameters:
font_name – TrueType font file name (resolved via
wolfhece.textpillow.load_font()).charset – List of characters to include. Defaults to ASCII printable + common accented latin characters.
- _cache: dict[str, GlyphAtlas][source]
- _metrics: dict[str, GlyphMetrics][source]
- classmethod get(font_name: str = 'arial.ttf') GlyphAtlas[source]
Return a cached atlas for font_name, creating it if needed.
- static _compute_sdf(bitmap: numpy.ndarray, spread: float) numpy.ndarray[source]
Compute a single-channel SDF from a greyscale bitmap.
Returns array in
[0, 1]: 0.5 = edge, >0.5 = inside, <0.5 = outside. Falls back to a simple ramp when scipy is not installed.Kept as a building block for
_compute_msdf().
- static _compute_msdf(bitmap: numpy.ndarray, spread: float) numpy.ndarray[source]
Compute a 3-channel MSDF from a greyscale bitmap.
Each RGB channel encodes the signed distance to edge pixels whose gradient direction (outward normal) falls in a different 120° sector. The fragment shader recovers sharp corners via
median(r, g, b).Returns
(H, W, 3)float32 array in[0, 1]. Falls back to 3× duplicated SDF when scipy is not available.
- get_metrics(char: str) GlyphMetrics | None[source]
Return metrics for char, or
Noneif absent from the atlas.
- property metrics: dict[str, GlyphMetrics][source]
Full metrics dictionary (read-only view).