00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <algorithm>
00024
00025
00026 #include <SDL.h>
00027 #include <SDL_image.h>
00028
00029
00030
00031
00032
00033 #include "util/base/exception.h"
00034 #include "util/structures/rect.h"
00035 #include "util/utf8/utf8.h"
00036 #include "video/image.h"
00037 #include "video/renderbackend.h"
00038
00039 #include "imagefontbase.h"
00040
00041 namespace FIFE {
00042
00043 ImageFontBase::ImageFontBase() : FontBase() {
00044 }
00045
00046 ImageFontBase::~ImageFontBase() {
00047 type_glyphs::iterator i = m_glyphs.begin();
00048 for(; i != m_glyphs.end(); ++i) {
00049 SDL_FreeSurface(i->second.surface);
00050 }
00051
00052 }
00053
00054 int32_t ImageFontBase::getWidth(const std::string& text) const {
00055 int32_t w = 0;
00056 std::string::const_iterator text_it = text.begin();
00057 while(text_it != text.end()) {
00058 uint32_t codepoint = utf8::next(text_it,text.end());
00059 type_glyphs::const_iterator it = m_glyphs.find( codepoint );
00060
00061 if( it != m_glyphs.end() ) {
00062 w += it->second.surface->w + getGlyphSpacing();
00063 continue;
00064 }
00065
00066 if( m_placeholder.surface ) {
00067 w += m_placeholder.surface->w + getGlyphSpacing();
00068 }
00069 }
00070 return w;
00071 }
00072
00073 int32_t ImageFontBase::getHeight() const {
00074 return mHeight;
00075 }
00076
00077 SDL_Surface *ImageFontBase::renderString(const std::string& text) {
00078 SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
00079 getWidth(text),getHeight(),32,
00080 RMASK, GMASK, BMASK ,AMASK);
00081
00082 SDL_FillRect(surface,0,0x00000000);
00083
00084 SDL_Rect dst;
00085 dst.x = dst.y = 0;
00086 s_glyph *glyph = 0;
00087
00088 std::string::const_iterator text_it = text.begin();
00089 while(text_it != text.end()) {
00090 uint32_t codepoint = utf8::next(text_it,text.end());
00091 type_glyphs::iterator it = m_glyphs.find( codepoint );
00092
00093 if( it == m_glyphs.end() ) {
00094 if( !m_placeholder.surface ) {
00095 continue;
00096 }
00097 glyph = &m_placeholder;
00098 } else {
00099 glyph = &(it->second);
00100 }
00101 dst.y = glyph->offset.y;
00102 dst.x += glyph->offset.x;
00103
00104 SDL_BlitSurface(glyph->surface,0,surface,&dst);
00105 dst.x += glyph->surface->w + getGlyphSpacing();
00106 }
00107
00108 return surface;
00109 }
00110
00111 void ImageFontBase::setColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
00112 }
00113 }