Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Font.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Font.h"
00010 
00011 #include "Log.h"
00012 #include "Path.h"
00013 #include "TTFException.h"
00014 #include "SDLException.h"
00015 #include "Outline.h"
00016 
00017 //-----------------------------------------------------------------
00018 /**
00019  * Create new font from file.
00020  * Initialized TTF_Init when necessary.
00021  * @param file_ttf path to ttf file
00022  * @param height font height
00023  * @throws TTFException when cannot open font
00024  */
00025 Font::Font(const Path &file_ttf, int height)
00026 {
00027     m_ttfont = TTF_OpenFont(file_ttf.getNative().c_str(), height);
00028     if (!m_ttfont) {
00029         throw TTFException(ExInfo("OpenFont")
00030                 .addInfo("file", file_ttf.getNative()));
00031     }
00032 
00033     //NOTE: bg color will be set to be transparent
00034     SDL_Color bg = {10, 10, 10, 0};
00035     m_bg = bg;
00036 }
00037 //-----------------------------------------------------------------
00038 Font::~Font()
00039 {
00040     TTF_CloseFont(m_ttfont);
00041 }
00042 //-----------------------------------------------------------------
00043 /**
00044  * Prepare font rendering.
00045  * @throws TTFException when cannot init SDL_ttf.
00046  */
00047 void
00048 Font::init()
00049 {
00050     if (TTF_Init() < 0) {
00051         throw TTFException(ExInfo("Init"));
00052     }
00053 }
00054 //-----------------------------------------------------------------
00055 /**
00056  * Deinit font subsystem.
00057  */
00058 void
00059 Font::shutdown()
00060 {
00061     TTF_Quit();
00062 }
00063 
00064 //-----------------------------------------------------------------
00065     int
00066 Font::calcTextWidth(const std::string &text)
00067 {
00068     int w;
00069     TTF_SizeUTF8(m_ttfont, text.c_str(), &w, NULL);
00070     return w;
00071 }
00072 //-----------------------------------------------------------------
00073 /**
00074  * Render text with this color.
00075  * @param text utf-8 encoded text
00076  * @param color text color
00077  * @return new rendered surface
00078  * @throws TTFException when render fails
00079  * @throws SDLException when converting fails
00080  */
00081 SDL_Surface *
00082 Font::renderText(const std::string &text, const SDL_Color &color) const
00083 {
00084     const char *content = text.c_str();
00085     if (text.empty()) {
00086         content = " ";
00087         LOG_WARNING(ExInfo("empty text to render")
00088                 .addInfo("r", color.r)
00089                 .addInfo("g", color.g)
00090                 .addInfo("b", color.b));
00091     }
00092 
00093     SDL_Surface *raw_surface = TTF_RenderUTF8_Shaded(m_ttfont, content,
00094             color, m_bg);
00095     if (!raw_surface) {
00096         throw TTFException(ExInfo("RenderUTF8")
00097                 .addInfo("text", text));
00098     }
00099 
00100     //NOTE: at index 0 is bg color
00101     if (SDL_SetColorKey(raw_surface, SDL_SRCCOLORKEY, 0) < 0) {
00102         throw SDLException(ExInfo("SetColorKey"));
00103     }
00104 
00105     SDL_Surface *surface = SDL_DisplayFormat(raw_surface);
00106     if (!surface) {
00107         throw SDLException(ExInfo("DisplayFormat"));
00108     }
00109     SDL_FreeSurface(raw_surface);
00110 
00111     return surface;
00112 }
00113 //-----------------------------------------------------------------
00114 /**
00115  * Render text with black outline around font.
00116  * @param text utf-8 encoded text
00117  * @param color text color
00118  * @param outlineWidth outline width
00119  * @return new rendered surface
00120  */
00121 SDL_Surface *
00122 Font::renderTextOutlined(const std::string &text,
00123                 const SDL_Color &color, int outlineWidth) const
00124 {
00125     SDL_Surface *surface = renderText(" " + text + " ", color);
00126     SDL_Color black = {0, 0, 0, 255};
00127     Outline outline(black, outlineWidth);
00128 
00129     outline.drawOnColorKey(surface);
00130     return surface;
00131 }
00132 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2