_font cache whitespace

This commit is contained in:
2023-09-10 12:52:24 +03:00
parent 479b65fab5
commit a2edead792
4 changed files with 112 additions and 126 deletions

View File

@@ -167,15 +167,14 @@ void Font::Invalidate()
#endif
}
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<int32>& characterKernings)
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontLineCharacterCache>* characterEntries)
{
int32 textLength = text.Length();
if (textLength == 0)
return;
int32 cursorX = 0;
int32 kerning;
FontLineCache tmpLine;
FontCharacterEntry entry;
FontLineCharacterCache characterCache;
Char previous = 0;
float scale = layout.Scale / FontManager::FontScale;
int32 boundsWidth = layout.Bounds.GetWidth() / scale;
@@ -198,8 +197,8 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
// Cache current character
const Char currentChar = text[currentIndex];
const bool isWhitespace = StringUtils::IsWhitespace(currentChar);
const bool isWrapChar = isWhitespace || StringUtils::IsUpper(currentChar) || !StringUtils::IsAlnum(currentChar);
characterCache.IsWhitespace = StringUtils::IsWhitespace(currentChar);
const bool isWrapChar = characterCache.IsWhitespace || StringUtils::IsUpper(currentChar) || !StringUtils::IsAlnum(currentChar);
// Check if character can wrap words
if (isWrapChar && currentIndex != 0)
@@ -219,19 +218,19 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
else
{
// Get character entry
GetCharacter(currentChar, entry);
GetCharacter(currentChar, characterCache.Entry);
// Get kerning
if (!isWhitespace && previous != 0)
if (!characterCache.IsWhitespace && previous != 0)
{
kerning = GetKerning(previous, entry.Character);
characterCache.Kerning = GetKerning(previous, currentChar);
}
else
{
kerning = 0;
characterCache.Kerning = 0;
}
previous = currentChar;
xAdvance = (kerning + entry.AdvanceX);
xAdvance = (characterCache.Kerning + characterCache.Entry.AdvanceX);
// Check if character fits the line or skip wrapping
if (cursorX + xAdvance <= boundsWidth || layout.TextWrapping == TextWrapping::NoWrap)
@@ -281,8 +280,8 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
break;
}
characterEntries.Add(entry);
characterKernings.Add(kerning);
if (characterEntries != nullptr)
characterEntries->Add(characterCache);
}
// Check if move to another line
@@ -362,9 +361,7 @@ Float2 Font::MeasureText(const StringView& text, const TextLayoutOptions& layout
// Process text
Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries;
Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances);
ProcessText(text, lines, layout);
// Calculate bounds
Float2 max = Float2::Zero;
@@ -385,9 +382,7 @@ int32 Font::HitTestText(const StringView& text, const Float2& location, const Te
// Process text
Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries;
Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances);
ProcessText(text, lines, layout);
ASSERT(lines.HasItems());
float scale = layout.Scale / FontManager::FontScale;
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
@@ -461,9 +456,7 @@ Float2 Font::GetCharPosition(const StringView& text, int32 index, const TextLayo
// Process text
Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries;
Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances);
ProcessText(text, lines, layout);
ASSERT(lines.HasItems());
float scale = layout.Scale / FontManager::FontScale;
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;