mirror of
https://github.com/esphome/esphome.git
synced 2025-10-18 09:43:47 +01:00
Refactor font creation to save stack (#1707)
This commit is contained in:
@@ -170,7 +170,7 @@ void DisplayBuffer::print(int x, int y, Font *font, Color color, TextAlign align
|
||||
// Unknown char, skip
|
||||
ESP_LOGW(TAG, "Encountered character without representation in font: '%c'", text[i]);
|
||||
if (!font->get_glyphs().empty()) {
|
||||
uint8_t glyph_width = font->get_glyphs()[0].width_;
|
||||
uint8_t glyph_width = font->get_glyphs()[0].glyph_data_->width;
|
||||
for (int glyph_x = 0; glyph_x < glyph_width; glyph_x++)
|
||||
for (int glyph_y = 0; glyph_y < height; glyph_y++)
|
||||
this->draw_pixel_at(glyph_x + x_at, glyph_y + y_start, color);
|
||||
@@ -193,7 +193,7 @@ void DisplayBuffer::print(int x, int y, Font *font, Color color, TextAlign align
|
||||
}
|
||||
}
|
||||
|
||||
x_at += glyph.width_ + glyph.offset_x_;
|
||||
x_at += glyph.glyph_data_->width + glyph.glyph_data_->offset_x;
|
||||
|
||||
i += match_length;
|
||||
}
|
||||
@@ -345,35 +345,27 @@ void DisplayBuffer::strftime(int x, int y, Font *font, const char *format, time:
|
||||
}
|
||||
#endif
|
||||
|
||||
Glyph::Glyph(const char *a_char, const uint8_t *data_start, uint32_t offset, int offset_x, int offset_y, int width,
|
||||
int height)
|
||||
: char_(a_char),
|
||||
data_(data_start + offset),
|
||||
offset_x_(offset_x),
|
||||
offset_y_(offset_y),
|
||||
width_(width),
|
||||
height_(height) {}
|
||||
bool Glyph::get_pixel(int x, int y) const {
|
||||
const int x_data = x - this->offset_x_;
|
||||
const int y_data = y - this->offset_y_;
|
||||
if (x_data < 0 || x_data >= this->width_ || y_data < 0 || y_data >= this->height_)
|
||||
const int x_data = x - this->glyph_data_->offset_x;
|
||||
const int y_data = y - this->glyph_data_->offset_y;
|
||||
if (x_data < 0 || x_data >= this->glyph_data_->width || y_data < 0 || y_data >= this->glyph_data_->height)
|
||||
return false;
|
||||
const uint32_t width_8 = ((this->width_ + 7u) / 8u) * 8u;
|
||||
const uint32_t width_8 = ((this->glyph_data_->width + 7u) / 8u) * 8u;
|
||||
const uint32_t pos = x_data + y_data * width_8;
|
||||
return pgm_read_byte(this->data_ + (pos / 8u)) & (0x80 >> (pos % 8u));
|
||||
return pgm_read_byte(this->glyph_data_->data + (pos / 8u)) & (0x80 >> (pos % 8u));
|
||||
}
|
||||
const char *Glyph::get_char() const { return this->char_; }
|
||||
const char *Glyph::get_char() const { return this->glyph_data_->a_char; }
|
||||
bool Glyph::compare_to(const char *str) const {
|
||||
// 1 -> this->char_
|
||||
// 2 -> str
|
||||
for (uint32_t i = 0;; i++) {
|
||||
if (this->char_[i] == '\0')
|
||||
if (this->glyph_data_->a_char[i] == '\0')
|
||||
return true;
|
||||
if (str[i] == '\0')
|
||||
return false;
|
||||
if (this->char_[i] > str[i])
|
||||
if (this->glyph_data_->a_char[i] > str[i])
|
||||
return false;
|
||||
if (this->char_[i] < str[i])
|
||||
if (this->glyph_data_->a_char[i] < str[i])
|
||||
return true;
|
||||
}
|
||||
// this should not happen
|
||||
@@ -381,19 +373,19 @@ bool Glyph::compare_to(const char *str) const {
|
||||
}
|
||||
int Glyph::match_length(const char *str) const {
|
||||
for (uint32_t i = 0;; i++) {
|
||||
if (this->char_[i] == '\0')
|
||||
if (this->glyph_data_->a_char[i] == '\0')
|
||||
return i;
|
||||
if (str[i] != this->char_[i])
|
||||
if (str[i] != this->glyph_data_->a_char[i])
|
||||
return 0;
|
||||
}
|
||||
// this should not happen
|
||||
return 0;
|
||||
}
|
||||
void Glyph::scan_area(int *x1, int *y1, int *width, int *height) const {
|
||||
*x1 = this->offset_x_;
|
||||
*y1 = this->offset_y_;
|
||||
*width = this->width_;
|
||||
*height = this->height_;
|
||||
*x1 = this->glyph_data_->offset_x;
|
||||
*y1 = this->glyph_data_->offset_y;
|
||||
*width = this->glyph_data_->width;
|
||||
*height = this->glyph_data_->height;
|
||||
}
|
||||
int Font::match_next_glyph(const char *str, int *match_length) {
|
||||
int lo = 0;
|
||||
@@ -423,17 +415,17 @@ void Font::measure(const char *str, int *width, int *x_offset, int *baseline, in
|
||||
if (glyph_n < 0) {
|
||||
// Unknown char, skip
|
||||
if (!this->get_glyphs().empty())
|
||||
x += this->get_glyphs()[0].width_;
|
||||
x += this->get_glyphs()[0].glyph_data_->width;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const Glyph &glyph = this->glyphs_[glyph_n];
|
||||
if (!has_char)
|
||||
min_x = glyph.offset_x_;
|
||||
min_x = glyph.glyph_data_->offset_x;
|
||||
else
|
||||
min_x = std::min(min_x, x + glyph.offset_x_);
|
||||
x += glyph.width_ + glyph.offset_x_;
|
||||
min_x = std::min(min_x, x + glyph.glyph_data_->offset_x);
|
||||
x += glyph.glyph_data_->width + glyph.glyph_data_->offset_x;
|
||||
|
||||
i += match_length;
|
||||
has_char = true;
|
||||
@@ -442,8 +434,10 @@ void Font::measure(const char *str, int *width, int *x_offset, int *baseline, in
|
||||
*width = x - min_x;
|
||||
}
|
||||
const std::vector<Glyph> &Font::get_glyphs() const { return this->glyphs_; }
|
||||
Font::Font(std::vector<Glyph> &&glyphs, int baseline, int bottom)
|
||||
: glyphs_(std::move(glyphs)), baseline_(baseline), bottom_(bottom) {}
|
||||
Font::Font(const GlyphData *data, int data_nr, int baseline, int bottom) : baseline_(baseline), bottom_(bottom) {
|
||||
for (int i = 0; i < data_nr; ++i)
|
||||
glyphs_.emplace_back(data + i);
|
||||
}
|
||||
|
||||
bool Image::get_pixel(int x, int y) const {
|
||||
if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
|
||||
|
@@ -338,10 +338,18 @@ class DisplayPage {
|
||||
DisplayPage *next_{nullptr};
|
||||
};
|
||||
|
||||
struct GlyphData {
|
||||
const char *a_char;
|
||||
const uint8_t *data;
|
||||
int offset_x;
|
||||
int offset_y;
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
class Glyph {
|
||||
public:
|
||||
Glyph(const char *a_char, const uint8_t *data_start, uint32_t offset, int offset_x, int offset_y, int width,
|
||||
int height);
|
||||
Glyph(const GlyphData *data) : glyph_data_(data) {}
|
||||
|
||||
bool get_pixel(int x, int y) const;
|
||||
|
||||
@@ -357,12 +365,7 @@ class Glyph {
|
||||
friend Font;
|
||||
friend DisplayBuffer;
|
||||
|
||||
const char *char_;
|
||||
const uint8_t *data_;
|
||||
int offset_x_;
|
||||
int offset_y_;
|
||||
int width_;
|
||||
int height_;
|
||||
const GlyphData *glyph_data_;
|
||||
};
|
||||
|
||||
class Font {
|
||||
@@ -373,7 +376,7 @@ class Font {
|
||||
* @param baseline The y-offset from the top of the text to the baseline.
|
||||
* @param bottom The y-offset from the top of the text to the bottom (i.e. height).
|
||||
*/
|
||||
Font(std::vector<Glyph> &&glyphs, int baseline, int bottom);
|
||||
Font(const GlyphData *data, int data_nr, int baseline, int bottom);
|
||||
|
||||
int match_next_glyph(const char *str, int *match_length);
|
||||
|
||||
|
Reference in New Issue
Block a user