00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pent_include.h"
00020
00021 #if defined(HAVE_SDL_SDL_TTF_H)
00022 #include <SDL/SDL_ttf.h>
00023 #else
00024 #include "SDL_ttf.h"
00025 #endif
00026
00027 #include "FontManager.h"
00028
00029 #include "Font.h"
00030 #include "GameData.h"
00031 #include "ShapeFont.h"
00032 #include "FontShapeArchive.h"
00033 #include "IDataSource.h"
00034 #include "FileSystem.h"
00035 #include "TTFont.h"
00036 #include "JPFont.h"
00037 #include "PaletteManager.h"
00038 #include "Palette.h"
00039 #include "SettingManager.h"
00040
00041 FontManager* FontManager::fontmanager = 0;
00042
00043 FontManager::FontManager(bool ttf_antialiasing_) : ttf_antialiasing(ttf_antialiasing_)
00044 {
00045 con.Print(MM_INFO, "Creating Font Manager...\n");
00046
00047 assert(fontmanager == 0);
00048 fontmanager = this;
00049
00050 SettingManager* settingman = SettingManager::get_instance();
00051 settingman->setDefault("ttf_highres", true);
00052
00053 TTF_Init();
00054 }
00055
00056 FontManager::~FontManager()
00057 {
00058 con.Print(MM_INFO, "Destroying Font Manager...\n");
00059
00060 resetGameFonts();
00061
00062 for (unsigned int i = 0; i < ttfonts.size(); ++i)
00063 delete ttfonts[i];
00064 ttfonts.clear();
00065
00066 std::map<TTFId, TTF_Font*>::iterator iter;
00067 for (iter = ttf_fonts.begin(); iter != ttf_fonts.end(); ++iter)
00068 TTF_CloseFont(iter->second);
00069 ttf_fonts.clear();
00070
00071 TTF_Quit();
00072
00073 assert(fontmanager == this);
00074 fontmanager = 0;
00075 }
00076
00077
00078 void FontManager::resetGameFonts()
00079 {
00080 for (unsigned int i = 0; i < overrides.size(); ++i)
00081 delete overrides[i];
00082 overrides.clear();
00083 }
00084
00085 Pentagram::Font* FontManager::getGameFont(unsigned int fontnum,
00086 bool allowOverride)
00087 {
00088 if (allowOverride && fontnum < overrides.size() && overrides[fontnum])
00089 return overrides[fontnum];
00090
00091 return GameData::get_instance()->getFonts()->getFont(fontnum);
00092 }
00093
00094 Pentagram::Font* FontManager::getTTFont(unsigned int fontnum)
00095 {
00096 if (fontnum >= ttfonts.size())
00097 return 0;
00098 return ttfonts[fontnum];
00099 }
00100
00101
00102 TTF_Font* FontManager::getTTF_Font(std::string filename, int pointsize)
00103 {
00104 TTFId id;
00105 id.filename = filename;
00106 id.pointsize = pointsize;
00107
00108 std::map<TTFId, TTF_Font*>::iterator iter;
00109 iter = ttf_fonts.find(id);
00110
00111 if (iter != ttf_fonts.end())
00112 return iter->second;
00113
00114 IDataSource* fontids;
00115 fontids = FileSystem::get_instance()->ReadFile("@data/" + filename);
00116 if (!fontids) {
00117 perr << "Failed to open TTF: @data/" << filename << std::endl;
00118 return 0;
00119 }
00120
00121
00122
00123 TTF_Font* font = TTF_OpenFontRW(fontids->getRWops(), 1, pointsize);
00124
00125 if (!font) {
00126 perr << "Failed to open TTF: @data/" << filename
00127 << ": " << TTF_GetError() << std::endl;
00128 return 0;
00129 }
00130
00131 ttf_fonts[id] = font;
00132
00133 #ifdef DEBUG
00134 pout << "Opened TTF: @data/" << filename << "." << std::endl;
00135 #endif
00136
00137 return font;
00138 }
00139
00140 void FontManager::setOverride(unsigned int fontnum, Pentagram::Font* override)
00141 {
00142 if (fontnum >= overrides.size())
00143 overrides.resize(fontnum+1);
00144
00145 if (overrides[fontnum])
00146 delete overrides[fontnum];
00147
00148 overrides[fontnum] = override;
00149 }
00150
00151
00152 bool FontManager::addTTFOverride(unsigned int fontnum, std::string filename,
00153 int pointsize, uint32 rgb, int bordersize,
00154 bool SJIS)
00155 {
00156 TTF_Font* f = getTTF_Font(filename, pointsize);
00157 if (!f)
00158 return false;
00159
00160 TTFont* font = new TTFont(f, rgb, bordersize, ttf_antialiasing, SJIS);
00161 SettingManager* settingman = SettingManager::get_instance();
00162 bool highres;
00163 settingman->get("ttf_highres", highres);
00164 font->setHighRes(highres);
00165
00166 setOverride(fontnum, font);
00167
00168 #ifdef DEBUG
00169 pout << "Added TTF override for font " << fontnum << std::endl;
00170 #endif
00171
00172 return true;
00173 }
00174
00175 bool FontManager::addJPOverride(unsigned int fontnum,
00176 unsigned int jpfont, uint32 rgb)
00177 {
00178 ShapeFont* jf = p_dynamic_cast<ShapeFont*>(GameData::get_instance()->getFonts()->getFont(jpfont));
00179 if (!jf)
00180 return false;
00181
00182 JPFont* font = new JPFont(jf, fontnum);
00183
00184 setOverride(fontnum, font);
00185
00186 PaletteManager* palman = PaletteManager::get_instance();
00187 PaletteManager::PalIndex fontpal = static_cast<PaletteManager::PalIndex>
00188 (PaletteManager::Pal_JPFontStart+fontnum);
00189 palman->duplicate(PaletteManager::Pal_Game, fontpal);
00190 Pentagram::Palette* pal = palman->getPalette(fontpal);
00191
00192
00193
00194 for (int i = 1; i < 4; ++i) {
00195 pal->palette[3*i+0] = (rgb >> 16) & 0xFF;
00196 pal->palette[3*i+1] = (rgb >> 8) & 0xFF;
00197 pal->palette[3*i+2] = (rgb) & 0xFF;
00198 }
00199 palman->updatedFont(fontpal);
00200
00201 #ifdef DEBUG
00202 pout << "Added JP override for font " << fontnum << std::endl;
00203 #endif
00204
00205 return true;
00206 }
00207
00208
00209 bool FontManager::loadTTFont(unsigned int fontnum, std::string filename,
00210 int pointsize, uint32 rgb, int bordersize)
00211 {
00212 TTF_Font* f = getTTF_Font(filename, pointsize);
00213 if (!f)
00214 return false;
00215
00216 TTFont* font = new TTFont(f, rgb, bordersize, ttf_antialiasing, false);
00217
00218
00219 SettingManager* settingman = SettingManager::get_instance();
00220 bool highres;
00221 settingman->get("ttf_highres", highres);
00222 font->setHighRes(highres);
00223
00224 if (fontnum >= ttfonts.size())
00225 ttfonts.resize(fontnum+1);
00226
00227 if (ttfonts[fontnum])
00228 delete ttfonts[fontnum];
00229
00230 ttfonts[fontnum] = font;
00231
00232 return true;
00233 }
00234