ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
font.cpp
Go to the documentation of this file.
1#include "font.h"
2
4#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace font {
9static const char *const TAG = "font";
10
11#ifdef USE_LVGL_FONT
12static const uint8_t OPA4_TABLE[16] = {0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255};
13
14static const uint8_t OPA2_TABLE[4] = {0, 85, 170, 255};
15
16const void *Font::get_glyph_bitmap(lv_font_glyph_dsc_t *dsc, lv_draw_buf_t *draw_buf) {
17 const auto *font = dsc->resolved_font;
18 auto *const fe = (Font *) font->dsc;
19
20 const auto *gd = fe->get_glyph_data_(dsc->gid.index);
21 if (gd == nullptr) {
22 return nullptr;
23 }
24
25 const uint8_t *bitmap_in = gd->data;
26 uint8_t *bitmap_out_tmp = draw_buf->data;
27 int32_t i = 0;
28 int32_t x, y;
29 uint32_t stride = lv_draw_buf_width_to_stride(gd->width, LV_COLOR_FORMAT_A8);
30
31 switch (fe->get_bpp()) {
32 case 1: {
33 uint8_t mask = 0;
34 uint8_t byte = 0;
35 for (y = 0; y != gd->height; y++) {
36 for (x = 0; x != gd->width; x++) {
37 if (mask == 0) {
38 mask = 0x80;
39 byte = *bitmap_in++;
40 }
41 bitmap_out_tmp[x] = byte & mask ? 255 : 0;
42 mask >>= 1;
43 }
44 bitmap_out_tmp += stride;
45 }
46 } break;
47
48 case 2:
49 for (y = 0; y != gd->height; y++) {
50 for (x = 0; x != gd->width; x++, i++) {
51 switch (i & 0x3) {
52 default:
53 bitmap_out_tmp[x] = OPA2_TABLE[(*bitmap_in) >> 6];
54 break;
55 case 1:
56 bitmap_out_tmp[x] = OPA2_TABLE[((*bitmap_in) >> 4) & 0x3];
57 break;
58 case 2:
59 bitmap_out_tmp[x] = OPA2_TABLE[((*bitmap_in) >> 2) & 0x3];
60 break;
61 case 3:
62 bitmap_out_tmp[x] = OPA2_TABLE[((*bitmap_in) >> 0) & 0x3];
63 bitmap_in++;
64 }
65 }
66 bitmap_out_tmp += stride;
67 }
68 break;
69
70 case 4:
71 for (y = 0; y != gd->height; y++) {
72 for (x = 0; x != gd->width; x++, i++) {
73 i = i & 0x1;
74 if (i == 0) {
75 bitmap_out_tmp[x] = OPA4_TABLE[(*bitmap_in) >> 4];
76 } else if (i == 1) {
77 bitmap_out_tmp[x] = OPA4_TABLE[(*bitmap_in) & 0xF];
78 bitmap_in++;
79 }
80 }
81 bitmap_out_tmp += stride;
82 }
83 break;
84
85 case 8:
86 memcpy(bitmap_out_tmp, bitmap_in, gd->width * gd->height);
87 break;
88 default:
89 ESP_LOGD(TAG, "Unknown bpp: %d", fe->get_bpp());
90 break;
91 }
92 return draw_buf;
93}
94
95bool Font::get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, uint32_t unicode_letter, uint32_t next) {
96 auto *fe = (Font *) font->dsc;
97 const auto *gd = fe->get_glyph_data_(unicode_letter);
98 if (gd == nullptr) {
99 return false;
100 }
101 dsc->adv_w = gd->advance;
102 dsc->ofs_x = gd->offset_x;
103 dsc->ofs_y = fe->height_ - gd->height - gd->offset_y - fe->lv_font_.base_line;
104 dsc->box_w = gd->width;
105 dsc->box_h = gd->height;
106 dsc->is_placeholder = 0;
107 dsc->format = (lv_font_glyph_format_t) fe->get_bpp();
108 dsc->gid.index = unicode_letter;
109 return true;
110}
111
112const Glyph *Font::get_glyph_data_(uint32_t unicode_letter) {
113 if (unicode_letter == this->last_letter_ && this->last_letter_ != 0)
114 return this->last_data_;
115 auto *glyph = this->find_glyph(unicode_letter);
116 if (glyph == nullptr) {
117 return nullptr;
118 }
119 this->last_data_ = glyph;
120 this->last_letter_ = unicode_letter;
121 return glyph;
122}
123#endif
124
135static uint32_t extract_unicode_codepoint(const char *utf8_str, size_t *length) {
136 // Safely cast to uint8_t* for correct bitwise operations on bytes
137 const uint8_t *current = reinterpret_cast<const uint8_t *>(utf8_str);
138 uint32_t code_point = 0;
139 uint8_t c1 = *current++;
140
141 // check for end of string
142 if (c1 == 0) {
143 *length = 0;
144 return 0;
145 }
146
147 // --- 1-Byte Sequence: 0xxxxxxx (ASCII) ---
148 if (c1 < 0x80) {
149 // Valid ASCII byte.
150 code_point = c1;
151 // Optimization: No need to check for continuation bytes.
152 }
153 // --- 2-Byte Sequence: 110xxxxx 10xxxxxx ---
154 else if ((c1 & 0xE0) == 0xC0) {
155 uint8_t c2 = *current++;
156
157 // Error Check 1: Check if c2 is a valid continuation byte (10xxxxxx)
158 if ((c2 & 0xC0) != 0x80) {
159 *length = 0;
160 return 0;
161 }
162
163 code_point = (c1 & 0x1F) << 6;
164 code_point |= (c2 & 0x3F);
165
166 // Error Check 2: Overlong check (2-byte must be > 0x7F)
167 if (code_point <= 0x7F) {
168 *length = 0;
169 return 0;
170 }
171 }
172 // --- 3-Byte Sequence: 1110xxxx 10xxxxxx 10xxxxxx ---
173 else if ((c1 & 0xF0) == 0xE0) {
174 uint8_t c2 = *current++;
175 uint8_t c3 = *current++;
176
177 // Error Check 1: Check continuation bytes
178 if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80)) {
179 *length = 0;
180 return 0;
181 }
182
183 code_point = (c1 & 0x0F) << 12;
184 code_point |= (c2 & 0x3F) << 6;
185 code_point |= (c3 & 0x3F);
186
187 // Error Check 2: Overlong check (3-byte must be > 0x7FF)
188 // Also check for surrogates (0xD800-0xDFFF)
189 if (code_point <= 0x7FF || (code_point >= 0xD800 && code_point <= 0xDFFF)) {
190 *length = 0;
191 return 0;
192 }
193 }
194 // --- 4-Byte Sequence: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx ---
195 else if ((c1 & 0xF8) == 0xF0) {
196 uint8_t c2 = *current++;
197 uint8_t c3 = *current++;
198 uint8_t c4 = *current++;
199
200 // Error Check 1: Check continuation bytes
201 if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80) || ((c4 & 0xC0) != 0x80)) {
202 *length = 0;
203 return 0;
204 }
205
206 code_point = (c1 & 0x07) << 18;
207 code_point |= (c2 & 0x3F) << 12;
208 code_point |= (c3 & 0x3F) << 6;
209 code_point |= (c4 & 0x3F);
210
211 // Error Check 2: Overlong check (4-byte must be > 0xFFFF)
212 // Also check for valid Unicode range (must be <= 0x10FFFF)
213 if (code_point <= 0xFFFF || code_point > 0x10FFFF) {
214 *length = 0;
215 return 0;
216 }
217 }
218 // --- Invalid leading byte (e.g., 10xxxxxx or 11111xxx) ---
219 else {
220 *length = 0;
221 return 0;
222 }
223 *length = current - reinterpret_cast<const uint8_t *>(utf8_str);
224 return code_point;
225}
226
227Font::Font(const Glyph *data, int data_nr, int baseline, int height, int descender, int xheight, int capheight,
228 uint8_t bpp)
229 : glyphs_(ConstVector(data, data_nr)),
230 baseline_(baseline),
231 height_(height),
232 descender_(descender),
233 linegap_(height - baseline - descender),
234 xheight_(xheight),
235 capheight_(capheight),
236 bpp_(bpp) {
237#ifdef USE_LVGL_FONT
238 this->lv_font_.dsc = this;
239 this->lv_font_.line_height = this->get_height();
240 this->lv_font_.base_line = this->lv_font_.line_height - this->get_baseline();
241 this->lv_font_.get_glyph_dsc = get_glyph_dsc_cb;
242 this->lv_font_.get_glyph_bitmap = get_glyph_bitmap;
243 this->lv_font_.subpx = LV_FONT_SUBPX_NONE;
244 this->lv_font_.underline_position = -1;
245 this->lv_font_.underline_thickness = 1;
246#endif
247}
248
249const Glyph *Font::find_glyph(uint32_t codepoint) const {
250 int lo = 0;
251 int hi = this->glyphs_.size() - 1;
252 while (lo != hi) {
253 int mid = (lo + hi + 1) / 2;
254 if (this->glyphs_[mid].is_less_or_equal(codepoint)) {
255 lo = mid;
256 } else {
257 hi = mid - 1;
258 }
259 }
260 auto *result = &this->glyphs_[lo];
261 if (result->code_point == codepoint)
262 return result;
263 return nullptr;
264}
265
266#ifdef USE_DISPLAY
267void Font::measure(const char *str, int *width, int *x_offset, int *baseline, int *height) {
268 *baseline = this->baseline_;
269 *height = this->height_;
270 int min_x = 0;
271 bool has_char = false;
272 int x = 0;
273 for (;;) {
274 size_t length;
275 auto code_point = extract_unicode_codepoint(str, &length);
276 if (length == 0)
277 break;
278 str += length;
279 auto *glyph = this->find_glyph(code_point);
280 if (glyph == nullptr) {
281 // Unknown char, skip
282 if (!this->glyphs_.empty())
283 x += this->glyphs_[0].advance;
284 continue;
285 }
286
287 if (!has_char) {
288 min_x = glyph->offset_x;
289 } else {
290 min_x = std::min(min_x, x + glyph->offset_x);
291 }
292 x += glyph->advance;
293
294 has_char = true;
295 }
296 *x_offset = min_x;
297 *width = x - min_x;
298}
299
300void Font::print(int x_start, int y_start, display::Display *display, Color color, const char *text, Color background) {
301 int x_at = x_start;
302 for (;;) {
303 size_t length;
304 auto code_point = extract_unicode_codepoint(text, &length);
305 if (length == 0)
306 break;
307 text += length;
308 auto *glyph = this->find_glyph(code_point);
309 if (glyph == nullptr) {
310 // Unknown char, skip
311 ESP_LOGW(TAG, "Codepoint 0x%08" PRIx32 " not found in font", code_point);
312 if (!this->glyphs_.empty()) {
313 uint8_t glyph_width = this->glyphs_[0].advance;
314 display->rectangle(x_at, y_start, glyph_width, this->height_, color);
315 x_at += glyph_width;
316 }
317 continue;
318 }
319
320 const uint8_t *data = glyph->data;
321 const int max_x = x_at + glyph->offset_x + glyph->width;
322 const int max_y = y_start + glyph->offset_y + glyph->height;
323
324 uint8_t bitmask = 0;
325 uint8_t pixel_data = 0;
326 uint8_t bpp_max = (1 << this->bpp_) - 1;
327 auto diff_r = (float) color.r - (float) background.r;
328 auto diff_g = (float) color.g - (float) background.g;
329 auto diff_b = (float) color.b - (float) background.b;
330 auto diff_w = (float) color.w - (float) background.w;
331 auto b_r = (float) background.r;
332 auto b_g = (float) background.g;
333 auto b_b = (float) background.b;
334 auto b_w = (float) background.w;
335 for (int glyph_y = y_start + glyph->offset_y; glyph_y != max_y; glyph_y++) {
336 for (int glyph_x = x_at + glyph->offset_x; glyph_x != max_x; glyph_x++) {
337 uint8_t pixel = 0;
338 for (uint8_t bit_num = 0; bit_num != this->bpp_; bit_num++) {
339 if (bitmask == 0) {
340 pixel_data = progmem_read_byte(data++);
341 bitmask = 0x80;
342 }
343 pixel <<= 1;
344 if ((pixel_data & bitmask) != 0)
345 pixel |= 1;
346 bitmask >>= 1;
347 }
348 if (pixel == bpp_max) {
349 display->draw_pixel_at(glyph_x, glyph_y, color);
350 } else if (pixel != 0) {
351 auto on = (float) pixel / (float) bpp_max;
352 auto blended = Color((uint8_t) (diff_r * on + b_r), (uint8_t) (diff_g * on + b_g),
353 (uint8_t) (diff_b * on + b_b), (uint8_t) (diff_w * on + b_w));
354 display->draw_pixel_at(glyph_x, glyph_y, blended);
355 }
356 }
357 }
358 x_at += glyph->advance;
359 }
360}
361#endif
362} // namespace font
363} // namespace esphome
Lightweight read-only view over a const array stored in RODATA (will typically be in flash memory) Av...
Definition helpers.h:124
void rectangle(int x1, int y1, int width, int height, Color color=COLOR_ON)
Draw the outline of a rectangle with the top left point at [x1,y1] and the bottom right point at [x1+...
Definition display.cpp:100
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition display.h:336
lv_font_t lv_font_
Definition font.h:92
void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override
Definition font.cpp:267
const Glyph * find_glyph(uint32_t codepoint) const
Definition font.cpp:249
void print(int x_start, int y_start, display::Display *display, Color color, const char *text, Color background) override
Definition font.cpp:300
static bool get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, uint32_t unicode_letter, uint32_t next)
Definition font.cpp:95
int get_height()
Definition font.h:69
uint32_t last_letter_
Definition font.h:96
const Glyph * get_glyph_data_(uint32_t unicode_letter)
Definition font.cpp:112
int get_baseline()
Definition font.h:68
const Glyph * last_data_
Definition font.h:97
static const void * get_glyph_bitmap(lv_font_glyph_dsc_t *dsc, lv_draw_buf_t *draw_buf)
Definition font.cpp:16
uint8_t bpp_
Definition font.h:90
ConstVector< Glyph > glyphs_
Definition font.h:83
Font(const Glyph *data, int data_nr, int baseline, int height, int descender, int xheight, int capheight, uint8_t bpp=1)
Construct the font with the given glyphs.
Definition font.cpp:227
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:34
static void uint32_t
uint8_t w
Definition color.h:42
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6