Patch 2793067: fix trunk with OGRE_THREAD_SUPPORT=1 on non-Windows platforms (don...
[ogre3d.git] / OgreMain / src / OgreFontManager.cpp
blob04a4193d783d900627195aaeb0704ea2f1121663
1 /*-------------------------------------------------------------------------
2 This source file is a part of OGRE
3 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
7 Copyright (c) 2000-2006 Torus Knot Software Ltd
8 Also see acknowledgements in Readme.html
10 This library is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License (LGPL) as
12 published by the Free Software Foundation; either version 2.1 of the
13 License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this library; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or go to
23 http://www.gnu.org/copyleft/lesser.txt
24 -------------------------------------------------------------------------*/
25 #include "OgreStableHeaders.h"
27 #include "OgreFontManager.h"
28 #include "OgreLogManager.h"
29 #include "OgreStringConverter.h"
30 #include "OgreStringVector.h"
31 #include "OgreException.h"
32 #include "OgreResourceGroupManager.h"
34 namespace Ogre
36 //---------------------------------------------------------------------
37 template<> FontManager * Singleton< FontManager >::ms_Singleton = 0;
38 FontManager* FontManager::getSingletonPtr(void)
40 return ms_Singleton;
42 FontManager& FontManager::getSingleton(void)
44 assert( ms_Singleton ); return ( *ms_Singleton );
46 //---------------------------------------------------------------------
47 FontManager::FontManager() : ResourceManager()
49 // Loading order
50 mLoadOrder = 200.0f;
51 // Scripting is supported by this manager
52 mScriptPatterns.push_back("*.fontdef");
53 // Register scripting with resource group manager
54 ResourceGroupManager::getSingleton()._registerScriptLoader(this);
56 // Resource type
57 mResourceType = "Font";
59 // Register with resource group manager
60 ResourceGroupManager::getSingleton()._registerResourceManager(mResourceType, this);
64 //---------------------------------------------------------------------
65 FontManager::~FontManager()
67 // Unregister with resource group manager
68 ResourceGroupManager::getSingleton()._unregisterResourceManager(mResourceType);
69 // Unegister scripting with resource group manager
70 ResourceGroupManager::getSingleton()._unregisterScriptLoader(this);
73 //---------------------------------------------------------------------
74 Resource* FontManager::createImpl(const String& name, ResourceHandle handle,
75 const String& group, bool isManual, ManualResourceLoader* loader,
76 const NameValuePairList* params)
78 return OGRE_NEW Font(this, name, handle, group, isManual, loader);
80 //---------------------------------------------------------------------
81 void FontManager::parseScript(DataStreamPtr& stream, const String& groupName)
83 String line;
84 FontPtr pFont;
86 while( !stream->eof() )
88 line = stream->getLine();
89 // Ignore blanks & comments
90 if( !line.length() || line.substr( 0, 2 ) == "//" )
92 continue;
94 else
96 if (pFont.isNull())
98 // No current font
99 // So first valid data should be font name
100 if (StringUtil::startsWith(line, "font "))
102 // chop off the 'particle_system ' needed by new compilers
103 line = line.substr(5);
105 pFont = create(line, groupName);
106 pFont->_notifyOrigin(stream->getName());
107 // Skip to and over next {
108 stream->skipLine("{");
110 else
112 // Already in font
113 if (line == "}")
115 // Finished
116 pFont.setNull();
117 // NB font isn't loaded until required
119 else
121 parseAttribute(line, pFont);
127 //---------------------------------------------------------------------
128 void FontManager::parseAttribute(const String& line, FontPtr& pFont)
130 vector<String>::type params = StringUtil::split(line);
131 String& attrib = params[0];
132 StringUtil::toLowerCase(attrib);
133 if (attrib == "type")
135 // Check params
136 if (params.size() != 2)
138 logBadAttrib(line, pFont);
139 return;
141 // Set
142 StringUtil::toLowerCase(params[1]);
143 if (params[1] == "truetype")
145 pFont->setType(FT_TRUETYPE);
147 else
149 pFont->setType(FT_IMAGE);
153 else if (attrib == "source")
155 // Check params
156 if (params.size() != 2)
158 logBadAttrib(line, pFont);
159 return;
161 // Set
162 pFont->setSource(params[1]);
164 else if (attrib == "glyph")
166 // Check params
167 if (params.size() != 6)
169 logBadAttrib(line, pFont);
170 return;
172 // Set
173 // Support numeric and character glyph specification
174 Font::CodePoint cp;
175 if (params[1].at(0) == 'u' && params[1].size() > 1)
177 // Unicode glyph spec
178 String trimmed = params[1].substr(1);
179 cp = StringConverter::parseUnsignedInt(trimmed);
181 else
183 // Direct character
184 cp = params[1].at(0);
186 pFont->setGlyphTexCoords(
187 cp,
188 StringConverter::parseReal(params[2]),
189 StringConverter::parseReal(params[3]),
190 StringConverter::parseReal(params[4]),
191 StringConverter::parseReal(params[5]), 1.0 ); // assume image is square
193 else if (attrib == "size")
195 // Check params
196 if (params.size() != 2)
198 logBadAttrib(line, pFont);
199 return;
201 // Set
202 pFont->setTrueTypeSize(
203 StringConverter::parseReal(params[1]) );
205 else if (attrib == "resolution")
207 // Check params
208 if (params.size() != 2)
210 logBadAttrib(line, pFont);
211 return;
213 // Set
214 pFont->setTrueTypeResolution(
215 (uint)StringConverter::parseReal(params[1]) );
217 else if (attrib == "antialias_colour")
219 // Check params
220 if (params.size() != 2)
222 logBadAttrib(line, pFont);
223 return;
225 // Set
226 pFont->setAntialiasColour(StringConverter::parseBool(params[1]));
228 else if (attrib == "code_points")
230 for (size_t c = 1; c < params.size(); ++c)
232 String& item = params[c];
233 StringVector itemVec = StringUtil::split(item, "-");
234 if (itemVec.size() == 2)
236 pFont->addCodePointRange(Font::CodePointRange(
237 StringConverter::parseLong(itemVec[0]),
238 StringConverter::parseLong(itemVec[1])));
246 //---------------------------------------------------------------------
247 void FontManager::logBadAttrib(const String& line, FontPtr& pFont)
249 LogManager::getSingleton().logMessage("Bad attribute line: " + line +
250 " in font " + pFont->getName());