_font cache processtext entry

This commit is contained in:
2023-09-03 20:11:51 +03:00
parent 4ad78fdb80
commit 54761f40fe
3 changed files with 57 additions and 3 deletions

View File

@@ -129,6 +129,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
FontLineCache tmpLine;
FontCharacterEntry entry;
FontCharacterEntry previous;
Array<FontCharacterEntry> CharacterEntries(textLength);
float scale = layout.Scale / FontManager::FontScale;
float boundsWidth = layout.Bounds.GetWidth();
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
@@ -233,6 +234,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
break;
}
}
CharacterEntries.Add(entry);
// Check if move to another line
if (moveLine)
@@ -242,6 +244,8 @@ 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;
@@ -265,6 +269,8 @@ 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;
}

View File

@@ -92,6 +92,46 @@ 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>
@@ -111,12 +151,17 @@ 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 = true };
enum { Value = false };
};
// Font glyph metrics:

View File

@@ -1281,7 +1281,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
font->ProcessText(text, Lines, layout);
// Render all lines
FontCharacterEntry entry;
//FontCharacterEntry entry;
Render2DDrawCall drawCall;
if (customMaterial)
{
@@ -1299,6 +1299,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
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
@@ -1308,7 +1309,8 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
if (currentChar != '\n')
{
// Get character entry
font->GetCharacter(currentChar, entry, enableFallbackFonts);
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
const FontCharacterEntry& entry = line.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)
@@ -1365,6 +1367,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
// Move
pointer.X += entry.AdvanceX * scale;
}
entryIndex++;
}
}
}