diff --git a/REFERENCE.md b/REFERENCE.md index 036dfcc..09c7854 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -645,20 +645,60 @@ renderer.setDefaultFont(@constCast(&render.font_8x16_latin1)); ``` **TrueType Fonts** (`src/render/ttf.zig`): + +TTF fonts are fully supported with real glyph rasterization and antialiasing. + ```zig const TtfFont = zcatgui.render.TtfFont; +const FontRef = zcatgui.render.FontRef; -var ttf = try TtfFont.loadFromFile(allocator, "font.ttf"); +// Load TTF from file +var ttf = try TtfFont.loadFromFile(allocator, "/usr/share/fonts/adwaita-sans-fonts/AdwaitaSans-Regular.ttf"); defer ttf.deinit(); -ttf.setSize(16); // Set render size +// Set render size (glyphs cached per size) +ttf.setSize(14); // 14pt + +// Get metrics const metrics = ttf.getGlyphMetrics('A'); -ttf.textWidth("Hello"); +const width = ttf.textWidth("Hello"); +const height = ttf.lineHeight(); +const asc = ttf.ascent(); // Baseline to top +const desc = ttf.descent(); // Baseline to bottom + +// Draw text directly +ttf.drawText(&framebuffer, x, y, "¡Hola, España!", color, clip); + +// Or use FontRef for unified bitmap/TTF API +const font_ref = FontRef{ .ttf = &ttf }; +font_ref.drawText(&framebuffer, x, y, "Hello", color, clip); ``` -> **Note**: TTF rendering currently uses placeholder glyphs. Full TTF rasterization -> (bezier curve rendering) is planned for future releases. For production use, -> prefer the bitmap fonts which have complete glyph rendering. +**Features:** +- Real bezier curve rasterization (quadratic beziers) +- Antialiasing via 2x supersampling +- Glyph caching (by codepoint + size) +- Alpha blending for smooth edges +- Full Unicode support (BMP via cmap format 4, full via format 12) + +**Recommended fonts (free, good Latin-1 support):** +| Font | Location (Fedora) | Notes | +|------|-------------------|-------| +| AdwaitaSans | `/usr/share/fonts/adwaita-sans-fonts/` | GNOME default, modern | +| DejaVu Sans | `/usr/share/fonts/dejavu-sans-fonts/` | Very complete | +| Droid Sans | `/usr/share/fonts/google-droid-sans-fonts/` | Android default | + +**Switching between Bitmap and TTF:** +```zig +// Use FontRef to switch fonts without changing widget code +var current_font: FontRef = .{ .bitmap = &render.font_8x16_latin1 }; + +// Later, switch to TTF +current_font = .{ .ttf = &ttf_font }; + +// Widget code stays the same +current_font.drawText(fb, x, y, text, color, clip); +``` ### UTF-8 Text Rendering