diff --git a/README.md b/README.md index a151e65..ffe373a 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ License This code is licensed under public domain. -Author ------- +Authors +------- * [Cristian Pallarés](https://github.com/Skyrpex) - Original code * [Lukas Dürrenberger](https://github.com/eXpl0it3r/) - Conversion to the new SFML2 API @@ -20,6 +20,10 @@ How to use 1. Include the header and the source to your project. 2. Link to SFML2 (obviously :P!). +3. Use a C++11 ready compiler. + +Note: For a non C++11 ready compilers, there is a [support branch](https://github.com/Skyrpex/RichText/tree/support/no-c%2B%2B11). +However, it's not guaranteed to be fully updated. Repository ---------- diff --git a/RichText.cpp b/RichText.cpp index 121b2b0..a4a13f1 100644 --- a/RichText.cpp +++ b/RichText.cpp @@ -43,11 +43,7 @@ const std::vector &RichText::Line::getTexts() const void RichText::Line::appendText(sf::Text text) { // Set text offset - text.setPosition(m_bounds.width, 0.f); - - // Update bounds - m_bounds.height = std::max(m_bounds.height, text.getGlobalBounds().height); - m_bounds.width += text.getGlobalBounds().width; + updateTextAndGeometry(text); // Push back m_texts.push_back(std::move(text)); @@ -83,12 +79,21 @@ void RichText::Line::updateGeometry() const { m_bounds = sf::FloatRect(); - for (sf::Text &text : m_texts) { - text.setPosition(m_bounds.width, 0.f); + for (sf::Text &text : m_texts) + updateTextAndGeometry(text); +} - m_bounds.height = std::max(m_bounds.height, text.getGlobalBounds().height); - m_bounds.width += text.getGlobalBounds().width; - } + +//////////////////////////////////////////////////////////////////////////////// +void RichText::Line::updateTextAndGeometry(sf::Text &text) const +{ + // Set text offset + text.setPosition(m_bounds.width, 0.f); + + // Update bounds + int lineSpacing = text.getFont()->getLineSpacing(text.getCharacterSize()); + m_bounds.height = std::max(m_bounds.height, static_cast(lineSpacing)); + m_bounds.width += text.getGlobalBounds().width; } @@ -308,6 +313,7 @@ sf::Text RichText::createText(const sf::String &string) const text.setString(string); text.setColor(m_currentColor); text.setStyle(m_currentStyle); + text.setCharacterSize(m_characterSize); if (m_font) text.setFont(*m_font); diff --git a/RichText.hpp b/RichText.hpp index d355eb1..51d4103 100644 --- a/RichText.hpp +++ b/RichText.hpp @@ -74,6 +74,11 @@ public: ////////////////////////////////////////////////////////////////////// void updateGeometry() const; + ////////////////////////////////////////////////////////////////////// + // Update geometry for a given text + ////////////////////////////////////////////////////////////////////// + void updateTextAndGeometry(sf::Text &text) const; + ////////////////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////////////////