ESPHome 2025.12.1
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 {
14namespace font {
15
16class Font;
17
18class Glyph {
19 public:
20 constexpr Glyph(uint32_t code_point, const uint8_t *data, int advance, int offset_x, int offset_y, int width,
21 int height)
23 data(data),
27 width(width),
28 height(height) {}
29
30 bool is_less_or_equal(uint32_t other) const { return this->code_point <= other; }
31
32 const uint32_t code_point;
33 const uint8_t *data;
37 int width;
38 int height;
39};
40
41class Font
42#ifdef USE_DISPLAY
43 : public display::BaseFont
44#endif
45{
46 public:
58 Font(const Glyph *data, int data_nr, int baseline, int height, int descender, int xheight, int capheight,
59 uint8_t bpp = 1);
60
61 const Glyph *find_glyph(uint32_t codepoint) const;
62
63#ifdef USE_DISPLAY
64 void print(int x_start, int y_start, display::Display *display, Color color, const char *text,
65 Color background) override;
66 void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override;
67#endif
68 inline int get_baseline() { return this->baseline_; }
69 inline int get_height() { return this->height_; }
70 inline int get_ascender() { return this->baseline_; }
71 inline int get_descender() { return this->descender_; }
72 inline int get_linegap() { return this->linegap_; }
73 inline int get_xheight() { return this->xheight_; }
74 inline int get_capheight() { return this->capheight_; }
75 inline int get_bpp() { return this->bpp_; }
76#ifdef USE_LVGL_FONT
77 const lv_font_t *get_lv_font() const { return &this->lv_font_; }
78#endif
79
80 const ConstVector<Glyph> &get_glyphs() const { return glyphs_; }
81
82 protected:
90 uint8_t bpp_; // bits per pixel
91#ifdef USE_LVGL_FONT
92 lv_font_t lv_font_{};
93 static const uint8_t *get_glyph_bitmap(const lv_font_t *font, uint32_t unicode_letter);
94 static bool get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, uint32_t unicode_letter, uint32_t next);
95 const Glyph *get_glyph_data_(uint32_t unicode_letter);
96 uint32_t last_letter_{};
97 const Glyph *last_data_{};
98#endif
99};
100
101} // namespace font
102} // namespace esphome
Lightweight read-only view over a const array stored in RODATA (will typically be in flash memory) Av...
Definition helpers.h:118
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:192
const Glyph * find_glyph(uint32_t codepoint) const
Definition font.cpp:174
int get_ascender()
Definition font.h:70
int get_linegap()
Definition font.h:72
static const uint8_t * get_glyph_bitmap(const lv_font_t *font, uint32_t unicode_letter)
Definition font.cpp:12
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:21
int get_xheight()
Definition font.h:73
int get_height()
Definition font.h:69
int get_capheight()
Definition font.h:74
uint32_t last_letter_
Definition font.h:96
const Glyph * get_glyph_data_(uint32_t unicode_letter)
Definition font.cpp:37
int get_baseline()
Definition font.h:68
const ConstVector< Glyph > & get_glyphs() const
Definition font.h:80
const Glyph * last_data_
Definition font.h:97
int get_descender()
Definition font.h:71
const lv_font_t * get_lv_font() const
Definition font.h:77
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:152
const uint8_t * data
Definition font.h:33
bool is_less_or_equal(uint32_t other) const
Definition font.h:30
const uint32_t code_point
Definition font.h:32
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:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string print()