Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIFontManager.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIFontManager.cpp
00003         created:        21/2/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements the FontManager class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 // Start of CEGUI namespace section
00039 namespace CEGUI
00040 {
00041 /*************************************************************************
00042         Static Data Definitions
00043 *************************************************************************/
00044 // singleton instance pointer
00045 template<> FontManager* Singleton<FontManager>::ms_Singleton    = NULL;
00046 
00047 
00048 
00049 /*************************************************************************
00050         constructor
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         Destructor
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         Create a font from a definition file
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     // if this was the first font created, set it as the default font
00101     if (d_fonts.size() == 1)
00102     {
00103         System::getSingleton().setDefaultFont(temp);
00104     }
00105 
00106         return temp; 
00107 }
00108 
00109 
00110 /*************************************************************************
00111         Create a font from an installed OS font
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         // first ensure name uniqueness
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     // if this was the first font created, set it as the default font
00129     if (d_fonts.size() == 1)
00130     {
00131         System::getSingleton().setDefaultFont(temp);
00132     }
00133 
00134         return temp; 
00135 }
00136 
00137 
00138 /*************************************************************************
00139         Destroy the named font
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         Destroys the given Font object
00160 *************************************************************************/
00161 void FontManager::destroyFont(Font* font)
00162 {
00163         if (font != NULL)
00164         {
00165                 destroyFont(font->getName());
00166         }
00167 
00168 }
00169 
00170 
00171 /*************************************************************************
00172         Destroys all Font objects registered in the system
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         Check to see if a font is available
00186 *************************************************************************/
00187 bool FontManager::isFontPresent(const String& name) const
00188 {
00189         return (d_fonts.find(name) != d_fonts.end());
00190 }
00191 
00192 
00193 /*************************************************************************
00194         Return a pointer to the named font
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         Notify the FontManager of the current (usually new) display
00211         resolution.
00212 *************************************************************************/
00213 void FontManager::notifyScreenResolution(const Size& size)
00214 {
00215         // notify all attached Font objects of the change in resolution
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         Return a FontManager::FontIterator object to iterate over the
00240         available Font objects.
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     // output xml header
00253     out_stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
00254 
00255     // output font data
00256     font->writeXMLToStream(out_stream);
00257 }
00258 
00259 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:31 2005 for Crazy Eddies GUI System by  doxygen 1.4.3