void breakTextLines(sf::Text& t, float maxX) { sf::String s = t.getString(); std::size_t lastBreakCharIdx = s.getSize(); static sf::String const breakBeforeChars("([{\"'`'"); static auto const isBreakBeforeChar = [] (sf::Uint32 c) { return breakBeforeChars.find(c) != sf::String::InvalidPos; }; for (std::size_t i = 0; i < s.getSize(); ++i) { if (t.findCharacterPos(i).x > maxX) { if (lastBreakCharIdx > i) lastBreakCharIdx = i; if (s.getSize() > lastBreakCharIdx && !std::iswgraph(static_cast<std::wint_t>(s[lastBreakCharIdx + 1])) ) { s[lastBreakCharIdx + 1] = '\n'; } else { s.insert(lastBreakCharIdx + 1, '\n'); } t.setString(s); i += 1; } if (!std::iswalnum(static_cast<std::wint_t>(s[i]))) { lastBreakCharIdx = i; if (i > 0 && isBreakBeforeChar(s[i])) lastBreakCharIdx -= 1; } } }
float getStrWidth(sf::Text const &ref) { float ret = static_cast<std::string const&>(ref.getString()) .length(); if (ref.getCharacterSize() <= 12) return ((ref.getCharacterSize() - 4)) * ret; if (ref.getCharacterSize() >= 20) return ((ref.getCharacterSize() - 8) * ret); return ((ref.getCharacterSize() - 5) * ret); }
void assetHandle::trimTextToRectangleWidth(sf::Text &text, sf::RectangleShape &rect) { while(true) { sf::FloatRect floatRect = text.getGlobalBounds(); if ((floatRect.left + floatRect.width) > rect.getSize().x) { std::string newString = text.getString(); newString = newString.substr(0, newString.size()-1); text.setString(newString); } else break; } }
inline void setStr(const std::string& mStr) { if(impl.getString() == "") { impl.setString(mStr); taAppear(); taJump(); createTA(10.f) += [this, mStr](auto&, FT) { }; return; } createTA() += [this, mStr](auto&, FT) { impl.setString(mStr); }; taJump(); }
Label::Label(Updatable *parent, const sf::Text &text, const sf::Vector2f &position, int characterSize) : Widget(parent, sf::FloatRect(position.x, position.y, characterSize * text.getString().getSize(), characterSize)), m_text(text) { m_focus=false; setCharacterSize(characterSize); }
Primitive::Ptr Renderer::CreateText( const sf::Text& text, sf::Color background_color_hint ) { Primitive::Ptr primitive( new Primitive ); const sf::Font& font = text.getFont(); unsigned int character_size = text.getCharacterSize(); sf::Color color = text.getColor(); if( m_preblend ) { color = sf::Color::White; } sf::Vector2f atlas_offset = LoadFont( font, character_size, background_color_hint, text.getColor() ); const sf::String& str = text.getString(); std::size_t length = str.getSize(); float horizontal_spacing = static_cast<float>( font.getGlyph( L' ', character_size, false ).advance ); float vertical_spacing = static_cast<float>( font.getLineSpacing( character_size ) ); sf::Vector2f position( std::floor( text.getPosition().x + .5f ), std::floor( text.getPosition().y + static_cast<float>( character_size ) + .5f ) ); const static float tab_spaces = 2.f; sf::Uint32 previous_character = 0; for( std::size_t index = 0; index < length; ++index ) { sf::Uint32 current_character = str[index]; position.x += static_cast<float>( font.getKerning( previous_character, current_character, character_size ) ); switch( current_character ) { case L' ': position.x += horizontal_spacing; continue; case L'\t': position.x += horizontal_spacing * tab_spaces; continue; case L'\n': position.y += vertical_spacing; position.x = 0.f; continue; case L'\v': position.y += vertical_spacing * tab_spaces; continue; default: break; } const sf::Glyph& glyph = font.getGlyph( current_character, character_size, false ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; Primitive::Vertex vertex3; vertex0.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top ) ); vertex1.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex2.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top ) ); vertex3.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex0.color = color; vertex1.color = color; vertex2.color = color; vertex3.color = color; // Let SFML cast the Rect for us. sf::FloatRect texture_rect( glyph.textureRect ); vertex0.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top ); vertex1.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top + texture_rect.height ); vertex2.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top ); vertex3.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top + texture_rect.height ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex3 ); position.x += static_cast<float>( glyph.advance ); previous_character = current_character; } AddPrimitive( primitive ); return primitive; }
inline auto& operator+=(const std::string& mStr) { setStr(impl.getString() + mStr); return *this; }