FastLED 3.9.15
Loading...
Searching...
No Matches
truetype.h
Go to the documentation of this file.
1#pragma once
2
3// TrueType Font API for FastLED
4//
5// Minimal example:
6//
7// #include "fl/font/truetype.h"
8// #include "fl/font/truetype.cpp.hpp" // Include ONCE in your project
9//
10// // Option 1: Use default embedded font (Covenant5x5 - 9.9KB, 5x5 pixel)
11// auto font = fl::Font::loadDefault();
12// fl::FontRenderer renderer(font, 10.0f); // 10px height
13//
14// // Option 2: Load custom font
15// auto font = fl::Font::load(fl::span<const uint8_t>(ttf_data, ttf_size));
16// fl::FontRenderer renderer(font, 14.0f); // 14px height
17//
18// // Render a character
19// fl::GlyphBitmap glyph = renderer.render('A');
20// for (int y = 0; y < glyph.height; ++y) {
21// for (int x = 0; x < glyph.width; ++x) {
22// uint8_t alpha = glyph.getPixel(x, y); // 0-255
23// // Draw to LED at (screenX + glyph.xOffset + x, screenY + glyph.yOffset + y)
24// }
25// }
26//
27// // String measurement and kerning
28// float width = renderer.measureString("Hello");
29// float kern = renderer.getKerning('A', 'V');
30
31#include "fl/stl/vector.h"
32#include "fl/stl/shared_ptr.h"
33#include "fl/stl/span.h"
34#include "fl/stl/stdint.h"
35#include "fl/stl/noexcept.h"
36
37namespace fl {
38
39// Forward declarations
40class FontImpl; // IWYU pragma: keep
41class FontRendererImpl; // IWYU pragma: keep
42
43// Font metrics returned by Font::getMetrics()
45 i32 ascent; // Units above baseline
46 i32 descent; // Units below baseline (typically negative)
47 i32 lineGap; // Additional spacing between lines
48 i32 x0, y0; // Bounding box min
49 i32 x1, y1; // Bounding box max
50};
51
52// Glyph (single character) metrics
54 i32 advanceWidth; // Horizontal advance after glyph
55 i32 leftSideBearing; // Left side bearing
56 i32 x0, y0; // Bounding box min
57 i32 x1, y1; // Bounding box max
58 bool isEmpty; // True if glyph has no visual representation
59};
60
61// Rendered glyph data
63 fl::vector<u8> data; // Grayscale bitmap (0=transparent, 255=opaque)
64 i32 width; // Bitmap width in pixels
65 i32 height; // Bitmap height in pixels
66 i32 xOffset; // X offset from origin to top-left of bitmap
67 i32 yOffset; // Y offset from origin to top-left of bitmap (typically negative)
68
70
71 // Returns true if this bitmap has valid data
72 bool valid() const { return !data.empty() && width > 0 && height > 0; }
73
74 // Get pixel value at (x, y) - returns 0 if out of bounds
75 u8 getPixel(i32 x, i32 y) const {
76 if (data.empty() || x < 0 || y < 0 || x >= width || y >= height) {
77 return 0;
78 }
79 return data[y * width + x];
80 }
81};
82
83// Font class - represents a loaded TrueType font
84class Font {
85public:
86 // Load the default embedded font (Covenant5x5 - 9.9KB, 5x5 pixel font)
87 // Returns nullptr if loading fails
89
90 // Load a font from raw TrueType data (.ttf file contents)
91 // Returns nullptr if the font data is invalid
93
94 // Load a specific font from a TrueType collection (.ttc file)
95 // fontIndex: 0-based index of the font in the collection
96 static fl::shared_ptr<Font> load(fl::span<const u8> fontData, i32 fontIndex);
97
98 virtual ~Font() FL_NOEXCEPT = default;
99
100 // Get the number of fonts in this file (1 for .ttf, possibly more for .ttc)
101 virtual i32 getNumFonts() const = 0;
102
103 // Get overall font metrics (unscaled)
104 virtual FontMetrics getMetrics() const = 0;
105
106 // Get scale factor to achieve a specific pixel height
107 virtual float getScaleForPixelHeight(float pixelHeight) const = 0;
108
109 // Get glyph metrics for a unicode codepoint (unscaled)
110 virtual GlyphMetrics getGlyphMetrics(i32 codepoint) const = 0;
111
112 // Get kerning adjustment between two characters (unscaled)
113 // Returns the adjustment to add to advance width
114 virtual i32 getKerning(i32 codepoint1, i32 codepoint2) const = 0;
115
116 // Render a single character to a grayscale bitmap
117 // Returns an empty GlyphBitmap if the character doesn't exist
118 virtual GlyphBitmap renderGlyph(i32 codepoint, float scale) const = 0;
119
120 // Render with antialiasing control
121 // oversampleX/Y: 1 = no oversampling, 2+ = oversample for smoother edges
122 virtual GlyphBitmap renderGlyph(i32 codepoint, float scale,
123 i32 oversampleX, i32 oversampleY) const = 0;
124
125protected:
126 Font() FL_NOEXCEPT = default;
127};
128
130
131// FontRenderer - convenient wrapper for rendering at a specific size
133public:
134 // Create a renderer for the given font at the specified pixel height
135 FontRenderer(FontPtr font, float pixelHeight);
136
138
139 // Check if renderer is valid
140 bool valid() const { return mFont != nullptr; }
141
142 // Get the pixel height this renderer was created with
143 float pixelHeight() const { return mPixelHeight; }
144
145 // Get the scale factor being used
146 float scale() const { return mScale; }
147
148 // Get scaled font metrics
150 float ascent;
151 float descent;
152 float lineGap;
153 float lineHeight() const { return ascent - descent + lineGap; }
154 };
155 ScaledMetrics getScaledMetrics() const;
156
157 // Render a character at the current size
158 // Uses 2x2 oversampling by default for smooth edges on LED displays
159 GlyphBitmap render(i32 codepoint) const;
160
161 // Render with custom oversampling
162 GlyphBitmap render(i32 codepoint, i32 oversampleX, i32 oversampleY) const;
163
164 // Render without antialiasing (1x1 oversampling)
165 GlyphBitmap renderNoAA(i32 codepoint) const;
166
167 // Get the advance width for a character (in pixels)
168 float getAdvance(i32 codepoint) const;
169
170 // Get kerning between two characters (in pixels)
171 float getKerning(i32 codepoint1, i32 codepoint2) const;
172
173 // Calculate the width of a string (in pixels)
174 // Includes kerning between characters
175 float measureString(const char* str) const;
176 float measureString(fl::span<const char> str) const;
177
178private:
181 float mScale;
182};
183
184} // namespace fl
fl::UISlider scale("Scale", 4,.1, 4,.1)
virtual GlyphMetrics getGlyphMetrics(i32 codepoint) const =0
virtual FontMetrics getMetrics() const =0
virtual i32 getKerning(i32 codepoint1, i32 codepoint2) const =0
virtual i32 getNumFonts() const =0
virtual GlyphBitmap renderGlyph(i32 codepoint, float scale) const =0
virtual ~Font() FL_NOEXCEPT=default
Font() FL_NOEXCEPT=default
static fl::shared_ptr< Font > load(fl::span< const u8 > fontData)
virtual float getScaleForPixelHeight(float pixelHeight) const =0
static fl::shared_ptr< Font > loadDefault()
FontRenderer(FontPtr font, float pixelHeight)
~FontRenderer() FL_NOEXCEPT
bool valid() const
Definition truetype.h:140
float pixelHeight() const
Definition truetype.h:143
float scale() const
Definition truetype.h:146
fl::shared_ptr< Font > FontPtr
Definition truetype.h:129
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
u8 getPixel(i32 x, i32 y) const
Definition truetype.h:75
bool valid() const
Definition truetype.h:72
fl::vector< u8 > data
Definition truetype.h:63
GlyphBitmap() FL_NOEXCEPT
Definition truetype.h:69