cvkg-runic-text

cvkg-runic-text is the authoritative, high-performance text shaping, layout, and rasterization engine for the Cyber Viking Kvasir Graph (CVKG) framework. It bridges the gap between raw unicode strings and renderable GPU geometries, serving as a dedicated, stateless typography processor.
By isolating the heavy computational overhead of text shaping and system font database queries, cvkg-runic-text prevents operating system font-linking quirks and external HarfBuzz/Swash compilation cycles from impacting the compilation speed and safety of core CVKG subsystems.
Core Architecture & Data Flow
cvkg-runic-text operates as a pure data-driven pipeline. It accepts styled character streams and viewport constraints, computes layout geometry, resolves glyph-level fallbacks, caches shaping results, and outputs rasterized bitmaps or positioned glyph indexes ready for direct drawing by the GPU renderer (cvkg-render-gpu).
graph TD
Spans["Raw Spans (TextSpan Slice)"] --> BiDi["BiDi Analysis (unicode-bidi)"]
BiDi --> Fontdb["Font Query & Fallback (fontdb)"]
Fontdb --> Shaping["Complex OpenType Shaping (rustybuzz / HarfBuzz)"]
Shaping --> Fallback["Glyph-Level Fallback Resolution"]
Fallback --> Lines["Word Wrapping & Line Layout (layout_lines)"]
Lines --> Align["Alignment & Overflow Adjustments"]
Align --> Cache["Deterministic LRU Cache (swash-keyed)"]
Cache --> Output["ShapedText Result"]
Output --> Hits["Interactive Subsystem (hit_testing, selections)"]
Output --> Raster["Rasterization Engine (swash Scaler & Render)"]
Key Capabilities & Features
1. Complex Script & OpenType Shaping
- Fully integrated with
rustybuzz(HarfBuzz) to support ligatures, kerning, contextual alternates, and complex non-Latin scripts (Arabic, Devanagari, Hanzi, etc.). - Custom OpenType feature activation (e.g., standard ligatures
liga, kerningkern, contextual alternatescalt, or discretionary ligaturesdlig) viaOpenTypeFeaturetag/value tuples.
2. Variable Font Support
- Custom font axes setting via
VariableAxis, enabling seamless rendering of variable fonts across dynamic weight (wght), width (wdth), italicization (ital), and slant (slnt) vectors.
3. Rich Multi-Span Text Layout
- Facilitates styling text blocks using a slice of
TextSpans, allowing distinct colors, font sizes, families, letter spacing, word spacing, and text decorations within a single line or paragraph.
4. Bidirectional (BiDi) Layout
- Automatic LTR (Left-To-Right) and RTL (Right-To-Left) paragraph/span categorization powered by
unicode-bidi, enabling unified multi-lingual user interfaces.
5. Multi-Line Word Wrapping & Text Alignment
- Flexible text alignment modes:
Start,End,Center, andJustify. - Four distinct text overflow handling strategies:
Clip: Hard clip at boundaries.Ellipsis: Truncates overflowing text with a contextual...marker.Visible: Render text outside its constraints.WordWrap: Wraps words to the next line dynamically when constraints are met.
- Line height scaling using relative multipliers (e.g.,
1.2x) or absolute pixel heights.
6. Robust Glyph-Level Font Fallback
- Dynamically falls back on a character-by-character level. If the primary font is missing a character (glyph ID
0), the shaper checks system and user fallbacks sequentially to resolve the glyph, avoiding empty boxes or rendering breaks.
7. Interactive Caret Selection & Hit-Testing
- High-precision geometric mapping built directly into the layout result:
hit_test(byte_index): Maps a logical byte offset to its corresponding shaped visual glyph and cluster.cursor_position(byte_index): Computes the precise caret coordinates(x, line_index)on the visual layout.selection_rects(start, end): Calculates bounding boxes spanning across lines/glyphs for flawless highlighting.
8. High-Performance LRU Shaping Cache
- Transparently speeds up consecutive frames with an LRU cache keyed by a deterministic, process-independent
CacheKey(usingswashfont hashes rather than standard SlotMapfontdb::IDs which differ across runs).
9. High-Fidelity Swash Rasterization
- Leverages the
swashoutline scale renderer to generate RGBA premultiplied subpixel-offset glyph bitmaps. Supports falling back to fallback fonts during rasterization if a specific glyph cannot be rendered in the primary face.
Public API Reference
Core Types
| Struct / Enum | Description |
|---|---|
RunicTextEngine |
The central state container managing the loaded fontdb database, custom font binaries, the LRU shape cache, and the ScaleContext scaling/rasterization builder. |
TextStyle |
Rich text styling description. Supports a fluent, builder-like API. |
TextSpan |
Represents a styled slice of text with a logical byte_offset mapping. |
ShapedText |
The complete product of a layout pass, containing positioned GlyphInstances, layout bounds, line lists, and font metrics. |
GlyphInstance |
Spatial details of a shaped glyph (advance width, cluster ID, absolute baseline coordinates). |
GlyphImage |
A rasterized RGBA bitmap output for direct GPU texture atlas uploading. |
LineInfo |
Bounding information for an individual visual line of text. |
FontMetrics |
|
FontAxisInfo |
Describes a single variable font axis (tag, name, min/max/default values). |
Engine Methods
| Method | Description |
|---|---|
query_font_axes(family, font_size) |
Query a font's variable axes from the fvar table. Returns Ok(None) for non-variable fonts. |
Configuration Enums
TextAlign:Start(Default),End,Center,JustifyTextOverflow:Clip,Ellipsis,Visible,WordWrap(Default)LineHeight:Multiple(f32)(Default is1.2),Fixed(f32)TextDecorations:underline,strikethrough,overlineboolean flags.
Code Examples
1. Composing Rich Text & Building Styles
The TextStyle struct provides a fluent builder API for customizing typography:
use ;
// Create a primary style with custom metrics, variable axis, and ligatures
let bold_accent_style = new
.with_weight // Bold weight
.with_color // Vibrant Amber RGBA
.with_letter_spacing // Cyber-spaced kerning
.with_line_height_multiple // Clean line vertical spacing
.with_axis // Apply variable weight
.with_feature // Ensure standard ligatures
.with_underline; // Underline decoration
2. Multi-Line Layout with Alignment & Wrapping
Initialize the shaper engine, load custom fonts, and run a multi-span layout pass:
use ;
3. Interactive Caret Selection & Hit-Testing
Map screen mouse clicks to character boundaries and draw selection highlights:
use ShapedText;
4. Glyph Rasterization for Texture Atlases
Extract shaped glyph IDs and rasterize them into raw RGBA pixel arrays for GPU texturing:
use ;
Backward Compatibility Mode
For backward compatibility with legacy cvkg-render-gpu versions, RunicTextEngine exposes single-span APIs that wrap the multi-span layout architecture transparently:
// Basic single-span shaping
let shaped = engine.shape;
// Basic glyph key lookup and rasterization
if let Some = engine.rasterize
Known Limitations
- System Font Fallback: Font fallback relies on standard system locations; for consistent multi-platform UI branding (desktop/WASM), bundle and load critical branding fonts using
load_font_data. - Large Document Chunking: Large blocks of text (such as long documents or files) should be split into dynamic chunks rather than submitted as a single massive layouter pass to prevent shaping cache eviction overhead.
- Glyph Hinting: Visual rhythm is prioritized over absolute font alignment. Subpixel positioning is enabled, but standard hinting is disabled to maintain smooth fluid motion during layout scaling and scaling animations.