_font kerning cache
This commit is contained in:
@@ -119,7 +119,7 @@ void Font::Invalidate()
|
|||||||
_characters.Clear();
|
_characters.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries)
|
void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines, const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<float>& characterKernings)
|
||||||
{
|
{
|
||||||
int32 textLength = text.Length();
|
int32 textLength = text.Length();
|
||||||
if (textLength == 0)
|
if (textLength == 0)
|
||||||
@@ -235,6 +235,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
characterEntries.Add(entry);
|
characterEntries.Add(entry);
|
||||||
|
characterKernings.Add((float)kerning * scale);
|
||||||
|
|
||||||
// Check if move to another line
|
// Check if move to another line
|
||||||
if (moveLine)
|
if (moveLine)
|
||||||
@@ -314,7 +315,8 @@ 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;
|
||||||
ProcessText(text, lines, layout, characterEntries);
|
Array<float> xAdvances;
|
||||||
|
ProcessText(text, lines, layout, characterEntries, xAdvances);
|
||||||
|
|
||||||
// Calculate bounds
|
// Calculate bounds
|
||||||
Float2 max = Float2::Zero;
|
Float2 max = Float2::Zero;
|
||||||
@@ -336,7 +338,8 @@ 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;
|
||||||
ProcessText(text, lines, layout, characterEntries);
|
Array<float> 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;
|
||||||
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
|
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
|
||||||
@@ -411,7 +414,8 @@ 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;
|
||||||
ProcessText(text, lines, layout, characterEntries);
|
Array<float> 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;
|
||||||
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
|
float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale;
|
||||||
|
|||||||
@@ -344,7 +344,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);
|
void ProcessText(const StringView& text, Array<FontLineCache>& outputLines, API_PARAM(Ref) const TextLayoutOptions& layout, Array<FontCharacterEntry>& characterEntries, Array<float>& characterKernings);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Processes text to get cached lines for rendering.
|
/// Processes text to get cached lines for rendering.
|
||||||
@@ -356,7 +356,8 @@ public:
|
|||||||
{
|
{
|
||||||
Array<FontLineCache> lines;
|
Array<FontLineCache> lines;
|
||||||
Array<FontCharacterEntry> characterEntries;
|
Array<FontCharacterEntry> characterEntries;
|
||||||
ProcessText(text, lines, layout, characterEntries);
|
Array<float> xAdvances;
|
||||||
|
ProcessText(text, lines, layout, characterEntries, xAdvances);
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +372,8 @@ public:
|
|||||||
{
|
{
|
||||||
Array<FontLineCache> lines;
|
Array<FontLineCache> lines;
|
||||||
Array<FontCharacterEntry> characterEntries;
|
Array<FontCharacterEntry> characterEntries;
|
||||||
ProcessText(textRange.Substring(text), lines, layout, characterEntries);
|
Array<float> xAdvances;
|
||||||
|
ProcessText(textRange.Substring(text), lines, layout, characterEntries, xAdvances);
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1272,15 +1272,19 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
|
|||||||
FontTextureAtlas* fontAtlas = nullptr;
|
FontTextureAtlas* fontAtlas = nullptr;
|
||||||
Float2 invAtlasSize = Float2::One;
|
Float2 invAtlasSize = Float2::One;
|
||||||
FontCharacterEntry previous;
|
FontCharacterEntry previous;
|
||||||
int32 kerning;
|
//int32 kerning;
|
||||||
float scale = layout.Scale / FontManager::FontScale;
|
float scale = layout.Scale / FontManager::FontScale;
|
||||||
const bool enableFallbackFonts = EnumHasAllFlags(Features, RenderingFeatures::FallbackFonts);
|
const bool enableFallbackFonts = EnumHasAllFlags(Features, RenderingFeatures::FallbackFonts);
|
||||||
|
float fontHeightOffset = Math::Ceil((font->GetHeight() + font->GetDescender()) * scale);
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
characterEntries.Clear();
|
characterEntries.Clear();
|
||||||
font->ProcessText(text, Lines, layout, characterEntries);
|
characterKernings.Clear();
|
||||||
|
font->ProcessText(text, Lines, layout, characterEntries, characterKernings);
|
||||||
|
|
||||||
// Render all lines
|
// Render all lines
|
||||||
//FontCharacterEntry entry;
|
//FontCharacterEntry entry;
|
||||||
@@ -1313,6 +1317,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
|
|||||||
// Get character entry
|
// Get character entry
|
||||||
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
|
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
|
||||||
const FontCharacterEntry& entry = characterEntries[entryIndex];
|
const FontCharacterEntry& entry = characterEntries[entryIndex];
|
||||||
|
float kerning = characterKernings[entryIndex];
|
||||||
|
|
||||||
// Check if need to select/change font atlas (since characters even in the same font may be located in different atlases)
|
// 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)
|
if (fontAtlas == nullptr || entry.TextureIndex != fontAtlasIndex)
|
||||||
@@ -1335,7 +1340,7 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
|
|||||||
|
|
||||||
// Get kerning
|
// Get kerning
|
||||||
const bool isWhitespace = StringUtils::IsWhitespace(currentChar);
|
const bool isWhitespace = StringUtils::IsWhitespace(currentChar);
|
||||||
if (!isWhitespace && previous.IsValid)
|
/*if (!isWhitespace && previous.IsValid)
|
||||||
{
|
{
|
||||||
kerning = entry.Font->GetKerning(previous.Character, entry.Character);
|
kerning = entry.Font->GetKerning(previous.Character, entry.Character);
|
||||||
}
|
}
|
||||||
@@ -1343,15 +1348,18 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
|
|||||||
{
|
{
|
||||||
kerning = 0;
|
kerning = 0;
|
||||||
}
|
}
|
||||||
pointer.X += (float)kerning * scale;
|
pointer.X += (float)kerning * scale;*/
|
||||||
|
|
||||||
|
pointer.X += kerning;
|
||||||
previous = entry;
|
previous = entry;
|
||||||
|
|
||||||
// Omit whitespace characters
|
// Omit whitespace characters
|
||||||
if (!isWhitespace)
|
if (!isWhitespace)
|
||||||
|
//if (xAdvance != 0.0f)
|
||||||
{
|
{
|
||||||
// Calculate character size and atlas coordinates
|
// Calculate character size and atlas coordinates
|
||||||
const float x = pointer.X + entry.OffsetX * scale;
|
const float x = pointer.X + entry.OffsetX * scale;
|
||||||
const float y = pointer.Y - entry.OffsetY * scale + Math::Ceil((font->GetHeight() + font->GetDescender()) * scale);
|
const float y = pointer.Y - entry.OffsetY * scale + fontHeightOffset;
|
||||||
|
|
||||||
Rectangle charRect(x, y, entry.UVSize.X * scale, entry.UVSize.Y * scale);
|
Rectangle charRect(x, y, entry.UVSize.X * scale, entry.UVSize.Y * scale);
|
||||||
charRect.Offset(layout.Bounds.Location);
|
charRect.Offset(layout.Bounds.Location);
|
||||||
|
|||||||
@@ -159,7 +159,8 @@ void TextRender::UpdateLayout()
|
|||||||
// Perform layout
|
// Perform layout
|
||||||
Array<FontLineCache> lines;
|
Array<FontLineCache> lines;
|
||||||
Array<FontCharacterEntry> characterEntries;
|
Array<FontCharacterEntry> characterEntries;
|
||||||
font->ProcessText(text, lines, _layoutOptions, characterEntries);
|
Array<float> xAdvances;
|
||||||
|
font->ProcessText(text, lines, _layoutOptions, characterEntries, xAdvances);
|
||||||
|
|
||||||
// Prepare buffers capacity
|
// Prepare buffers capacity
|
||||||
_ib.Data.EnsureCapacity(text.Length() * 6 * sizeof(uint16));
|
_ib.Data.EnsureCapacity(text.Length() * 6 * sizeof(uint16));
|
||||||
|
|||||||
Reference in New Issue
Block a user