Removed a lot of unnecessary text processing

This commit is contained in:
Leon Styhre 2024-08-06 20:31:26 +02:00
parent b2e796c664
commit ee61283e2b
4 changed files with 21 additions and 28 deletions

View file

@ -308,8 +308,6 @@ void DateTimeEditComponent::updateTextCache()
dispString = mUppercase ? Utils::String::toUpper(getDisplayString()) : getDisplayString();
}
std::shared_ptr<Font> font {getFont()};
// Used to initialize all glyphs, which is needed to populate mMaxGlyphHeight.
font->loadGlyphs(dispString + "\n");
mTextCache = std::unique_ptr<TextCache>(font->buildTextCache(dispString, 0, 0, mColor));
if (mAlignRight)

View file

@ -479,12 +479,9 @@ void TextComponent::onTextChanged()
if (!mFont || text.empty() || mSize.x < 0.0f)
return;
float lineHeight {0.0f};
const bool isScrollable {mParent && mParent->isScrollable()};
std::shared_ptr<Font> font {mFont};
// Used to initialize all glyphs, which is needed to populate mMaxGlyphHeight.
lineHeight = mFont->loadGlyphs(text + "\n") * mLineSpacing;
const float lineHeight {mFont->getHeight(mLineSpacing)};
const bool isScrollable {mParent && mParent->isScrollable()};
const bool isMultiline {mAutoCalcExtent.y == 1 || mSize.y * mRelativeScale > lineHeight};
float offsetY {0.0f};

View file

@ -48,6 +48,16 @@ Font::Font(float size, const std::string& path)
ResourceData data {ResourceManager::getInstance().getFileData(fontPath)};
mFontFace = std::make_unique<FontFace>(std::move(data), mFontSize, path, mFontHB);
// Use the letter 'S' as a size reference.
mLetterHeight = getGlyph('S')->rows;
// As no faces should contain a newline glyph, requesting this character normally returns
// the size of the font. However there are instances where this is calculated to a slightly
// different size than the actual font size, and in this case we want to use this instead
// of the font size to avoid some minor sizing issues.
if (getGlyph('\n')->rows > mMaxGlyphHeight)
mMaxGlyphHeight = getGlyph('\n')->rows;
}
Font::~Font()
@ -133,6 +143,8 @@ glm::vec2 Font::sizeText(std::string text, float lineSpacing)
int Font::loadGlyphs(const std::string& text)
{
mMaxGlyphHeight = static_cast<int>(std::round(mFontSize));
if (getGlyph('\n')->rows > mMaxGlyphHeight)
mMaxGlyphHeight = getGlyph('\n')->rows;
std::vector<ShapeSegment> segmentsHB;
shapeText(text, segmentsHB);
@ -522,14 +534,6 @@ glm::vec2 Font::getWrappedTextCursorOffset(const std::string& wrappedText,
return glm::vec2 {lineWidth, yPos};
}
float Font::getLetterHeight()
{
if (mLetterHeight == 0.0f)
return mFontSize * 0.737f; // Only needed if face does not contain the letter 'S'.
else
return mLetterHeight;
}
std::shared_ptr<Font> Font::getFromTheme(const ThemeData::ThemeElement* elem,
unsigned int properties,
const std::shared_ptr<Font>& orig,
@ -1034,10 +1038,6 @@ Font::Glyph* Font::getGlyph(const unsigned int id)
return nullptr;
}
// Use the letter 'S' as a size reference.
if (mLetterHeight == 0 && id == 'S')
mLetterHeight = static_cast<float>(glyphSize.y);
// Create glyph.
Glyph& glyph {mGlyphMap[id]};
@ -1110,10 +1110,6 @@ Font::Glyph* Font::getGlyphByIndex(const unsigned int id, hb_font_t* fontArg, in
return nullptr;
}
// Use the letter 'S' as a size reference.
if (mLetterHeight == 0 && id == 'S')
mLetterHeight = static_cast<float>(glyphSize.y);
// Create glyph.
Glyph& glyph {mGlyphMapByIndex[std::make_tuple(id, returnedFont, xAdvance)]};

View file

@ -80,9 +80,10 @@ public:
// Returns the expected size of a string when rendered. Extra spacing is applied to the Y axis.
glm::vec2 sizeText(std::string text, float lineSpacing = 1.5f);
// Used to determine mMaxGlyphHeight upfront which is needed for accurate text sizing by
// wrapText and buildTextCache. This is required as the requested font height is not
// guaranteed and can be exceeded by a few pixels for some glyphs.
// This determines mMaxGlyphHeight upfront which is useful for accurate text sizing by
// wrapText and buildTextCache as the requested font height is not guaranteed and could be
// exceeded by a few pixels for some glyphs. However in most instances setting mMaxGlyphHeight
// to the font size is good enough, meaning this somehow expensive operation could be omitted.
int loadGlyphs(const std::string& text);
TextCache* buildTextCache(const std::string& text,
@ -115,8 +116,9 @@ public:
const float lineSpacing = 1.5f);
// Return overall height including line spacing.
float getHeight(float lineSpacing = 1.5f) const { return mMaxGlyphHeight * lineSpacing; }
float getLetterHeight();
const float getHeight(float lineSpacing = 1.5f) const { return mMaxGlyphHeight * lineSpacing; }
// This uses the letter 'S' as a size reference.
const float getLetterHeight() { return mLetterHeight; }
void reload(ResourceManager& rm) override { rebuildTextures(); }
void unload(ResourceManager& rm) override { unloadTextures(); }