From 9e2f2eb27faeb412a75b59d19c781b5dd6e96664 Mon Sep 17 00:00:00 2001 From: Maximilian Wagenbach Date: Tue, 21 Jul 2015 19:00:01 +0200 Subject: [PATCH] Implemented letter spacing in sf::Text. --- include/SFML/Graphics/Text.hpp | 26 ++++++++++++++++++++++++++ src/SFML/Graphics/Text.cpp | 28 ++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index b135ceb8..d109ae58 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -144,6 +144,21 @@ public: //////////////////////////////////////////////////////////// void setCharacterSize(unsigned int size); + //////////////////////////////////////////////////////////// + /// \brief Set the additional letter spacing offset + /// + /// The spacing between letters is defined by the font. + /// This method enables you to set an additional spacing + /// between letters. By default the additional letter + /// spacing offset is 0. + /// + /// \param spacing New additional letter spacing offset, in pixel + /// + /// \see getLetterSpacing + /// + //////////////////////////////////////////////////////////// + void setLetterSpacing(float spacing); + //////////////////////////////////////////////////////////// /// \brief Set the text's style /// @@ -260,6 +275,16 @@ public: //////////////////////////////////////////////////////////// unsigned int getCharacterSize() const; + //////////////////////////////////////////////////////////// + /// \brief Get the size of the additional letter spacing offset + /// + /// \return Size of the additional letter spacing offset, in pixel + /// + /// \see setLetterSpacing + /// + //////////////////////////////////////////////////////////// + float getLetterSpacing() const; + //////////////////////////////////////////////////////////// /// \brief Get the text's style /// @@ -385,6 +410,7 @@ private: String m_string; ///< String to display const Font* m_font; ///< Font used to display the string unsigned int m_characterSize; ///< Base size of characters, in pixels + float m_letterSpacing; ///< Additional spacing offset between letters, in pixel Uint32 m_style; ///< Text style (see Style enum) Color m_fillColor; ///< Text fill color Color m_outlineColor; ///< Text outline color diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 6b6344c8..24808ed0 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -77,6 +77,7 @@ Text::Text() : m_string (), m_font (NULL), m_characterSize (30), +m_letterSpacing (0.f), m_style (Regular), m_fillColor (255, 255, 255), m_outlineColor (0, 0, 0), @@ -96,6 +97,7 @@ Text::Text(const String& string, const Font& font, unsigned int characterSize) : m_string (string), m_font (&font), m_characterSize (characterSize), +m_letterSpacing (0.f), m_style (Regular), m_fillColor (255, 255, 255), m_outlineColor (0, 0, 0), @@ -143,6 +145,17 @@ void Text::setCharacterSize(unsigned int size) } +//////////////////////////////////////////////////////////// +void Text::setLetterSpacing(float spacing) +{ + if (m_letterSpacing != spacing) + { + m_letterSpacing = spacing; + m_geometryNeedUpdate = true; + } +} + + //////////////////////////////////////////////////////////// void Text::setStyle(Uint32 style) { @@ -229,6 +242,13 @@ unsigned int Text::getCharacterSize() const } +//////////////////////////////////////////////////////////// +float Text::getLetterSpacing() const +{ + return m_letterSpacing; +} + + //////////////////////////////////////////////////////////// Uint32 Text::getStyle() const { @@ -277,7 +297,7 @@ Vector2f Text::findCharacterPos(std::size_t index) const // Precompute the variables needed by the algorithm bool bold = (m_style & Bold) != 0; - float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance); + float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing; float vspace = static_cast(m_font->getLineSpacing(m_characterSize)); // Compute the position @@ -300,7 +320,7 @@ Vector2f Text::findCharacterPos(std::size_t index) const } // For regular characters, add the advance offset of the glyph - position.x += static_cast(m_font->getGlyph(curChar, m_characterSize, bold).advance); + position.x += static_cast(m_font->getGlyph(curChar, m_characterSize, bold).advance) + m_letterSpacing; } // Transform the position to global coordinates @@ -385,7 +405,7 @@ void Text::ensureGeometryUpdate() const float strikeThroughOffset = xBounds.top + xBounds.height / 2.f; // Precompute the variables needed by the algorithm - float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance); + float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing; float vspace = static_cast(m_font->getLineSpacing(m_characterSize)); float x = 0.f; float y = static_cast(m_characterSize); @@ -486,7 +506,7 @@ void Text::ensureGeometryUpdate() const } // Advance to the next character - x += glyph.advance; + x += glyph.advance + m_letterSpacing; } // If we're using the underlined style, add the last line