[5] | 1 | /*------------------------------------------------------------------------- |
---|
| 2 | This source file is a part of OGRE |
---|
| 3 | (Object-oriented Graphics Rendering Engine) |
---|
| 4 | |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 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. |
---|
| 14 | |
---|
| 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. |
---|
| 19 | |
---|
| 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" |
---|
| 26 | |
---|
| 27 | #include "OgreFontManager.h" |
---|
| 28 | #include "OgreLogManager.h" |
---|
| 29 | #include "OgreStringConverter.h" |
---|
| 30 | #include "OgreStringVector.h" |
---|
| 31 | #include "OgreException.h" |
---|
| 32 | #include "OgreResourceGroupManager.h" |
---|
| 33 | |
---|
| 34 | namespace Ogre |
---|
| 35 | { |
---|
| 36 | //--------------------------------------------------------------------- |
---|
| 37 | template<> FontManager * Singleton< FontManager >::ms_Singleton = 0; |
---|
| 38 | FontManager* FontManager::getSingletonPtr(void) |
---|
| 39 | { |
---|
| 40 | return ms_Singleton; |
---|
| 41 | } |
---|
| 42 | FontManager& FontManager::getSingleton(void) |
---|
| 43 | { |
---|
| 44 | assert( ms_Singleton ); return ( *ms_Singleton ); |
---|
| 45 | } |
---|
| 46 | //--------------------------------------------------------------------- |
---|
| 47 | FontManager::FontManager() : ResourceManager() |
---|
| 48 | { |
---|
| 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); |
---|
| 55 | |
---|
| 56 | // Resource type |
---|
| 57 | mResourceType = "Font"; |
---|
| 58 | |
---|
| 59 | // Register with resource group manager |
---|
| 60 | ResourceGroupManager::getSingleton()._registerResourceManager(mResourceType, this); |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | } |
---|
| 64 | //--------------------------------------------------------------------- |
---|
| 65 | FontManager::~FontManager() |
---|
| 66 | { |
---|
| 67 | // Unregister with resource group manager |
---|
| 68 | ResourceGroupManager::getSingleton()._unregisterResourceManager(mResourceType); |
---|
| 69 | // Unegister scripting with resource group manager |
---|
| 70 | ResourceGroupManager::getSingleton()._unregisterScriptLoader(this); |
---|
| 71 | |
---|
| 72 | } |
---|
| 73 | //--------------------------------------------------------------------- |
---|
| 74 | Resource* FontManager::createImpl(const String& name, ResourceHandle handle, |
---|
| 75 | const String& group, bool isManual, ManualResourceLoader* loader, |
---|
| 76 | const NameValuePairList* params) |
---|
| 77 | { |
---|
| 78 | return new Font(this, name, handle, group, isManual, loader); |
---|
| 79 | } |
---|
| 80 | //--------------------------------------------------------------------- |
---|
| 81 | void FontManager::parseScript(DataStreamPtr& stream, const String& groupName) |
---|
| 82 | { |
---|
| 83 | String line; |
---|
| 84 | FontPtr pFont; |
---|
| 85 | |
---|
| 86 | while( !stream->eof() ) |
---|
| 87 | { |
---|
| 88 | line = stream->getLine(); |
---|
| 89 | // Ignore blanks & comments |
---|
| 90 | if( !line.length() || line.substr( 0, 2 ) == "//" ) |
---|
| 91 | { |
---|
| 92 | continue; |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | if (pFont.isNull()) |
---|
| 97 | { |
---|
| 98 | // No current font |
---|
| 99 | // So first valid data should be font name |
---|
| 100 | pFont = create(line, groupName); |
---|
| 101 | pFont->_notifyOrigin(stream->getName()); |
---|
| 102 | // Skip to and over next { |
---|
| 103 | stream->skipLine("{"); |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | // Already in font |
---|
| 108 | if (line == "}") |
---|
| 109 | { |
---|
| 110 | // Finished |
---|
| 111 | pFont.setNull(); |
---|
| 112 | // NB font isn't loaded until required |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | parseAttribute(line, pFont); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | //--------------------------------------------------------------------- |
---|
| 123 | void FontManager::parseAttribute(const String& line, FontPtr& pFont) |
---|
| 124 | { |
---|
| 125 | std::vector<String> params = StringUtil::split(line); |
---|
| 126 | String& attrib = params[0]; |
---|
| 127 | StringUtil::toLowerCase(attrib); |
---|
| 128 | if (attrib == "type") |
---|
| 129 | { |
---|
| 130 | // Check params |
---|
| 131 | if (params.size() != 2) |
---|
| 132 | { |
---|
| 133 | logBadAttrib(line, pFont); |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | // Set |
---|
| 137 | StringUtil::toLowerCase(params[1]); |
---|
| 138 | if (params[1] == "truetype") |
---|
| 139 | { |
---|
| 140 | pFont->setType(FT_TRUETYPE); |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
| 144 | pFont->setType(FT_IMAGE); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | } |
---|
| 148 | else if (attrib == "source") |
---|
| 149 | { |
---|
| 150 | // Check params |
---|
| 151 | if (params.size() != 2) |
---|
| 152 | { |
---|
| 153 | logBadAttrib(line, pFont); |
---|
| 154 | return; |
---|
| 155 | } |
---|
| 156 | // Set |
---|
| 157 | pFont->setSource(params[1]); |
---|
| 158 | } |
---|
| 159 | else if (attrib == "glyph") |
---|
| 160 | { |
---|
| 161 | // Check params |
---|
| 162 | if (params.size() != 6) |
---|
| 163 | { |
---|
| 164 | logBadAttrib(line, pFont); |
---|
| 165 | return; |
---|
| 166 | } |
---|
| 167 | // Set |
---|
| 168 | // Support numeric and character glyph specification |
---|
| 169 | Font::CodePoint cp; |
---|
| 170 | if (params[1].at(0) == 'u' && params[1].size() > 1) |
---|
| 171 | { |
---|
| 172 | // Unicode glyph spec |
---|
| 173 | String trimmed = params[1].substr(1); |
---|
| 174 | cp = StringConverter::parseUnsignedInt(trimmed); |
---|
| 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | // Direct character |
---|
| 179 | cp = params[1].at(0); |
---|
| 180 | } |
---|
| 181 | pFont->setGlyphTexCoords( |
---|
| 182 | cp, |
---|
| 183 | StringConverter::parseReal(params[2]), |
---|
| 184 | StringConverter::parseReal(params[3]), |
---|
| 185 | StringConverter::parseReal(params[4]), |
---|
| 186 | StringConverter::parseReal(params[5]), 1.0 ); // assume image is square |
---|
| 187 | } |
---|
| 188 | else if (attrib == "size") |
---|
| 189 | { |
---|
| 190 | // Check params |
---|
| 191 | if (params.size() != 2) |
---|
| 192 | { |
---|
| 193 | logBadAttrib(line, pFont); |
---|
| 194 | return; |
---|
| 195 | } |
---|
| 196 | // Set |
---|
| 197 | pFont->setTrueTypeSize( |
---|
| 198 | StringConverter::parseReal(params[1]) ); |
---|
| 199 | } |
---|
| 200 | else if (attrib == "resolution") |
---|
| 201 | { |
---|
| 202 | // Check params |
---|
| 203 | if (params.size() != 2) |
---|
| 204 | { |
---|
| 205 | logBadAttrib(line, pFont); |
---|
| 206 | return; |
---|
| 207 | } |
---|
| 208 | // Set |
---|
| 209 | pFont->setTrueTypeResolution( |
---|
| 210 | (uint)StringConverter::parseReal(params[1]) ); |
---|
| 211 | } |
---|
| 212 | else if (attrib == "antialias_colour") |
---|
| 213 | { |
---|
| 214 | // Check params |
---|
| 215 | if (params.size() != 2) |
---|
| 216 | { |
---|
| 217 | logBadAttrib(line, pFont); |
---|
| 218 | return; |
---|
| 219 | } |
---|
| 220 | // Set |
---|
| 221 | pFont->setAntialiasColour(StringConverter::parseBool(params[1])); |
---|
| 222 | } |
---|
| 223 | else if (attrib == "code_points") |
---|
| 224 | { |
---|
| 225 | for (size_t c = 1; c < params.size(); ++c) |
---|
| 226 | { |
---|
| 227 | String& item = params[c]; |
---|
| 228 | StringVector itemVec = StringUtil::split(item, "-"); |
---|
| 229 | if (itemVec.size() == 2) |
---|
| 230 | { |
---|
| 231 | pFont->addCodePointRange(Font::CodePointRange( |
---|
| 232 | StringConverter::parseLong(itemVec[0]), |
---|
| 233 | StringConverter::parseLong(itemVec[1]))); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | } |
---|
| 241 | //--------------------------------------------------------------------- |
---|
| 242 | void FontManager::logBadAttrib(const String& line, FontPtr& pFont) |
---|
| 243 | { |
---|
| 244 | LogManager::getSingleton().logMessage("Bad attribute line: " + line + |
---|
| 245 | " in font " + pFont->getName()); |
---|
| 246 | |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | } |
---|