diff -r 5720a0f9114b lib/irrlicht/include/IGUIEnvironment.h --- a/lib/irrlicht/include/IGUIEnvironment.h Tue Sep 22 23:02:48 2009 +0200 +++ b/lib/irrlicht/include/IGUIEnvironment.h Tue Sep 22 23:48:08 2009 +0200 @@ -34,6 +34,7 @@ namespace gui namespace gui { +class IGUIFontFactory; class IGUIElement; class IGUIFont; class IGUISpriteBank; @@ -157,19 +158,30 @@ public: virtual IGUIImageList* createImageList( video::ITexture* texture, core::dimension2d imageSize, bool useAlphaChannel ) = 0; - //! Returns pointer to the font with the specified filename. + //! Returns pointer to the font with the specified fontname. /** Loads the font if it was not loaded before. - \param filename Filename of the Font. + \param fontname Fontname, usually the filename of the font. \return Pointer to the font. Returns 0 if the font could not be loaded. This pointer should not be dropped. See IReferenceCounted::drop() for more information. */ - virtual IGUIFont* getFont(const io::path& filename) = 0; + virtual IGUIFont* getFont(const io::path& fontname) = 0; //! Returns the default built-in font. /** \return Pointer to the default built-in font. This pointer should not be dropped. See IReferenceCounted::drop() for more information. */ virtual IGUIFont* getBuiltInFont() const = 0; + + //! Adds a font factory to the gui environment. + /** Use this to extend the gui environment with new fonts which it should be + able to create automaticaly, for example when loading data from xml files. */ + virtual void registerGUIFontFactory(IGUIFontFactory* factoryToAdd) = 0; + + //! Returns amount of registered font factories. + virtual u32 getRegisteredGUIFontFactoryCount() const = 0; + + //! Returns a font factory by index + virtual IGUIFontFactory* getGUIFontFactory(u32 index) const = 0; //! Returns pointer to the sprite bank with the specified file name. /** Loads the bank if it was not loaded before. diff -r 5720a0f9114b lib/irrlicht/include/IGUIFont.h --- a/lib/irrlicht/include/IGUIFont.h Tue Sep 22 23:02:48 2009 +0200 +++ b/lib/irrlicht/include/IGUIFont.h Tue Sep 22 23:48:08 2009 +0200 @@ -9,6 +9,7 @@ #include "SColor.h" #include "rect.h" #include "irrString.h" +#include "path.h" namespace irr { @@ -95,6 +96,15 @@ public: \param invisible: string of symbols, which are not send down to the videodriver */ virtual void setInvisibleCharacters( const wchar_t *s ) = 0; + + //! Access the font name + io::path& getName() + { + return Name; + } + +protected: + io::path Name; }; } // end namespace gui diff -r 5720a0f9114b lib/irrlicht/include/IGUIFontFactory.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/irrlicht/include/IGUIFontFactory.h Tue Sep 22 23:48:08 2009 +0200 @@ -0,0 +1,42 @@ +// Copyright (C) 2002-2007 Nikolaus Gebhardt +// This file is part of the "Irrlicht Engine". +// For conditions of distribution and use, see copyright notice in irrlicht.h + +#ifndef __I_GUI_FONT_FACTORY_H_INCLUDED__ +#define __I_GUI_FONT_FACTORY_H_INCLUDED__ + +#include "IReferenceCounted.h" +#include "EGUIElementTypes.h" + +namespace irr +{ + + +namespace gui +{ + class IGUIFont; + + //! Interface making it possible to dynamicly create fonts + /** To be able to add custom fonts to Irrlicht and to make it possible for the + scene manager to save and load them, simply implement this interface and register it + in your gui environment via IGUIEnvironment::registerGUIFontFactory. + Note: When implementing your own font factory, don't call IGUIEnvironment::grab() to + increase the reference counter of the environment. This is not necessary because the + it will grab() the factory anyway, and otherwise cyclic references will be created. + */ + class IGUIFontFactory : public virtual IReferenceCounted + { + public: + virtual ~IGUIFontFactory() {}; + + //! adds a font to the GUI Environment based on its name + /** \param fontName: Usually filename of the font to add. But specific factories might use this name otherwise. + \return Returns pointer to the new font or null if not successful. */ + virtual IGUIFont* addGUIFont(const io::path& fontName) = 0; + }; + + +} // end namespace gui +} // end namespace irr + +#endif // __I_GUI_FONT_FACTORY_H_INCLUDED__ diff -r 5720a0f9114b lib/irrlicht/include/irrlicht.h --- a/lib/irrlicht/include/irrlicht.h Tue Sep 22 23:02:48 2009 +0200 +++ b/lib/irrlicht/include/irrlicht.h Tue Sep 22 23:48:08 2009 +0200 @@ -80,6 +80,7 @@ #include "IGUIElement.h" #include "IGUIElementFactory.h" #include "IGUIEnvironment.h" +#include "IGUIFontFactory.h" #include "IGUIFileOpenDialog.h" #include "IGUIFont.h" #include "IGUIFontBitmap.h" diff -r 5720a0f9114b lib/irrlicht/source/Irrlicht/CGUIEnvironment.cpp --- a/lib/irrlicht/source/Irrlicht/CGUIEnvironment.cpp Tue Sep 22 23:02:48 2009 +0200 +++ b/lib/irrlicht/source/Irrlicht/CGUIEnvironment.cpp Tue Sep 22 23:48:08 2009 +0200 @@ -39,6 +39,7 @@ #include "CDefaultGUIElementFactory.h" #include "IWriteFile.h" #include "IXMLWriter.h" +#include "IGUIFontFactory.h" #include "BuiltInFont.h" #include "os.h" @@ -153,6 +154,9 @@ CGUIEnvironment::~CGUIEnvironment() // remove all factories for (i=0; idrop(); + + for (i=0; idrop(); } @@ -1319,72 +1323,78 @@ IGUIFont* CGUIEnvironment::getFont(const if (index != -1) return Fonts[index].Font; - // font doesn't exist, attempt to load it + // check if one of the factories can create it + for (s32 i=0; i<(int)GUIFontFactoryList.size() && !ifont; ++i) + { + ifont = GUIFontFactoryList[i]->addGUIFont(filename); + } + // try the traditional way (without factories, but we could maybe add a default factory for this someday) + if ( !ifont ) + { + // font doesn't exist, attempt to load it - // does the file exist? + // does the file exist? + if (!FileSystem->existFile(filename)) + { + os::Printer::log("Could not load font because the file does not exist", f.Filename.c_str(), ELL_ERROR); + return 0; + } + io::IXMLReader *xml = FileSystem->createXMLReader(filename); + if (xml) + { + // this is an XML font, but we need to know what type + EGUI_FONT_TYPE t = EGFT_CUSTOM; - if (!FileSystem->existFile(filename)) - { - os::Printer::log("Could not load font because the file does not exist", f.Filename, ELL_ERROR); - return 0; - } - - io::IXMLReader *xml = FileSystem->createXMLReader(filename ); - if (xml) - { - // this is an XML font, but we need to know what type - EGUI_FONT_TYPE t = EGFT_CUSTOM; - - bool found=false; - while(xml->read() && !found) - { - if (xml->getNodeType() == io::EXN_ELEMENT) + bool found=false; + while(xml->read() && !found) { - if (core::stringw(L"font") == xml->getNodeName()) + if (xml->getNodeType() == io::EXN_ELEMENT) { - if (core::stringw(L"vector") == xml->getAttributeValue(L"type")) + if (core::stringw(L"font") == xml->getNodeName()) { - t = EGFT_VECTOR; - found=true; + if (core::stringw(L"vector") == xml->getAttributeValue(L"type")) + { + t = EGFT_VECTOR; + found=true; + } + else if (core::stringw(L"bitmap") == xml->getAttributeValue(L"type")) + { + t = EGFT_BITMAP; + found=true; + } + else found=true; } - else if (core::stringw(L"bitmap") == xml->getAttributeValue(L"type")) - { - t = EGFT_BITMAP; - found=true; - } - else found=true; } } + if (t==EGFT_BITMAP) + { + CGUIFont* font = new CGUIFont(this, filename); + ifont = (IGUIFont*)font; + // change working directory, for loading textures + io::path workingDir = FileSystem->getWorkingDirectory(); + FileSystem->changeWorkingDirectoryTo(FileSystem->getFileDir(f.Filename).c_str()); + + // load the font + if (!font->load(xml)) + { + font->drop(); + font = 0; + ifont = 0; + } + // change working dir back again + FileSystem->changeWorkingDirectoryTo( workingDir.c_str()); + } + else if (t==EGFT_VECTOR) + { + // todo: vector fonts + os::Printer::log("Unable to load font, XML vector fonts are not supported yet", f.Filename.c_str(), ELL_ERROR); + + //CGUIFontVector* font = new CGUIFontVector(Driver); + //ifont = (IGUIFont*)font; + //if (!font->load(xml)) + } + xml->drop(); } - - if (t==EGFT_BITMAP) - { - CGUIFont* font = new CGUIFont(this, filename); - ifont = (IGUIFont*)font; - // change working directory, for loading textures - io::path workingDir = FileSystem->getWorkingDirectory(); - FileSystem->changeWorkingDirectoryTo(FileSystem->getFileDir(f.Filename)); - - // load the font - if (!font->load(xml)) - { - font->drop(); - font = 0; - ifont = 0; - } - // change working dir back again - FileSystem->changeWorkingDirectoryTo( workingDir ); - } - else if (t==EGFT_VECTOR) - { - // todo: vector fonts - os::Printer::log("Unable to load font, XML vector fonts are not supported yet", f.Filename.c_str(), ELL_ERROR); - - //CGUIFontVector* font = new CGUIFontVector(Driver); - //ifont = (IGUIFont*)font; - //if (!font->load(xml)) - } - xml->drop(); } @@ -1401,7 +1411,7 @@ IGUIFont* CGUIEnvironment::getFont(const } // add to fonts. - + ifont->getName() = filename; f.Font = ifont; Fonts.push_back(f); @@ -1543,6 +1553,30 @@ IGUIEnvironment* createGUIEnvironment(io return new CGUIEnvironment(fs, Driver, op); } +//! Adds a font factory to the gui environment. +void CGUIEnvironment::registerGUIFontFactory(IGUIFontFactory* factoryToAdd) +{ + if (factoryToAdd) + { + factoryToAdd->grab(); + GUIFontFactoryList.push_back(factoryToAdd); + } +} + +//! Returns amount of registered font factories. +u32 CGUIEnvironment::getRegisteredGUIFontFactoryCount() const +{ + return GUIFontFactoryList.size(); +} + +//! Returns a font factory by index +IGUIFontFactory* CGUIEnvironment::getGUIFontFactory(u32 index) const +{ + if (index* rectangle=0, IGUIElement* parent=0, s32 id=-1); @@ -286,6 +297,7 @@ private: SToolTip ToolTip; core::array GUIElementFactoryList; + core::array GUIFontFactoryList; core::array Fonts; core::array Banks;