Astrelis Text - Text rendering with cosmic-text
This crate provides modular text rendering capabilities:
- Font management with system fonts and custom fonts
- Text builder with styling (size, color, alignment, etc.)
- GPU-accelerated text rendering with zero-cost backend selection
- Signed Distance Field (SDF) rendering for scalable text and effects
Zero-Cost Renderer Selection
Choose the renderer that fits your memory budget:
| Renderer | Memory | Use Case |
|---|---|---|
[BitmapTextRenderer] |
~8 MB | Small text, UI labels, no effects needed |
[SdfTextRenderer] |
~8 MB | Large text, titles, needs shadows/outlines/glows |
[FontRenderer] |
~16 MB | Mixed usage, backwards compatibility (default) |
Memory can be further reduced with [TextRendererConfig]:
small(): 512x512 atlas (~1 MB per renderer)medium(): 1024x1024 atlas (~4 MB per renderer)large(): 2048x2048 atlas (~8 MB per renderer, default)
Quick Start
use ;
use GraphicsContext;
use Vec2;
let context = new_owned_sync.expect;
let font_system = with_system_fonts;
let mut renderer = new;
// Create styled text with builder pattern
let text = new
.size
.color
.bold;
// Prepare and draw
let mut buffer = renderer.prepare;
renderer.draw_text;
// Render to a render pass
// renderer.render(render_pass, viewport_size);
Features
- System Fonts: Automatically loads all system fonts
- Custom Fonts: Load .ttf and .otf files from disk or memory
- Rich Styling: Font size, weight, style, color, alignment, wrapping
- Builder Pattern: Fluent API for text configuration
- GPU Accelerated: WGPU-based rendering with texture atlas
- Text Layout: Multi-line text with automatic wrapping
- Asset Integration: Load fonts through the asset system (with
assetfeature) - SDF Rendering: Resolution-independent text scaling and effects
- Text Effects: Shadows, outlines, glows, and more
SDF (Signed Distance Field) Rendering
SDF rendering enables sharp text at any scale and high-quality effects. The renderer uses a hybrid approach for optimal quality:
- Bitmap atlas for small text (< 24px) without effects - sharper at small sizes
- SDF atlas for large text (>= 24px) or text with effects - scalable and smooth
When to Use SDF
SDF rendering is automatically enabled for:
- Large text (24px and above)
- Text with effects (shadows, outlines, glows)
- Text that needs to scale dynamically
Basic SDF Usage
use ;
use Vec2;
// Text with a drop shadow
let text = new
.size
.with_shadow;
// Text with an outline
let text = new
.size
.with_outline;
// Combine multiple effects
let text = new
.size
.with_shadow
.with_outline
.with_glow;
Force SDF Mode
You can force SDF rendering for better scalability:
use Text;
let text = new
.size
.sdf; // Force SDF even for small text
Examples
Run the examples to see text rendering in action: