mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 16:38:16 +01:00
small DisplayBuffer images and font update (#4044)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
1cf3424ebe
commit
86a8e1f4a6
4 changed files with 14 additions and 8 deletions
|
@ -117,7 +117,7 @@ async def to_code(config):
|
|||
data[pos] = rgb & 255
|
||||
pos += 1
|
||||
|
||||
elif config[CONF_TYPE] == "BINARY":
|
||||
elif config[CONF_TYPE] in ["BINARY", "TRANSPARENT_BINARY"]:
|
||||
width8 = ((width + 7) // 8) * 8
|
||||
data = [0 for _ in range((height * width8 // 8) * frames)]
|
||||
for frameIndex in range(frames):
|
||||
|
|
|
@ -452,7 +452,7 @@ int Font::match_next_glyph(const char *str, int *match_length) {
|
|||
}
|
||||
void Font::measure(const char *str, int *width, int *x_offset, int *baseline, int *height) {
|
||||
*baseline = this->baseline_;
|
||||
*height = this->bottom_;
|
||||
*height = this->height_;
|
||||
int i = 0;
|
||||
int min_x = 0;
|
||||
bool has_char = false;
|
||||
|
@ -483,7 +483,7 @@ 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(const GlyphData *data, int data_nr, int baseline, int bottom) : baseline_(baseline), bottom_(bottom) {
|
||||
Font::Font(const GlyphData *data, int data_nr, int baseline, int height) : baseline_(baseline), height_(height) {
|
||||
for (int i = 0; i < data_nr; ++i)
|
||||
glyphs_.emplace_back(data + i);
|
||||
}
|
||||
|
@ -527,6 +527,7 @@ int Image::get_height() const { return this->height_; }
|
|||
ImageType Image::get_type() const { return this->type_; }
|
||||
Image::Image(const uint8_t *data_start, int width, int height, ImageType type)
|
||||
: width_(width), height_(height), type_(type), data_start_(data_start) {}
|
||||
int Image::get_current_frame() const { return 0; }
|
||||
|
||||
bool Animation::get_pixel(int x, int y) const {
|
||||
if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
|
||||
|
|
|
@ -448,18 +448,20 @@ 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(const GlyphData *data, int data_nr, int baseline, int bottom);
|
||||
Font(const GlyphData *data, int data_nr, int baseline, int height);
|
||||
|
||||
int match_next_glyph(const char *str, int *match_length);
|
||||
|
||||
void measure(const char *str, int *width, int *x_offset, int *baseline, int *height);
|
||||
inline int get_baseline() { return this->baseline_; }
|
||||
inline int get_height() { return this->height_; }
|
||||
|
||||
const std::vector<Glyph> &get_glyphs() const;
|
||||
|
||||
protected:
|
||||
std::vector<Glyph> glyphs_;
|
||||
int baseline_;
|
||||
int bottom_;
|
||||
int height_;
|
||||
};
|
||||
|
||||
class Image {
|
||||
|
@ -473,6 +475,8 @@ class Image {
|
|||
int get_height() const;
|
||||
ImageType get_type() const;
|
||||
|
||||
virtual int get_current_frame() const;
|
||||
|
||||
protected:
|
||||
int width_;
|
||||
int height_;
|
||||
|
@ -489,7 +493,7 @@ class Animation : public Image {
|
|||
Color get_grayscale_pixel(int x, int y) const override;
|
||||
|
||||
int get_animation_frame_count() const;
|
||||
int get_current_frame() const;
|
||||
int get_current_frame() const override;
|
||||
void next_frame();
|
||||
void prev_frame();
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ IMAGE_TYPE = {
|
|||
"RGB24": ImageType.IMAGE_TYPE_RGB24,
|
||||
"TRANSPARENT_BINARY": ImageType.IMAGE_TYPE_TRANSPARENT_BINARY,
|
||||
"RGB565": ImageType.IMAGE_TYPE_RGB565,
|
||||
"TRANSPARENT_IMAGE": ImageType.IMAGE_TYPE_TRANSPARENT_BINARY,
|
||||
}
|
||||
|
||||
Image_ = display.display_ns.class_("Image")
|
||||
|
@ -105,7 +106,7 @@ async def to_code(config):
|
|||
data[pos] = rgb & 255
|
||||
pos += 1
|
||||
|
||||
elif config[CONF_TYPE] == "BINARY":
|
||||
elif (config[CONF_TYPE] == "BINARY") or (config[CONF_TYPE] == "TRANSPARENT_BINARY"):
|
||||
image = image.convert("1", dither=dither)
|
||||
width8 = ((width + 7) // 8) * 8
|
||||
data = [0 for _ in range(height * width8 // 8)]
|
||||
|
@ -116,7 +117,7 @@ async def to_code(config):
|
|||
pos = x + y * width8
|
||||
data[pos // 8] |= 0x80 >> (pos % 8)
|
||||
|
||||
elif config[CONF_TYPE] == "TRANSPARENT_BINARY":
|
||||
elif config[CONF_TYPE] == "TRANSPARENT_IMAGE":
|
||||
image = image.convert("RGBA")
|
||||
width8 = ((width + 7) // 8) * 8
|
||||
data = [0 for _ in range(height * width8 // 8)]
|
||||
|
|
Loading…
Reference in a new issue