Implement smoothing for fonts

This commit is contained in:
Jonny Paton 2020-09-19 21:48:14 +01:00 committed by Lukas Dürrenberger
parent a96057866d
commit 59df9d0d88
2 changed files with 52 additions and 2 deletions

View File

@ -275,6 +275,32 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const Texture& getTexture(unsigned int characterSize) const; const Texture& getTexture(unsigned int characterSize) const;
////////////////////////////////////////////////////////////
/// \brief Enable or disable the smooth filter
///
/// When the filter is activated, the font appears smoother
/// so that pixels are less noticeable. However if you want
/// the font to look exactly the same as its source file,
/// you should disable it.
/// The smooth filter is enabled by default.
///
/// \param smooth True to enable smoothing, false to disable it
///
/// \see isSmooth
///
////////////////////////////////////////////////////////////
void setSmooth(bool smooth);
////////////////////////////////////////////////////////////
/// \brief Tell whether the smooth filter is enabled or not
///
/// \return True if smoothing is enabled, false if it is disabled
///
/// \see setSmooth
///
////////////////////////////////////////////////////////////
bool isSmooth() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator /// \brief Overload of assignment operator
/// ///
@ -373,6 +399,7 @@ private:
void* m_streamRec; //!< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details) void* m_streamRec; //!< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details)
void* m_stroker; //!< Pointer to the stroker (it is typeless to avoid exposing implementation details) void* m_stroker; //!< Pointer to the stroker (it is typeless to avoid exposing implementation details)
int* m_refCount; //!< Reference counter used by implicit sharing int* m_refCount; //!< Reference counter used by implicit sharing
bool m_isSmooth; //!< Status of the smooth filter
Info m_info; //!< Information about the font Info m_info; //!< Information about the font
mutable PageTable m_pages; //!< Table containing the glyphs pages by character size mutable PageTable m_pages; //!< Table containing the glyphs pages by character size
mutable std::vector<Uint8> m_pixelBuffer; //!< Pixel buffer holding a glyph's pixels before being written to the texture mutable std::vector<Uint8> m_pixelBuffer; //!< Pixel buffer holding a glyph's pixels before being written to the texture

View File

@ -88,6 +88,7 @@ m_face (NULL),
m_streamRec(NULL), m_streamRec(NULL),
m_stroker (NULL), m_stroker (NULL),
m_refCount (NULL), m_refCount (NULL),
m_isSmooth (true),
m_info () m_info ()
{ {
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
@ -105,7 +106,8 @@ m_stroker (copy.m_stroker),
m_refCount (copy.m_refCount), m_refCount (copy.m_refCount),
m_info (copy.m_info), m_info (copy.m_info),
m_pages (copy.m_pages), m_pages (copy.m_pages),
m_pixelBuffer(copy.m_pixelBuffer) m_pixelBuffer(copy.m_pixelBuffer),
m_isSmooth (copy.m_isSmooth)
{ {
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
m_stream = NULL; m_stream = NULL;
@ -468,6 +470,26 @@ const Texture& Font::getTexture(unsigned int characterSize) const
return m_pages[characterSize].texture; return m_pages[characterSize].texture;
} }
////////////////////////////////////////////////////////////
void Font::setSmooth(bool smooth)
{
if (smooth != m_isSmooth)
{
m_isSmooth = smooth;
for (sf::Font::PageTable::iterator page = m_pages.begin(); page != m_pages.end(); ++page)
{
page->second.texture.setSmooth(m_isSmooth);
}
}
}
////////////////////////////////////////////////////////////
bool Font::isSmooth() const
{
return m_isSmooth;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Font& Font::operator =(const Font& right) Font& Font::operator =(const Font& right)
@ -482,6 +504,7 @@ Font& Font::operator =(const Font& right)
std::swap(m_info, temp.m_info); std::swap(m_info, temp.m_info);
std::swap(m_pages, temp.m_pages); std::swap(m_pages, temp.m_pages);
std::swap(m_pixelBuffer, temp.m_pixelBuffer); std::swap(m_pixelBuffer, temp.m_pixelBuffer);
std::swap(m_isSmooth, temp.m_isSmooth);
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
std::swap(m_stream, temp.m_stream); std::swap(m_stream, temp.m_stream);
@ -734,7 +757,7 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height)
// Make the texture 2 times bigger // Make the texture 2 times bigger
Texture newTexture; Texture newTexture;
newTexture.create(textureWidth * 2, textureHeight * 2); newTexture.create(textureWidth * 2, textureHeight * 2);
newTexture.setSmooth(true); newTexture.setSmooth(m_isSmooth);
newTexture.update(page.texture); newTexture.update(page.texture);
page.texture.swap(newTexture); page.texture.swap(newTexture);
} }