Something went wrong. Try again.
Massive multiplayer game aiming to create a collaborative musical environment.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298/*********************************************************************** filename: CEGUIFontManager.cpp created: 21/2/2004 author: Paul D Turner
purpose: Implements the FontManager class*************************************************************************//*************************************************************************** * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. ***************************************************************************/#include "CEGUIFontManager.h"#include "CEGUIExceptions.h"#include "CEGUILogger.h"#include "CEGUIFont.h"#include "CEGUISystem.h"#include "CEGUIXMLParser.h"#include "CEGUIFont_xmlHandler.h"#include "CEGUIFreeTypeFont.h"#include "CEGUIPixmapFont.h"
// Start of CEGUI namespace sectionnamespace CEGUI{
/************************************************************************* Static Data Definitions*************************************************************************/
// Font schema filenamestatic const String FontSchemaName ("Font.xsd");static const String FontTypeFreeType ("FreeType");static const String FontTypePixmap ("Pixmap");
// singleton instance pointertemplate<> FontManager* Singleton<FontManager>::ms_Singleton = 0;
/************************************************************************* constructor*************************************************************************/FontManager::FontManager(void){ char addr_buff[32]; sprintf(addr_buff, "(%p)", static_cast<void*>(this)); Logger::getSingleton().logEvent("CEGUI::FontManager singleton created. " + String(addr_buff));}
/************************************************************************* Destructor*************************************************************************/FontManager::~FontManager(void){ Logger::getSingleton().logEvent("---- Begining cleanup of Font system ----"); destroyAllFonts();
char addr_buff[32]; sprintf(addr_buff, "(%p)", static_cast<void*>(this)); Logger::getSingleton().logEvent("CEGUI::FontManager singleton destroyed. " + String(addr_buff));}
/************************************************************************* Create a font from a definition file*************************************************************************/Font* FontManager::createFont(const String& filename, const String& resourceGroup){ if (filename.empty ()) throw InvalidRequestException ("FontManager::createFont - Filename supplied for Font loading must be valid");
Logger::getSingleton ().logEvent ("Attempting to create Font from the information specified in file '" + filename + "'.");
// create handler object Font_xmlHandler handler;
// do parse (which uses handler to create actual data) try { System::getSingleton ().getXMLParser ()->parseXMLFile ( handler, filename, FontSchemaName, resourceGroup.empty () ? Font::getDefaultResourceGroup () : resourceGroup); } catch (...) { if (handler.d_font) destroyFont (handler.d_font->getProperty ("Name"));
Logger::getSingleton ().logEvent ("FontManager::createFont - loading of Font from file '" + filename +"' failed.", Errors); throw; }
// if this was the first font created, set it as the default font if (d_fonts.size () == 1) System::getSingleton ().setDefaultFont (handler.d_font);
return handler.d_font;}
/************************************************************************* Create a font from an installed OS font*************************************************************************/Font* FontManager::createFont (const String& type, const String& name, const String& fontname, const String& resourceGroup){ Logger::getSingleton ().logEvent ("Attempting to create Font '" + name + "' using the font file '" + fontname + ".");
// first ensure name uniqueness if (isFontPresent (name)) throw AlreadyExistsException ("FontManager::createFont - A font named '" + name + "' already exists.");
Font *temp; // FreeType fonts if (type == FontTypeFreeType) temp = new FreeTypeFont (name, fontname, resourceGroup.empty () ? Font::getDefaultResourceGroup () : resourceGroup); // Imageset based font else if (type == FontTypePixmap) temp = new PixmapFont (name, fontname, resourceGroup.empty () ? Font::getDefaultResourceGroup () : resourceGroup); // error (should never happen) else throw FileIOException ("FontManager::createFont - The value '" + type + "' for the Font:Type attribute is unknown.");
d_fonts [name] = temp;
// if this was the first font created, set it as the default font if (d_fonts.size () == 1) System::getSingleton ().setDefaultFont (temp);
return temp;}
/************************************************************************* Create a font given its type and the respective XML attributes*************************************************************************/Font *FontManager::createFont (const String &type, const XMLAttributes& attributes){ Font *temp;
// TrueType fonts if (type == FontTypeFreeType) temp = new FreeTypeFont (attributes); // static (Image based) font else if (type == FontTypePixmap) temp = new PixmapFont (attributes); // error (should never happen) else throw FileIOException ("FontManager::createFont - The value for the Font:Type attribute '" + type + "' is unknown.");
String name = temp->getProperty ("Name"); if (isFontPresent (name)) { delete temp; throw AlreadyExistsException ("FontManager::createFont - A font named '" + name + "' already exists."); }
d_fonts [name] = temp;
return temp;}
/************************************************************************* Destroy the named font*************************************************************************/void FontManager::destroyFont(const String& name){ FontRegistry::iterator pos = d_fonts.find(name);
if (pos != d_fonts.end()) { String tmpName(name);
char addr_buff[32]; sprintf(addr_buff, "(%p)", static_cast<void*>(pos->second));
delete pos->second; d_fonts.erase(pos);
Logger::getSingleton().logEvent("Font '" + tmpName +"' has been " "destroyed. " + addr_buff); }
}
/************************************************************************* Destroys the given Font object*************************************************************************/void FontManager::destroyFont(Font* font){ if (font) { destroyFont(font->getProperty ("Name")); }
}
/************************************************************************* Destroys all Font objects registered in the system*************************************************************************/void FontManager::destroyAllFonts(void){ while (!d_fonts.empty()) { destroyFont(d_fonts.begin()->first); }
}
/************************************************************************* Check to see if a font is available*************************************************************************/bool FontManager::isFontPresent(const String& name) const{ return (d_fonts.find(name) != d_fonts.end());}
/************************************************************************* Return a pointer to the named font*************************************************************************/Font* FontManager::getFont(const String& name) const{ FontRegistry::const_iterator pos = d_fonts.find(name);
if (pos == d_fonts.end()) { throw UnknownObjectException("FontManager::getFont - A Font object with the specified name '" + name +"' does not exist within the system"); }
return pos->second;}
/************************************************************************* Notify the FontManager of the current (usually new) display resolution.*************************************************************************/void FontManager::notifyScreenResolution(const Size& size){ // notify all attached Font objects of the change in resolution FontRegistry::iterator pos = d_fonts.begin(), end = d_fonts.end();
for (; pos != end; ++pos) { pos->second->notifyScreenResolution(size); }
}
/************************************************************************* Return a FontManager::FontIterator object to iterate over the available Font objects.*************************************************************************/FontManager::FontIterator FontManager::getIterator(void) const{ return FontIterator(d_fonts.begin(), d_fonts.end());}
void FontManager::writeFontToStream(const String& name, OutStream& out_stream) const{ const Font* font = getFont(name); XMLSerializer xml(out_stream); // output font data font->writeXMLToStream(xml);}
} // End of CEGUI namespace section