Image* FontBase::getAsImageMultiline(const std::string& text) { const uint8_t newline_utf8 = '\n'; uint32_t newline; utf8::utf8to32(&newline_utf8,&newline_utf8 + 1,&newline); //std::cout << "Text:" << text << std::endl; Image* image = m_pool.getRenderedText(this, text); if (!image) { std::vector<SDL_Surface*> lines; std::string::const_iterator it = text.begin(); // split text as needed int render_width = 0, render_height = 0; do { uint32_t codepoint = 0; std::string line; while( codepoint != newline && it != text.end() ) { codepoint = utf8::next(it,text.end()); if( codepoint != newline ) utf8::append(codepoint, back_inserter(line)); } //std::cout << "Line:" << line << std::endl; SDL_Surface* text_surface = renderString(line); if (text_surface->w > render_width) { render_width = text_surface->w; } lines.push_back(text_surface); } while (it != text.end()); render_height = (getRowSpacing() + getHeight()) * lines.size(); SDL_Surface* final_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, render_width,render_height,32, RMASK, GMASK, BMASK ,AMASK); if (!final_surface) { throw SDLException(std::string("CreateRGBSurface failed: ") + SDL_GetError()); } SDL_FillRect(final_surface, 0, 0x00000000); int ypos = 0; for (std::vector<SDL_Surface*>::iterator i = lines.begin(); i != lines.end(); ++i) { SDL_Rect dst_rect = { 0, 0, 0, 0 }; dst_rect.y = ypos; SDL_SetAlpha(*i,0,SDL_ALPHA_OPAQUE); SDL_BlitSurface(*i,0,final_surface,&dst_rect); ypos += getRowSpacing() + getHeight(); SDL_FreeSurface(*i); } image = RenderBackend::instance()->createImage(final_surface); m_pool.addRenderedText(this, text, image); } return image; }
int ImageFont::drawGlyph(Graphics* graphics, unsigned char glyph, int x, int y) { // This is needed for drawing the glyph in the middle // if we have spacing. int yoffset = getRowSpacing() / 2; if (mGlyph[glyph].width == 0) { graphics->drawRectangle(Rectangle(x, y + 1 + yoffset, mGlyph[(int)(' ')].width - 1, mGlyph[(int)(' ')].height - 2)); return mGlyph[(int)(' ')].width + mGlyphSpacing; } graphics->drawImage(mImage, mGlyph[glyph].x, mGlyph[glyph].y, x, y + yoffset, mGlyph[glyph].width, mGlyph[glyph].height); return mGlyph[glyph].width + mGlyphSpacing; }
void SDLTrueTypeFont::drawString(gcn::Graphics* graphics, const std::string& text, const int x, const int y) { if (text == "") return; // This is needed for drawing the Glyph in the middle if we have spacing int yoffset = getRowSpacing() / 2; gcn::Color col = graphics->getColor(); std::string colstr; colstr += static_cast<char>(col.r); colstr += static_cast<char>(col.g); colstr += static_cast<char>(col.b); boost::shared_ptr<gcn::OpenGLImage> image = image_cache_.fetch(make_pair(text, colstr)); if (!image) { SDL_Color sdlCol; sdlCol.b = col.b; sdlCol.r = col.r; sdlCol.g = col.g; SDL_Surface *textSurface; if (anti_alias_) { textSurface = TTF_RenderUTF8_Blended(font_, text.c_str(), sdlCol); } else { textSurface = TTF_RenderUTF8_Solid(font_, text.c_str(), sdlCol); } SDL_LockSurface(textSurface); { image.reset(new gcn::OpenGLImage((const unsigned int*)textSurface->pixels, textSurface->w, textSurface->h)); } SDL_UnlockSurface(textSurface); SDL_FreeSurface(textSurface); image_cache_.insert(make_pair(text, colstr), image); } graphics->drawImage(image.get(), 0, 0, x, y + yoffset, image->getWidth(), image->getHeight()); }
void SDLTrueTypeFont::drawString(gcn::Graphics* graphics, const std::string& text, const int x, const int y) { if (text == "") { return; } SDLGraphics *sdlGraphics = dynamic_cast<SDLGraphics *>(graphics); if (sdlGraphics == NULL) { throw GCN_EXCEPTION("SDLTrueTypeFont::drawString. Graphics object not an SDL graphics object!"); return; } // This is needed for drawing the Glyph in the middle if we have spacing int yoffset = getRowSpacing() / 2; Color col = sdlGraphics->getColor(); SDL_Color sdlCol; sdlCol.b = col.b; sdlCol.r = col.r; sdlCol.g = col.g; SDL_Surface *textSurface; if (mAntiAlias) { textSurface = TTF_RenderText_Blended(mFont, text.c_str(), sdlCol); } else { textSurface = TTF_RenderText_Solid(mFont, text.c_str(), sdlCol); } SDL_Rect dst, src; dst.x = x; dst.y = y + yoffset; src.w = textSurface->w; src.h = textSurface->h; src.x = 0; src.y = 0; sdlGraphics->drawSDLSurface(textSurface, src, dst); SDL_FreeSurface(textSurface); }
void GuiFont::drawMultiLineString(gcn::Graphics* graphics, const std::string& text, int32_t x, int32_t y) { if (text == "") { return; } int32_t yoffset = getRowSpacing() / 2; const gcn::ClipRectangle& clip = graphics->getCurrentClipArea(); Image* image = getAsImageMultiline(text); FIFE::Rect rect; rect.x = x + clip.xOffset; rect.y = y + clip.yOffset + yoffset; rect.w = image->getWidth(); rect.h = image->getHeight(); if (!rect.intersects(Rect(clip.x,clip.y,clip.width,clip.height)) ) { return; } image->render(rect); }
int TrueTypeFont::getHeight() const { return TTF_FontHeight(mFont) + getRowSpacing(); }