From e5bb4d3fb7bb55c3e721f0cc0c105eb8521bd5a1 Mon Sep 17 00:00:00 2001 From: yaapu Date: Fri, 11 Jun 2021 14:44:39 +0000 Subject: [PATCH] colorlcd: fix for drawNumber INVERS and text vertical and horizontal offsets --- radio/src/lua/api_colorlcd.cpp | 52 ++++++++++++++++++++++++++++++++-- radio/src/lua/api_colorlcd.h | 3 ++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/radio/src/lua/api_colorlcd.cpp b/radio/src/lua/api_colorlcd.cpp index 356f0a43f..a39df5517 100644 --- a/radio/src/lua/api_colorlcd.cpp +++ b/radio/src/lua/api_colorlcd.cpp @@ -27,6 +27,28 @@ #include "api_colorlcd.h" BitmapBuffer* luaLcdBuffer = nullptr; + +static int8_t getTextHorizontalOffset(LcdFlags flags) +{ + // no need to adjust if not right aligned + if (!(flags & RIGHT)) { + return 0; + } + const uint8_t font_index = FONT_INDEX(flags); + if (font_index >= sizeof(text_horizontal_offset)) { + return 0; + } + return text_horizontal_offset[font_index]; +} + +static int8_t getTextVerticalOffset(LcdFlags flags) +{ + const uint8_t font_index = FONT_INDEX(flags); + if (font_index >= sizeof(text_vertical_offset)) { + return 0; + } + return text_vertical_offset[font_index]; +} /*luadoc @function lcd.refresh() @@ -202,6 +224,10 @@ static int luaLcdDrawText(lua_State *L) const char * s = luaL_checkstring(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); + // apply text offsets, needed to align 2.4.x to 2.3.x font baselines + x += getTextHorizontalOffset(att); + y += getTextVerticalOffset(att); + bool shadowed = att & SHADOWED; bool invers = (att & INVERS); if (att & BLINK) invers = invers && !(BLINK_ON_PHASE); @@ -256,6 +282,10 @@ static int luaLcdDrawTimer(lua_State *L) int seconds = luaL_checkinteger(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); + // apply text offsets, needed to align 2.4.x to 2.3.x font baselines + x += getTextHorizontalOffset(att); + y += getTextVerticalOffset(att); + bool shadowed = att & SHADOWED; att = (att & 0xFFFF) | COLOR(COLOR_VAL(att)); @@ -295,8 +325,26 @@ static int luaLcdDrawNumber(lua_State *L) int val = luaL_checkinteger(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); + bool invers = (att & INVERS); att = (att & 0xFFFF) | COLOR(COLOR_VAL(att)); + // apply text offsets, needed to align 2.4.x to 2.3.x font baselines + x += getTextHorizontalOffset(att); + y += getTextVerticalOffset(att); + + char s[49]; + BitmapBuffer::formatNumberAsString(s, 49, val, att & 0xFFFF); + + if (invers) { + int height = getFontHeight(att & 0xFFFF); + int width = getTextWidth(s, 255, att); + luaLcdBuffer->drawSolidFilledRect( + x - INVERT_BOX_MARGIN, y - INVERT_BOX_MARGIN, + width + 2 * INVERT_BOX_MARGIN, height + 2 * INVERT_BOX_MARGIN, + FOCUS_BGCOLOR); + att = (att & 0xFFFF) | FOCUS_COLOR; + } + if ((att & SHADOWED) && !(att & INVERS)) { luaLcdBuffer->drawNumber(x, y, val, att & 0xFFFF); } @@ -664,9 +712,9 @@ static int luaLcdDrawGauge(lua_State *L) int num = luaL_checkinteger(L, 5); int den = luaL_checkinteger(L, 6); unsigned int flags = luaL_optunsigned(L, 7, 0); - + flags = (flags & 0xFFFF) | COLOR(COLOR_VAL(flags)); - + luaLcdBuffer->drawRect(x, y, w, h, 1, 0xff, flags); uint8_t len = limit((uint8_t)1, uint8_t(w*num/den), uint8_t(w)); luaLcdBuffer->drawSolidFilledRect(x+1, y+1, len, h-2, flags); diff --git a/radio/src/lua/api_colorlcd.h b/radio/src/lua/api_colorlcd.h index ae5f17109..13d480a66 100644 --- a/radio/src/lua/api_colorlcd.h +++ b/radio/src/lua/api_colorlcd.h @@ -33,5 +33,8 @@ constexpr coord_t INVERT_BOX_MARGIN = 2; +constexpr int8_t text_horizontal_offset[7] {-2,-2,-2,-2,-2,-2,-2}; +constexpr int8_t text_vertical_offset[7] {2,0,0,2,3,3,7}; + extern bool luaLcdAllowed; extern BitmapBuffer * luaLcdBuffer;