From 4e46790eda581deafd55d4677c57fad2c6ebab2d Mon Sep 17 00:00:00 2001 From: gilesp1729 Date: Tue, 9 Jul 2024 14:58:37 +1000 Subject: [PATCH 1/5] Make Adafruit_GFX_Button::drawButton work with custom fonts by using getTextBounds to obtain the text bounds. --- Adafruit_GFX.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Adafruit_GFX.cpp b/Adafruit_GFX.cpp index af989002..da5848fa 100644 --- a/Adafruit_GFX.cpp +++ b/Adafruit_GFX.cpp @@ -1674,6 +1674,8 @@ void Adafruit_GFX_Button::initButtonUL(Adafruit_GFX *gfx, int16_t x1, /**************************************************************************/ void Adafruit_GFX_Button::drawButton(bool inverted) { uint16_t fill, outline, text; + int16_t x, y; + uint16_t w, h; if (!inverted) { fill = _fillcolor; @@ -1688,11 +1690,16 @@ void Adafruit_GFX_Button::drawButton(bool inverted) { uint8_t r = min(_w, _h) / 4; // Corner radius _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill); _gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline); - - _gfx->setCursor(_x1 + (_w / 2) - (strlen(_label) * 3 * _textsize_x), - _y1 + (_h / 2) - (4 * _textsize_y)); _gfx->setTextColor(text); _gfx->setTextSize(_textsize_x, _textsize_y); + // System font is drawn from the upper left, but custom fonts are + // drawn from the lower left. Adjust by the Y returned from getTextBounds(). + _gfx->getTextBounds(_label, _x1, _y1, &x, &y, &w, &h); + _gfx->setCursor + ( + _x1 + (_w / 2) - (w / 2), + _y1 + (_h / 2) - (h / 2) + (_y1 - y) + ); _gfx->print(_label); } From 81764008a40eeda0a5803cb49a6e27eb825fbcf0 Mon Sep 17 00:00:00 2001 From: gilesp1729 Date: Tue, 9 Jul 2024 20:23:16 +1000 Subject: [PATCH 2/5] Reformat a line of code that apparently violated some coding standard. --- Adafruit_GFX.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Adafruit_GFX.cpp b/Adafruit_GFX.cpp index da5848fa..7a2f9d86 100644 --- a/Adafruit_GFX.cpp +++ b/Adafruit_GFX.cpp @@ -1695,11 +1695,8 @@ void Adafruit_GFX_Button::drawButton(bool inverted) { // System font is drawn from the upper left, but custom fonts are // drawn from the lower left. Adjust by the Y returned from getTextBounds(). _gfx->getTextBounds(_label, _x1, _y1, &x, &y, &w, &h); - _gfx->setCursor - ( - _x1 + (_w / 2) - (w / 2), - _y1 + (_h / 2) - (h / 2) + (_y1 - y) - ); + _gfx->setCursor(_x1 + (_w / 2) - (w / 2), + _y1 + (_h / 2) - (h / 2) + (_y1 - y)); _gfx->print(_label); } From 68366b07165202255279aa9c0c800083b22361ed Mon Sep 17 00:00:00 2001 From: gilesp1729 Date: Sat, 20 Jul 2024 18:15:13 +1000 Subject: [PATCH 3/5] Make charBounds virtual, so getTextBounds can use an override. --- Adafruit_GFX.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_GFX.h b/Adafruit_GFX.h index 63c6ab68..564c5b62 100644 --- a/Adafruit_GFX.h +++ b/Adafruit_GFX.h @@ -230,7 +230,7 @@ class Adafruit_GFX : public Print { int16_t getCursorY(void) const { return cursor_y; }; protected: - void charBounds(unsigned char c, int16_t *x, int16_t *y, int16_t *minx, + virtual void charBounds(unsigned char c, int16_t *x, int16_t *y, int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy); int16_t WIDTH; ///< This is the 'raw' display width - never changes int16_t HEIGHT; ///< This is the 'raw' display height - never changes From b40954ba967fc96808b0d9184a5060a06564db0c Mon Sep 17 00:00:00 2001 From: gilesp1729 Date: Sun, 21 Jul 2024 07:29:30 +1000 Subject: [PATCH 4/5] Fix coding standard changes again picked up by PR build. --- Adafruit_GFX.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Adafruit_GFX.h b/Adafruit_GFX.h index 564c5b62..bb2c6ef5 100644 --- a/Adafruit_GFX.h +++ b/Adafruit_GFX.h @@ -230,8 +230,9 @@ class Adafruit_GFX : public Print { int16_t getCursorY(void) const { return cursor_y; }; protected: - virtual void charBounds(unsigned char c, int16_t *x, int16_t *y, int16_t *minx, - int16_t *miny, int16_t *maxx, int16_t *maxy); + virtual void charBounds(unsigned char c, int16_t *x, int16_t *y, + int16_t *minx, int16_t *miny, int16_t *maxx, + int16_t *maxy); int16_t WIDTH; ///< This is the 'raw' display width - never changes int16_t HEIGHT; ///< This is the 'raw' display height - never changes int16_t _width; ///< Display width as modified by current rotation From 7de74877299763eacc49441f4588447eedb31fa5 Mon Sep 17 00:00:00 2001 From: gilesp1729 Date: Sun, 21 Jul 2024 08:13:42 +1000 Subject: [PATCH 5/5] Spaces and tabs! --- Adafruit_GFX.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_GFX.h b/Adafruit_GFX.h index bb2c6ef5..478f385a 100644 --- a/Adafruit_GFX.h +++ b/Adafruit_GFX.h @@ -231,8 +231,8 @@ class Adafruit_GFX : public Print { protected: virtual void charBounds(unsigned char c, int16_t *x, int16_t *y, - int16_t *minx, int16_t *miny, int16_t *maxx, - int16_t *maxy); + int16_t *minx, int16_t *miny, int16_t *maxx, + int16_t *maxy); int16_t WIDTH; ///< This is the 'raw' display width - never changes int16_t HEIGHT; ///< This is the 'raw' display height - never changes int16_t _width; ///< Display width as modified by current rotation