Improve gfx_printLine calculation

replace/06f94b7a09643ebea60b6562a1123a203ae59487
Federico Amedeo Izzo 2021-04-10 11:00:06 +02:00
rodzic 3e326a43f0
commit 8045a9bcea
2 zmienionych plików z 14 dodań i 16 usunięć

Wyświetl plik

@ -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.

Wyświetl plik

@ -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);