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

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 
00033 #include <ft2build.h>
00034 #include FT_FREETYPE_H
00035 
00036 
00037 // Start of CEGUI namespace section
00038 namespace CEGUI
00039 {
00040 /*************************************************************************
00041         Static Data Definitions
00042 *************************************************************************/
00043 // singleton instance pointer
00044 template<> FontManager* Singleton<FontManager>::ms_Singleton    = NULL;
00045 
00046 
00047 
00048 /*************************************************************************
00049         constructor
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         Destructor
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         Create a font from a definition file
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         Create a font from an installed OS font
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         // first ensure name uniqueness
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         Destroy the named font
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         Destroys the given Font object
00147 *************************************************************************/
00148 void FontManager::destroyFont(Font* font)
00149 {
00150         if (font != NULL)
00151         {
00152                 destroyFont(font->getName());
00153         }
00154 
00155 }
00156 
00157 
00158 /*************************************************************************
00159         Destroys all Font objects registered in the system
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         Check to see if a font is available
00173 *************************************************************************/
00174 bool FontManager::isFontPresent(const String& name) const
00175 {
00176         return (d_fonts.find(name) != d_fonts.end());
00177 }
00178 
00179 
00180 /*************************************************************************
00181         Return a pointer to the named font
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         Notify the FontManager of the current (usually new) display
00198         resolution.
00199 *************************************************************************/
00200 void FontManager::notifyScreenResolution(const Size& size)
00201 {
00202         // notify all attached Font objects of the change in resolution
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         Return a FontManager::FontIterator object to iterate over the
00227         available Font objects.
00228 *************************************************************************/
00229 FontManager::FontIterator FontManager::getIterator(void) const
00230 {
00231         return FontIterator(d_fonts.begin(), d_fonts.end());
00232 }
00233 
00234 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:06 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1