From 2e4cfccd3a49d509b85ecb0e2ee61e583918f94b Mon Sep 17 00:00:00 2001 From: whowechina Date: Thu, 30 May 2024 19:46:33 +0800 Subject: [PATCH] Spacing and line wrap --- firmware/src/st7789.c | 45 +++++++++++++++++++++++++++++++------------ firmware/src/st7789.h | 3 ++- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/firmware/src/st7789.c b/firmware/src/st7789.c index 12fc46d..0b2744a 100644 --- a/firmware/src/st7789.c +++ b/firmware/src/st7789.c @@ -353,27 +353,48 @@ void st7789_char(int x, int y, char c, const lv_font_t *font, uint16_t color) } } -static uint16_t text_width(const char *text, const lv_font_t *font, uint16_t spacing) +static uint16_t spacing_x = 1, spacing_y = 1; + +void st7789_spacing(uint16_t dx, uint16_t dy) +{ + spacing_x = dx; + spacing_y = dy; +} + +static uint16_t text_width(const char *text, const lv_font_t *font) { uint16_t width = 0; - for (; *text; text++) { - width += (font->dsc[*text - font->range_start].adv_w >> 4) + spacing; + for (; *text && (*text != '\n'); text++) { + if (*text - font->range_start < font->range_length) { + width += (font->dsc[*text - font->range_start].adv_w >> 4) + spacing_x; + } } return width; } void st7789_text(int x, int y, const char *text, - const lv_font_t *font, uint16_t spacing, uint16_t color, alignment_t align) + const lv_font_t *font, uint16_t color, alignment_t align) { - int width = text_width(text, font, spacing); - if (align == ALIGN_CENTER) { - x -= width / 2; - } else if (align == ALIGN_RIGHT) { - x -= width; - } + bool newline = true; + int pos_x = x; for (; *text; text++) { - st7789_char(x, y, *text, font, color); - x += (font->dsc[*text - font->range_start].adv_w >> 4) + spacing; + if (*text == '\n') { + newline = true; + pos_x = x;; + y += font->line_height + spacing_y; + continue; + } + if (newline) { + int width = text_width(text, font); + if (align == ALIGN_CENTER) { + pos_x -= width / 2; + } else if (align == ALIGN_RIGHT) { + pos_x -= width; + } + newline = false; + } + st7789_char(pos_x, y, *text, font, color); + pos_x += (font->dsc[*text - font->range_start].adv_w >> 4) + spacing_x; } } diff --git a/firmware/src/st7789.h b/firmware/src/st7789.h index 61435eb..e4302ed 100644 --- a/firmware/src/st7789.h +++ b/firmware/src/st7789.h @@ -49,5 +49,6 @@ typedef enum { ALIGN_RIGHT } alignment_t; +void st7789_spacing(uint16_t dx, uint16_t dy); void st7789_text(int x, int y, const char *text, const lv_font_t *font, - uint16_t spacing, uint16_t color, alignment_t align); + uint16_t color, alignment_t align);