ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
8#ifdef USE_DISPLAY
10#endif
11#ifdef USE_LVGL_FONT
12#include <lvgl.h>
13#endif
14
15namespace esphome::font {
16
17class Font;
18
19class Glyph final {
20 public:
21 constexpr Glyph(uint32_t code_point, const uint8_t *data, int advance, int offset_x, int offset_y, int width,
22 int height)
24 data(data),
28 width(width),
29 height(height) {}
30
31 bool is_less_or_equal(uint32_t other) const { return this->code_point <= other; }
32
34 const uint8_t *data;
38 int width;
39 int height;
40};
41// The glyph table lives in flash, read with plain loads, which ESP8266 only allows for whole words.
42static_assert(std::is_same_v<decltype(Glyph::code_point), const uint32_t>, "Glyph fields must stay word sized");
43static_assert(std::is_same_v<decltype(Glyph::data), const uint8_t *>, "Glyph fields must stay word sized");
44static_assert(std::is_same_v<decltype(Glyph::advance), int>, "Glyph fields must stay word sized");
45static_assert(std::is_same_v<decltype(Glyph::offset_x), int>, "Glyph fields must stay word sized");
46static_assert(std::is_same_v<decltype(Glyph::offset_y), int>, "Glyph fields must stay word sized");
47static_assert(std::is_same_v<decltype(Glyph::width), int>, "Glyph fields must stay word sized");
48static_assert(std::is_same_v<decltype(Glyph::height), int>, "Glyph fields must stay word sized");
49#ifdef USE_ESP8266
50static_assert(alignof(Glyph) == sizeof(uint32_t), "Glyph fields must stay word sized");
51static_assert(sizeof(Glyph) == 7 * sizeof(uint32_t), "Glyph is read from flash with word loads");
52#endif
53
54class Font final
55#ifdef USE_DISPLAY
56 : public display::BaseFont
57#endif
58{
59 public:
71 Font(const Glyph *data, int data_nr, int baseline, int height, int descender, int xheight, int capheight,
72 uint8_t bpp = 1);
73
74 const Glyph *find_glyph(uint32_t codepoint) const;
75
76#ifdef USE_DISPLAY
77 void print(int x_start, int y_start, display::Display *display, Color color, const char *text,
78 Color background) override;
79 void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override;
80#endif
81 inline int get_baseline() { return this->baseline_; }
82 inline int get_height() { return this->height_; }
83 inline int get_ascender() { return this->baseline_; }
84 inline int get_descender() { return this->descender_; }
85 inline int get_linegap() { return this->linegap_; }
86 inline int get_xheight() { return this->xheight_; }
87 inline int get_capheight() { return this->capheight_; }
88 inline int get_bpp() { return this->bpp_; }
89#ifdef USE_LVGL_FONT
90 const lv_font_t *get_lv_font() const { return &this->lv_font_; }
91#endif
92
93 const ConstVector<Glyph> &get_glyphs() const { return glyphs_; }
94
95 protected:
103 uint8_t bpp_; // bits per pixel
104#ifdef USE_LVGL_FONT
105 lv_font_t lv_font_{};
106 static const void *get_glyph_bitmap(lv_font_glyph_dsc_t *dsc, lv_draw_buf_t *draw_buf);
107 static bool get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, uint32_t unicode_letter, uint32_t next);
108 const Glyph *get_glyph_data_(uint32_t unicode_letter);
111#endif
112};
113
114} // namespace esphome::font
Lightweight read-only view over a const array stored in RODATA (will typically be in flash memory).
Definition helpers.h:130
lv_font_t lv_font_
Definition font.h:105
void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override
Definition font.cpp:165
const Glyph * find_glyph(uint32_t codepoint) const
Definition font.cpp:147
int get_ascender()
Definition font.h:83
int get_linegap()
Definition font.h:85
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_xheight()
Definition font.h:86
int get_height()
Definition font.h:82
int get_capheight()
Definition font.h:87
uint32_t last_letter_
Definition font.h:109
const Glyph * get_glyph_data_(uint32_t unicode_letter)
Definition font.cpp:112
int get_baseline()
Definition font.h:81
const ConstVector< Glyph > & get_glyphs() const
Definition font.h:93
const Glyph * last_data_
Definition font.h:110
static const void * get_glyph_bitmap(lv_font_glyph_dsc_t *dsc, lv_draw_buf_t *draw_buf)
Definition font.cpp:16
int get_descender()
Definition font.h:84
const lv_font_t * get_lv_font() const
Definition font.h:90
ConstVector< Glyph > glyphs_
Definition font.h:96
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:125
const uint8_t * data
Definition font.h:34
bool is_less_or_equal(uint32_t other) const
Definition font.h:31
const uint32_t code_point
Definition font.h:33
constexpr Glyph(uint32_t code_point, const uint8_t *data, int advance, int offset_x, int offset_y, int width, int height)
Definition font.h:21
NameCopy * next
static void uint32_t
std::string print()