_font cache v2

This commit is contained in:
2023-09-04 22:43:57 +03:00
parent 54761f40fe
commit 43b2348d62
4 changed files with 21 additions and 62 deletions

View File

@@ -119,7 +119,7 @@ void Font::Invalidate()
_characters.Clear();
}
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout)
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries)
{
int32 textLength = text.Length();
if (textLength == 0)
@@ -234,7 +234,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
break;
}
}
CharacterEntries.Add(entry);
characterEntries.Add(entry);
// Check if move to another line
if (moveLine)
@@ -244,8 +244,6 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
tmpLine.Size.Y = baseLinesDistance;
tmpLine.LastCharIndex = Math::Max(tmpLine.LastCharIndex, tmpLine.FirstCharIndex);
outputLines.Add(tmpLine);
outputLines.Last().CharacterEntries.Set(CharacterEntries.Get(), CharacterEntries.Count());
CharacterEntries.Clear();
// Reset line
tmpLine.Location.Y += baseLinesDistance;
@@ -269,8 +267,6 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
tmpLine.Size.Y = baseLinesDistance;
tmpLine.LastCharIndex = textLength - 1;
outputLines.Add(tmpLine);
outputLines.Last().CharacterEntries.Set(CharacterEntries.Get(), CharacterEntries.Count());
CharacterEntries.Clear();
tmpLine.Location.Y += baseLinesDistance;
}
@@ -317,7 +313,8 @@ Float2 Font::MeasureText(const StringView& text, const TextLayoutOptions& layout
// Process text
Array<FontLineCache> lines;
ProcessText(text, lines, layout);
Array<FontCharacterEntry> characterEntries;
ProcessText(text, lines, layout, characterEntries);
// Calculate bounds
Float2 max = Float2::Zero;
@@ -338,7 +335,8 @@ int32 Font::HitTestText(const StringView& text, const Float2& location, const Te
// Process text
Array<FontLineCache> lines;
ProcessText(text, lines, layout);
Array<FontCharacterEntry> characterEntries;
ProcessText(text, lines, layout, characterEntries);
ASSERT(lines.HasItems());
float scale = layout.Scale / FontManager::FontScale;
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
@@ -412,7 +410,8 @@ Float2 Font::GetCharPosition(const StringView& text, int32 index, const TextLayo
// Process text
Array<FontLineCache> lines;
ProcessText(text, lines, layout);
Array<FontCharacterEntry> characterEntries;
ProcessText(text, lines, layout, characterEntries);
ASSERT(lines.HasItems());
float scale = layout.Scale / FontManager::FontScale;
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;

View File

@@ -92,46 +92,6 @@ API_STRUCT(NoDefault) struct FLAXENGINE_API FontLineCache
{
DECLARE_SCRIPTING_TYPE_MINIMAL(FontLineCache);
FontLineCache() = default;
FontLineCache(const FontLineCache& other)
{
Location = other.Location;
Size = other.Size;
FirstCharIndex = other.FirstCharIndex;
LastCharIndex = other.LastCharIndex;
CharacterEntries.Set(other.CharacterEntries.Get(), other.CharacterEntries.Count());
}
FontLineCache(FontLineCache&& other) noexcept
{
Location = other.Location;
Size = other.Size;
FirstCharIndex = other.FirstCharIndex;
LastCharIndex = other.LastCharIndex;
CharacterEntries = MoveTemp(other.CharacterEntries);
}
FontLineCache& operator=(const FontLineCache& other)
{
Location = other.Location;
Size = other.Size;
FirstCharIndex = other.FirstCharIndex;
LastCharIndex = other.LastCharIndex;
CharacterEntries.Set(other.CharacterEntries.Get(), other.CharacterEntries.Count());
return *this;
}
FontLineCache& operator=(FontLineCache&& other) noexcept
{
Location = other.Location;
Size = other.Size;
FirstCharIndex = other.FirstCharIndex;
LastCharIndex = other.LastCharIndex;
CharacterEntries = MoveTemp(other.CharacterEntries);
return *this;
}
/// <summary>
/// The root position of the line (upper left corner).
/// </summary>
@@ -151,17 +111,12 @@ API_STRUCT(NoDefault) struct FLAXENGINE_API FontLineCache
/// The last character index (from the input text), inclusive.
/// </summary>
API_FIELD() int32 LastCharIndex;
/// <summary>
/// Character entries for each character.
/// </summary>
API_FIELD() Array<struct FontCharacterEntry> CharacterEntries;
};
template<>
struct TIsPODType<FontLineCache>
{
enum { Value = false };
enum { Value = true };
};
// Font glyph metrics:
@@ -389,7 +344,7 @@ public:
/// <param name="text">The input text.</param>
/// <param name="layout">The layout properties.</param>
/// <param name="outputLines">The output lines list.</param>
void ProcessText(const StringView& text, Array<FontLineCache>& outputLines, API_PARAM(Ref) const TextLayoutOptions& layout);
void ProcessText(const StringView& text, Array<FontLineCache>& outputLines, API_PARAM(Ref) const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries);
/// <summary>
/// Processes text to get cached lines for rendering.
@@ -400,7 +355,8 @@ public:
API_FUNCTION() Array<FontLineCache> ProcessText(const StringView& text, API_PARAM(Ref) const TextLayoutOptions& layout)
{
Array<FontLineCache> lines;
ProcessText(text, lines, layout);
Array<FontCharacterEntry> characterEntries;
ProcessText(text, lines, layout, characterEntries);
return lines;
}
@@ -414,7 +370,8 @@ public:
API_FUNCTION() Array<FontLineCache> ProcessText(const StringView& text, API_PARAM(Ref) const TextRange& textRange, API_PARAM(Ref) const TextLayoutOptions& layout)
{
Array<FontLineCache> lines;
ProcessText(textRange.Substring(text), lines, layout);
Array<FontCharacterEntry> characterEntries;
ProcessText(textRange.Substring(text), lines, layout, characterEntries);
return lines;
}

View File

@@ -1278,7 +1278,9 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
// Process text to get lines
Lines.Clear();
font->ProcessText(text, Lines, layout);
static Array<FontCharacterEntry> characterEntries(128);
characterEntries.Clear();
font->ProcessText(text, Lines, layout, characterEntries);
// Render all lines
//FontCharacterEntry entry;
@@ -1293,13 +1295,13 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
drawCall.Type = DrawCallType::DrawChar;
drawCall.AsChar.Mat = nullptr;
}
int32 entryIndex = 0;
for (int32 lineIndex = 0; lineIndex < Lines.Count(); lineIndex++)
{
const FontLineCache& line = Lines[lineIndex];
Float2 pointer = line.Location;
// Render all characters from the line
int32 entryIndex = 0;
for (int32 charIndex = line.FirstCharIndex; charIndex <= line.LastCharIndex; charIndex++)
{
// Cache current character
@@ -1310,7 +1312,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
{
// Get character entry
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
const FontCharacterEntry& entry = line.CharacterEntries[entryIndex];
const FontCharacterEntry& entry = characterEntries[entryIndex];
// Check if need to select/change font atlas (since characters even in the same font may be located in different atlases)
if (fontAtlas == nullptr || entry.TextureIndex != fontAtlasIndex)

View File

@@ -158,7 +158,8 @@ void TextRender::UpdateLayout()
// Perform layout
Array<FontLineCache> lines;
font->ProcessText(text, lines, _layoutOptions);
Array<FontCharacterEntry> characterEntries;
font->ProcessText(text, lines, _layoutOptions, characterEntries);
// Prepare buffers capacity
_ib.Data.EnsureCapacity(text.Length() * 6 * sizeof(uint16));