Merge pull request #677 from jfrickmann/LuaDrawTextLines
Improved Lua draw text lines
This commit is contained in:
@@ -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 line = getFontHeight(flags & 0xFFFF);
|
||||||
coord_t space = getTextWidth(" ", 1, flags);
|
coord_t space = getTextWidth(" ", 1, flags);
|
||||||
coord_t word;
|
coord_t word;
|
||||||
const char * eos = str + strlen(str);
|
const char * nxt = str;
|
||||||
const char * nxt;
|
flags &= ~(VCENTERED | CENTERED | RIGHT);
|
||||||
|
|
||||||
while (str < eos) {
|
while (true) {
|
||||||
nxt = strstr(str, " ");
|
for (bool done = false; !done; nxt++) {
|
||||||
if (!nxt) nxt = eos;
|
switch (nxt[0]) {
|
||||||
|
case '-':
|
||||||
|
case '/':
|
||||||
|
case ':':
|
||||||
|
nxt++;
|
||||||
|
case ' ':
|
||||||
|
case '\n':
|
||||||
|
case '\0':
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nxt--;
|
||||||
word = getTextWidth(str, nxt - str, flags);
|
word = getTextWidth(str, nxt - str, flags);
|
||||||
if (x + word > left + width && x > left) {
|
if (x + word > left + width && x > left) {
|
||||||
x = left;
|
x = left;
|
||||||
y += line;
|
y += line;
|
||||||
}
|
}
|
||||||
if (y + line > top + height) break;
|
if (y + line > top + height) return;
|
||||||
dc->drawSizedText(x, y, str, nxt - str, flags);
|
dc->drawSizedText(x, y, str, nxt - str, flags);
|
||||||
str = nxt + 1;
|
x += word;
|
||||||
x += word + space;
|
switch (nxt[0]) {
|
||||||
|
case '\0':
|
||||||
|
return;
|
||||||
|
case '\n':
|
||||||
|
x = left;
|
||||||
|
y += line;
|
||||||
|
nxt++;
|
||||||
|
break;
|
||||||
|
case ' ':
|
||||||
|
x += space;
|
||||||
|
nxt++;
|
||||||
|
}
|
||||||
|
str = nxt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -300,7 +300,7 @@ Draw text inside rectangle (x,y,w,h) with line breaks
|
|||||||
|
|
||||||
@param text (string) text to display
|
@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
|
@status current Introduced in 2.5.0
|
||||||
*/
|
*/
|
||||||
@@ -316,9 +316,30 @@ static int luaLcdDrawTextLines(lua_State *L)
|
|||||||
const char * s = luaL_checkstring(L, 5);
|
const char * s = luaL_checkstring(L, 5);
|
||||||
LcdFlags flags = luaL_optunsigned(L, 6, 0);
|
LcdFlags flags = luaL_optunsigned(L, 6, 0);
|
||||||
|
|
||||||
// apply text offsets, needed to align 2.4.x to 2.3.x font baselines
|
bool invers = flags & INVERS;
|
||||||
x += getTextHorizontalOffset(flags);
|
if (flags & BLINK)
|
||||||
y += getTextVerticalOffset(flags);
|
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);
|
drawTextLines(luaLcdBuffer, x, y, w, h, s, flags);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user