Skip to main content

astrelis_text/
lib.rs

1//! Astrelis Text - Text rendering with cosmic-text
2//!
3//! This crate provides modular text rendering capabilities:
4//! - Font management with system fonts and custom fonts
5//! - Text builder with styling (size, color, alignment, etc.)
6//! - GPU-accelerated text rendering with zero-cost backend selection
7//! - Signed Distance Field (SDF) rendering for scalable text and effects
8//!
9//! ## Zero-Cost Renderer Selection
10//!
11//! Choose the renderer that fits your memory budget:
12//!
13//! | Renderer | Memory | Use Case |
14//! |----------|--------|----------|
15//! | [`BitmapTextRenderer`] | ~8 MB | Small text, UI labels, no effects needed |
16//! | [`SdfTextRenderer`] | ~8 MB | Large text, titles, needs shadows/outlines/glows |
17//! | [`FontRenderer`] | ~16 MB | Mixed usage, backwards compatibility (default) |
18//!
19//! Memory can be further reduced with [`TextRendererConfig`]:
20//! - `small()`: 512x512 atlas (~1 MB per renderer)
21//! - `medium()`: 1024x1024 atlas (~4 MB per renderer)
22//! - `large()`: 2048x2048 atlas (~8 MB per renderer, default)
23//!
24//! ## Quick Start
25//!
26//! ```rust,no_run
27//! use astrelis_text::{FontSystem, FontRenderer, Text, Color};
28//! use astrelis_render::GraphicsContext;
29//! use astrelis_core::math::Vec2;
30//!
31//! let context = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
32//! let font_system = FontSystem::with_system_fonts();
33//! let mut renderer = FontRenderer::new(context, font_system);
34//!
35//! // Create styled text with builder pattern
36//! let text = Text::new("Hello, World!")
37//!     .size(24.0)
38//!     .color(Color::WHITE)
39//!     .bold();
40//!
41//! // Prepare and draw
42//! let mut buffer = renderer.prepare(&text);
43//! renderer.draw_text(&mut buffer, Vec2::new(100.0, 100.0));
44//!
45//! // Render to a render pass
46//! // renderer.render(render_pass, viewport_size);
47//! ```
48//!
49//! ## Features
50//!
51//! - **System Fonts**: Automatically loads all system fonts
52//! - **Custom Fonts**: Load .ttf and .otf files from disk or memory
53//! - **Rich Styling**: Font size, weight, style, color, alignment, wrapping
54//! - **Builder Pattern**: Fluent API for text configuration
55//! - **GPU Accelerated**: WGPU-based rendering with texture atlas
56//! - **Text Layout**: Multi-line text with automatic wrapping
57//! - **Asset Integration**: Load fonts through the asset system (with `asset` feature)
58//! - **SDF Rendering**: Resolution-independent text scaling and effects
59//! - **Text Effects**: Shadows, outlines, glows, and more
60//!
61//! ## SDF (Signed Distance Field) Rendering
62//!
63//! SDF rendering enables sharp text at any scale and high-quality effects. The renderer uses
64//! a hybrid approach for optimal quality:
65//!
66//! - **Bitmap atlas** for small text (< 24px) without effects - sharper at small sizes
67//! - **SDF atlas** for large text (>= 24px) or text with effects - scalable and smooth
68//!
69//! ### When to Use SDF
70//!
71//! SDF rendering is automatically enabled for:
72//! - Large text (24px and above)
73//! - Text with effects (shadows, outlines, glows)
74//! - Text that needs to scale dynamically
75//!
76//! ### Basic SDF Usage
77//!
78//! ```rust,no_run
79//! use astrelis_text::{Text, TextEffect, Color};
80//! use astrelis_core::math::Vec2;
81//!
82//! // Text with a drop shadow
83//! let text = Text::new("Hello")
84//!     .size(32.0)
85//!     .with_shadow(Vec2::new(2.0, 2.0), Color::rgba(0.0, 0.0, 0.0, 0.5));
86//!
87//! // Text with an outline
88//! let text = Text::new("Bold")
89//!     .size(48.0)
90//!     .with_outline(2.0, Color::BLACK);
91//!
92//! // Combine multiple effects
93//! let text = Text::new("Glowing")
94//!     .size(36.0)
95//!     .with_shadow(Vec2::new(1.0, 1.0), Color::BLACK)
96//!     .with_outline(1.5, Color::WHITE)
97//!     .with_glow(4.0, Color::BLUE, 0.8);
98//! ```
99//!
100//! ### Force SDF Mode
101//!
102//! You can force SDF rendering for better scalability:
103//!
104//! ```rust,no_run
105//! use astrelis_text::Text;
106//!
107//! let text = Text::new("Scalable")
108//!     .size(16.0)
109//!     .sdf();  // Force SDF even for small text
110//! ```
111//!
112//! ## Examples
113//!
114//! Run the examples to see text rendering in action:
115//!
116//! ```bash
117//! cargo run --package astrelis-text --example text_demo
118//! cargo run --package astrelis-text --example text_effects
119//! cargo run --package astrelis-text --example rich_text_demo
120//! ```
121
122pub mod cache;
123pub mod decoration;
124pub mod editor;
125pub mod effects;
126pub mod error;
127pub mod font;
128pub mod pipeline;
129pub mod renderer;
130pub mod rich_text;
131pub mod sdf;
132pub mod shaping;
133pub mod text;
134
135#[cfg(feature = "asset")]
136pub mod asset;
137
138// Re-export main types
139pub use cache::{ShapeKey, ShapedTextData, TextShapeCache};
140pub use decoration::{
141    BackgroundGeometry, DecorationGeometry, DecorationQuad, DecorationQuadType, LineStyle,
142    StrikethroughStyle, TextBounds, TextDecoration, UnderlineStyle, generate_decoration_geometry,
143    generate_decoration_quads,
144};
145pub use editor::{TextCursor, TextEditor, TextSelection};
146pub use effects::{
147    EffectRenderConfig, TextEffect, TextEffectType, TextEffects, TextEffectsBuilder,
148};
149pub use error::{TextError, TextResult};
150pub use font::{FontAttributes, FontDatabase, FontStretch, FontStyle, FontSystem, FontWeight};
151pub use pipeline::{
152    RequestId, ShapedTextResult as PipelineShapedTextResult, SyncTextShaper, TextPipeline,
153    TextShapeRequest, TextShaper,
154};
155pub use renderer::{
156    // Common types
157    AtlasEntry,
158    // Renderers
159    BitmapTextRenderer,
160    DecorationRenderer,
161    DecorationVertex,
162    FontRenderer,
163    GlyphPlacement,
164    SdfAtlasEntry,
165    SdfCacheKey,
166    SdfParams,
167    SdfTextRenderer,
168    SharedContext,
169    TextBuffer,
170    TextRender,
171    TextRendererConfig,
172    TextVertex,
173};
174pub use rich_text::{RichText, RichTextBuilder, TextSpan, TextSpanStyle};
175pub use sdf::{SdfConfig, TextRenderMode, generate_sdf, generate_sdf_smooth};
176pub use shaping::{
177    ShapedGlyph, ShapedTextResult, extract_glyphs_from_buffer, measure_text_fast, shape_text,
178};
179pub use text::{LineBreakConfig, Text, TextAlign, TextMetrics, TextWrap, VerticalAlign};
180
181// Re-export asset types when feature is enabled
182#[cfg(feature = "asset")]
183pub use asset::{FontAsset, FontFormat, FontLoader};
184
185// Re-export cosmic-text types needed for retained rendering
186pub use cosmic_text::CacheKey;
187
188// Re-export Color from astrelis-render
189pub use astrelis_render::Color;
190
191// Re-export math types from astrelis-core
192pub use astrelis_core::math::Vec2;