Squash duplicated sf::Font glyphs to single chars

Before this change, `sf::Font` always rendered/provided one character
per Unicode codepoint, even if that character wasn't represented by the
current font file or duplicated. This caused more texture space to be
used than necessary, which is especially apparent, when trying to render
a large amount of unhandled glyphs (the texture would literally fill up
with empty squares representing missing characters).
This commit is contained in:
Mario Liebisch 2018-07-29 09:25:48 +02:00
parent bcb013b45b
commit dc0dfd601a
No known key found for this signature in database
GPG Key ID: 32B56AC1F87EFBF9

View File

@ -71,10 +71,10 @@ namespace
return output; return output;
} }
// Combine outline thickness, boldness and codepoint into a single 64-bit key // Combine outline thickness, boldness and font glyph index into a single 64-bit key
sf::Uint64 combine(float outlineThickness, bool bold, sf::Uint32 codePoint) sf::Uint64 combine(float outlineThickness, bool bold, sf::Uint32 index)
{ {
return (static_cast<sf::Uint64>(reinterpret<sf::Uint32>(outlineThickness)) << 32) | (static_cast<sf::Uint64>(bold) << 31) | codePoint; return (static_cast<sf::Uint64>(reinterpret<sf::Uint32>(outlineThickness)) << 32) | (static_cast<sf::Uint64>(bold) << 31) | index;
} }
} }
@ -346,8 +346,8 @@ const Glyph& Font::getGlyph(Uint32 codePoint, unsigned int characterSize, bool b
// Get the page corresponding to the character size // Get the page corresponding to the character size
GlyphTable& glyphs = m_pages[characterSize].glyphs; GlyphTable& glyphs = m_pages[characterSize].glyphs;
// Build the key by combining the code point, bold flag, and outline thickness // Build the key by combining the glyph index (based on code point), bold flag, and outline thickness
Uint64 key = combine(outlineThickness, bold, codePoint); Uint64 key = combine(outlineThickness, bold, FT_Get_Char_Index(static_cast<FT_Face>(m_face), codePoint));
// Search the glyph into the cache // Search the glyph into the cache
GlyphTable::const_iterator it = glyphs.find(key); GlyphTable::const_iterator it = glyphs.find(key);