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     FontManager* FontManager::getSingletonPtr(void)
    00040     {
    00041         return ms_Singleton;
    00042     }
    00043     FontManager& FontManager::getSingleton(void)
    00044     {
    00045         assert( ms_Singleton );  return ( *ms_Singleton );
    00046     }
    00047     //---------------------------------------------------------------------------------------------
    00048
    00049     //---------------------------------------------------------------------
    00050     Resource* FontManager::create(const String& name)
    00051     {
    00052         // Check name not already used
    00053         if (getByName(name) != 0)
    00054             Except(Exception::ERR_DUPLICATE_ITEM, "Font " + name + " already exists.",
    00055                 "FontManager::create");
    00056
    00057         Font* f = new Font(name);
    00058         // Don't load yet, defer until used
    00059
    00060         mResources.insert(ResourceMap::value_type(name, f));
    00061         return f;
    00062     }
    00063     //---------------------------------------------------------------------
    00064     void FontManager::parseScript( DataChunk& chunk )
    00065     {
    00066         String line;
    00067         Font *pFont = 0;
    00068
    00069         while( !chunk.isEOF() )
    00070         {
    00071             line = chunk.getLine();
    00072             // Ignore blanks & comments
    00073             if( !line.length() || line.substr( 0, 2 ) == "//" )
    00074             {
    00075                 continue;
    00076             }
    00077             else
    00078             {
    00079                 if (pFont == 0)
    00080                 {
    00081                     // No current font
    00082                     // So first valid data should be font name
    00083                     pFont = (Font*)create(line);
    00084                     // Skip to and over next {
    00085                     chunk.skipUpTo("{");
    00086                 }
    00087                 else
    00088                 {
    00089                     // Already in font
    00090                     if (line == "}")
    00091                     {
    00092                         // Finished
    00093                         pFont = 0;
    00094                         // NB font isn't loaded until required
    00095                     }
    00096                     else
    00097                     {
    00098                         parseAttribute(line, pFont);
    00099                     }
    00100                 }
    00101             }
    00102         }
    00103     }
    00104     //---------------------------------------------------------------------
    00105     void FontManager::parseAllSources( const String& extension )
    00106     {
    00107         StringVector fontfiles;
    00108         SDDataChunk chunk;
    00109
    00110         std::vector< ArchiveEx * >::iterator i = mVFS.begin();
    00111
    00112         for( ; i < mVFS.end(); i++ )
    00113         {
    00114             fontfiles = (*i)->getAllNamesLike( "./", extension );
    00115             for( StringVector::iterator j = fontfiles.begin(); j != fontfiles.end(); j++ )
    00116             {
    00117                 DataChunk *p = &chunk;
    00118                 (*i)->fileRead( *j, &p );
    00119                 parseScript( chunk );
    00120             }
    00121         }
    00122         for( i = mCommonVFS.begin(); i < mCommonVFS.end(); i++ )
    00123         {
    00124             fontfiles = (*i)->getAllNamesLike( "./", extension );
    00125             for( StringVector::iterator j = fontfiles.begin(); j != fontfiles.end(); j++ )
    00126             {
    00127                 DataChunk *p = &chunk;
    00128                 (*i)->fileRead( *j, &p );
    00129                 parseScript( chunk );
    00130             }
    00131         }
    00132     }
    00133     //---------------------------------------------------------------------
    00134     void FontManager::parseAttribute(const String& line, Font* pFont)
    00135     {
    00136         std::vector<String> params = StringUtil::split(line);
    00137         String& attrib = params[0];
    00138         StringUtil::toLowerCase(attrib);
    00139         if (attrib == "type")
    00140         {
    00141             // Check params
    00142             if (params.size() != 2)
    00143             {
    00144                 logBadAttrib(line, pFont);
    00145                 return;
    00146             }
    00147             // Set
    00148             StringUtil::toLowerCase(params[1]);
    00149             if (params[1] == "truetype")
    00150             {
    00151                 pFont->setType(FT_TRUETYPE);
    00152             }
    00153             else
    00154             {
    00155                 pFont->setType(FT_IMAGE);
    00156             }
    00157
    00158         }
    00159         else if (attrib == "source")
    00160         {
    00161             // Check params
    00162             if (params.size() != 2)
    00163             {
    00164                 logBadAttrib(line, pFont);
    00165                 return;
    00166             }
    00167             // Set
    00168             pFont->setSource(params[1]);
    00169         }
    00170         else if (attrib == "glyph")
    00171         {
    00172             // Check params
    00173             if (params.size() != 6)
    00174             {
    00175                 logBadAttrib(line, pFont);
    00176                 return;
    00177             }
    00178             // Set
    00179             pFont->setGlyphTexCoords(
    00180                 params[1].at(0),
    00181                 StringConverter::parseReal(params[2]),
    00182                 StringConverter::parseReal(params[3]),
    00183                 StringConverter::parseReal(params[4]),
    00184                 StringConverter::parseReal(params[5]) );
    00185         }
    00186         else if (attrib == "size")
    00187         {
    00188             // Check params
    00189             if (params.size() != 2)
    00190             {
    00191                 logBadAttrib(line, pFont);
    00192                 return;
    00193             }
    00194             // Set
    00195             pFont->setTrueTypeSize(
    00196                 StringConverter::parseReal(params[1]) );
    00197         }
    00198         else if (attrib == "resolution")
    00199         {
    00200             // Check params
    00201             if (params.size() != 2)
    00202             {
    00203                 logBadAttrib(line, pFont);
    00204                 return;
    00205             }
    00206             // Set
    00207             pFont->setTrueTypeResolution(
    00208                 (uint)StringConverter::parseReal(params[1]) );
    00209         }
    00210         else if (attrib == "antialias_colour")
    00211         {
    00212             // Check params
    00213             if (params.size() != 2)
    00214             {
    00215                 logBadAttrib(line, pFont);
    00216                 return;
    00217             }
    00218             // Set
    00219             pFont->setAntialiasColour(StringConverter::parseBool(params[1]));
    00220         }
    00221
    00222
    00223
    00224     }
    00225     //---------------------------------------------------------------------
    00226     void FontManager::logBadAttrib(const String& line, Font* pFont)
    00227     {
    00228         LogManager::getSingleton().logMessage("Bad attribute line: " + line +
    00229             " in font " + pFont->getName());
    00230
    00231     }
    00232
    00233 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:24 2004