ESPHome  2024.12.2
font.cpp
Go to the documentation of this file.
1 #include "lvgl_esphome.h"
2 
3 #ifdef USE_LVGL_FONT
4 namespace esphome {
5 namespace lvgl {
6 
7 static 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 
17 static 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->offset_x + gd->width;
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 
32 FontEngine::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 
44 const lv_font_t *FontEngine::get_lv_font() { return &this->lv_font_; }
45 
46 const 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 lv_font_t * get_lv_font()
Definition: font.cpp:44
const font::GlyphData * get_glyph_data(uint32_t unicode_letter)
Definition: font.cpp:46
const font::GlyphData * last_data_
Definition: lvgl_esphome.h:128
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
FontEngine(font::Font *esp_font)
Definition: font.cpp:32