Skip to content

Commit

Permalink
Implemented letter spacing in sf::Text.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Wagenbach committed Jul 23, 2015
1 parent 26fc872 commit ca2672e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
26 changes: 26 additions & 0 deletions include/SFML/Graphics/Text.hpp
Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions src/SFML/Graphics/Text.cpp
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -146,6 +159,13 @@ unsigned int Text::getCharacterSize() const
}


////////////////////////////////////////////////////////////
float Text::getLetterSpacing() const
{
return m_letterSpacing;
}


////////////////////////////////////////////////////////////
Uint32 Text::getStyle() const
{
Expand Down Expand Up @@ -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<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance);
float hspace = static_cast<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing;
float vspace = static_cast<float>(m_font->getLineSpacing(m_characterSize));

// Compute the position
Expand All @@ -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<float>(m_font->getGlyph(curChar, m_characterSize, bold).advance);
position.x += static_cast<float>(m_font->getGlyph(curChar, m_characterSize, bold).advance) + m_letterSpacing;
}

// Transform the position to global coordinates
Expand Down Expand Up @@ -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<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance);
float hspace = static_cast<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing;
float vspace = static_cast<float>(m_font->getLineSpacing(m_characterSize));
float x = 0.f;
float y = static_cast<float>(m_characterSize);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca2672e

Please sign in to comment.