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
00033 #include <ft2build.h>
00034 #include FT_FREETYPE_H
00035
00036
00037
00038 namespace CEGUI
00039 {
00040
00041
00042
00043
00044 template<> FontManager* Singleton<FontManager>::ms_Singleton = NULL;
00045
00046
00047
00048
00049
00050
00051 FontManager::FontManager(void)
00052 {
00053 d_implData = new FontManagerImplData;
00054
00055 if (FT_Init_FreeType(&d_implData->d_ftlib))
00056 {
00057 throw GenericException((utf8*)"FontManager::FontManager - Failed to initialise the FreeType library.");
00058 }
00059
00060 Logger::getSingleton().logEvent((utf8*)"CEGUI::FontManager singleton created.");
00061 }
00062
00063
00064
00065
00066
00067 FontManager::~FontManager(void)
00068 {
00069 Logger::getSingleton().logEvent((utf8*)"---- Begining cleanup of Font system ----");
00070 destroyAllFonts();
00071
00072 FT_Done_FreeType(d_implData->d_ftlib);
00073 delete d_implData;
00074
00075 Logger::getSingleton().logEvent((utf8*)"CEGUI::FontManager singleton destroyed.");
00076 }
00077
00078
00079
00080
00081
00082 Font* FontManager::createFont(const String& filename, const String& resourceGroup)
00083 {
00084 Logger::getSingleton().logEvent((utf8*)"Attempting to create Font from the information specified in file '" + filename + "'.");
00085
00086 Font* temp = new Font(filename, resourceGroup, new Font::FontImplData(d_implData->d_ftlib));
00087
00088 String name = temp->getName();
00089
00090 if (isFontPresent(name))
00091 {
00092 delete temp;
00093
00094 throw AlreadyExistsException((utf8*)"FontManager::createFont - A font named '" + name + "' already exists.");
00095 }
00096
00097 d_fonts[name] = temp;
00098
00099 return temp;
00100 }
00101
00102
00103
00104
00105
00106 Font* FontManager::createFont(const String& name, const String& fontname, uint size, uint flags, const String& resourceGroup)
00107 {
00108 char strbuf[16];
00109 sprintf(strbuf, "%d", size);
00110 Logger::getSingleton().logEvent((utf8*)"Attempting to create Font '" + name + "' using the font file '" + fontname + "' and a size of " + strbuf + ".");
00111
00112
00113 if (isFontPresent(name))
00114 {
00115 throw AlreadyExistsException((utf8*)"FontManager::createFont - A font named '" + name + "' already exists.");
00116 }
00117
00118 Font* temp = new Font(name, fontname, resourceGroup, size, flags, new Font::FontImplData(d_implData->d_ftlib));
00119 d_fonts[name] = temp;
00120
00121 return temp;
00122 }
00123
00124
00125
00126
00127
00128 void FontManager::destroyFont(const String& name)
00129 {
00130 FontRegistry::iterator pos = d_fonts.find(name);
00131
00132 if (pos != d_fonts.end())
00133 {
00134 String tmpName(name);
00135
00136 delete pos->second;
00137 d_fonts.erase(pos);
00138
00139 Logger::getSingleton().logEvent((utf8*)"Font '" + tmpName +"' has been destroyed.");
00140 }
00141
00142 }
00143
00144
00145
00146
00147
00148 void FontManager::destroyFont(Font* font)
00149 {
00150 if (font != NULL)
00151 {
00152 destroyFont(font->getName());
00153 }
00154
00155 }
00156
00157
00158
00159
00160
00161 void FontManager::destroyAllFonts(void)
00162 {
00163 while (!d_fonts.empty())
00164 {
00165 destroyFont(d_fonts.begin()->first);
00166 }
00167
00168 }
00169
00170
00171
00172
00173
00174 bool FontManager::isFontPresent(const String& name) const
00175 {
00176 return (d_fonts.find(name) != d_fonts.end());
00177 }
00178
00179
00180
00181
00182
00183 Font* FontManager::getFont(const String& name) const
00184 {
00185 FontRegistry::const_iterator pos = d_fonts.find(name);
00186
00187 if (pos == d_fonts.end())
00188 {
00189 throw UnknownObjectException("FontManager::getFont - A Font object with the specified name '" + name +"' does not exist within the system");
00190 }
00191
00192 return pos->second;
00193 }
00194
00195
00196
00197
00198
00199
00200 void FontManager::notifyScreenResolution(const Size& size)
00201 {
00202
00203 FontRegistry::iterator pos = d_fonts.begin(), end = d_fonts.end();
00204
00205 for (; pos != end; ++pos)
00206 {
00207 pos->second->notifyScreenResolution(size);
00208 }
00209
00210 }
00211
00212
00213 FontManager& FontManager::getSingleton(void)
00214 {
00215 return Singleton<FontManager>::getSingleton();
00216 }
00217
00218
00219 FontManager* FontManager::getSingletonPtr(void)
00220 {
00221 return Singleton<FontManager>::getSingletonPtr();
00222 }
00223
00224
00225
00226
00227
00228
00229 FontManager::FontIterator FontManager::getIterator(void) const
00230 {
00231 return FontIterator(d_fonts.begin(), d_fonts.end());
00232 }
00233
00234 }