truetypefont.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <cassert>
00024
00025
00026 #include <SDL.h>
00027
00028
00029
00030
00031
00032 #include "util/base/exception.h"
00033 #include "util/structures/rect.h"
00034 #include "util/utf8/utf8.h"
00035 #include "video/image.h"
00036 #include "video/renderbackend.h"
00037
00038 #include "truetypefont.h"
00039
00040 namespace FIFE {
00041
00042 TrueTypeFont::TrueTypeFont(const std::string& filename, int size)
00043 : FIFE::FontBase() {
00044 mFilename = filename;
00045 mFont = NULL;
00046
00047 mFont = TTF_OpenFont(filename.c_str(), size);
00048
00049 if (mFont == NULL) {
00050 throw FIFE::CannotOpenFile(filename + " (" + TTF_GetError() + ")");
00051 }
00052 mColor.r = mColor.g = mColor.b = 255;
00053 }
00054
00055 TrueTypeFont::~TrueTypeFont() {
00056 TTF_CloseFont(mFont);
00057 }
00058
00059 int TrueTypeFont::getWidth(const std::string& text) const {
00060 int w, h;
00061 assert( utf8::is_valid(text.begin(), text.end()) );
00062 TTF_SizeUTF8(mFont, text.c_str(), &w, &h);
00063 return w;
00064 }
00065
00066 int TrueTypeFont::getHeight() const {
00067 return TTF_FontHeight(mFont) + getRowSpacing();
00068 }
00069
00070 SDL_Surface* TrueTypeFont::renderString(const std::string& text) {
00071 if( text.empty() ) {
00072 SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
00073 1,getHeight(),32,
00074 RMASK, GMASK, BMASK ,AMASK);
00075 SDL_FillRect(surface,0,0x00000000);
00076 return surface;
00077 }
00078
00079 SDL_Surface* renderedText = 0;
00080 if (m_antiAlias) {
00081 renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
00082 } else {
00083 renderedText = TTF_RenderUTF8_Solid(mFont, text.c_str(), mColor);
00084 }
00085
00086
00087 if (renderedText == 0 && !m_antiAlias) {
00088 renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
00089 }
00090
00091 if (renderedText == 0) {
00092 throw FIFE::SDLException(TTF_GetError());
00093 }
00094 return renderedText;
00095 }
00096
00097 void TrueTypeFont::setColor(Uint8 r, Uint8 g, Uint8 b) {
00098 mColor.r = r;
00099 mColor.g = g;
00100 mColor.b = b;
00101 }
00102 }