FontDatabase.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <ft2build.h>
  33. #include FT_FREETYPE_H
  34. namespace Rocket {
  35. namespace Core {
  36. FontDatabase* FontDatabase::instance = NULL;
  37. typedef std::map< String, FontEffect* > FontEffectCache;
  38. FontEffectCache font_effect_cache;
  39. static FT_Library ft_library = NULL;
  40. FontDatabase::FontDatabase()
  41. {
  42. ROCKET_ASSERT(instance == NULL);
  43. instance = this;
  44. }
  45. FontDatabase::~FontDatabase()
  46. {
  47. ROCKET_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. }
  58. return true;
  59. }
  60. void FontDatabase::Shutdown()
  61. {
  62. if (instance != NULL)
  63. {
  64. FreeType::FontProvider::Shutdown();
  65. delete instance;
  66. }
  67. }
  68. // Loads a new font face.
  69. bool FontDatabase::LoadFontFace(const String& file_name)
  70. {
  71. return FreeType::FontProvider::LoadFontFace(file_name);
  72. }
  73. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  74. bool FontDatabase::LoadFontFace(const String& file_name, const String& family, Font::Style style, Font::Weight weight)
  75. {
  76. return FreeType::FontProvider::LoadFontFace(file_name, family, style, weight);
  77. }
  78. // Adds a new font face to the database, loading from memory.
  79. bool FontDatabase::LoadFontFace(FontProviderType font_provider_type, const byte* data, int data_length)
  80. {
  81. return FreeType::FontProvider::LoadFontFace(data, data_length);
  82. }
  83. // Adds a new font face to the database, loading from memory, ignoring any family, style and weight information stored in the face itself.
  84. bool FontDatabase::LoadFontFace(FontProviderType font_provider_type, const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  85. {
  86. return FreeType::FontProvider::LoadFontFace(data, data_length, family, style, weight);
  87. }
  88. // Returns a handle to a font face that can be used to position and render text.
  89. FontFaceHandle* FontDatabase::GetFontFaceHandle(const String& family, const String& charset, Font::Style style, Font::Weight weight, int size)
  90. {
  91. size_t provider_index, provider_count;
  92. provider_count = font_provider_table.size();
  93. for(provider_index = 0; provider_index < provider_count; ++provider_index)
  94. {
  95. FontFaceHandle * face_handle = font_provider_table[ provider_index ]->GetFontFaceHandle(family, charset, style, weight, size);
  96. if(face_handle)
  97. {
  98. return face_handle;
  99. }
  100. }
  101. return NULL;
  102. }
  103. // Returns a font effect, either a newly-instanced effect from the factory or an identical shared
  104. // effect.
  105. FontEffect* FontDatabase::GetFontEffect(const String& name, const PropertyDictionary& properties)
  106. {
  107. // The caching here should be moved into the Factory for optimal behaviour. This system has a
  108. // few shortfalls:
  109. // * ignores default properties
  110. // * could be shared with decorators as well
  111. // Generate a key so we can distinguish unique property sets quickly.
  112. typedef std::list< std::pair< String, String > > PropertyList;
  113. PropertyList sorted_properties;
  114. for (PropertyMap::const_iterator property_iterator = properties.GetProperties().begin(); property_iterator != properties.GetProperties().end(); ++property_iterator)
  115. {
  116. // Skip the font-effect declaration.
  117. if (property_iterator->first == "font-effect")
  118. continue;
  119. PropertyList::iterator insert = sorted_properties.begin();
  120. while (insert != sorted_properties.end() &&
  121. insert->first < property_iterator->first)
  122. ++insert;
  123. sorted_properties.insert(insert, PropertyList::value_type(property_iterator->first, property_iterator->second.Get< String >()));
  124. }
  125. // Generate the font effect's key from the properties.
  126. String key = name + ";";
  127. for (PropertyList::iterator i = sorted_properties.begin(); i != sorted_properties.end(); ++i)
  128. key += i->first + ":" + i->second + ";";
  129. // Check if we have a previously instanced effect.
  130. FontEffectCache::iterator i = font_effect_cache.find(key);
  131. if (i != font_effect_cache.end())
  132. {
  133. FontEffect* effect = i->second;
  134. effect->AddReference();
  135. return effect;
  136. }
  137. FontEffect* font_effect = Factory::InstanceFontEffect(name, properties);
  138. if (font_effect == NULL)
  139. return NULL;
  140. font_effect_cache[key] = font_effect;
  141. return font_effect;
  142. }
  143. // Removes a font effect from the font database's cache.
  144. void FontDatabase::ReleaseFontEffect(const FontEffect* effect)
  145. {
  146. for (FontEffectCache::iterator i = font_effect_cache.begin(); i != font_effect_cache.end(); ++i)
  147. {
  148. if (i->second == effect)
  149. {
  150. font_effect_cache.erase(i);
  151. return;
  152. }
  153. }
  154. }
  155. void FontDatabase::AddFontProvider(FontProvider * provider)
  156. {
  157. instance->font_provider_table.push_back(provider);
  158. }
  159. void FontDatabase::RemoveFontProvider(FontProvider * provider)
  160. {
  161. for(FontProviderTable::iterator i = instance->font_provider_table.begin(); i != instance->font_provider_table.end(); ++i)
  162. {
  163. if(*i == provider)
  164. {
  165. instance->font_provider_table.erase(i);
  166. return;
  167. }
  168. }
  169. }
  170. }
  171. }