From bdcdfffe11acab20a77dafd4ba0b3b644083de1e Mon Sep 17 00:00:00 2001 From: binary1248 Date: Thu, 1 May 2014 02:32:56 +0200 Subject: [PATCH] Replaced Text underline offset/thickness with nicer font dependent values. --- include/SFML/Graphics/Font.hpp | 29 ++++++++++++++++++++++++ src/SFML/Graphics/Font.cpp | 40 ++++++++++++++++++++++++++++++++++ src/SFML/Graphics/Text.cpp | 4 ++-- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 7766fa71..f40e4a6d 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -196,6 +196,35 @@ public : //////////////////////////////////////////////////////////// int getLineSpacing(unsigned int characterSize) const; + //////////////////////////////////////////////////////////// + /// \brief Get the position of the underline + /// + /// Underline position is the vertical offset to apply between the + /// baseline and the underline. + /// + /// \param characterSize Reference character size + /// + /// \return Underline position, in pixels + /// + /// \see getUnderlineThickness + /// + //////////////////////////////////////////////////////////// + int getUnderlinePosition(unsigned int characterSize) const; + + //////////////////////////////////////////////////////////// + /// \brief Get the thickness of the underline + /// + /// Underline thickness is the vertical size of the underline. + /// + /// \param characterSize Reference character size + /// + /// \return Underline thickness, in pixels + /// + /// \see getUnderlinePosition + /// + //////////////////////////////////////////////////////////// + int getUnderlineThickness(unsigned int characterSize) const; + //////////////////////////////////////////////////////////// /// \brief Retrieve the texture containing the loaded glyphs of a certain size /// diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 25559e17..4b49e7be 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -363,6 +363,46 @@ int Font::getLineSpacing(unsigned int characterSize) const } +//////////////////////////////////////////////////////////// +int Font::getUnderlinePosition(unsigned int characterSize) const +{ + FT_Face face = static_cast(m_face); + + if (face && setCurrentSize(characterSize)) + { + // Return a fixed position if font is a bitmap font + if (!FT_IS_SCALABLE(face)) + return characterSize / 10; + + return (FT_MulFix(face->underline_position, face->size->metrics.y_scale) >> 6); + } + else + { + return 0; + } +} + + +//////////////////////////////////////////////////////////// +int Font::getUnderlineThickness(unsigned int characterSize) const +{ + FT_Face face = static_cast(m_face); + + if (face && setCurrentSize(characterSize)) + { + // Return a fixed thickness if font is a bitmap font + if (!FT_IS_SCALABLE(face)) + return characterSize / 14; + + return (FT_MulFix(face->underline_thickness, face->size->metrics.y_scale) >> 6); + } + else + { + return 0; + } +} + + //////////////////////////////////////////////////////////// const Texture& Font::getTexture(unsigned int characterSize) const { diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index f524442b..90208cf6 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -262,8 +262,8 @@ void Text::ensureGeometryUpdate() const bool bold = (m_style & Bold) != 0; bool underlined = (m_style & Underlined) != 0; float italic = (m_style & Italic) ? 0.208f : 0.f; // 12 degrees - float underlineOffset = m_characterSize * 0.1f; - float underlineThickness = m_characterSize * (bold ? 0.1f : 0.07f); + float underlineOffset = static_cast(m_font->getUnderlinePosition(m_characterSize)); + float underlineThickness = static_cast(m_font->getUnderlineThickness(m_characterSize)); // Precompute the variables needed by the algorithm float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance);