diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index 65f4f257..dae6ef2d 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -149,9 +149,9 @@ public: /// /// The default spacing between lines is defined by the font. /// This method enables you to set a factor for the spacing - /// between lines. By default the line spacing offset is 1. + /// between lines. By default the line spacing factor is 1. /// - /// \param spacing New line spacing factor + /// \param spacingFactor New line spacing factor /// /// \see getLineSpacing /// @@ -162,10 +162,15 @@ public: /// \brief Set the letter spacing factor /// /// The default spacing between letters is defined by the font. - /// This method enables you to set a factor to the spacing - /// between letters. By default the letter spacing factor is 1. + /// This factor doesn't directly apply to the existing + /// spacing between each character, it rather adds a fixed + /// space between them which is calculated from the font + /// metrics and the character size. + /// Note that factors below 1 (including negative numbers) bring + /// characters closer to each other. + /// By default the letter spacing factor is 1. /// - /// \param spacing New letter spacing factor + /// \param spacingFactor New letter spacing factor /// /// \see getLetterSpacing /// diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 643197dd..3773616d 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -317,7 +317,9 @@ Vector2f Text::findCharacterPos(std::size_t index) const // Precompute the variables needed by the algorithm bool isBold = m_style & Bold; - float whitespaceWidth = m_font->getGlyph(L' ', m_characterSize, isBold).advance * m_letterSpacingFactor; + float whitespaceWidth = m_font->getGlyph(L' ', m_characterSize, isBold).advance; + float letterSpacing = ( whitespaceWidth / 3.f ) * ( m_letterSpacingFactor - 1.f ); + whitespaceWidth += letterSpacing; float lineSpacing = m_font->getLineSpacing(m_characterSize) * m_lineSpacingFactor; // Compute the position @@ -340,7 +342,7 @@ Vector2f Text::findCharacterPos(std::size_t index) const } // For regular characters, add the advance offset of the glyph - position.x += m_font->getGlyph(curChar, m_characterSize, isBold).advance * m_letterSpacingFactor; + position.x += m_font->getGlyph(curChar, m_characterSize, isBold).advance + letterSpacing; } // Transform the position to global coordinates @@ -425,7 +427,9 @@ void Text::ensureGeometryUpdate() const float strikeThroughOffset = xBounds.top + xBounds.height / 2.f; // Precompute the variables needed by the algorithm - float whitespaceWidth = m_font->getGlyph(L' ', m_characterSize, isBold).advance * m_letterSpacingFactor; + float whitespaceWidth = m_font->getGlyph(L' ', m_characterSize, isBold).advance; + float letterSpacing = ( whitespaceWidth / 3.f ) * ( m_letterSpacingFactor - 1.f ); + whitespaceWidth += letterSpacing; float lineSpacing = m_font->getLineSpacing(m_characterSize) * m_lineSpacingFactor; float x = 0.f; float y = static_cast(m_characterSize); @@ -525,7 +529,7 @@ void Text::ensureGeometryUpdate() const } // Advance to the next character - x += glyph.advance * m_letterSpacingFactor; + x += glyph.advance + letterSpacing; } // If we're using the underlined style, add the last line