ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_DISPLAY
8#endif
9#ifdef USE_LVGL_FONT
10#include <lvgl.h>
11#endif
12
13namespace esphome::font {
14
15class Font;
16
17class Glyph {
18 public:
19 constexpr Glyph(uint32_t code_point, const uint8_t *data, int advance, int offset_x, int offset_y, int width,
20 int height)
22 data(data),
26 width(width),
27 height(height) {}
28
29 bool is_less_or_equal(uint32_t other) const { return this->code_point <= other; }
30
32 const uint8_t *data;
36 int width;
37 int height;
38};
39
40class Font
41#ifdef USE_DISPLAY
42 : public display::BaseFont
43#endif
44{
45 public:
57 Font(const Glyph *data, int data_nr, int baseline, int height, int descender, int xheight, int capheight,
58 uint8_t bpp = 1);
59
60 const Glyph *find_glyph(uint32_t codepoint) const;
61
62#ifdef USE_DISPLAY
63 void print(int x_start, int y_start, display::Display *display, Color color, const char *text,
64 Color background) override;
65 void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override;
66#endif
67 inline int get_baseline() { return this->baseline_; }
68 inline int get_height() { return this->height_; }
69 inline int get_ascender() { return this->baseline_; }
70 inline int get_descender() { return this->descender_; }
71 inline int get_linegap() { return this->linegap_; }
72 inline int get_xheight() { return this->xheight_; }
73 inline int get_capheight() { return this->capheight_; }
74 inline int get_bpp() { return this->bpp_; }
75#ifdef USE_LVGL_FONT
76 const lv_font_t *get_lv_font() const { return &this->lv_font_; }
77#endif
78
79 const ConstVector<Glyph> &get_glyphs() const { return glyphs_; }
80
81 protected:
89 uint8_t bpp_; // bits per pixel
90#ifdef USE_LVGL_FONT
91 lv_font_t lv_font_{};
92 static const void *get_glyph_bitmap(lv_font_glyph_dsc_t *dsc, lv_draw_buf_t *draw_buf);
93 static bool get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, uint32_t unicode_letter, uint32_t next);
94 const Glyph *get_glyph_data_(uint32_t unicode_letter);
96 const Glyph *last_data_{};
97#endif
98};
99
100} // namespace esphome::font
Lightweight read-only view over a const array stored in RODATA (will typically be in flash memory) Av...
Definition helpers.h:131
lv_font_t lv_font_
Definition font.h:91
void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override
Definition font.cpp:266
const Glyph * find_glyph(uint32_t codepoint) const
Definition font.cpp:248
int get_ascender()
Definition font.h:69
int get_linegap()
Definition font.h:71
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:94
int get_xheight()
Definition font.h:72
int get_height()
Definition font.h:68
int get_capheight()
Definition font.h:73
uint32_t last_letter_
Definition font.h:95
const Glyph * get_glyph_data_(uint32_t unicode_letter)
Definition font.cpp:111
int get_baseline()
Definition font.h:67
const ConstVector< Glyph > & get_glyphs() const
Definition font.h:79
const Glyph * last_data_
Definition font.h:96
static const void * get_glyph_bitmap(lv_font_glyph_dsc_t *dsc, lv_draw_buf_t *draw_buf)
Definition font.cpp:15
int get_descender()
Definition font.h:70
const lv_font_t * get_lv_font() const
Definition font.h:76
uint8_t bpp_
Definition font.h:89
ConstVector< Glyph > glyphs_
Definition font.h:82
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:226
const uint8_t * data
Definition font.h:32
bool is_less_or_equal(uint32_t other) const
Definition font.h:29
const uint32_t code_point
Definition font.h:31
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:19
static void uint32_t
std::string print()