Implemented letter spacing in sf::Text.

This commit is contained in:
Maximilian Wagenbach 2015-07-21 19:00:01 +02:00 committed by Maximilian Wagenbach
parent e31b925234
commit 9e2f2eb27f
2 changed files with 50 additions and 4 deletions

View File

@ -144,6 +144,21 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void setCharacterSize(unsigned int size); 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 /// \brief Set the text's style
/// ///
@ -260,6 +275,16 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
unsigned int getCharacterSize() const; 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 /// \brief Get the text's style
/// ///
@ -385,6 +410,7 @@ private:
String m_string; ///< String to display String m_string; ///< String to display
const Font* m_font; ///< Font used to display the string const Font* m_font; ///< Font used to display the string
unsigned int m_characterSize; ///< Base size of characters, in pixels 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) Uint32 m_style; ///< Text style (see Style enum)
Color m_fillColor; ///< Text fill color Color m_fillColor; ///< Text fill color
Color m_outlineColor; ///< Text outline color Color m_outlineColor; ///< Text outline color

View File

@ -77,6 +77,7 @@ Text::Text() :
m_string (), m_string (),
m_font (NULL), m_font (NULL),
m_characterSize (30), m_characterSize (30),
m_letterSpacing (0.f),
m_style (Regular), m_style (Regular),
m_fillColor (255, 255, 255), m_fillColor (255, 255, 255),
m_outlineColor (0, 0, 0), m_outlineColor (0, 0, 0),
@ -96,6 +97,7 @@ Text::Text(const String& string, const Font& font, unsigned int characterSize) :
m_string (string), m_string (string),
m_font (&font), m_font (&font),
m_characterSize (characterSize), m_characterSize (characterSize),
m_letterSpacing (0.f),
m_style (Regular), m_style (Regular),
m_fillColor (255, 255, 255), m_fillColor (255, 255, 255),
m_outlineColor (0, 0, 0), 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) 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 Uint32 Text::getStyle() const
{ {
@ -277,7 +297,7 @@ Vector2f Text::findCharacterPos(std::size_t index) const
// Precompute the variables needed by the algorithm // Precompute the variables needed by the algorithm
bool bold = (m_style & Bold) != 0; 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)); float vspace = static_cast<float>(m_font->getLineSpacing(m_characterSize));
// Compute the position // 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 // 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 // Transform the position to global coordinates
@ -385,7 +405,7 @@ void Text::ensureGeometryUpdate() const
float strikeThroughOffset = xBounds.top + xBounds.height / 2.f; float strikeThroughOffset = xBounds.top + xBounds.height / 2.f;
// Precompute the variables needed by the algorithm // 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 vspace = static_cast<float>(m_font->getLineSpacing(m_characterSize));
float x = 0.f; float x = 0.f;
float y = static_cast<float>(m_characterSize); float y = static_cast<float>(m_characterSize);
@ -486,7 +506,7 @@ void Text::ensureGeometryUpdate() const
} }
// Advance to the next character // Advance to the next character
x += glyph.advance; x += glyph.advance + m_letterSpacing;
} }
// If we're using the underlined style, add the last line // If we're using the underlined style, add the last line