FontDatabase.cpp 7.8 KB

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