Skip to main content

Crate densa_text

Crate densa_text 

Source
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.

  • Font wraps an OpenType/TrueType font (skrifa): character mapping, advances, line metrics. Font::bundled_mono is JetBrains Mono, shipped under the SIL OFL 1.1.
  • rasterize turns a glyph outline into an 8-bit coverage bitmap (zeno), optionally shifted by a fraction of a pixel.
  • GlyphCache packs 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 the AtlasUpdates 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§

AtlasConfig
Shape of the glyph atlas: square pages stacked vertically in one texture.
AtlasLimitError
The device cannot hold the atlas: its texture limit is below the atlas already in use.
CacheStats
Cumulative cache counters.
CachedGlyph
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 Font for the lifetime of the process.
GlyphBitmap
An 8-bit coverage bitmap of one glyph, positioned relative to the pen.
GlyphCache
Rasterized glyphs packed into a paged coverage atlas with per-glyph LRU eviction.
GlyphId
A glyph index within a font.
GlyphMap
Cached character-to-glyph mapping for one font at one size.
GlyphRun
Consecutive glyphs sharing one font, size and color: what a layout hands a renderer.
GlyphUpload
Pixels a renderer must copy into its atlas texture.
LineMetrics
Vertical metrics of a font at a given size, in pixels (y up: descent is negative).
MappedGlyph
A character’s glyph and advance at the map’s size.
PositionedGlyph
A glyph placed by a text layout, relative to the layout’s origin.

Enums§

AtlasUpdate
A change the renderer must apply to its atlas texture, in queue order.
FontError
Why font data could not be loaded.
GlyphUnavailable
Why GlyphCache::get has 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 for bin at the returned pixel.
rasterize
Rasterizes glyph at px pixels per em, shifted right by x_offset pixels (use a fraction in 0..1 for subpixel positioning).

Type Aliases§

FontBytes
Font file bytes shared without copying: a Vec, a &'static [u8], or another library’s shared buffer (such as a parley/fontique Blob).