From da79517b36d8aa3cb730d1a6423757e1a26b4675 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Wed, 30 Apr 2014 14:10:59 +0200 Subject: [PATCH] Fix bitmap font resize failing silently when resizing to an unsupported pixel size (#448), fixed getKerning returning scaled values even for bitmap fonts. --- include/SFML/Graphics/Font.hpp | 10 ++++++++++ include/SFML/Graphics/Text.hpp | 14 ++++++++++++++ src/SFML/Graphics/Font.cpp | 25 +++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 245f24a2..7766fa71 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -152,6 +152,10 @@ public : //////////////////////////////////////////////////////////// /// \brief Retrieve a glyph of the font /// + /// If the font is a bitmap font, not all character sizes + /// might be available. If the glyph is not available at the + /// requested size, an empty glyph is returned. + /// /// \param codePoint Unicode code point of the character to get /// \param characterSize Reference character size /// \param bold Retrieve the bold version or the regular one? @@ -379,6 +383,12 @@ private : /// with this class. However, it may be useful to access the /// font metrics or rasterized glyphs for advanced usage. /// +/// Note that if the font is a bitmap font, it is not scalable, +/// thus not all requested sizes will be available to use. This +/// needs to be taken into consideration when using sf::Text. +/// If you need to display text of a certain size, make sure the +/// corresponding bitmap font that supports that size is used. +/// /// \see sf::Text /// //////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index c9a188ed..302d9d21 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -72,6 +72,13 @@ public : //////////////////////////////////////////////////////////// /// \brief Construct the text from a string, font and size /// + /// Note that if the used font is a bitmap font, it is not + /// scalable, thus not all requested sizes will be available + /// to use. This needs to be taken into consideration when + /// setting the character size. If you need to display text + /// of a certain size, make sure the corresponding bitmap + /// font that supports that size is used. + /// /// \param string Text assigned to the string /// \param font Font used to draw the string /// \param characterSize Base size of characters, in pixels @@ -122,6 +129,13 @@ public : /// /// The default size is 30. /// + /// Note that if the used font is a bitmap font, it is not + /// scalable, thus not all requested sizes will be available + /// to use. This needs to be taken into consideration when + /// setting the character size. If you need to display text + /// of a certain size, make sure the corresponding bitmap + /// font that supports that size is used. + /// /// \param size New character size, in pixels /// /// \see getCharacterSize diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index ac0b447a..25559e17 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -332,6 +332,10 @@ int Font::getKerning(Uint32 first, Uint32 second, unsigned int characterSize) co FT_Vector kerning; FT_Get_Kerning(face, index1, index2, FT_KERNING_DEFAULT, &kerning); + // X advance is already in pixels for bitmap fonts + if (!FT_IS_SCALABLE(face)) + return kerning.x; + // Return the X advance return kerning.x >> 6; } @@ -476,7 +480,8 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c int ascender = face->size->metrics.ascender >> 6; // Offset to make up for empty space between ascender and virtual top of the typeface - int offset = characterSize - ascender; + // Only applied to scalable fonts i.e. not to bitmap fonts + int offset = FT_IS_SCALABLE(face) ? (characterSize - ascender) : 0; if ((width > 0) && (height > 0)) { @@ -627,7 +632,23 @@ bool Font::setCurrentSize(unsigned int characterSize) const if (currentSize != characterSize) { - return FT_Set_Pixel_Sizes(face, 0, characterSize) == 0; + FT_Error result = FT_Set_Pixel_Sizes(face, 0, characterSize); + + if (result == FT_Err_Invalid_Pixel_Size) + { + // In the case of bitmap fonts, resizing can + // fail if the requested size is not available + if (!FT_IS_SCALABLE(face)) + { + err() << "Failed to set bitmap font size to " << characterSize << std::endl; + err() << "Available sizes are: "; + for (int i = 0; i < face->num_fixed_sizes; ++i) + err() << face->available_sizes[i].height << " "; + err() << std::endl; + } + } + + return result == FT_Err_Ok; } else {