ESPHome 2025.5.0
Loading...
Searching...
No Matches
font.cpp
Go to the documentation of this file.
1#include "lvgl_esphome.h"
2
3#ifdef USE_LVGL_FONT
4namespace esphome {
5namespace lvgl {
6
7static const uint8_t *get_glyph_bitmap(const lv_font_t *font, uint32_t unicode_letter) {
8 auto *fe = (FontEngine *) font->dsc;
9 const auto *gd = fe->get_glyph_data(unicode_letter);
10 if (gd == nullptr)
11 return nullptr;
12 // esph_log_d(TAG, "Returning bitmap @ %X", (uint32_t)gd->data);
13
14 return gd->data;
15}
16
17static bool get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, uint32_t unicode_letter, uint32_t next) {
18 auto *fe = (FontEngine *) font->dsc;
19 const auto *gd = fe->get_glyph_data(unicode_letter);
20 if (gd == nullptr)
21 return false;
22 dsc->adv_w = gd->advance;
23 dsc->ofs_x = gd->offset_x;
24 dsc->ofs_y = fe->height - gd->height - gd->offset_y - fe->baseline;
25 dsc->box_w = gd->width;
26 dsc->box_h = gd->height;
27 dsc->is_placeholder = 0;
28 dsc->bpp = fe->bpp;
29 return true;
30}
31
32FontEngine::FontEngine(font::Font *esp_font) : font_(esp_font) {
33 this->bpp = esp_font->get_bpp();
34 this->lv_font_.dsc = this;
35 this->lv_font_.line_height = this->height = esp_font->get_height();
36 this->lv_font_.base_line = this->baseline = this->lv_font_.line_height - esp_font->get_baseline();
37 this->lv_font_.get_glyph_dsc = get_glyph_dsc_cb;
38 this->lv_font_.get_glyph_bitmap = get_glyph_bitmap;
39 this->lv_font_.subpx = LV_FONT_SUBPX_NONE;
40 this->lv_font_.underline_position = -1;
41 this->lv_font_.underline_thickness = 1;
42}
43
44const lv_font_t *FontEngine::get_lv_font() { return &this->lv_font_; }
45
46const font::GlyphData *FontEngine::get_glyph_data(uint32_t unicode_letter) {
47 if (unicode_letter == last_letter_)
48 return this->last_data_;
49 uint8_t unicode[5];
50 memset(unicode, 0, sizeof unicode);
51 if (unicode_letter > 0xFFFF) {
52 unicode[0] = 0xF0 + ((unicode_letter >> 18) & 0x7);
53 unicode[1] = 0x80 + ((unicode_letter >> 12) & 0x3F);
54 unicode[2] = 0x80 + ((unicode_letter >> 6) & 0x3F);
55 unicode[3] = 0x80 + (unicode_letter & 0x3F);
56 } else if (unicode_letter > 0x7FF) {
57 unicode[0] = 0xE0 + ((unicode_letter >> 12) & 0xF);
58 unicode[1] = 0x80 + ((unicode_letter >> 6) & 0x3F);
59 unicode[2] = 0x80 + (unicode_letter & 0x3F);
60 } else if (unicode_letter > 0x7F) {
61 unicode[0] = 0xC0 + ((unicode_letter >> 6) & 0x1F);
62 unicode[1] = 0x80 + (unicode_letter & 0x3F);
63 } else {
64 unicode[0] = unicode_letter;
65 }
66 int match_length;
67 int glyph_n = this->font_->match_next_glyph(unicode, &match_length);
68 if (glyph_n < 0)
69 return nullptr;
70 this->last_data_ = this->font_->get_glyphs()[glyph_n].get_glyph_data();
71 this->last_letter_ = unicode_letter;
72 return this->last_data_;
73}
74} // namespace lvgl
75} // namespace esphome
76#endif // USES_LVGL_FONT
const std::vector< Glyph, ExternalRAMAllocator< Glyph > > & get_glyphs() const
Definition font.h:70
int get_height()
Definition font.h:67
int get_baseline()
Definition font.h:66
int match_next_glyph(const uint8_t *str, int *match_length)
Definition font.cpp:54
const font::GlyphData * get_glyph_data(uint32_t unicode_letter)
Definition font.cpp:46
const lv_font_t * get_lv_font()
Definition font.cpp:44
FontEngine(font::Font *esp_font)
Definition font.cpp:32
const font::GlyphData * last_data_
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7