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 = line.split(); 00137 String attrib = params[0].toLowerCase(); 00138 if (attrib == "type") 00139 { 00140 // Check params 00141 if (params.size() != 2) 00142 { 00143 logBadAttrib(line, pFont); 00144 return; 00145 } 00146 // Set 00147 if (params[1].toLowerCase() == "truetype") 00148 { 00149 pFont->setType(FT_TRUETYPE); 00150 } 00151 else 00152 { 00153 pFont->setType(FT_IMAGE); 00154 } 00155 00156 } 00157 else if (attrib == "source") 00158 { 00159 // Check params 00160 if (params.size() != 2) 00161 { 00162 logBadAttrib(line, pFont); 00163 return; 00164 } 00165 // Set 00166 pFont->setSource(params[1]); 00167 } 00168 else if (attrib == "glyph") 00169 { 00170 // Check params 00171 if (params.size() != 6) 00172 { 00173 logBadAttrib(line, pFont); 00174 return; 00175 } 00176 // Set 00177 pFont->setGlyphTexCoords( 00178 params[1].at(0), 00179 StringConverter::parseReal(params[2]), 00180 StringConverter::parseReal(params[3]), 00181 StringConverter::parseReal(params[4]), 00182 StringConverter::parseReal(params[5]) ); 00183 } 00184 else if (attrib == "size") 00185 { 00186 // Check params 00187 if (params.size() != 2) 00188 { 00189 logBadAttrib(line, pFont); 00190 return; 00191 } 00192 // Set 00193 pFont->setTrueTypeSize( 00194 StringConverter::parseReal(params[1]) ); 00195 } 00196 else if (attrib == "resolution") 00197 { 00198 // Check params 00199 if (params.size() != 2) 00200 { 00201 logBadAttrib(line, pFont); 00202 return; 00203 } 00204 // Set 00205 pFont->setTrueTypeResolution( 00206 (uint)StringConverter::parseReal(params[1]) ); 00207 } 00208 else if (attrib == "antialias_colour") 00209 { 00210 // Check params 00211 if (params.size() != 2) 00212 { 00213 logBadAttrib(line, pFont); 00214 return; 00215 } 00216 // Set 00217 pFont->setAntialiasColour(StringConverter::parseBool(params[1])); 00218 } 00219 00220 00221 00222 } 00223 //--------------------------------------------------------------------- 00224 void FontManager::logBadAttrib(const String& line, Font* pFont) 00225 { 00226 LogManager::getSingleton().logMessage("Bad attribute line: " + line + 00227 " in font " + pFont->getName()); 00228 00229 } 00230 00231 }
Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:10 2004