richtext renderer now supports changing of font size

This commit is contained in:
2021-09-06 00:35:26 +02:00
parent 738b98927a
commit d3352aa587

View File

@@ -28,21 +28,37 @@ int16_t renderRichText(std::string_view str, int32_t poX, int32_t poY, uint8_t f
if (str.empty()) if (str.empty())
return 0; return 0;
const int16_t fontHeight = tft.fontHeight(font);
const uint8_t oldFont = font;
const uint16_t oldColor = tft.textcolor; const uint16_t oldColor = tft.textcolor;
int16_t width{}; int16_t width{};
const auto drawString = [&poX, &poY, &font, &width, &fontHeight, &oldFont](std::string_view str) {
const auto addedWith = tft.drawString(str, poX, poY, font);
if (font != oldFont)
{
if (const int16_t newFontHeight = tft.fontHeight(font); newFontHeight < fontHeight)
{
tft.fillRect(poX, poY + newFontHeight,
addedWith, fontHeight - newFontHeight,
tft.textbgcolor);
}
}
poX += addedWith;
width += addedWith;
};
again: again:
if (const auto index = str.find('&'); index != std::string_view::npos) if (const auto index = str.find('&'); index != std::string_view::npos)
{ {
{ {
std::string_view tempStr{std::begin(str), index}; std::string_view tempStr{std::begin(str), index};
if (!tempStr.empty()) if (!tempStr.empty())
{ drawString(tempStr);
const auto addedWith = tft.drawString(tempStr, poX, poY, font);
poX += addedWith;
width += addedWith;
}
} }
auto newIter = std::begin(str) + index + 1; auto newIter = std::begin(str) + index + 1;
@@ -84,27 +100,32 @@ again:
} }
break; break;
} }
// case '&': case 'f':
// { case 's':
// const char buf[1] = { '&' }; case 'm':
// const auto addedWith = tft.drawString(std::string_view{buf, std::size(buf)}, poX, poY, font); {
// poX += addedWith; font = [&controlChar,&oldFont]() -> uint8_t {
// width += addedWith; switch (controlChar)
{
case 'f': return oldFont;
case 's': return 2;
case 'm': return 4;
}
__builtin_unreachable();
}();
// auto newNewIter = newIter + 1; auto newNewIter = newIter + 1;
// if (newNewIter != std::end(str)) if (newNewIter != std::end(str))
// { {
// str = std::string_view(newNewIter, std::distance(newNewIter, std::end(str))); str = std::string_view(newNewIter, std::distance(newNewIter, std::end(str)));
// goto again; goto again;
// } }
// break; break;
// } }
default: default:
{ {
const char buf[2] = { '&', controlChar }; const char buf[2] = { '&', controlChar };
const auto addedWith = tft.drawString(std::string_view{buf, std::size(buf)}, poX, poY, font); drawString(std::string_view{buf, std::size(buf)});
poX += addedWith;
width += addedWith;
auto newNewIter = newIter + 1; auto newNewIter = newIter + 1;
if (newNewIter != std::end(str)) if (newNewIter != std::end(str))
@@ -119,16 +140,12 @@ again:
else else
{ {
const char buf[1] = { '&' }; const char buf[1] = { '&' };
const auto addedWith = tft.drawString(std::string_view{buf, std::size(buf)}, poX, poY, font); drawString(std::string_view{buf, std::size(buf)});
poX += addedWith;
width += addedWith;
} }
} }
else if (!str.empty()) else if (!str.empty())
{ {
const auto addedWith = tft.drawString(str, poX, poY, font); drawString(str);
poX += addedWith;
width += addedWith;
} }
tft.setTextColor(oldColor, tft.textbgcolor); tft.setTextColor(oldColor, tft.textbgcolor);