From 95448f8c115d342b55e2a035a88586a47d627df7 Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Thu, 30 May 2024 23:09:31 +0200 Subject: [PATCH] Added getAscent and getDescent functions to Font --- include/SFML/Graphics/Font.hpp | 24 +++++++++++++++++++ src/SFML/Graphics/Font.cpp | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 7de8d47f7..db2fb9c24 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -206,6 +206,30 @@ public: //////////////////////////////////////////////////////////// float getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold = false) const; + //////////////////////////////////////////////////////////// + /// \brief Get the font's ascent + /// + /// The ascent is the distance between the top of the font and the baseline. + /// + /// \param characterSize Reference character size + /// + /// \return Font's ascent, in pixels + /// + //////////////////////////////////////////////////////////// + float getAscent(unsigned int characterSize) const; + + //////////////////////////////////////////////////////////// + /// \brief Get the font's descent + /// + /// The descent is the distance between the baseline and the bottom of the font. + /// + /// \param characterSize Reference character size + /// + /// \return Font's descent, in pixels + /// + //////////////////////////////////////////////////////////// + float getDescent(unsigned int characterSize) const; + //////////////////////////////////////////////////////////// /// \brief Get the line spacing /// diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index c052dc1e9..db485e678 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -375,6 +375,48 @@ float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int c } +//////////////////////////////////////////////////////////// +float Font::getAscent(unsigned int characterSize) const +{ + assert(m_fontHandles); + + FT_Face face = m_fontHandles->face; + + if (setCurrentSize(characterSize)) + { + if (!FT_IS_SCALABLE(face)) + return static_cast(face->size->metrics.ascender) / static_cast(1 << 6); + + return static_cast(FT_MulFix(face->ascender, face->size->metrics.y_scale)) / static_cast(1 << 6); + } + else + { + return 0.f; + } +} + + +//////////////////////////////////////////////////////////// +float Font::getDescent(unsigned int characterSize) const +{ + assert(m_fontHandles); + + FT_Face face = m_fontHandles->face; + + if (setCurrentSize(characterSize)) + { + if (!FT_IS_SCALABLE(face)) + return static_cast(-face->size->metrics.descender) / static_cast(1 << 6); + + return static_cast(FT_MulFix(-face->descender, face->size->metrics.y_scale)) / static_cast(1 << 6); + } + else + { + return 0.f; + } +} + + //////////////////////////////////////////////////////////// float Font::getLineSpacing(unsigned int characterSize) const {