Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreFontManager.cpp

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002 This source file is a part of OGRE
00003 (Object-oriented Graphics Rendering Engine)
00004 
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This library is free software; you can redistribute it and/or modify it
00011 under the terms of the GNU Lesser General Public License (LGPL) as 
00012 published by the Free Software Foundation; either version 2.1 of the 
00013 License, or (at your option) any later version.
00014 
00015 This library is distributed in the hope that it will be useful, but 
00016 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00017 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00018 License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License 
00021 along with this library; if not, write to the Free Software Foundation, 
00022 Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA or go to
00023 http://www.gnu.org/copyleft/lesser.txt
00024 -------------------------------------------------------------------------*/
00025 #include "OgreStableHeaders.h"
00026 
00027 #include "OgreFontManager.h"
00028 #include "OgreFont.h"
00029 #include "OgreSDDataChunk.h"
00030 #include "OgreLogManager.h"
00031 #include "OgreStringConverter.h"
00032 #include "OgreStringVector.h"
00033 #include "OgreException.h"
00034 
00035 namespace Ogre
00036 {
00037     //---------------------------------------------------------------------------------------------
00038     template<> FontManager * Singleton< FontManager >::ms_Singleton = 0;
00039     //---------------------------------------------------------------------------------------------
00040 
00041     //---------------------------------------------------------------------
00042     Resource* FontManager::create(const String& name)
00043     {
00044         // Check name not already used
00045         if (getByName(name) != 0)
00046             Except(Exception::ERR_DUPLICATE_ITEM, "Font " + name + " already exists.",
00047                 "FontManager::create");
00048 
00049         Font* f = new Font(name);
00050         // Don't load yet, defer until used
00051 
00052         mResources.insert(ResourceMap::value_type(name, f));
00053         return f;
00054     }
00055     //---------------------------------------------------------------------
00056     void FontManager::parseScript( DataChunk& chunk )
00057     {
00058         String line;
00059         Font *pFont = 0;
00060 
00061         while( !chunk.isEOF() )
00062         {
00063             line = chunk.getLine();
00064             // Ignore blanks & comments
00065             if( !line.length() || line.substr( 0, 2 ) == "//" )
00066             {
00067                 continue;
00068             }
00069             else
00070             {
00071                 if (pFont == 0)
00072                 {
00073                     // No current font
00074                     // So first valid data should be font name
00075                     pFont = (Font*)create(line);
00076                     // Skip to and over next {
00077                     chunk.skipUpTo("{");
00078                 }
00079                 else
00080                 {
00081                     // Already in font
00082                     if (line == "}")
00083                     {
00084                         // Finished 
00085                         pFont = 0;
00086                         // NB font isn't loaded until required
00087                     }
00088                     else
00089                     {
00090                         parseAttribute(line, pFont);
00091                     }
00092                 }
00093             }
00094         }
00095     }
00096     //---------------------------------------------------------------------
00097     void FontManager::parseAllSources( const String& extension )
00098     {
00099         StringVector fontfiles;
00100         SDDataChunk chunk;
00101 
00102         std::vector< ArchiveEx * >::iterator i = mVFS.begin();
00103 
00104         for( ; i < mVFS.end(); i++ )
00105         {
00106             fontfiles = (*i)->getAllNamesLike( "./", extension );
00107             for( StringVector::iterator j = fontfiles.begin(); j != fontfiles.end(); j++ )
00108             {
00109                 DataChunk *p = &chunk;
00110                 (*i)->fileRead( *j, &p );
00111                 parseScript( chunk );
00112             }
00113         }
00114         for( i = mCommonVFS.begin(); i < mCommonVFS.end(); i++ )
00115         {
00116             fontfiles = (*i)->getAllNamesLike( "./", extension );
00117             for( StringVector::iterator j = fontfiles.begin(); j != fontfiles.end(); j++ )
00118             {
00119                 DataChunk *p = &chunk;
00120                 (*i)->fileRead( *j, &p );
00121                 parseScript( chunk );
00122             }
00123         }
00124     }
00125     //---------------------------------------------------------------------
00126     void FontManager::parseAttribute(const String& line, Font* pFont)
00127     {
00128         std::vector<String> params = line.split();
00129         String attrib = params[0].toLowerCase();
00130         if (attrib == "type")
00131         {
00132             // Check params
00133             if (params.size() != 2)
00134             {
00135                 logBadAttrib(line, pFont);
00136                 return;
00137             }
00138             // Set
00139             if (params[1].toLowerCase() == "truetype")
00140             {
00141                 pFont->setType(FT_TRUETYPE);
00142             }
00143             else
00144             {
00145                 pFont->setType(FT_IMAGE);
00146             }
00147 
00148         }
00149         else if (attrib == "source")
00150         {
00151             // Check params
00152             if (params.size() != 2)
00153             {
00154                 logBadAttrib(line, pFont);
00155                 return;
00156             }
00157             // Set
00158             pFont->setSource(params[1]);
00159         }
00160         else if (attrib == "glyph")
00161         {
00162             // Check params
00163             if (params.size() != 6)
00164             {
00165                 logBadAttrib(line, pFont);
00166                 return;
00167             }
00168             // Set
00169             pFont->setGlyphTexCoords(
00170                 params[1].at(0), 
00171                 StringConverter::parseReal(params[2]),
00172                 StringConverter::parseReal(params[3]),
00173                 StringConverter::parseReal(params[4]),
00174                 StringConverter::parseReal(params[5]) );
00175         }
00176         else if (attrib == "size")
00177         {
00178             // Check params
00179             if (params.size() != 2)
00180             {
00181                 logBadAttrib(line, pFont);
00182                 return;
00183             }
00184             // Set
00185             pFont->setTrueTypeSize(
00186                 StringConverter::parseReal(params[1]) );
00187         }
00188         else if (attrib == "resolution")
00189         {
00190             // Check params
00191             if (params.size() != 2)
00192             {
00193                 logBadAttrib(line, pFont);
00194                 return;
00195             }
00196             // Set
00197             pFont->setTrueTypeResolution(
00198                 (uint)StringConverter::parseReal(params[1]) );
00199         }
00200         else if (attrib == "antialias_colour")
00201         {
00202             // Check params
00203             if (params.size() != 2)
00204             {
00205                 logBadAttrib(line, pFont);
00206                 return;
00207             }
00208             // Set
00209             pFont->setAntialiasColour(StringConverter::parseBool(params[1]));
00210         }
00211 
00212 
00213 
00214     }
00215     //---------------------------------------------------------------------
00216     void FontManager::logBadAttrib(const String& line, Font* pFont)
00217     {
00218         LogManager::getSingleton().logMessage("Bad attribute line: " + line +
00219             " in font " + pFont->getName());
00220 
00221     }
00222     //---------------------------------------------------------------------
00223     FontManager& FontManager::getSingleton(void)
00224     {
00225         return Singleton<FontManager>::getSingleton();
00226     }
00227 
00228 }

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:10 2004