33 #include "util/structures/rect.h"
34 #include "util/base/exception.h"
35 #include "util/utf8/utf8.h"
36 #include "video/image.h"
37 #include "video/renderbackend.h"
52 void FontBase::invalidate() {
57 mRowSpacing = spacing;
65 mGlyphSpacing = spacing;
73 m_antiAlias = antiAlias;
84 int32_t FontBase::getStringIndexAt(
const std::string &text, int32_t x)
const {
85 assert( utf8::is_valid(text.begin(), text.end()) );
86 std::string::const_iterator cur;
87 if (text.size() == 0)
return 0;
92 utf8::next(cur, text.end());
95 while(cur != text.end()) {
96 buff = std::string(text.begin(), cur);
101 utf8::next(cur, text.end());
115 SDL_Surface* textSurface = renderString(text);
116 image = RenderBackend::instance()->createImage(textSurface);
123 const uint8_t newline_utf8 =
'\n';
125 utf8::utf8to32(&newline_utf8,&newline_utf8 + 1,&newline);
129 std::vector<SDL_Surface*> lines;
130 std::string::const_iterator it = text.begin();
132 int32_t render_width = 0, render_height = 0;
134 uint32_t codepoint = 0;
136 while( codepoint != newline && it != text.end() )
138 codepoint = utf8::next(it,text.end());
139 if( codepoint != newline )
140 utf8::append(codepoint, back_inserter(line));
143 SDL_Surface* text_surface = renderString(line);
144 if (text_surface->w > render_width) {
145 render_width = text_surface->w;
147 lines.push_back(text_surface);
148 }
while (it != text.end());
151 SDL_Surface* final_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
152 render_width,render_height,32,
153 RMASK, GMASK, BMASK ,AMASK);
154 if (!final_surface) {
155 throw SDLException(std::string(
"CreateRGBSurface failed: ") + SDL_GetError());
157 SDL_FillRect(final_surface, 0, 0x00000000);
159 for (std::vector<SDL_Surface*>::iterator i = lines.begin(); i != lines.end(); ++i) {
160 SDL_Rect dst_rect = { 0, 0, 0, 0 };
163 SDL_SetAlpha(*i,0,SDL_ALPHA_OPAQUE);
164 SDL_BlitSurface(*i,0,final_surface,&dst_rect);
168 image = RenderBackend::instance()->createImage(final_surface);
174 std::string FontBase::splitTextToWidth (
const std::string& text, int32_t render_width) {
175 const uint32_t whitespace =
' ';
176 const uint8_t newline_utf8 =
'\n';
178 utf8::utf8to32(&newline_utf8,&newline_utf8 + 1,&newline);
179 if (render_width <= 0 || text.empty()) {
184 std::string::const_iterator pos = text.begin();
185 std::list<std::pair<size_t,std::string::const_iterator> > break_pos;
186 bool firstLine =
true;
188 while( pos != text.end())
197 bool haveNewLine =
false;
198 while(
getWidth(line) < render_width && pos != text.end() )
200 uint32_t codepoint = utf8::next(pos, text.end());
201 if (codepoint == whitespace && !line.empty())
202 break_pos.push_back( std::make_pair(line.length(),pos) );
204 if( codepoint != newline )
205 utf8::append(codepoint, back_inserter(line) );
208 if( codepoint == newline ) {
218 if( pos == text.end() )
221 if( break_pos.empty() ) {
225 if( utf8::distance(line.begin(),line.end()) <= 1 && line !=
"\n") {
240 line = line.substr(0,break_pos.back().first);
241 pos = break_pos.back().second;
245 if( !line.empty() ) {
int32_t getGlyphSpacing() const
void invalidateCachedText()
Image * getAsImageMultiline(const std::string &text)
void setAntiAlias(bool antiAlias)
Image * getRenderedText(FontBase *fontbase, const std::string &text)
void setRowSpacing(int32_t spacing)
void addRenderedText(FontBase *fontbase, const std::string &text, Image *image)
int32_t getRowSpacing() const
virtual int32_t getHeight() const =0
Image * getAsImage(const std::string &text)
void setGlyphSpacing(int32_t spacing)
SDL_Color getColor() const
virtual int32_t getWidth(const std::string &text) const =0