1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ultima/nuvie/core/nuvie_defs.h"
24 #include "ultima/nuvie/conf/configuration.h"
25 #include "ultima/nuvie/gui/gui.h"
26 #include "ultima/nuvie/files/nuvie_io_file.h"
27 #include "ultima/nuvie/files/u6_lib_n.h"
28 #include "ultima/nuvie/files/nuvie_bmp_file.h"
29 #include "ultima/nuvie/fonts/font_manager.h"
30 #include "ultima/nuvie/fonts/font.h"
31 #include "ultima/nuvie/fonts/conv_font.h"
32 #include "ultima/nuvie/fonts/u6_font.h"
33 #include "ultima/nuvie/fonts/wou_font.h"
34 #include "ultima/nuvie/misc/u6_misc.h"
35 
36 namespace Ultima {
37 namespace Nuvie {
38 
FontManager(Configuration * cfg)39 FontManager::FontManager(Configuration *cfg) {
40 	config = cfg;
41 	num_fonts = 0;
42 
43 	conv_font = NULL;
44 	conv_garg_font = NULL;
45 	conv_font_data = NULL;
46 	conv_font_widths = NULL;
47 }
48 
~FontManager()49 FontManager::~FontManager() {
50 	Std::vector<Font *>::iterator font;
51 
52 	for (font = fonts.begin(); font != fonts.end();) {
53 		delete *font;
54 		font++;
55 	}
56 	if (conv_font) {
57 		delete conv_font;
58 	}
59 	if (conv_garg_font) {
60 		delete conv_garg_font;
61 	}
62 	if (conv_font_data) {
63 		free(conv_font_data);
64 	}
65 	if (conv_font_widths) {
66 		free(conv_font_widths);
67 	}
68 }
69 
init(nuvie_game_t game_type)70 bool FontManager::init(nuvie_game_t game_type) {
71 	initConvFonts(game_type);
72 
73 	if (game_type == NUVIE_GAME_U6)
74 		return initU6();
75 
76 	return initWOUSystemFont();
77 }
78 
initU6()79 bool FontManager::initU6() {
80 	U6Font *font;
81 	unsigned char *font_data;
82 	Std::string filename;
83 	NuvieIOFileRead u6_ch;
84 
85 	config_get_path(config, "u6.ch", filename);
86 
87 	if (u6_ch.open(filename) == false)
88 		return false;
89 
90 	font_data = u6_ch.readAll();
91 	if (font_data == NULL)
92 		return false;
93 
94 // english font
95 	font = new U6Font();
96 	font->init(font_data, 128, 0);
97 	fonts.push_back(font);
98 	num_fonts++;
99 
100 // runic & gargoyle font
101 	font = new U6Font();
102 	font->init(&font_data[128 * 8], 128, 0);
103 	fonts.push_back(font);
104 	num_fonts++;
105 
106 	free(font_data);
107 	return true;
108 }
109 
initWOU(Std::string filename)110 bool FontManager::initWOU(Std::string filename) {
111 	WOUFont *font;
112 	Std::string path;
113 	U6Lib_n lib_file;
114 
115 	config_get_path(config, filename, path);
116 
117 	lib_file.open(path, 4, NUVIE_GAME_MD); //can be either SE or MD just as long as it isn't set to U6 type.
118 
119 	font = new WOUFont();
120 	unsigned char *buf = lib_file.get_item(0);
121 	font->initWithBuffer(buf, lib_file.get_item_size(0)); //buf will be freed by ~Font()
122 	fonts.push_back(font);
123 	num_fonts++;
124 	/*
125 	     font = new Font();
126 	     font->init(path.c_str());
127 	     fonts.push_back(font);
128 	     num_fonts++;
129 	*/
130 	return true;
131 }
132 
initWOUSystemFont()133 bool FontManager::initWOUSystemFont() {
134 	U6Font *font;
135 	Std::string path;
136 	U6Lib_n lib_file;
137 
138 	config_get_path(config, "system.lzc", path);
139 
140 	lib_file.open(path, 4, NUVIE_GAME_MD);
141 
142 	font = new U6Font();
143 	unsigned char *buf = lib_file.get_item(3);
144 	font->init(buf, 128, 0);
145 	font->setDefaultColor(FONT_COLOR_WOU_NORMAL);
146 	font->setDefaultHighlightColor(FONT_COLOR_WOU_HIGHLIGHT);
147 	free(buf);
148 	fonts.push_back(font);
149 	num_fonts++;
150 
151 	return true;
152 }
153 
initConvFonts(nuvie_game_t game_type)154 bool FontManager::initConvFonts(nuvie_game_t game_type) {
155 	char filename[7]; // u6.bmp\0 or u6.dat\0
156 	Std::string datadir = GUI::get_gui()->get_data_dir();
157 	Std::string path;
158 
159 	build_path(datadir, "images", path);
160 	datadir = path;
161 	build_path(datadir, "gumps", path);
162 	datadir = path;
163 	build_path(datadir, "fonts", path);
164 	datadir = path;
165 
166 	Std::string imagefile;
167 	sprintf(filename, "%s.bmp", get_game_tag(Game::get_game()->get_game_type()));
168 
169 	build_path(datadir, filename, imagefile);
170 
171 	NuvieBmpFile bmp;
172 
173 	bmp.load(imagefile);
174 
175 	conv_font_data = bmp.getRawIndexedDataCopy();
176 
177 	Std::string widthfile;
178 	sprintf(filename, "%s.dat", get_game_tag(Game::get_game()->get_game_type()));
179 
180 	build_path(datadir, filename, widthfile);
181 
182 	NuvieIOFileRead datfile;
183 	datfile.open(widthfile);
184 	uint32 bytes_read;
185 	conv_font_widths = datfile.readBuf(256, &bytes_read);
186 	datfile.close();
187 
188 	conv_font = new ConvFont();
189 	((ConvFont *)conv_font)->init(conv_font_data, conv_font_widths, 256, 0);
190 
191 	if (game_type == NUVIE_GAME_U6) {
192 		conv_garg_font = new ConvFont();
193 		((ConvFont *)conv_garg_font)->init(conv_font_data, conv_font_widths, 256, 128);
194 	}
195 
196 	return true;
197 }
198 
get_font(uint16 font_number)199 Font *FontManager::get_font(uint16 font_number) {
200 	if (num_fonts > 0 && font_number < num_fonts)
201 		return fonts[font_number]; //fonts.at(font_number);
202 
203 	return NULL;
204 }
205 
206 } // End of namespace Nuvie
207 } // End of namespace Ultima
208