small cleanup in TextPainter

This commit is contained in:
Kae 2023-06-23 19:32:41 +10:00
parent 49c487cd82
commit 0135629301
2 changed files with 34 additions and 30 deletions

View File

@ -325,13 +325,43 @@ void TextPainter::cleanup(int64_t timeout) {
m_fontTextureGroup.cleanup(timeout); m_fontTextureGroup.cleanup(timeout);
} }
void TextPainter::applyCommands(String const& unsplitCommands) {
auto commands = unsplitCommands.split(',');
for (auto& command : commands) {
try {
if (command == "reset") {
m_renderSettings = m_savedRenderSettings;
} else if (command == "set") {
m_savedRenderSettings = m_renderSettings;
} else if (command == "shadow") {
m_renderSettings.mode = (FontMode)((int)m_renderSettings.mode | (int)FontMode::Shadow);
} else if (command == "noshadow") {
m_renderSettings.mode = (FontMode)((int)m_renderSettings.mode & (-1 ^ (int)FontMode::Shadow));
} else if (command.beginsWith("font=")) {
m_renderSettings.font = command.substr(5);
} else if (command.beginsWith("directives=")) {
// Honestly this is really stupid but I just couldn't help myself
// Should probably limit in the future
m_renderSettings.directives = command.substr(11);
} else {
// expects both #... sequences and plain old color names.
Color c = jsonToColor(command);
c.setAlphaF(c.alphaF() * ((float)m_savedRenderSettings.color[3]) / 255);
m_renderSettings.color = c.toRgba();
}
} catch (JsonException&) {
} catch (ColorException&) {
}
}
}
RectF TextPainter::doRenderText(String const& s, TextPositioning const& position, bool reallyRender, unsigned* charLimit) { RectF TextPainter::doRenderText(String const& s, TextPositioning const& position, bool reallyRender, unsigned* charLimit) {
Vec2F pos = position.pos; Vec2F pos = position.pos;
StringList lines = wrapText(s, position.wrapWidth); StringList lines = wrapText(s, position.wrapWidth);
int height = (lines.size() - 1) * m_lineSpacing * m_fontSize + m_fontSize; int height = (lines.size() - 1) * m_lineSpacing * m_fontSize + m_fontSize;
RenderSettings savedRenderSettings = m_renderSettings; RenderSettings backupRenderSettings = m_renderSettings;
m_savedRenderSettings = m_renderSettings; m_savedRenderSettings = m_renderSettings;
if (position.vAnchor == VerticalAnchor::BottomAnchor) if (position.vAnchor == VerticalAnchor::BottomAnchor)
@ -348,7 +378,7 @@ RectF TextPainter::doRenderText(String const& s, TextPositioning const& position
break; break;
} }
m_renderSettings = move(savedRenderSettings); m_renderSettings = move(backupRenderSettings);
return bounds; return bounds;
} }
@ -393,33 +423,7 @@ RectF TextPainter::doRenderLine(String const& s, TextPositioning const& position
bounds.combine(glyphBounds); bounds.combine(glyphBounds);
pos.pos[0] += glyphBounds.width(); pos.pos[0] += glyphBounds.width();
} else if (c == Text::EndEsc) { } else if (c == Text::EndEsc) {
auto commands = escapeCode.split(','); applyCommands(escapeCode);
for (auto& command : commands) {
try {
if (command == "reset") {
m_renderSettings = m_savedRenderSettings;
} else if (command == "set") {
m_savedRenderSettings = m_renderSettings;
} else if (command == "shadow") {
m_renderSettings.mode = (FontMode)((int)m_renderSettings.mode | (int)FontMode::Shadow);
} else if (command == "noshadow") {
m_renderSettings.mode = (FontMode)((int)m_renderSettings.mode & (-1 ^ (int)FontMode::Shadow));
} else if (command.beginsWith("font=")) {
m_renderSettings.font = command.substr(5);
} else if (command.beginsWith("directives=")) {
// Honestly this is really stupid but I just couldn't help myself
// Should probably limit in the future
m_renderSettings.directives = command.substr(11);
} else {
// expects both #... sequences and plain old color names.
Color c = jsonToColor(command);
c.setAlphaF(c.alphaF() * ((float)m_savedRenderSettings.color[3]) / 255);
m_renderSettings.color = c.toRgba();
}
} catch (JsonException&) {
} catch (ColorException&) {
}
}
escape = false; escape = false;
escapeCode = ""; escapeCode = "";
} }

View File

@ -79,7 +79,7 @@ public:
void reloadFonts(); void reloadFonts();
void cleanup(int64_t textureTimeout); void cleanup(int64_t textureTimeout);
void applyCommands(String const& unsplitCommands);
private: private:
struct RenderSettings { struct RenderSettings {
FontMode mode; FontMode mode;