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; } } }
void wordwrap(){ //divide the sentence into words vector< string > words = explode( ' ', log_text ); vector< bool > newline( words.size() ); int limit_right = log_x + log_background -> width() - log_padding_x * 2; for( unsigned index = 0; index < words.size(); ++index ){ log_text.clear(); for( unsigned i = 0; i <= index; i++ ){ log_text += words[i] + ( newline[ i ] ? '\n' : ' ' ); } log_text_display.setString( log_text ); if( log_text_display.findCharacterPos( log_text.size() ).x >= limit_right ){ newline[ index - 1 ] = true; } } log_text.clear(); for( unsigned i = 0; i < words.size(); i++ ){ log_text += words[i] + ( newline[ i ] ? '\n' : ' ' ); } }
void init_char_width(){ if (!PTSANS.loadFromFile("PTN57F.ttf" )) return; PTSANS_loaded = true; string tq; int tmp = log_string.getPosition().x; for( int i = 255; i--; ){ tq = (char) i; log_string.setString( tq ); char_width[i] = log_string.findCharacterPos( 1 ).x - tmp; } }