00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "CEGUIFontManager.h"
00027 #include "CEGUIExceptions.h"
00028 #include "CEGUILogger.h"
00029 #include "CEGUIFont.h"
00030 #include "CEGUIFontManager_implData.h"
00031 #include "CEGUIFont_implData.h"
00032 #include "CEGUISystem.h"
00033
00034 #include <ft2build.h>
00035 #include FT_FREETYPE_H
00036
00037
00038
00039 namespace CEGUI
00040 {
00041
00042
00043
00044
00045 template<> FontManager* Singleton<FontManager>::ms_Singleton = NULL;
00046
00047
00048
00049
00050
00051
00052 FontManager::FontManager(void)
00053 {
00054 d_implData = new FontManagerImplData;
00055
00056 if (FT_Init_FreeType(&d_implData->d_ftlib))
00057 {
00058 throw GenericException((utf8*)"FontManager::FontManager - Failed to initialise the FreeType library.");
00059 }
00060
00061 Logger::getSingleton().logEvent((utf8*)"CEGUI::FontManager singleton created.");
00062 }
00063
00064
00065
00066
00067
00068 FontManager::~FontManager(void)
00069 {
00070 Logger::getSingleton().logEvent((utf8*)"---- Begining cleanup of Font system ----");
00071 destroyAllFonts();
00072
00073 FT_Done_FreeType(d_implData->d_ftlib);
00074 delete d_implData;
00075
00076 Logger::getSingleton().logEvent((utf8*)"CEGUI::FontManager singleton destroyed.");
00077 }
00078
00079
00080
00081
00082
00083 Font* FontManager::createFont(const String& filename, const String& resourceGroup)
00084 {
00085 Logger::getSingleton().logEvent((utf8*)"Attempting to create Font from the information specified in file '" + filename + "'.");
00086
00087 Font* temp = new Font(filename, resourceGroup, new Font::FontImplData(d_implData->d_ftlib));
00088
00089 String name = temp->getName();
00090
00091 if (isFontPresent(name))
00092 {
00093 delete temp;
00094
00095 throw AlreadyExistsException((utf8*)"FontManager::createFont - A font named '" + name + "' already exists.");
00096 }
00097
00098 d_fonts[name] = temp;
00099
00100
00101 if (d_fonts.size() == 1)
00102 {
00103 System::getSingleton().setDefaultFont(temp);
00104 }
00105
00106 return temp;
00107 }
00108
00109
00110
00111
00112
00113 Font* FontManager::createFont(const String& name, const String& fontname, uint size, uint flags, const String& resourceGroup)
00114 {
00115 char strbuf[16];
00116 sprintf(strbuf, "%d", size);
00117 Logger::getSingleton().logEvent((utf8*)"Attempting to create Font '" + name + "' using the font file '" + fontname + "' and a size of " + strbuf + ".");
00118
00119
00120 if (isFontPresent(name))
00121 {
00122 throw AlreadyExistsException((utf8*)"FontManager::createFont - A font named '" + name + "' already exists.");
00123 }
00124
00125 Font* temp = new Font(name, fontname, resourceGroup, size, flags, new Font::FontImplData(d_implData->d_ftlib));
00126 d_fonts[name] = temp;
00127
00128
00129 if (d_fonts.size() == 1)
00130 {
00131 System::getSingleton().setDefaultFont(temp);
00132 }
00133
00134 return temp;
00135 }
00136
00137
00138
00139
00140
00141 void FontManager::destroyFont(const String& name)
00142 {
00143 FontRegistry::iterator pos = d_fonts.find(name);
00144
00145 if (pos != d_fonts.end())
00146 {
00147 String tmpName(name);
00148
00149 delete pos->second;
00150 d_fonts.erase(pos);
00151
00152 Logger::getSingleton().logEvent((utf8*)"Font '" + tmpName +"' has been destroyed.");
00153 }
00154
00155 }
00156
00157
00158
00159
00160
00161 void FontManager::destroyFont(Font* font)
00162 {
00163 if (font != NULL)
00164 {
00165 destroyFont(font->getName());
00166 }
00167
00168 }
00169
00170
00171
00172
00173
00174 void FontManager::destroyAllFonts(void)
00175 {
00176 while (!d_fonts.empty())
00177 {
00178 destroyFont(d_fonts.begin()->first);
00179 }
00180
00181 }
00182
00183
00184
00185
00186
00187 bool FontManager::isFontPresent(const String& name) const
00188 {
00189 return (d_fonts.find(name) != d_fonts.end());
00190 }
00191
00192
00193
00194
00195
00196 Font* FontManager::getFont(const String& name) const
00197 {
00198 FontRegistry::const_iterator pos = d_fonts.find(name);
00199
00200 if (pos == d_fonts.end())
00201 {
00202 throw UnknownObjectException("FontManager::getFont - A Font object with the specified name '" + name +"' does not exist within the system");
00203 }
00204
00205 return pos->second;
00206 }
00207
00208
00209
00210
00211
00212
00213 void FontManager::notifyScreenResolution(const Size& size)
00214 {
00215
00216 FontRegistry::iterator pos = d_fonts.begin(), end = d_fonts.end();
00217
00218 for (; pos != end; ++pos)
00219 {
00220 pos->second->notifyScreenResolution(size);
00221 }
00222
00223 }
00224
00225
00226 FontManager& FontManager::getSingleton(void)
00227 {
00228 return Singleton<FontManager>::getSingleton();
00229 }
00230
00231
00232 FontManager* FontManager::getSingletonPtr(void)
00233 {
00234 return Singleton<FontManager>::getSingletonPtr();
00235 }
00236
00237
00238
00239
00240
00241
00242 FontManager::FontIterator FontManager::getIterator(void) const
00243 {
00244 return FontIterator(d_fonts.begin(), d_fonts.end());
00245 }
00246
00247
00248 void FontManager::writeFontToStream(const String& name, OutStream& out_stream) const
00249 {
00250 const Font* font = getFont(name);
00251
00252
00253 out_stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
00254
00255
00256 font->writeXMLToStream(out_stream);
00257 }
00258
00259 }