| 68 | } |
| 69 | |
| 70 | void Label::WrapText() { |
| 71 | const std::string& font_name( Context::Get().GetEngine().GetProperty<std::string>( "FontName", shared_from_this() ) ); |
| 72 | unsigned int font_size( Context::Get().GetEngine().GetProperty<unsigned int>( "FontSize", shared_from_this() ) ); |
| 73 | const sf::Font& font( *Context::Get().GetEngine().GetResourceManager().GetFont( font_name ) ); |
| 74 | |
| 75 | std::u32string wrapped_text; |
| 76 | std::u32string text( m_text.begin(), m_text.end() ); |
| 77 | |
| 78 | std::u32string line; |
| 79 | |
| 80 | while( !text.empty() ) { |
| 81 | auto next_newline = text.find( L'\n' ); |
| 82 | |
| 83 | line = text.substr( 0, next_newline ); |
| 84 | |
| 85 | if( next_newline != std::u32string::npos ) { |
| 86 | text.erase( 0, next_newline + 1 ); |
| 87 | } |
| 88 | else { |
| 89 | text.clear(); |
| 90 | } |
| 91 | |
| 92 | if( !wrapped_text.empty() ) { |
| 93 | wrapped_text += L'\n'; |
| 94 | } |
| 95 | |
| 96 | // Check if line needs to be wrapped. |
| 97 | if( Context::Get().GetEngine().GetTextStringMetrics( line, font, font_size ).x <= GetAllocation().size.x ) { |
| 98 | wrapped_text += line; |
| 99 | } |
| 100 | else { |
| 101 | // Line needs to be wrapped. |
| 102 | while( !line.empty() ) { |
| 103 | auto last_space = line.size(); |
| 104 | |
| 105 | while( Context::Get().GetEngine().GetTextStringMetrics( line.substr( 0, last_space ), font, font_size ).x > GetAllocation().size.x ) { |
| 106 | last_space = line.find_last_of( L' ', last_space - 1 ); |
| 107 | |
| 108 | if( last_space == std::u32string::npos ) { |
| 109 | wrapped_text += line; |
| 110 | line.clear(); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if( last_space != std::u32string::npos ) { |
| 116 | wrapped_text += line.substr( 0, last_space ); |
| 117 | line.erase( 0, last_space ); |
| 118 | } |
| 119 | |
| 120 | if( !line.empty() ) { |
| 121 | wrapped_text += L'\n'; |
| 122 | |
| 123 | // If this is a new line remove the leading space. |
| 124 | if( line[0] == L' ' ) { |
| 125 | line.erase( 0, 1 ); |
| 126 | } |
| 127 | } |
nothing calls this directly
no test coverage detected