Added font size overflow restrictions to TextComponent and DateTimeComponent.

Also fixed a crash that could occur in TextComponent when blank/dummy fonts were used.
This commit is contained in:
Leon Styhre 2022-10-10 20:37:04 +02:00
parent 610ac9adb3
commit 39c9bd2cbc
2 changed files with 24 additions and 4 deletions

View file

@ -209,6 +209,14 @@ void DateTimeComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
}
}
float maxHeight {0.0f};
if (!theme->isLegacyTheme() && properties & elem->has("size")) {
const glm::vec2 size {elem->get<glm::vec2>("size")};
if (size.x != 0.0f && size.y != 0.0f)
maxHeight = mSize.y * 2.0f;
}
// Legacy themes only.
if (properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
setUppercase(elem->get<bool>("forceUppercase"));
@ -216,5 +224,5 @@ void DateTimeComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
if (properties & LINE_SPACING && elem->has("lineSpacing"))
setLineSpacing(glm::clamp(elem->get<float>("lineSpacing"), 0.5f, 3.0f));
setFont(Font::getFromTheme(elem, properties, mFont));
setFont(Font::getFromTheme(elem, properties, mFont, maxHeight));
}

View file

@ -69,7 +69,7 @@ TextComponent::TextComponent(const std::string& text,
void TextComponent::onSizeChanged()
{
mAutoCalcExtent = glm::ivec2 {(getSize().x == 0), (getSize().y == 0)};
mAutoCalcExtent = glm::ivec2 {getSize().x == 0, getSize().y == 0};
onTextChanged();
}
@ -254,8 +254,12 @@ void TextComponent::onTextChanged()
text = mText; // Original case.
}
if (mFont && mAutoCalcExtent.x)
if (mFont && mAutoCalcExtent.x) {
mSize = mFont->sizeText(text, mLineSpacing);
// This can happen under special circumstances like when a blank/dummy font is used.
if (mSize.x == 0.0f)
return;
}
if (!mFont || text.empty() || mSize.x < 0.0f)
return;
@ -453,6 +457,14 @@ void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
}
}
float maxHeight {0.0f};
if (!theme->isLegacyTheme() && properties & elem->has("size")) {
const glm::vec2 size {elem->get<glm::vec2>("size")};
if (size.x != 0.0f && size.y != 0.0f)
maxHeight = mSize.y * 2.0f;
}
// Legacy themes only.
if (properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
setUppercase(elem->get<bool>("forceUppercase"));
@ -460,5 +472,5 @@ void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
if (properties & LINE_SPACING && elem->has("lineSpacing"))
setLineSpacing(glm::clamp(elem->get<float>("lineSpacing"), 0.5f, 3.0f));
setFont(Font::getFromTheme(elem, properties, mFont));
setFont(Font::getFromTheme(elem, properties, mFont, maxHeight));
}