On This Page
TrueType Font Support
guideXOS now includes experimental support for loading and rendering TrueType (.ttf) and OpenType (.otf) fonts, enabling scalable text rendering without pre-generated bitmap fonts.
TrueType support is currently in **early development**. The implementation provides basic font parsing and placeholder glyph rendering. Full contour rasterization is planned for future versions.
Overview
TrueType fonts are the industry standard for scalable font rendering. Unlike bitmap fonts (which store pre-rendered pixel grids), TrueType fonts store mathematical outlines of each character that can be scaled to any size without loss of quality.
Why TrueType?
- Resolution Independence: Text looks sharp at any size
- Smaller Files: One TTF file vs hundreds of bitmap PNG files
- Unicode Support: Access to thousands of characters
- Professional Typography: Kerning, ligatures, hinting (future)
- Industry Standard: Use any .ttf/.otf font
Architecture
TrueTypeFont Class
The main font loader in Kernel\Misc\TrueTypeFont.cs
handles:
- Parsing TTF/OTF binary format
- Reading font tables (cmap, glyf, head, hhea, hmtx, maxp, loca)
- Character-to-glyph mapping via cmap
- Glyph metrics (advance width, bounding boxes)
- Rasterizing glyphs to bitmaps
Font Loading Pipeline
- Load TTF File: Read font binary data from filesystem
- Parse Tables: Locate required font tables in file
- Cache Metrics: Store glyph offsets and advance widths
- Build cmap: Map Unicode characters to glyph indices
- Ready: Font ready for rendering
Usage
1. Loading a TrueType Font
using guideXOS.Misc;
using guideXOS.FS;
// Load font from file at 16pt
byte[] fontData = File.ReadAllBytes("fonts/DejaVuSans.ttf");
TrueTypeFont font = new TrueTypeFont(fontData, 16);
2. Rendering Text
// Draw string with white color
font.DrawString(Framebuffer.Graphics, 100, 100, "Hello World!", 0xFFFFFFFF);
// Measure string width
int width = font.MeasureString("Hello World!");
// Get advance width for single character
int charWidth = font.GetAdvanceWidth('A');
3. Rendering Individual Characters
// Render character to bitmap
Image glyph = font.RenderChar('A');
// Draw glyph manually
Graphics.DrawImage(glyph, x, y);
// Dispose when done
glyph.Dispose();
Font Tables Parsed
| Table | Purpose | Status |
|---|---|---|
| 'cmap' | Character-to-glyph mapping | ? Format 4 supported |
| 'glyf' | Glyph outline data | ?? Partially implemented |
| 'head' | Font header (units per em) | ? Fully supported |
| 'hhea' | Horizontal metrics header | ? Fully supported |
| 'hmtx' | Horizontal metrics (advance widths) | ? Fully supported |
| 'loca' | Glyph location index | ? Both formats supported |
| 'maxp' | Maximum profile (glyph count) | ? Fully supported |
Current Limitations
- Glyph contours are not yet rasterized (placeholder boxes shown)
- No Bézier curve support for smooth outlines
- Composite glyphs (accented characters) not supported
- No hinting or grid-fitting
- No kerning table support
- Only cmap format 4 (BMP Unicode) supported
- No OpenType advanced features (GSUB/GPOS)
Future Enhancements
- ? Full glyph contour parsing and rasterization
- ? Quadratic Bézier curve rendering
- ? Scanline rasterization with anti-aliasing
- ? Glyph caching for performance
- ? Kerning table (kern) support
- ? Vertical metrics (vhea/vmtx)
Bitmap Fonts vs TrueType Fonts
| Aspect | Bitmap Fonts | TrueType Fonts |
|---|---|---|
| File Format | PNG sprite sheet | .ttf/.otf binary |
| Scalability | ? Fixed sizes only | ? Any size |
| Rendering Speed | ? Very fast (blit) | ?? Slower (rasterize) |
| Memory Usage | ?? High (cached bitmaps) | ? Lower (outline data) |
| Character Set | ?? Limited (~100 chars) | ? Thousands (Unicode) |
| File Size | ?? 50-200 KB per font | ? 30-500 KB total |
| Quality | ?? Pixelated when scaled | ? Always crisp |
| Best For | Console, retro UI, pixel art | GUI, documents, modern UI |
Technical Details
TrueType File Structure
TTF File:
??? Offset Table (signature, table count)
??? Table Directory (table tags and offsets)
??? Font Tables:
? ??? cmap (character mapping)
? ??? glyf (glyph outlines)
? ??? head (font header)
? ??? hhea (horizontal header)
? ??? hmtx (horizontal metrics)
? ??? loca (glyph locations)
? ??? maxp (maximum profile)
? ??? ... (optional tables)
Coordinate System
TrueType fonts use a coordinate system called "Font Units per EM" (typically 1024, 2048, or 4096). All glyph coordinates are in this space and must be scaled to the desired pixel size:
pixelSize = (fontUnits / unitsPerEm) * fontSize
Example: 16pt font with 2048 units per EM:
scale = 16 / 2048 = 0.0078125
If glyph width = 1000 units ? pixel width = 7.8125 ? 8 pixels
Recommended Fonts
| Font Family | Type | Best For | License |
|---|---|---|---|
| DejaVu Sans | Sans-serif | UI, menus, buttons | Free (Public Domain) |
| DejaVu Serif | Serif | Documents, body text | Free (Public Domain) |
| DejaVu Sans Mono | Monospace | Code, terminal, console | Free (Public Domain) |
| Liberation Sans | Sans-serif | UI, headings | Free (GPL + exception) |
| Noto Sans | Sans-serif | Multilingual support | Free (OFL) |