Skip to content

Make Adafruit_GFX_Button::drawButton work with custom fonts by using … #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Adafruit_GFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1688,11 +1690,13 @@ 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);
}

Expand Down
5 changes: 3 additions & 2 deletions Adafruit_GFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ 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,
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
Expand Down
Loading