FontDatabase.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include <Rocket/Core/FontDatabase.h>
  29. #include <Rocket/Core/FontFamily.h>
  30. #include <Rocket/Core.h>
  31. #include <Rocket/Core/FreeType/FontProvider.h>
  32. #include <Rocket/Core/BitmapFont/FontProvider.h>
  33. namespace Rocket {
  34. namespace Core {
  35. FontDatabase* FontDatabase::instance = NULL;
  36. FontDatabase::FontProviderTable FontDatabase::font_provider_table;
  37. typedef std::unordered_map< String, FontEffect* > FontEffectCache;
  38. FontEffectCache font_effect_cache;
  39. FontDatabase::FontDatabase()
  40. {
  41. ROCKET_ASSERT(instance == NULL);
  42. instance = this;
  43. }
  44. FontDatabase::~FontDatabase()
  45. {
  46. ROCKET_ASSERT(instance == this);
  47. instance = NULL;
  48. }
  49. bool FontDatabase::Initialise()
  50. {
  51. if (instance == NULL)
  52. {
  53. new FontDatabase();
  54. if(!FreeType::FontProvider::Initialise())
  55. return false;
  56. if(!BitmapFont::FontProvider::Initialise())
  57. return false;
  58. }
  59. return true;
  60. }
  61. void FontDatabase::Shutdown()
  62. {
  63. if (instance != NULL)
  64. {
  65. FreeType::FontProvider::Shutdown();
  66. BitmapFont::FontProvider::Shutdown();
  67. delete instance;
  68. }
  69. }
  70. // Loads a new font face.
  71. bool FontDatabase::LoadFontFace(const String& file_name)
  72. {
  73. FontProviderType font_provider_type = GetFontProviderType(file_name);
  74. switch(font_provider_type)
  75. {
  76. case FreeType:
  77. return FreeType::FontProvider::LoadFontFace(file_name);
  78. case BitmapFont:
  79. return BitmapFont::FontProvider::LoadFontFace(file_name);
  80. default:
  81. return false;
  82. }
  83. }
  84. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  85. bool FontDatabase::LoadFontFace(const String& file_name, const String& family, Font::Style style, Font::Weight weight)
  86. {
  87. FontProviderType font_provider_type = GetFontProviderType(file_name);
  88. switch(font_provider_type)
  89. {
  90. case FreeType:
  91. return FreeType::FontProvider::LoadFontFace(file_name, family, style, weight);
  92. case BitmapFont:
  93. return BitmapFont::FontProvider::LoadFontFace(file_name, family, style, weight);
  94. default:
  95. return false;
  96. }
  97. }
  98. // Adds a new font face to the database, loading from memory.
  99. bool FontDatabase::LoadFontFace(FontProviderType font_provider_type, const byte* data, int data_length)
  100. {
  101. switch(font_provider_type)
  102. {
  103. case FreeType:
  104. return FreeType::FontProvider::LoadFontFace(data, data_length);
  105. case BitmapFont:
  106. return BitmapFont::FontProvider::LoadFontFace(data, data_length);
  107. default:
  108. return false;
  109. }
  110. }
  111. // Adds a new font face to the database, loading from memory, ignoring any family, style and weight information stored in the face itself.
  112. bool FontDatabase::LoadFontFace(FontProviderType font_provider_type, const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  113. {
  114. switch(font_provider_type)
  115. {
  116. case FreeType:
  117. return FreeType::FontProvider::LoadFontFace(data, data_length, family, style, weight);
  118. case BitmapFont:
  119. return BitmapFont::FontProvider::LoadFontFace(data, data_length, family, style, weight);
  120. default:
  121. return false;
  122. }
  123. }
  124. FontDatabase::FontProviderType FontDatabase::GetFontProviderType(const String& file_name)
  125. {
  126. if(file_name.Find(".fnt") != String::npos)
  127. {
  128. return BitmapFont;
  129. }
  130. else
  131. {
  132. return FreeType;
  133. }
  134. }
  135. // Returns a handle to a font face that can be used to position and render text.
  136. FontFaceHandle* FontDatabase::GetFontFaceHandle(const String& family, const String& charset, Font::Style style, Font::Weight weight, int size)
  137. {
  138. size_t provider_index, provider_count;
  139. provider_count = font_provider_table.size();
  140. for(provider_index = 0; provider_index < provider_count; ++provider_index)
  141. {
  142. FontFaceHandle * face_handle = font_provider_table[ provider_index ]->GetFontFaceHandle(family, charset, style, weight, size);
  143. if(face_handle)
  144. {
  145. return face_handle;
  146. }
  147. }
  148. return NULL;
  149. }
  150. // Returns a font effect, either a newly-instanced effect from the factory or an identical shared
  151. // effect.
  152. FontEffect* FontDatabase::GetFontEffect(const String& name, const PropertyDictionary& properties)
  153. {
  154. // The caching here should be moved into the Factory for optimal behaviour. This system has a
  155. // few shortfalls:
  156. // * ignores default properties
  157. // * could be shared with decorators as well
  158. // Generate a key so we can distinguish unique property sets quickly.
  159. typedef std::list< std::pair< String, String > > PropertyList;
  160. PropertyList sorted_properties;
  161. for (PropertyMap::const_iterator property_iterator = properties.GetProperties().begin(); property_iterator != properties.GetProperties().end(); ++property_iterator)
  162. {
  163. // Skip the font-effect declaration.
  164. if (property_iterator->first == "font-effect")
  165. continue;
  166. PropertyList::iterator insert = sorted_properties.begin();
  167. while (insert != sorted_properties.end() &&
  168. insert->first < property_iterator->first)
  169. ++insert;
  170. sorted_properties.insert(insert, PropertyList::value_type(property_iterator->first, property_iterator->second.Get< String >()));
  171. }
  172. // Generate the font effect's key from the properties.
  173. String key = name + ";";
  174. for (PropertyList::iterator i = sorted_properties.begin(); i != sorted_properties.end(); ++i)
  175. key += i->first + ":" + i->second + ";";
  176. // Check if we have a previously instanced effect.
  177. FontEffectCache::iterator i = font_effect_cache.find(key);
  178. if (i != font_effect_cache.end())
  179. {
  180. FontEffect* effect = i->second;
  181. effect->AddReference();
  182. return effect;
  183. }
  184. FontEffect* font_effect = Factory::InstanceFontEffect(name, properties);
  185. if (font_effect == NULL)
  186. return NULL;
  187. font_effect_cache[key] = font_effect;
  188. return font_effect;
  189. }
  190. // Removes a font effect from the font database's cache.
  191. void FontDatabase::ReleaseFontEffect(const FontEffect* effect)
  192. {
  193. for (FontEffectCache::iterator i = font_effect_cache.begin(); i != font_effect_cache.end(); ++i)
  194. {
  195. if (i->second == effect)
  196. {
  197. font_effect_cache.erase(i);
  198. return;
  199. }
  200. }
  201. }
  202. void FontDatabase::AddFontProvider(FontProvider * provider)
  203. {
  204. instance->font_provider_table.push_back(provider);
  205. }
  206. void FontDatabase::RemoveFontProvider(FontProvider * provider)
  207. {
  208. for(FontProviderTable::iterator i = instance->font_provider_table.begin(); i != instance->font_provider_table.end(); ++i)
  209. {
  210. if(*i == provider)
  211. {
  212. instance->font_provider_table.erase(i);
  213. return;
  214. }
  215. }
  216. }
  217. }
  218. }