1 /*
2  *
3  *   Copyright (c) 1994, 2002, 2003 Johannes Prix
4  *   Copyright (c) 1994, 2002 Reinhard Prix
5  *   Copyright (c) 2004-2010 Arthur Huillet
6  *   Copyright (c) 2015 Samuel Degrande
7  *
8  *
9  *  This file is part of Freedroid
10  *
11  *  Freedroid is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  Freedroid is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with Freedroid; see the file COPYING. If not, write to the
23  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  *  MA  02111-1307  USA
25  */
26 
27 /**
28  * \file font.c
29  * \brief This file contains the functions used to process fonts, independently
30  *        of the font implementation by itself.
31  *        Currently we use bitmaps fonts (see BFont.c)
32  */
33 
34 #define _font_c
35 
36 #include "system.h"
37 
38 #include "defs.h"
39 #include "struct.h"
40 #include "global.h"
41 #include "proto.h"
42 #include "BFont.h"
43 
44 static struct {
45 	struct font **font_ref;
46 	char *filename;
47 	struct font font;
48 } fonts_def[] = {
49 		{ &Para_Font,        "parafont.png",     { 0, -2, NULL } },
50 		{ &Menu_Font,        "cpuFont.png",      { 0, -4, NULL } },
51 		{ &Messagevar_Font,  "small_white.png",  { 0,  0, NULL } },
52 		{ &Messagestat_Font, "small_blue.png",   { 0,  0, NULL } },
53 		{ &Red_Font,         "font05_red.png",   { 0, -2, NULL } },
54 		{ &Blue_Font,        "font05_white.png", { 0, -2, NULL } },
55 		{ &FPS_Display_Font, "font05.png",       { 0, -2, NULL } },
56 		{ &Messagered_Font,  "small_red.png",    { 0,  0, NULL } }
57 };
58 
59 /* Current font */
60 static struct font *_current_font;
61 
62 /**
63  * This function should load all the fonts we'll be using via the SDL
64  * BFont library in FreedroidRPG.
65  */
init_fonts(void)66 void init_fonts(void)
67 {
68 	int i;
69 	char fpath[PATH_MAX];
70 
71 	for (i = 0; i < sizeof(fonts_def)/sizeof(fonts_def[0]); i++) {
72 		if (!find_encoded_file(fonts_def[i].filename, FONT_DIR, fpath, PLEASE_INFORM | IS_FATAL)) {
73 			error_message(__FUNCTION__, "A Bfont file was not found (filename: %s - encoding: %s).",
74 					PLEASE_INFORM, fonts_def[i].filename, lang_get_encoding());
75 			continue;
76 		}
77 
78 		int rtn = load_bfont(fpath, &fonts_def[i].font);
79 		if (rtn == FALSE) {
80 			error_message(__FUNCTION__, "A Bfont file could not be loaded (filename: %s - encoding: %s).",
81 					PLEASE_INFORM | IS_FATAL, fonts_def[i].filename, lang_get_encoding());
82 			return;
83 		}
84 
85 		*fonts_def[i].font_ref = &fonts_def[i].font;
86 	}
87 }
88 
free_fonts(void)89 void free_fonts(void)
90 {
91 	int i;
92 
93 	for (i = 0; i < sizeof(fonts_def)/sizeof(fonts_def[0]); i++) {
94 		free_bfont(&fonts_def[i].font);
95 		*fonts_def[i].font_ref = NULL;
96 	}
97 }
98 
99 /**
100  * Set the current font
101  */
set_current_font(struct font * font)102 void set_current_font(struct font* font)
103 {
104 	_current_font = font;
105 }
106 
107 /**
108  * Returns the pointer to the current font structure in use
109  */
get_current_font(void)110 struct font *get_current_font(void)
111 {
112 	return _current_font;
113 }
114 
115 /**
116  * Get letter-spacing for specified font.
117  *
118  * Letter-spacing refers to the overall spacing of a word or block of text
119  * affecting its overall density and texture.
120  */
get_letter_spacing(struct font * font)121 int get_letter_spacing(struct font *font)
122 {
123 	return font->letter_spacing;
124 }
125 
126 /**
127  * Return the font height
128  */
get_font_height(struct font * font)129 int get_font_height(struct font *font)
130 {
131 	return font->height;
132 }
133 
134 /**
135  * Handle font switching on special characters (\x) or formatting tags (aka bbcodes).
136  * Returns 1 if the font was changed and 0 if it was not.
137  */
handle_switch_font_char(char ** ptr)138 int handle_switch_font_char(char **ptr)
139 {
140 	if (**ptr == '[' && *(*ptr+2) != ']')
141 		return FALSE;
142 
143 	int index = 0;
144 	int incr = 1;
145 
146 	if (**ptr == '[') {
147 		(*ptr)++;
148 		index = 2;
149 		incr = 2;
150 	}
151 
152 	if (**ptr == font_switchto_red[index]) {
153 		set_current_font(Red_Font);
154 		(*ptr) += incr;
155 		return TRUE;
156 	} else if (**ptr == font_switchto_blue[index]) {
157 		set_current_font(Blue_Font);
158 		(*ptr) += incr;
159 		return TRUE;
160 	} else if (**ptr == font_switchto_neon[index]) {
161 		set_current_font(FPS_Display_Font);
162 		(*ptr) += incr;
163 		return TRUE;
164 	} else if (**ptr == font_switchto_msgstat[index]) {
165 		set_current_font(Messagestat_Font);
166 		(*ptr) += incr;
167 		return TRUE;
168 	} else if (**ptr == font_switchto_msgvar[index]) {
169 		set_current_font(Messagevar_Font);
170 		(*ptr) += incr;
171 		return TRUE;
172 	}
173 
174 	return FALSE;
175 }
176 
put_string_centered(struct font * font,int y,const char * text)177 void put_string_centered(struct font *font, int y, const char *text)
178 {
179 	put_string(font, Screen->w / 2 - text_width(font, text) / 2, y, text);
180 }
181 
put_string_right(struct font * font,int y,const char * text)182 void put_string_right(struct font *font, int y, const char *text)
183 {
184 	put_string(font, Screen->w - text_width(font, text) - 13, y, text);
185 }
186 
put_string_left(struct font * font,int y,const char * text)187 void put_string_left(struct font *font, int y, const char *text)
188 {
189 	put_string(font, 13, y, text);
190 }
191 
192 #undef _font_c
193