Fixed letter spacing being letter width dependent

This commit is contained in:
Radek Dutkiewicz 2018-02-05 15:14:08 +00:00 committed by Lukas Dürrenberger
parent 186294607f
commit deeb3a95f3
2 changed files with 18 additions and 9 deletions

View File

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

View File

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