_font small opt

This commit is contained in:
2023-09-09 14:45:22 +03:00
parent 49497d1a78
commit 0c8064fb0c
4 changed files with 25 additions and 27 deletions

View File

@@ -163,19 +163,18 @@ void Font::Invalidate()
#endif #endif
} }
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<float>& characterKernings) void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<int32>& characterKernings)
{ {
int32 textLength = text.Length(); int32 textLength = text.Length();
if (textLength == 0) if (textLength == 0)
return; return;
float cursorX = 0; int32 cursorX = 0;
int32 kerning; int32 kerning;
FontLineCache tmpLine; FontLineCache tmpLine;
FontCharacterEntry entry; FontCharacterEntry entry;
FontCharacterEntry previous; Char previous = 0;
Array<FontCharacterEntry> CharacterEntries(textLength);
float scale = layout.Scale / FontManager::FontScale; float scale = layout.Scale / FontManager::FontScale;
float boundsWidth = layout.Bounds.GetWidth(); int32 boundsWidth = layout.Bounds.GetWidth() / scale;
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale; float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
tmpLine.Location = Float2::Zero; tmpLine.Location = Float2::Zero;
tmpLine.Size = Float2::Zero; tmpLine.Size = Float2::Zero;
@@ -183,22 +182,22 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
tmpLine.LastCharIndex = -1; tmpLine.LastCharIndex = -1;
int32 lastWrapCharIndex = INVALID_INDEX; int32 lastWrapCharIndex = INVALID_INDEX;
float lastWrapCharX = 0; int32 lastWrapCharX = 0;
bool lastMoveLine = false; bool lastMoveLine = false;
// Process each character to split text into single lines // Process each character to split text into single lines
for (int32 currentIndex = 0; currentIndex < textLength;) for (int32 currentIndex = 0; currentIndex < textLength;)
{ {
bool moveLine = false; bool moveLine = false;
float xAdvance = 0; int32 xAdvance = 0;
int32 nextCharIndex = currentIndex + 1; int32 nextCharIndex = currentIndex + 1;
// Cache current character // Cache current character
const Char currentChar = text[currentIndex]; const Char currentChar = text[currentIndex];
const bool isWhitespace = StringUtils::IsWhitespace(currentChar); const bool isWhitespace = StringUtils::IsWhitespace(currentChar);
const bool isWrapChar = isWhitespace || StringUtils::IsUpper(currentChar) || !StringUtils::IsAlnum(currentChar);
// Check if character can wrap words // Check if character can wrap words
const bool isWrapChar = !StringUtils::IsAlnum(currentChar) || isWhitespace || StringUtils::IsUpper(currentChar);
if (isWrapChar && currentIndex != 0) if (isWrapChar && currentIndex != 0)
{ {
lastWrapCharIndex = currentIndex; lastWrapCharIndex = currentIndex;
@@ -219,16 +218,16 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
GetCharacter(currentChar, entry); GetCharacter(currentChar, entry);
// Get kerning // Get kerning
if (!isWhitespace && previous.IsValid) if (!isWhitespace && previous != 0)
{ {
kerning = entry.Font->GetKerning(previous.Character, entry.Character); kerning = GetKerning(previous, entry.Character);
} }
else else
{ {
kerning = 0; kerning = 0;
} }
previous = entry; previous = currentChar;
xAdvance = (kerning + entry.AdvanceX) * scale; xAdvance = (kerning + entry.AdvanceX);
// Check if character fits the line or skip wrapping // Check if character fits the line or skip wrapping
if (cursorX + xAdvance <= boundsWidth || layout.TextWrapping == TextWrapping::NoWrap) if (cursorX + xAdvance <= boundsWidth || layout.TextWrapping == TextWrapping::NoWrap)
@@ -279,15 +278,14 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
} }
characterEntries.Add(entry); characterEntries.Add(entry);
characterKernings.Add((float)kerning * scale); characterKernings.Add(kerning);
} }
// Check if move to another line // Check if move to another line
if (moveLine) if (moveLine)
{ {
// Add line // Add line
tmpLine.Size.X = cursorX; tmpLine.Size.X = (float)cursorX * scale;
tmpLine.Size.Y = baseLinesDistance; tmpLine.Size.Y = baseLinesDistance;
tmpLine.LastCharIndex = Math::Max(tmpLine.LastCharIndex, tmpLine.FirstCharIndex); tmpLine.LastCharIndex = Math::Max(tmpLine.LastCharIndex, tmpLine.FirstCharIndex);
outputLines.Add(tmpLine); outputLines.Add(tmpLine);
@@ -299,7 +297,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
cursorX = 0; cursorX = 0;
lastWrapCharIndex = INVALID_INDEX; lastWrapCharIndex = INVALID_INDEX;
lastWrapCharX = 0; lastWrapCharX = 0;
previous.IsValid = false; previous = 0;
} }
currentIndex = nextCharIndex; currentIndex = nextCharIndex;
@@ -310,7 +308,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
if (tmpLine.LastCharIndex >= tmpLine.FirstCharIndex || text[textLength - 1] == '\n') if (tmpLine.LastCharIndex >= tmpLine.FirstCharIndex || text[textLength - 1] == '\n')
{ {
// Add line // Add line
tmpLine.Size.X = cursorX; tmpLine.Size.X = (float)cursorX * scale;
tmpLine.Size.Y = baseLinesDistance; tmpLine.Size.Y = baseLinesDistance;
tmpLine.LastCharIndex = textLength; tmpLine.LastCharIndex = textLength;
outputLines.Add(tmpLine); outputLines.Add(tmpLine);
@@ -361,7 +359,7 @@ Float2 Font::MeasureText(const StringView& text, const TextLayoutOptions& layout
// Process text // Process text
Array<FontLineCache> lines; Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries; Array<FontCharacterEntry> characterEntries;
Array<float> xAdvances; Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances); ProcessText(text, lines, layout, characterEntries, xAdvances);
// Calculate bounds // Calculate bounds
@@ -384,7 +382,7 @@ int32 Font::HitTestText(const StringView& text, const Float2& location, const Te
// Process text // Process text
Array<FontLineCache> lines; Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries; Array<FontCharacterEntry> characterEntries;
Array<float> xAdvances; Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances); ProcessText(text, lines, layout, characterEntries, xAdvances);
ASSERT(lines.HasItems()); ASSERT(lines.HasItems());
float scale = layout.Scale / FontManager::FontScale; float scale = layout.Scale / FontManager::FontScale;
@@ -460,7 +458,7 @@ Float2 Font::GetCharPosition(const StringView& text, int32 index, const TextLayo
// Process text // Process text
Array<FontLineCache> lines; Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries; Array<FontCharacterEntry> characterEntries;
Array<float> xAdvances; Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances); ProcessText(text, lines, layout, characterEntries, xAdvances);
ASSERT(lines.HasItems()); ASSERT(lines.HasItems());
float scale = layout.Scale / FontManager::FontScale; float scale = layout.Scale / FontManager::FontScale;

View File

@@ -350,7 +350,7 @@ public:
/// <param name="text">The input text.</param> /// <param name="text">The input text.</param>
/// <param name="layout">The layout properties.</param> /// <param name="layout">The layout properties.</param>
/// <param name="outputLines">The output lines list.</param> /// <param name="outputLines">The output lines list.</param>
void ProcessText(const StringView& text, Array<FontLineCache>& outputLines, API_PARAM(Ref) const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<float>& characterKernings); void ProcessText(const StringView& text, Array<FontLineCache>& outputLines, API_PARAM(Ref) const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<int32>& characterKernings);
/// <summary> /// <summary>
/// Processes text to get cached lines for rendering. /// Processes text to get cached lines for rendering.
@@ -362,7 +362,7 @@ public:
{ {
Array<FontLineCache> lines; Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries; Array<FontCharacterEntry> characterEntries;
Array<float> xAdvances; Array<int32> xAdvances;
ProcessText(text, lines, layout, characterEntries, xAdvances); ProcessText(text, lines, layout, characterEntries, xAdvances);
return lines; return lines;
} }
@@ -378,7 +378,7 @@ public:
{ {
Array<FontLineCache> lines; Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries; Array<FontCharacterEntry> characterEntries;
Array<float> xAdvances; Array<int32> xAdvances;
ProcessText(textRange.Substring(text), lines, layout, characterEntries, xAdvances); ProcessText(textRange.Substring(text), lines, layout, characterEntries, xAdvances);
return lines; return lines;
} }

View File

@@ -1357,7 +1357,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
// Process text to get lines // Process text to get lines
Lines.Clear(); Lines.Clear();
static Array<FontCharacterEntry> characterEntries(128); static Array<FontCharacterEntry> characterEntries(128);
static Array<float> characterKernings(128); static Array<int32> characterKernings(128);
characterEntries.Clear(); characterEntries.Clear();
characterKernings.Clear(); characterKernings.Clear();
@@ -1409,7 +1409,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
} }
pointer.X += (float)kerning * scale;*/ pointer.X += (float)kerning * scale;*/
pointer.X += kerning; pointer.X += kerning * scale;
previous = entry; previous = entry;
// Omit whitespace characters // Omit whitespace characters

View File

@@ -159,7 +159,7 @@ void TextRender::UpdateLayout()
// Perform layout // Perform layout
Array<FontLineCache> lines; Array<FontLineCache> lines;
Array<FontCharacterEntry> characterEntries; Array<FontCharacterEntry> characterEntries;
Array<float> xAdvances; Array<int32> xAdvances;
font->ProcessText(text, lines, _layoutOptions, characterEntries, xAdvances); font->ProcessText(text, lines, _layoutOptions, characterEntries, xAdvances);
// Prepare buffers capacity // Prepare buffers capacity