Skip to content

Commit

Permalink
Deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Oct 2, 2023
1 parent 0c17cc5 commit d2af1f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
17 changes: 17 additions & 0 deletions include/SFML/Graphics/Text.hpp
Expand Up @@ -446,6 +446,23 @@ class SFML_GRAPHICS_API Text : public Drawable, public Transformable
////////////////////////////////////////////////////////////
void updateLineOffsets() const;

////////////////////////////////////////////////////////////
/// \brief Structure storing spacing information
///
////////////////////////////////////////////////////////////
struct Spacing
{
float whitespaceWidth{};
float letterSpacing{};
float lineSpacing{};
};

////////////////////////////////////////////////////////////
/// \brief Calculate various text spacing values
///
////////////////////////////////////////////////////////////
Spacing getSpacing() const;

////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Expand Down
35 changes: 19 additions & 16 deletions src/SFML/Graphics/Text.cpp
Expand Up @@ -311,11 +311,8 @@ Vector2f Text::findCharacterPos(std::size_t index) const
updateLineOffsets();

// Precompute the variables needed by the algorithm
const bool isBold = m_style & Bold;
float whitespaceWidth = m_font->getGlyph(U' ', m_characterSize, isBold).advance;
const float letterSpacing = (whitespaceWidth / 3.f) * (m_letterSpacingFactor - 1.f);
whitespaceWidth += letterSpacing;
const float lineSpacing = m_font->getLineSpacing(m_characterSize) * m_lineSpacingFactor;
const bool isBold = m_style & Bold;
const auto [whitespaceWidth, letterSpacing, lineSpacing] = getSpacing();

// Compute the position
Vector2f position(m_lineOffsets[0], 0.f); // There will always be at least one line
Expand Down Expand Up @@ -428,12 +425,9 @@ void Text::ensureGeometryUpdate() const
const float strikeThroughOffset = xBounds.top + xBounds.height / 2.f;

// Precompute the variables needed by the algorithm
float whitespaceWidth = m_font->getGlyph(U' ', m_characterSize, isBold).advance;
const float letterSpacing = (whitespaceWidth / 3.f) * (m_letterSpacingFactor - 1.f);
whitespaceWidth += letterSpacing;
const float lineSpacing = m_font->getLineSpacing(m_characterSize) * m_lineSpacingFactor;
float x = m_lineOffsets[0]; // there will always be at least one line
auto y = static_cast<float>(m_characterSize);
const auto [whitespaceWidth, letterSpacing, lineSpacing] = getSpacing();
float x = m_lineOffsets[0]; // there will always be at least one line
auto y = static_cast<float>(m_characterSize);

// Create one quad for each character
auto minX = static_cast<float>(m_characterSize);
Expand Down Expand Up @@ -585,11 +579,8 @@ void Text::updateLineOffsets() const
std::vector<float> lineWidths;

// Precompute the variables needed by the algorithm
const bool isBold = m_style & Bold;
float whitespaceWidth = m_font->getGlyph(U' ', m_characterSize, isBold).advance;
const float letterSpacing = (whitespaceWidth / 3.f) * (m_letterSpacingFactor - 1.f);
whitespaceWidth += letterSpacing;
const float lineSpacing = m_font->getLineSpacing(m_characterSize) * m_lineSpacingFactor;
const bool isBold = m_style & Bold;
const auto [whitespaceWidth, letterSpacing, lineSpacing] = getSpacing();

Vector2f position;
std::uint32_t prevChar = 0;
Expand Down Expand Up @@ -647,4 +638,16 @@ void Text::updateLineOffsets() const
}
}


////////////////////////////////////////////////////////////
Text::Spacing Text::getSpacing() const
{
const bool isBold = m_style & Bold;
float whitespaceWidth = m_font->getGlyph(U' ', m_characterSize, isBold).advance;
const float letterSpacing = (whitespaceWidth / 3.f) * (m_letterSpacingFactor - 1.f);
whitespaceWidth += letterSpacing;
const float lineSpacing = m_font->getLineSpacing(m_characterSize) * m_lineSpacingFactor;
return {whitespaceWidth, letterSpacing, lineSpacing};
}

} // namespace sf

0 comments on commit d2af1f4

Please sign in to comment.