From 812dea70d058df7ce50f3b059cab27f11cb9f2b6 Mon Sep 17 00:00:00 2001 From: Maximilian Wagenbach Date: Thu, 23 Jul 2015 12:17:23 +0200 Subject: [PATCH] Implemented line spacing in sf::Text. --- include/SFML/Graphics/Text.hpp | 26 +++++++++++++++++++ src/SFML/Graphics/Text.cpp | 46 ++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index d109ae58..225f9151 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 line spacing offset + /// + /// The spacing between lines is defined by the font. + /// This method enables you to set an additional spacing + /// between lines. By default the additional line + /// spacing offset is 0. + /// + /// \param spacing New additional line spacing offset, in pixel + /// + /// \see getLineSpacing + /// + //////////////////////////////////////////////////////////// + void setLineSpacing(float spacing); + //////////////////////////////////////////////////////////// /// \brief Set the additional letter spacing offset /// @@ -285,6 +300,16 @@ public: //////////////////////////////////////////////////////////// float getLetterSpacing() const; + //////////////////////////////////////////////////////////// + /// \brief Get the size of the additional line spacing offset + /// + /// \return Size of the additional line spacing offset, in pixel + /// + /// \see setLineSpacing + /// + //////////////////////////////////////////////////////////// + float getLineSpacing() const; + //////////////////////////////////////////////////////////// /// \brief Get the text's style /// @@ -411,6 +436,7 @@ private: 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 + float m_lineSpacing; ///< Additional spacing offset between lines, 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 24808ed0..3c0abfb5 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -78,6 +78,7 @@ m_string (), m_font (NULL), m_characterSize (30), m_letterSpacing (0.f), +m_lineSpacing (0.f), m_style (Regular), m_fillColor (255, 255, 255), m_outlineColor (0, 0, 0), @@ -98,6 +99,7 @@ m_string (string), m_font (&font), m_characterSize (characterSize), m_letterSpacing (0.f), +m_lineSpacing (0.f), m_style (Regular), m_fillColor (255, 255, 255), m_outlineColor (0, 0, 0), @@ -156,6 +158,17 @@ void Text::setLetterSpacing(float spacing) } +//////////////////////////////////////////////////////////// +void Text::setLineSpacing(float spacing) +{ + if (m_lineSpacing != spacing) + { + m_lineSpacing = spacing; + m_geometryNeedUpdate = true; + } +} + + //////////////////////////////////////////////////////////// void Text::setStyle(Uint32 style) { @@ -249,6 +262,13 @@ float Text::getLetterSpacing() const } +//////////////////////////////////////////////////////////// +float Text::getLineSpacing() const +{ + return m_lineSpacing; +} + + //////////////////////////////////////////////////////////// Uint32 Text::getStyle() const { @@ -296,9 +316,9 @@ Vector2f Text::findCharacterPos(std::size_t index) const index = m_string.getSize(); // 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) + m_letterSpacing; - float vspace = static_cast(m_font->getLineSpacing(m_characterSize)); + bool bold = (m_style & Bold) != 0; + float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing; + const float lineSpacing = static_cast(m_font->getLineSpacing(m_characterSize))+ m_lineSpacing; // Compute the position Vector2f position; @@ -314,9 +334,9 @@ Vector2f Text::findCharacterPos(std::size_t index) const // Handle special characters switch (curChar) { - case ' ': position.x += hspace; continue; - case '\t': position.x += hspace * 4; continue; - case '\n': position.y += vspace; position.x = 0; continue; + case ' ': position.x += hspace; continue; + case '\t': position.x += hspace * 4; continue; + case '\n': position.y += lineSpacing; position.x = 0; continue; } // For regular characters, add the advance offset of the glyph @@ -405,10 +425,10 @@ 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) + m_letterSpacing; - float vspace = static_cast(m_font->getLineSpacing(m_characterSize)); - float x = 0.f; - float y = static_cast(m_characterSize); + float hspace = static_cast(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing; + const float lineSpacing = static_cast(m_font->getLineSpacing(m_characterSize)) + m_lineSpacing; + float x = 0.f; + float y = static_cast(m_characterSize); // Create one quad for each character float minX = static_cast(m_characterSize); @@ -451,9 +471,9 @@ void Text::ensureGeometryUpdate() const switch (curChar) { - case ' ': x += hspace; break; - case '\t': x += hspace * 4; break; - case '\n': y += vspace; x = 0; break; + case ' ': x += hspace; break; + case '\t': x += hspace * 4; break; + case '\n': y += lineSpacing; x = 0; break; } // Update the current bounds (max coordinates)