28 #include <boost/filesystem/convenience.hpp>
34 #include "util/structures/rect.h"
35 #include "util/base/exception.h"
36 #include "util/utf8/utf8.h"
37 #include "video/image.h"
38 #include "video/renderbackend.h"
53 void FontBase::invalidate() {
58 mRowSpacing = spacing;
66 mGlyphSpacing = spacing;
74 m_antiAlias = antiAlias;
85 int FontBase::getStringIndexAt(
const std::string &text,
int x)
const {
86 assert( utf8::is_valid(text.begin(), text.end()) );
87 std::string::const_iterator cur;
88 if (text.size() == 0)
return 0;
93 utf8::next(cur, text.end());
96 while(cur != text.end()) {
97 buff = std::string(text.begin(), cur);
102 utf8::next(cur, text.end());
116 SDL_Surface* textSurface = renderString(text);
117 image = RenderBackend::instance()->createImage(textSurface);
124 const uint8_t newline_utf8 =
'\n';
126 utf8::utf8to32(&newline_utf8,&newline_utf8 + 1,&newline);
130 std::vector<SDL_Surface*> lines;
131 std::string::const_iterator it = text.begin();
133 int render_width = 0, render_height = 0;
135 uint32_t codepoint = 0;
137 while( codepoint != newline && it != text.end() )
139 codepoint = utf8::next(it,text.end());
140 if( codepoint != newline )
141 utf8::append(codepoint, back_inserter(line));
144 SDL_Surface* text_surface = renderString(line);
145 if (text_surface->w > render_width) {
146 render_width = text_surface->w;
148 lines.push_back(text_surface);
149 }
while (it != text.end());
152 SDL_Surface* final_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
153 render_width,render_height,32,
154 RMASK, GMASK, BMASK ,AMASK);
155 if (!final_surface) {
156 throw SDLException(std::string(
"CreateRGBSurface failed: ") + SDL_GetError());
158 SDL_FillRect(final_surface, 0, 0x00000000);
160 for (std::vector<SDL_Surface*>::iterator i = lines.begin(); i != lines.end(); ++i) {
161 SDL_Rect dst_rect = { 0, 0, 0, 0 };
164 SDL_SetAlpha(*i,0,SDL_ALPHA_OPAQUE);
165 SDL_BlitSurface(*i,0,final_surface,&dst_rect);
169 image = RenderBackend::instance()->createImage(final_surface);
175 std::string FontBase::splitTextToWidth (
const std::string& text,
int render_width) {
176 const uint32_t whitespace =
' ';
177 const uint8_t newline_utf8 =
'\n';
179 utf8::utf8to32(&newline_utf8,&newline_utf8 + 1,&newline);
180 if (render_width <= 0 || text.empty()) {
185 std::string::const_iterator pos = text.begin();
186 std::list<std::pair<size_t,std::string::const_iterator> > break_pos;
187 bool firstLine =
true;
189 while( pos != text.end())
198 bool haveNewLine =
false;
199 while(
getWidth(line) < render_width && pos != text.end() )
201 uint32_t codepoint = utf8::next(pos, text.end());
202 if (codepoint == whitespace && !line.empty())
203 break_pos.push_back( std::make_pair(line.length(),pos) );
205 if( codepoint != newline )
206 utf8::append(codepoint, back_inserter(line) );
209 if( codepoint == newline ) {
219 if( pos == text.end() )
222 if( break_pos.empty() ) {
226 if( utf8::distance(line.begin(),line.end()) <= 1 && line !=
"\n") {
241 line = line.substr(0,break_pos.back().first);
242 pos = break_pos.back().second;
246 if( !line.empty() ) {