Implemented line spacing in sf::Text.

This commit is contained in:
Maximilian Wagenbach 2015-07-23 12:17:23 +02:00 committed by Maximilian Wagenbach
parent 9e2f2eb27f
commit 812dea70d0
2 changed files with 59 additions and 13 deletions

View File

@ -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

View File

@ -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<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing;
float vspace = static_cast<float>(m_font->getLineSpacing(m_characterSize));
bool bold = (m_style & Bold) != 0;
float hspace = static_cast<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing;
const float lineSpacing = static_cast<float>(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<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);
float hspace = static_cast<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance) + m_letterSpacing;
const float lineSpacing = static_cast<float>(m_font->getLineSpacing(m_characterSize)) + m_lineSpacing;
float x = 0.f;
float y = static_cast<float>(m_characterSize);
// Create one quad for each character
float minX = static_cast<float>(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)