1 /*
2  * client/Font.cpp
3  *
4  * This file is part of Leges Motus, a networked, 2D shooter set in zero gravity.
5  *
6  * Copyright 2009-2010 Andrew Ayer, Nathan Partlan, Jeffrey Pfau
7  *
8  * Leges Motus is free and open source software.  You may redistribute it and/or
9  * modify it under the terms of version 2, or (at your option) version 3, of the
10  * GNU General Public License (GPL), as published by the Free Software Foundation.
11  *
12  * Leges Motus is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14  * PARTICULAR PURPOSE.  See the full text of the GNU General Public License for
15  * further detail.
16  *
17  * For a full copy of the GNU General Public License, please see the COPYING file
18  * in the root of the source code tree.  You may also retrieve a copy from
19  * <http://www.gnu.org/licenses/gpl-2.0.txt>, or request a copy by writing to the
20  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307  USA
22  *
23  */
24 
25 #include "Font.hpp"
26 #include "common/Exception.hpp"
27 
28 using namespace LM;
29 using namespace std;
30 
Font(const char * filename,int size)31 Font::Font(const char* filename, int size) {
32 	if (!TTF_WasInit() && TTF_Init() == -1) {
33 		throw Exception("Could not init SDL_TTF!");
34 	}
35 	m_font = TTF_OpenFont(filename, size);
36 	if (!m_font) {
37 		throw Exception("Could not load font!");
38 	}
39 }
40 
~Font()41 Font::~Font() {
42 	if (m_font != NULL) {
43 		TTF_CloseFont(m_font);
44 	}
45 }
46 
set_font_style(bool bold,bool italic)47 void Font::set_font_style(bool bold, bool italic) {
48 	if (bold && italic) {
49 		TTF_SetFontStyle(m_font, TTF_STYLE_BOLD|TTF_STYLE_ITALIC);
50 	} else if (bold) {
51 		TTF_SetFontStyle(m_font, TTF_STYLE_BOLD);
52 	} else if (italic) {
53 		TTF_SetFontStyle(m_font, TTF_STYLE_ITALIC);
54 	} else {
55 		TTF_SetFontStyle(m_font, TTF_STYLE_NORMAL);
56 	}
57 }
58 
render_string(const string & text,const ConvolveKernel * kernel)59 Sprite* Font::render_string(const string& text, const ConvolveKernel* kernel) {
60 	SDL_Color white = { 255, 255, 255, 0 };
61 	SDL_Surface* rendered = TTF_RenderUTF8_Blended(m_font, text.c_str(), white);
62 	if (rendered == NULL) {
63 		return NULL;
64 	}
65 	if (kernel != NULL) {
66 		SDL_Surface* convolved = kernel->convolve(rendered);
67 		SDL_FreeSurface(rendered);
68 		rendered = convolved;
69 	}
70 	Sprite* text_sprite = new Sprite(rendered);
71 	SDL_FreeSurface(rendered);
72 	return text_sprite;
73 }
74 
size_string(const string & text,int * w,int * h)75 bool Font::size_string(const string& text, int* w, int* h) {
76 	return TTF_SizeText(m_font, text.c_str(), w, h) == 0;
77 }
78 
line_skip() const79 int Font::line_skip() const {
80 	return TTF_FontLineSkip(m_font);
81 }
82 
ascent() const83 int Font::ascent() const {
84 	return TTF_FontAscent(m_font);
85 }
86 
descent() const87 int Font::descent() const {
88 	return TTF_FontDescent(m_font);
89 }
90 
max_height() const91 int Font::max_height() const {
92 	return TTF_FontHeight(m_font);
93 }
94