diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index eceabd50b..344d36c1a 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -335,7 +335,7 @@ private: //////////////////////////////////////////////////////////// struct Page { - Page(); + explicit Page(bool smooth); GlyphTable glyphs; //!< Table mapping code points to their corresponding glyph Texture texture; //!< Texture containing the pixels of the glyphs @@ -349,6 +349,16 @@ private: //////////////////////////////////////////////////////////// void cleanup(); + //////////////////////////////////////////////////////////// + /// \brief Find or create the glyphs page corresponding to the given character size + /// + /// \param characterSize Reference character size + /// + /// \return The glyphs page corresponding to \a characterSize + /// + //////////////////////////////////////////////////////////// + Page& loadPage(unsigned int characterSize) const; + //////////////////////////////////////////////////////////// /// \brief Load a new glyph and store it in the cache /// diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index ad6412f0f..1dee95015 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -348,7 +348,7 @@ const Font::Info& Font::getInfo() const const Glyph& Font::getGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, float outlineThickness) const { // Get the page corresponding to the character size - GlyphTable& glyphs = m_pages[characterSize].glyphs; + GlyphTable& glyphs = loadPage(characterSize).glyphs; // Build the key by combining the glyph index (based on code point), bold flag, and outline thickness Uint64 key = combine(outlineThickness, bold, FT_Get_Char_Index(static_cast(m_face), codePoint)); @@ -476,7 +476,7 @@ float Font::getUnderlineThickness(unsigned int characterSize) const //////////////////////////////////////////////////////////// const Texture& Font::getTexture(unsigned int characterSize) const { - return m_pages[characterSize].texture; + return loadPage(characterSize).texture; } //////////////////////////////////////////////////////////// @@ -567,6 +567,18 @@ void Font::cleanup() } +//////////////////////////////////////////////////////////// +Font::Page& Font::loadPage(unsigned int characterSize) const +{ + // TODO: Remove this method and use try_emplace instead when updating to C++17 + PageTable::iterator pageIterator = m_pages.find(characterSize); + if (pageIterator == m_pages.end()) + pageIterator = m_pages.insert(std::make_pair(characterSize, Page(m_isSmooth))).first; + + return pageIterator->second; +} + + //////////////////////////////////////////////////////////// Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, float outlineThickness) const { @@ -652,7 +664,7 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f height += 2 * padding; // Get the glyphs page corresponding to the character size - Page& page = m_pages[characterSize]; + Page& page = loadPage(characterSize); // Find a good position for the new glyph into the texture glyph.textureRect = findGlyphRect(page, width, height); @@ -842,7 +854,7 @@ bool Font::setCurrentSize(unsigned int characterSize) const //////////////////////////////////////////////////////////// -Font::Page::Page() : +Font::Page::Page(bool smooth) : nextRow(3) { // Make sure that the texture is initialized by default @@ -856,7 +868,7 @@ nextRow(3) // Create the texture texture.loadFromImage(image); - texture.setSmooth(true); + texture.setSmooth(smooth); } } // namespace sf