ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
font.cpp
Go to the documentation of this file.
1#include "font.h"
2
4#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace font {
9
10static const char *const TAG = "font";
11
12const uint8_t *Glyph::get_char() const { return this->glyph_data_->a_char; }
13// Compare the char at the string position with this char.
14// Return true if this char is less than or equal the other.
15bool Glyph::compare_to(const uint8_t *str) const {
16 // 1 -> this->char_
17 // 2 -> str
18 for (uint32_t i = 0;; i++) {
19 if (this->glyph_data_->a_char[i] == '\0')
20 return true;
21 if (str[i] == '\0')
22 return false;
23 if (this->glyph_data_->a_char[i] > str[i])
24 return false;
25 if (this->glyph_data_->a_char[i] < str[i])
26 return true;
27 }
28 // this should not happen
29 return false;
30}
31int Glyph::match_length(const uint8_t *str) const {
32 for (uint32_t i = 0;; i++) {
33 if (this->glyph_data_->a_char[i] == '\0')
34 return i;
35 if (str[i] != this->glyph_data_->a_char[i])
36 return 0;
37 }
38 // this should not happen
39 return 0;
40}
41void Glyph::scan_area(int *x1, int *y1, int *width, int *height) const {
42 *x1 = this->glyph_data_->offset_x;
43 *y1 = this->glyph_data_->offset_y;
44 *width = this->glyph_data_->width;
45 *height = this->glyph_data_->height;
46}
47
48Font::Font(const GlyphData *data, int data_nr, int baseline, int height, int descender, int xheight, int capheight,
49 uint8_t bpp)
50 : baseline_(baseline),
51 height_(height),
52 descender_(descender),
53 linegap_(height - baseline - descender),
54 xheight_(xheight),
55 capheight_(capheight),
56 bpp_(bpp) {
57 glyphs_.reserve(data_nr);
58 for (int i = 0; i < data_nr; ++i)
59 glyphs_.emplace_back(&data[i]);
60}
61int Font::match_next_glyph(const uint8_t *str, int *match_length) {
62 int lo = 0;
63 int hi = this->glyphs_.size() - 1;
64 while (lo != hi) {
65 int mid = (lo + hi + 1) / 2;
66 if (this->glyphs_[mid].compare_to(str)) {
67 lo = mid;
68 } else {
69 hi = mid - 1;
70 }
71 }
72 *match_length = this->glyphs_[lo].match_length(str);
73 if (*match_length <= 0)
74 return -1;
75 return lo;
76}
77#ifdef USE_DISPLAY
78void Font::measure(const char *str, int *width, int *x_offset, int *baseline, int *height) {
79 *baseline = this->baseline_;
80 *height = this->height_;
81 int i = 0;
82 int min_x = 0;
83 bool has_char = false;
84 int x = 0;
85 while (str[i] != '\0') {
86 int match_length;
87 int glyph_n = this->match_next_glyph((const uint8_t *) str + i, &match_length);
88 if (glyph_n < 0) {
89 // Unknown char, skip
90 if (!this->get_glyphs().empty())
91 x += this->get_glyphs()[0].glyph_data_->advance;
92 i++;
93 continue;
94 }
95
96 const Glyph &glyph = this->glyphs_[glyph_n];
97 if (!has_char) {
98 min_x = glyph.glyph_data_->offset_x;
99 } else {
100 min_x = std::min(min_x, x + glyph.glyph_data_->offset_x);
101 }
102 x += glyph.glyph_data_->advance;
103
104 i += match_length;
105 has_char = true;
106 }
107 *x_offset = min_x;
108 *width = x - min_x;
109}
110void Font::print(int x_start, int y_start, display::Display *display, Color color, const char *text, Color background) {
111 int i = 0;
112 int x_at = x_start;
113 int scan_x1, scan_y1, scan_width, scan_height;
114 while (text[i] != '\0') {
115 int match_length;
116 int glyph_n = this->match_next_glyph((const uint8_t *) text + i, &match_length);
117 if (glyph_n < 0) {
118 // Unknown char, skip
119 ESP_LOGW(TAG, "Encountered character without representation in font: '%c'", text[i]);
120 if (!this->get_glyphs().empty()) {
121 uint8_t glyph_width = this->get_glyphs()[0].glyph_data_->advance;
122 display->filled_rectangle(x_at, y_start, glyph_width, this->height_, color);
123 x_at += glyph_width;
124 }
125
126 i++;
127 continue;
128 }
129
130 const Glyph &glyph = this->get_glyphs()[glyph_n];
131 glyph.scan_area(&scan_x1, &scan_y1, &scan_width, &scan_height);
132
133 const uint8_t *data = glyph.glyph_data_->data;
134 const int max_x = x_at + scan_x1 + scan_width;
135 const int max_y = y_start + scan_y1 + scan_height;
136
137 uint8_t bitmask = 0;
138 uint8_t pixel_data = 0;
139 uint8_t bpp_max = (1 << this->bpp_) - 1;
140 auto diff_r = (float) color.r - (float) background.r;
141 auto diff_g = (float) color.g - (float) background.g;
142 auto diff_b = (float) color.b - (float) background.b;
143 auto diff_w = (float) color.w - (float) background.w;
144 auto b_r = (float) background.r;
145 auto b_g = (float) background.g;
146 auto b_b = (float) background.b;
147 auto b_w = (float) background.w;
148 for (int glyph_y = y_start + scan_y1; glyph_y != max_y; glyph_y++) {
149 for (int glyph_x = x_at + scan_x1; glyph_x != max_x; glyph_x++) {
150 uint8_t pixel = 0;
151 for (int bit_num = 0; bit_num != this->bpp_; bit_num++) {
152 if (bitmask == 0) {
153 pixel_data = progmem_read_byte(data++);
154 bitmask = 0x80;
155 }
156 pixel <<= 1;
157 if ((pixel_data & bitmask) != 0)
158 pixel |= 1;
159 bitmask >>= 1;
160 }
161 if (pixel == bpp_max) {
162 display->draw_pixel_at(glyph_x, glyph_y, color);
163 } else if (pixel != 0) {
164 auto on = (float) pixel / (float) bpp_max;
165 auto blended = Color((uint8_t) (diff_r * on + b_r), (uint8_t) (diff_g * on + b_g),
166 (uint8_t) (diff_b * on + b_b), (uint8_t) (diff_w * on + b_w));
167 display->draw_pixel_at(glyph_x, glyph_y, blended);
168 }
169 }
170 }
171 x_at += glyph.glyph_data_->advance;
172
173 i += match_length;
174 }
175}
176#endif
177
178} // namespace font
179} // namespace esphome
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition display.h:226
void filled_rectangle(int x1, int y1, int width, int height, Color color=COLOR_ON)
Fill a rectangle with the top left point at [x1,y1] and the bottom right point at [x1+width,...
Definition display.cpp:105
void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) override
Definition font.cpp:78
void print(int x_start, int y_start, display::Display *display, Color color, const char *text, Color background) override
Definition font.cpp:110
const std::vector< Glyph, RAMAllocator< Glyph > > & get_glyphs() const
Definition font.h:81
Font(const GlyphData *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:48
int match_next_glyph(const uint8_t *str, int *match_length)
Definition font.cpp:61
uint8_t bpp_
Definition font.h:91
std::vector< Glyph, RAMAllocator< Glyph > > glyphs_
Definition font.h:84
const GlyphData * glyph_data_
Definition font.h:42
bool compare_to(const uint8_t *str) const
Definition font.cpp:15
void scan_area(int *x1, int *y1, int *width, int *height) const
Definition font.cpp:41
const uint8_t * get_char() const
Definition font.cpp:12
int match_length(const uint8_t *str) const
Definition font.cpp:31
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:58
uint8_t w
Definition color.h:33
uint8_t g
Definition color.h:25
uint8_t b
Definition color.h:29
uint8_t r
Definition color.h:21
const uint8_t * data
Definition font.h:17
const uint8_t * a_char
Definition font.h:16
uint16_t x
Definition tt21100.cpp:5