From 09c634dd555c9ed102f2f583813c4b943235be47 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 23 Nov 2013 21:34:54 +0100 Subject: GRAPHICS: Allow TTFFont to cache glyphs when required. This should allow TTFFont to display UTF-32 characters from fonts. --- graphics/fonts/ttf.cpp | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'graphics/fonts/ttf.cpp') diff --git a/graphics/fonts/ttf.cpp b/graphics/fonts/ttf.cpp index 7c48bdf99b..be3cd94efa 100644 --- a/graphics/fonts/ttf.cpp +++ b/graphics/fonts/ttf.cpp @@ -129,9 +129,11 @@ private: FT_UInt slot; }; - bool cacheGlyph(Glyph &glyph, uint chr); + bool cacheGlyph(Glyph &glyph, uint32 chr) const; typedef Common::HashMap GlyphCache; - GlyphCache _glyphs; + mutable GlyphCache _glyphs; + bool _allowLateCaching; + void assureCached(uint32 chr) const; bool _monochrome; bool _hasKerning; @@ -211,6 +213,9 @@ bool TTFFont::load(Common::SeekableReadStream &stream, int size, uint dpi, bool _height = _ascent - _descent + 1; if (!mapping) { + // Allow loading of all unicode characters. + _allowLateCaching = true; + // Load all ISO-8859-1 characters. for (uint i = 0; i < 256; ++i) { if (!cacheGlyph(_glyphs[i], i)) { @@ -218,6 +223,9 @@ bool TTFFont::load(Common::SeekableReadStream &stream, int size, uint dpi, bool } } } else { + // We have a fixed map of characters do not load more later. + _allowLateCaching = false; + for (uint i = 0; i < 256; ++i) { const uint32 unicode = mapping[i] & 0x7FFFFFFF; const bool isRequired = (mapping[i] & 0x80000000) != 0; @@ -244,6 +252,7 @@ int TTFFont::getMaxCharWidth() const { } int TTFFont::getCharWidth(uint32 chr) const { + assureCached(chr); GlyphCache::const_iterator glyphEntry = _glyphs.find(chr); if (glyphEntry == _glyphs.end()) return 0; @@ -255,6 +264,9 @@ int TTFFont::getKerningOffset(uint32 left, uint32 right) const { if (!_hasKerning) return 0; + assureCached(left); + assureCached(right); + FT_UInt leftGlyph, rightGlyph; GlyphCache::const_iterator glyphEntry; @@ -319,6 +331,7 @@ void renderGlyph(uint8 *dstPos, const int dstPitch, const uint8 *srcPos, const i } // End of anonymous namespace void TTFFont::drawChar(Surface *dst, uint32 chr, int x, int y, uint32 color) const { + assureCached(chr); GlyphCache::const_iterator glyphEntry = _glyphs.find(chr); if (glyphEntry == _glyphs.end()) return; @@ -390,7 +403,7 @@ void TTFFont::drawChar(Surface *dst, uint32 chr, int x, int y, uint32 color) con } } -bool TTFFont::cacheGlyph(Glyph &glyph, uint chr) { +bool TTFFont::cacheGlyph(Glyph &glyph, uint32 chr) const { FT_UInt slot = FT_Get_Char_Index(_face, chr); if (!slot) return false; @@ -479,6 +492,17 @@ bool TTFFont::cacheGlyph(Glyph &glyph, uint chr) { return true; } +void TTFFont::assureCached(uint32 chr) const { + if (!chr || !_allowLateCaching || _glyphs.contains(chr)) { + return; + } + + Glyph newGlyph; + if (cacheGlyph(newGlyph, chr)) { + _glyphs[chr] = newGlyph; + } +} + Font *loadTTFFont(Common::SeekableReadStream &stream, int size, uint dpi, bool monochrome, const uint32 *mapping) { TTFFont *font = new TTFFont(); -- cgit v1.2.3