1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2026-08-18 18:32:30 +02:00

Fix base line logic for fonts

This commit is contained in:
Norbert Walter
2026-02-28 18:33:32 +00:00
parent 5a30d7d022
commit 39c4e7fb32
+26 -13
View File
@@ -487,42 +487,55 @@ std::vector<String> wordwrap(String &line, uint16_t maxwidth) {
// Draw centered text // Draw centered text
void drawTextCenter(int16_t cx, int16_t cy, String text) { void drawTextCenter(int16_t cx, int16_t cy, String text) {
#ifdef DISPLAY_ST7796
auto oldDatum = getdisplay().getTextDatum();
getdisplay().setTextDatum(textdatum_t::middle_center);
getdisplay().drawString(text, cx, cy);
getdisplay().setTextDatum(oldDatum);
#else
int16_t x1, y1; int16_t x1, y1;
uint16_t w, h; uint16_t w, h;
#ifdef DISPLAY_ST7796
// LovyanGFX doesn't expose getTextBounds; use width/height helpers
w = getdisplay().textWidth(text);
h = getdisplay().fontHeight();
#else
getdisplay().getTextBounds(text, 0, 150, &x1, &y1, &w, &h); getdisplay().getTextBounds(text, 0, 150, &x1, &y1, &w, &h);
#endif
getdisplay().setCursor(cx - w / 2, cy + h / 2); getdisplay().setCursor(cx - w / 2, cy + h / 2);
getdisplay().print(text); getdisplay().print(text);
#endif
} }
// Draw centered botton with centered text // Draw centered botton with centered text
void drawButtonCenter(int16_t cx, int16_t cy, int8_t sx, int8_t sy, String text, uint16_t fg, uint16_t bg, bool inverted) { void drawButtonCenter(int16_t cx, int16_t cy, int8_t sx, int8_t sy, String text, uint16_t fg, uint16_t bg, bool inverted) {
int16_t x1, y1;
uint16_t w, h;
uint16_t color; uint16_t color;
#ifdef DISPLAY_ST7796 #ifndef DISPLAY_ST7796
w = getdisplay().textWidth(text); int16_t x1, y1;
h = getdisplay().fontHeight(); uint16_t w, h;
#else
getdisplay().getTextBounds(text, cx, cy, &x1, &y1, &w, &h); // Find text center getdisplay().getTextBounds(text, cx, cy, &x1, &y1, &w, &h); // Find text center
#endif #endif
getdisplay().setCursor(cx - w/2, cy + h/2); // Set cursor to center
//getdisplay().drawPixel(cx, cy, fg); // Debug pixel for center position //getdisplay().drawPixel(cx, cy, fg); // Debug pixel for center position
if (inverted) { if (inverted) {
getdisplay().fillRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button getdisplay().fillRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button
getdisplay().setTextColor(bg); getdisplay().setTextColor(bg);
#ifdef DISPLAY_ST7796
auto oldDatum = getdisplay().getTextDatum();
getdisplay().setTextDatum(textdatum_t::middle_center);
getdisplay().drawString(text, cx, cy);
getdisplay().setTextDatum(oldDatum);
#else
getdisplay().setCursor(cx - w/2, cy + h/2); // Set cursor to center
getdisplay().print(text); // Draw text getdisplay().print(text); // Draw text
#endif
} }
else{ else{
getdisplay().drawRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button getdisplay().drawRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button
getdisplay().setTextColor(fg); getdisplay().setTextColor(fg);
#ifdef DISPLAY_ST7796
auto oldDatum = getdisplay().getTextDatum();
getdisplay().setTextDatum(textdatum_t::middle_center);
getdisplay().drawString(text, cx, cy);
getdisplay().setTextDatum(oldDatum);
#else
getdisplay().setCursor(cx - w/2, cy + h/2); // Set cursor to center
getdisplay().print(text); // Draw text getdisplay().print(text); // Draw text
#endif
} }
} }