FontDatabase.cpp 6.5 KB

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