Repository navigation
Font System
Runtime font rendering system with TrueType loading, glyph atlas generation, and batched text layout for HUDs, dialogue, and UI overlays.
Source: SparkEngine/Source/Engine/Text/FontSystem.h
The Font System provides a complete text rendering pipeline built on top of stb_truetype. It loads TrueType fonts from disk, rasterizes glyphs into a packed atlas texture, and produces per-glyph quads that the renderer can batch into draw calls. Each font is loaded at a specific pixel size and assigned a unique FontId for later reference.
Text layout supports word wrapping via maxWidth, configurable line spacing and letter spacing, and left/center/right alignment. The system can measure text bounds without generating quads, which is useful for UI layout calculations. Bold and italic styles are synthesized (weight adjustment and shear transform) rather than requiring separate font files.
The glyph atlas uses a simple row-based packing algorithm. When a row fills up, the packer advances to the next row. If the atlas runs out of space a warning is logged. ASCII glyphs (32-126) are pre-rasterized by default on load; additional glyphs are rasterized on demand. The atlas exposes a dirty flag so the renderer knows when to re-upload the texture to the GPU.
| Class / Struct | Description |
|---|---|
FontSystem |
Singleton that manages font loading, glyph rasterization, text layout, and measurement |
TextStyle |
Parameters for text rendering: font, size, color, alignment, spacing, bold/italic |
GlyphMetrics |
Per-glyph metrics including advance, bearing, dimensions, and atlas UV coordinates |
TextQuad |
Output quad for a single visible glyph with screen position, UVs, and color |
TextBounds |
Bounding box result from MeasureText including width, height, and line count |
GlyphAtlas |
Single-channel bitmap atlas with packing state and dirty flag |
FontInfo |
Metadata for a loaded font: name, path, size, ascent, descent, glyph count |
auto& fonts = Spark::Text::FontSystem::GetInstance();
fonts.Initialize();
// Load a font at 24px
auto fontId = fonts.LoadFont("Assets/Fonts/Roboto.ttf", 24.0f);
// Configure text style
Spark::Text::TextStyle style;
style.fontId = fontId;
style.fontSize = 32.0f;
style.color = {1.0f, 0.8f, 0.0f, 1.0f};
style.alignment = Spark::Text::TextAlignment::Center;
style.maxWidth = 400.0f; // enable word wrapping
// Generate quads for rendering
auto quads = fonts.LayoutText("Hello, World!", 100.0f, 200.0f, style);
// Submit quads to sprite batch or text renderer...
// Measure without rendering (useful for UI layout)
auto bounds = fonts.MeasureText("Score: 1234", style);
float boxWidth = bounds.width;
float boxHeight = bounds.height;
// Check if atlas needs GPU re-upload
if (auto* atlas = fonts.GetAtlas(fontId); atlas && atlas->dirty)
{
UploadAtlasTexture(atlas->pixels.data(), atlas->width, atlas->height);
}| Method | Return | Description |
|---|---|---|
Initialize() |
void |
Initialize the font system and clear all state |
Shutdown() |
void |
Release all fonts and shut down |
LoadFont(path, size, preloadAscii) |
FontId |
Load a TrueType font file at the given pixel size |
UnloadFont(fontId) |
void |
Unload a font and free its atlas |
SetActiveFont(fontId) |
void |
Set the default font for layout calls |
GetActiveFont() |
FontId |
Get the current default font |
LayoutText(text, x, y, style) |
vector<TextQuad> |
Layout text into renderable glyph quads |
MeasureText(text, style) |
TextBounds |
Measure text bounding box without generating quads |
GetFontInfo(fontId) |
const FontInfo* |
Get metadata for a loaded font |
GetAtlas(fontId) |
const GlyphAtlas* |
Get atlas texture data for GPU upload |
GetFontCount() |
size_t |
Number of loaded fonts |
GetLoadedFonts() |
vector<FontId> |
All loaded font IDs |
SetDefaultAtlasSize(size) |
void |
Set atlas dimensions for newly loaded fonts |
| Field | Type | Default | Description |
|---|---|---|---|
fontId |
FontId |
INVALID_FONT_ID |
Font to use (0 uses active font) |
fontSize |
float |
24.0f |
Render size in pixels |
color |
TextColor |
White | RGBA text color |
alignment |
TextAlignment |
Left |
Horizontal alignment |
lineSpacing |
float |
1.2f |
Line height multiplier |
letterSpacing |
float |
0.0f |
Extra space between characters |
maxWidth |
float |
0.0f |
Max width for word wrapping (0 = no wrap) |
bold |
bool |
false |
Synthesized bold |
italic |
bool |
false |
Synthesized italic (shear) |
- UI System -- uses FontSystem for in-game UI text rendering
- 2D Rendering -- sprite batch can render text quads
- Localization System -- provides localized strings for text layout
- Dialogue System -- renders dialogue text via FontSystem
Published from 2b03dc797148. Edit the canonical source in wiki/.
- Documentation
- Docs route
- Wiki index
- Guides
- Tutorials
- Samples
- Examples
- API Reference
- API route
- Reference
- Build Guide
- Dependencies
- FAQ
- Changelog
- Roadmap
- Contributing
- Code of Conduct
- Home
- FAQ
- Getting Started
- Quick-Start Tutorial
- Making Your First Game
- Making Your First Multiplayer Game
- Artist Workflow Guide
- Editor Walkthrough
- Migration Guide
- How SparkEngine Works
- Architecture Overview
- Engine Architecture Flowchart
- Creating a Game Module
- Game Modules (catalog)
- Entity Component System
- Rendering and Graphics
- Physics
- Cloth Simulation
- Audio
- Input System
- Camera System
- Scripting with AngelScript
- Visual Scripting
- AI and Navigation
- Animation
- 2D Systems
- Networking
- Dedicated Server
- Multiplayer Quick Start
- Area Server Architecture
- Scene Management
- Large World Support
- Collaborative Editing
- Coroutine System
- Event System
- Event Response System
- Job System
- UI System
- UI Layout Extensions
- Localization
- Dialogue System
- Destruction System
- Replay System
- Achievement System
- Loading System
- Mod System
- Content Delivery
- Tween System
- Memory Integrity
- Gameplay Systems
- Terrain and Procedural Generation
- Save System
- Persistence System
- Day Night Cycle and Weather
- Cinematic Sequencer
- Runtime Prefabs
- SparkEditor
- Editor Tutorials
- SparkConsole
- SparkDaemon
- Shader Pipeline
- Asset Pipeline
- Asset Validation
- Asset Migration
- Game Packaging
- Online Services
- DataTable System
- Loot and Crafting System
- CSG System
- Font System
- Timer Manager
- Movie Render Pipeline
- HLOD and World Partition
- Remote Debug System
- Selection Manager
- Asset Dependency Graph
- Editor Automation
- File Watcher
- Project Templates
- System Requirements
- VR Support
- Mobile Platform
- Accessibility
- Platform Input
- Platform Certification
- Cross-Compilation: Wine Testing
- RHI Abstraction Layer
- D3D11 Backend
- D3D12 Backend
- Vulkan Backend
- OpenGL Backend
- Metal Backend
- DXR Raytracing
- Hybrid Ray Tracing
- Upscaling (DLSS/FSR)
- Render Graph
- Shader Graph
- GPU Particles
- GPU-Driven Rendering
- Volumetric Fog
- Volumetric Clouds
- Global Illumination
- Virtual Texturing
- Water Rendering
- Clustered Lighting
- Material System
- Post-Processing
- Shadow System
- Particle System
- Decal System
- Sky and Atmosphere
- Foliage System
- Mesh Shaders
- Neural Rendering
- Configuration Reference
- Performance Tips
- Benchmark Framework
- Threading Model
- Fuzz Policy and Parser Security
- Memory Safety
- Memory Management Patterns
- Build System and CMake Modules
- Profiler and Debugging
- Performance Profiling Guide
- Telemetry System
- Crash Reporting
- Golden Image Testing
- Utilities
- Testing
- Fuzz Policy and Parser Security
- Codebase Statistics
- Codebase Health
- Error Handling Patterns
- Hot Reload Overview
- Troubleshooting
- Contributing
- Workflow Patterns
- Build Optimizations
- CI Reproducible Builds
- GitHub API and PR Checks
- Git Rebase Conflicts
- Clang-Format
- Code Quality Violations
- AI Bloat Pattern
- MinGW + Wine Cross-Compilation
- Live Editor Testing
- Engine & Renderer Landscape
- DuetOS Portability Catalog
- Five-Engine Analysis
- Eleven-Engine Analysis
- ThorVG / Unity Graphics Analysis
- Advanced Techniques Catalog
- Third-Party Library Evaluation
- Engine Viability Evaluation
- Engine Feature Recommendations
- Project Recommendations
- Mac Compatibility Analysis
- Codebase Observations
- Codebase Bloat Audit
- Test Suite Audit
- Documentation Coverage Audit
- ThirdParty Dependencies Audit
- Load Test Baseline
- Gameplay Systems Status
- SparkGame Module Status
- Stub and Abandoned Features
- Memory Integrity System
- Memory Safety Evaluation
- Hardware Acceleration Systems
- Jolt Physics Integration
- GPU/CPU Separation Plan
- Daemon Services Architecture
- Reflection & Polymorphism Refactoring Plan
- SparkBuild In-Tree
- Wine No-JobSystem Breakthrough
- Wine Role and Fallback Tiers