Mercurial > fife-parpg
comparison engine/core/video/fonts/fontbase.cpp @ 160:9a05ba6735b1
Now textwrapping handles newlines correctly in trunk, too.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Tue, 14 Oct 2008 17:29:56 +0000 |
parents | 90005975cdbb |
children | 756b895e1dab |
comparison
equal
deleted
inserted
replaced
159:c305c5eda7ed | 160:9a05ba6735b1 |
---|---|
104 // split text as needed | 104 // split text as needed |
105 std::string::size_type pos, last_pos = 0; | 105 std::string::size_type pos, last_pos = 0; |
106 int length = 0; | 106 int length = 0; |
107 int render_width = 0, render_height = 0; | 107 int render_width = 0, render_height = 0; |
108 do { | 108 do { |
109 pos = text.find("\n", last_pos); | 109 pos = text.find('\n', last_pos); |
110 if (pos != std::string::npos) { | 110 if (pos != std::string::npos) { |
111 length = pos - last_pos; | 111 length = pos - last_pos; |
112 } else { | 112 } else { |
113 length = text.size() - last_pos; | 113 length = text.size() - last_pos; |
114 } | 114 } |
115 std::string sub = text.substr(last_pos, length); | 115 std::string sub = text.substr(last_pos, length); |
116 std::cerr << "substring: " << sub << "\n"; | |
116 SDL_Surface* text_surface = renderString(sub); | 117 SDL_Surface* text_surface = renderString(sub); |
117 if (text_surface->w > render_width) { | 118 if (text_surface->w > render_width) { |
118 render_width = text_surface->w; | 119 render_width = text_surface->w; |
119 } | 120 } |
120 lines.push_back(text_surface); | 121 lines.push_back(text_surface); |
162 line = "\n"; | 163 line = "\n"; |
163 } else { | 164 } else { |
164 firstLine = false; | 165 firstLine = false; |
165 } | 166 } |
166 | 167 |
168 bool haveNewLine = false; | |
167 while( getWidth(line) < render_width && pos < text.length() ) | 169 while( getWidth(line) < render_width && pos < text.length() ) |
168 { | 170 { |
169 if (text.at(pos) == ' ' && !line.empty()) | 171 if (text.at(pos) == ' ' && !line.empty()) |
170 break_pos.push_back( std::make_pair(line.length(),pos) ); | 172 break_pos.push_back( std::make_pair(line.length(),pos) ); |
171 line.push_back( text.at(pos) ); | 173 line.push_back( text.at(pos) ); |
172 ++pos; | 174 ++pos; |
173 | 175 |
174 // Special case: Already newlines in string: | 176 // Special case: Already newlines in string: |
175 if( text.at(pos-1) == '\n' ) { | 177 if( text.at(pos-1) == '\n' ) { |
176 if( line[0] == '\n' && line.length() > 1 ) | 178 if( line[line.size()-1] == '\n' ) |
177 line.erase(0); | 179 line.erase(line.size()-1); |
178 output.append(line); | 180 output.append(line); |
181 line = ""; | |
182 haveNewLine = true; | |
179 break; | 183 break; |
180 } | 184 } |
181 } | 185 } |
182 if( pos >= text.length() ) | 186 if( haveNewLine ) |
187 continue; | |
188 | |
189 if( pos >= text.length()) | |
183 break; | 190 break; |
184 | 191 |
185 if( break_pos.empty() ) { | 192 if( break_pos.empty() ) { |
186 // No break position and line length smaller than 2 | 193 // No break position and line length smaller than 2 |
187 // means the renderwidth is really screwed. Just continue | 194 // means the renderwidth is really screwed. Just continue |