Added getAscent and getDescent functions to Font

This commit is contained in:
Bruno Van de Velde 2024-05-30 23:09:31 +02:00
parent 7987d3cedc
commit 95448f8c11
2 changed files with 66 additions and 0 deletions

View File

@ -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
///

View File

@ -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<float>(face->size->metrics.ascender) / static_cast<float>(1 << 6);
return static_cast<float>(FT_MulFix(face->ascender, face->size->metrics.y_scale)) / static_cast<float>(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<float>(-face->size->metrics.descender) / static_cast<float>(1 << 6);
return static_cast<float>(FT_MulFix(-face->descender, face->size->metrics.y_scale)) / static_cast<float>(1 << 6);
}
else
{
return 0.f;
}
}
////////////////////////////////////////////////////////////
float Font::getLineSpacing(unsigned int characterSize) const
{