1 /*
2 Copyright (C) 2005 Matthias Braun <matze@braunis.de>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /**
20 * @author Matthias Braun
21 * @file FontManager.cpp
22 */
23
24 #include <config.h>
25
26 #include "FontManager.hpp"
27
28 #include <SDL_ttf.h>
29 #include <stdexcept>
30 #include <sstream>
31
32 #include "tinygettext/gettext.hpp"
33 #include "PhysfsStream/PhysfsSDL.hpp"
34
35 FontManager* fontManager = 0;
36
FontManager()37 FontManager::FontManager()
38 {
39 }
40
~FontManager()41 FontManager::~FontManager()
42 {
43 for(Fonts::iterator i = fonts.begin(); i != fonts.end(); ++i)
44 TTF_CloseFont(i->second);
45 }
46
47 TTF_Font*
getFont(Style style)48 FontManager::getFont(Style style)
49 {
50 FontInfo info;
51 info.name = style.font_family;
52 info.fontsize = (int) style.font_size;
53 info.fontstyle = 0;
54 if(style.italic)
55 info.fontstyle |= TTF_STYLE_ITALIC;
56 if(style.bold)
57 info.fontstyle |= TTF_STYLE_BOLD;
58
59 Fonts::iterator i = fonts.find(info);
60 if(i != fonts.end())
61 return i->second;
62
63 TTF_Font* font = 0;
64
65 // If there a special font for the current language use it.
66 std::string fontfile = "fonts/" + info.name + "-" + dictionaryManager->get_language() + ".ttf";
67 try{
68 font = TTF_OpenFontRW(getPhysfsSDLRWops(fontfile), 1, info.fontsize);
69 } catch(std::exception& ){
70 // No special font found? Use default font then.
71 fontfile = "fonts/" + info.name + ".ttf";
72 font = TTF_OpenFontRW(getPhysfsSDLRWops(fontfile), 1, info.fontsize);
73 }
74 if(!font) {
75 std::stringstream msg;
76 msg << "Error opening font '" << fontfile
77 << "': " << SDL_GetError();
78 throw std::runtime_error(msg.str());
79 }
80 if(info.fontstyle != 0)
81 TTF_SetFontStyle(font, info.fontstyle);
82
83 fonts.insert(std::make_pair(info, font));
84 return font;
85 }
86
87