Expand description
Real text for densa: fonts, glyph rasterization and a glyph atlas cache.
Domain-agnostic: any widget that draws text goes through these three pieces.
Fontwraps an OpenType/TrueType font (skrifa): character mapping, advances, line metrics.Font::bundled_monois JetBrains Mono, shipped under the SIL OFL 1.1.rasterizeturns a glyph outline into an 8-bit coverage bitmap (zeno), optionally shifted by a fraction of a pixel.GlyphCachepacks rasterized glyphs into a paged texture atlas (etagere) that grows a page at a time and, once at its limit, evicts the least recently used glyphs (never the current frame’s); it queues theAtlasUpdates the GPU texture must follow.
A warm cache answers lookups without allocating, so per-frame text drawing stays allocation-free; rasterizing a glyph seen for the first time allocates, once.
use densa_text::{quantize_x, AtlasConfig, Font, GlyphCache, GlyphMap};
let mut chars = GlyphMap::new(Font::bundled_mono()?, 14.0);
let mut cache = GlyphCache::new(AtlasConfig::default());
cache.begin_frame();
let a = chars.get('A').expect("the bundled font has A");
let (_, subpixel) = quantize_x(10.25); // a pen at x = 10.25 px
let placed = cache.get(chars.font(), a.id, chars.px(), subpixel)?;
assert!(placed.size[0] > 0.0, "rasterized into the atlas");
assert_eq!(cache.cached_glyphs(), 1);
// Asked again (next frame, say): found, not rasterized again.
cache.begin_frame();
cache.get(chars.font(), a.id, chars.px(), subpixel)?;
assert_eq!(cache.cached_glyphs(), 1);Structs§
- Atlas
Config - Shape of the glyph atlas: square pages stacked vertically in one texture.
- Atlas
Limit Error - The device cannot hold the atlas: its texture limit is below the atlas already in use.
- Cache
Stats - Cumulative cache counters.
- Cached
Glyph - A glyph ready to draw from the atlas.
- Font
- One face of an OpenType/TrueType font file or collection. Cheap to clone (the bytes are
shared); clones keep the same
FontId, so they share cached glyphs. - FontId
- Identifies one loaded
Fontfor the lifetime of the process. - Glyph
Bitmap - An 8-bit coverage bitmap of one glyph, positioned relative to the pen.
- Glyph
Cache - Rasterized glyphs packed into a paged coverage atlas with per-glyph LRU eviction.
- GlyphId
- A glyph index within a font.
- Glyph
Map - Cached character-to-glyph mapping for one font at one size.
- Glyph
Run - Consecutive glyphs sharing one font, size and color: what a layout hands a renderer.
- Glyph
Upload - Pixels a renderer must copy into its atlas texture.
- Line
Metrics - Vertical metrics of a font at a given size, in pixels (y up:
descentis negative). - Mapped
Glyph - A character’s glyph and advance at the map’s size.
- Positioned
Glyph - A glyph placed by a text layout, relative to the layout’s origin.
Enums§
- Atlas
Update - A change the renderer must apply to its atlas texture, in queue order.
- Font
Error - Why font data could not be loaded.
- Glyph
Unavailable - Why
GlyphCache::gethas no glyph to draw.
Constants§
- BUNDLED_
MONO - JetBrains Mono Regular, bundled under the SIL Open Font License 1.1 (
fonts/OFL.txt). Monospace (every glyph advances 0.6 em), with accented Latin letters. - MAX_
CACHED_ CHARS - Non-ASCII characters remembered at most; past it the memory is forgotten and rebuilt.
- SUBPIXEL_
STEPS - Subpixel positions per pixel: glyphs are rasterized at 0, 1/4, 1/2 and 3/4 px offsets.
Functions§
- quantize_
x - Splits a horizontal position into a whole pixel and a subpixel bin (
0..SUBPIXEL_STEPS), rounding to the nearest bin. Draw the glyph cached forbinat the returned pixel. - rasterize
- Rasterizes
glyphatpxpixels per em, shifted right byx_offsetpixels (use a fraction in0..1for subpixel positioning).
Type Aliases§
- Font
Bytes - Font file bytes shared without copying: a
Vec, a&'static [u8], or another library’s shared buffer (such as a parley/fontiqueBlob).