Merge pull request #677 from jfrickmann/LuaDrawTextLines

Improved Lua draw text lines
This commit is contained in:
Peter Feerick
2021-09-06 11:53:09 +10:00
committed by GitHub
2 changed files with 56 additions and 12 deletions
+31 -8
View File
@@ -511,20 +511,43 @@ void drawTextLines(BitmapBuffer * dc, coord_t left, coord_t top, coord_t width,
coord_t line = getFontHeight(flags & 0xFFFF);
coord_t space = getTextWidth(" ", 1, flags);
coord_t word;
const char * eos = str + strlen(str);
const char * nxt;
const char * nxt = str;
flags &= ~(VCENTERED | CENTERED | RIGHT);
while (str < eos) {
nxt = strstr(str, " ");
if (!nxt) nxt = eos;
while (true) {
for (bool done = false; !done; nxt++) {
switch (nxt[0]) {
case '-':
case '/':
case ':':
nxt++;
case ' ':
case '\n':
case '\0':
done = true;
}
}
nxt--;
word = getTextWidth(str, nxt - str, flags);
if (x + word > left + width && x > left) {
x = left;
y += line;
}
if (y + line > top + height) break;
if (y + line > top + height) return;
dc->drawSizedText(x, y, str, nxt - str, flags);
str = nxt + 1;
x += word + space;
x += word;
switch (nxt[0]) {
case '\0':
return;
case '\n':
x = left;
y += line;
nxt++;
break;
case ' ':
x += space;
nxt++;
}
str = nxt;
}
}
+25 -4
View File
@@ -300,7 +300,7 @@ Draw text inside rectangle (x,y,w,h) with line breaks
@param text (string) text to display
@param flags (optional) please see [Lcd functions overview](../lcd-functions-less-than-greater-than-luadoc-begin-lcd/lcd_functions-overview.html) for drawing flags and colors, and [Appendix](../../part_vii_-_appendix/fonts.md) for available characters in each font set. INVERS and BLINK are not implemented.
@param flags (optional) please see [Lcd functions overview](../lcd-functions-less-than-greater-than-luadoc-begin-lcd/lcd_functions-overview.html) for drawing flags and colors, and [Appendix](../../part_vii_-_appendix/fonts.md) for available characters in each font set. RIGHT, CENTER and VCENTER are not implemented.
@status current Introduced in 2.5.0
*/
@@ -316,9 +316,30 @@ static int luaLcdDrawTextLines(lua_State *L)
const char * s = luaL_checkstring(L, 5);
LcdFlags flags = luaL_optunsigned(L, 6, 0);
// apply text offsets, needed to align 2.4.x to 2.3.x font baselines
x += getTextHorizontalOffset(flags);
y += getTextVerticalOffset(flags);
bool invers = flags & INVERS;
if (flags & BLINK)
invers = invers && !BLINK_ON_PHASE;
if (invers) {
// Find inverse color or read from optional Lua argument
LcdFlags color = flagsRGB(flags);
LcdFlags invColor = luaL_optunsigned(L, 7, ~0u); // ~0 is impossible for color flag!
if (invColor == ~0u) {
RGB_SPLIT(COLOR_VAL(color), r, g, b);
invColor = COLOR2FLAGS(RGB_JOIN(31 - r, 63 - g, 31 - b));
} else
invColor = flagsRGB(invColor);
flags = (flags & 0xFFFF) | invColor;
// Draw color box
luaLcdBuffer->drawSolidFilledRect(x, y, w, h, color);
} else {
if ((flags & BLINK) && !BLINK_ON_PHASE)
return 0;
if (flags & SHADOWED)
drawTextLines(luaLcdBuffer, x+1, y+1, w, h, s, flags & 0xFFFF); // force black
flags = (flags & 0xFFFF) | flagsRGB(flags);
}
drawTextLines(luaLcdBuffer, x, y, w, h, s, flags);
return 0;