From ca2672e215644ad4968ab1750c9858cd0e05af72 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 c31ec611ac..d822a795f4 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -144,6 +144,21 @@ class SFML_GRAPHICS_API Text : public Drawable, public Transformable //////////////////////////////////////////////////////////// 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 /// @@ -213,6 +228,16 @@ class SFML_GRAPHICS_API Text : public Drawable, public Transformable //////////////////////////////////////////////////////////// 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 /// @@ -304,6 +329,7 @@ class SFML_GRAPHICS_API Text : public Drawable, public Transformable 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_color; ///< Text color mutable VertexArray m_vertices; ///< Vertex array containing the text's geometry diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 0635a0ee81..dddb323f15 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -38,6 +38,7 @@ Text::Text() : m_string (), m_font (NULL), m_characterSize (30), +m_letterSpacing (0.f), m_style (Regular), m_color (255, 255, 255), m_vertices (Triangles), @@ -53,6 +54,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_color (255, 255, 255), m_vertices (Triangles), @@ -96,6 +98,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) { @@ -146,6 +159,13 @@ unsigned int Text::getCharacterSize() const } +//////////////////////////////////////////////////////////// +float Text::getLetterSpacing() const +{ + return m_letterSpacing; +} + + //////////////////////////////////////////////////////////// Uint32 Text::getStyle() const { @@ -173,7 +193,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 @@ -196,7 +216,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 @@ -273,7 +293,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); @@ -370,7 +390,7 @@ void Text::ensureGeometryUpdate() const maxY = std::max(maxY, y + bottom); // Advance to the next character - x += glyph.advance; + x += glyph.advance + m_letterSpacing; } // If we're using the underlined style, add the last line