From 8045a9bcea40368db2b6fea6e9a8bd6739817e37 Mon Sep 17 00:00:00 2001 From: Federico Amedeo Izzo Date: Sat, 10 Apr 2021 11:00:06 +0200 Subject: [PATCH] Improve gfx_printLine calculation --- openrtx/include/interfaces/graphics.h | 4 ++-- openrtx/src/graphics.c | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/openrtx/include/interfaces/graphics.h b/openrtx/include/interfaces/graphics.h index 59cf33eb..af38676f 100644 --- a/openrtx/include/interfaces/graphics.h +++ b/openrtx/include/interfaces/graphics.h @@ -260,8 +260,8 @@ point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment, * The print position is calculated to fit the desired number of lines in the vertical space * @param cur: current line number over total (1-based) * @param tot: number of lines to fit in screen - * @param startY: starting Y coordinate to leave space for top bar - * @param endY: ending Y coordinate to leave space for bottom bar + * @param startY: starting Y coordinate to leave space at the top, use 0 to leave no space + * @param endY: ending Y coordinate to leave space at the bottom, use 0 to leave no space * @param startX: starting X coordinate to leave space on the screen sides * @param size: text font size, defined as enum. * @param alignment: text alignment type, defined as enum. diff --git a/openrtx/src/graphics.c b/openrtx/src/graphics.c index 83feba89..c8038790 100644 --- a/openrtx/src/graphics.c +++ b/openrtx/src/graphics.c @@ -492,8 +492,9 @@ point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment, return gfx_printBuffer(start, size, alignment, color, text); } -point_t gfx_printLine(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY, uint16_t startX, - fontSize_t size, textAlign_t alignment, color_t color, const char* fmt, ... ) +point_t gfx_printLine(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY, + uint16_t startX, fontSize_t size, textAlign_t alignment, + color_t color, const char* fmt, ... ) { // Get format string and arguments from var char va_list ap; @@ -501,21 +502,18 @@ point_t gfx_printLine(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY, vsnprintf(text, sizeof(text)-1, fmt, ap); va_end(ap); + // Estimate font height by reading the gliph | height + uint8_t fontH = gfx_getFontHeight(size); + // If endY is 0 set it to default value = SCREEN_HEIGHT - if(endY == 0) - endY = SCREEN_HEIGHT; + if(endY == 0) endY = SCREEN_HEIGHT; // Calculate print coordinates - // e.g. to print 2 lines we need 3 padding spaces - uint16_t step = (endY - startY) / (tot + 1); - uint16_t printY = startY + (step * cur); - - // Estimate font height by reading the gliph | height - uint8_t h = gfx_getFontHeight(size); - - // gfx_printBuffer() prints over the starting point - // Add half print_height to get vertically centered prints - printY += (h / 2); + uint16_t height = endY - startY; + // to print 2 lines we need 3 padding spaces + uint16_t gap = (height - (fontH * tot)) / (tot + 1); + // We need a gap and a line height for each line + uint16_t printY = startY + (cur * (gap + fontH)); point_t start = {startX, printY}; return gfx_printBuffer(start, size, alignment, color, text);