FontDatabase.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "FontFamily.h"
  30. #include <Rocket/Core.h>
  31. #include <ft2build.h>
  32. #include FT_FREETYPE_H
  33. namespace Rocket {
  34. namespace Core {
  35. FontDatabase* FontDatabase::instance = NULL;
  36. typedef std::map< String, FontEffect* > FontEffectCache;
  37. FontEffectCache font_effect_cache;
  38. static FT_Library ft_library = NULL;
  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. FT_Error result = FT_Init_FreeType(&ft_library);
  55. if (result != 0)
  56. {
  57. Log::Message(Log::LT_ERROR, "Failed to initialise FreeType, error %d.", result);
  58. Shutdown();
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. void FontDatabase::Shutdown()
  65. {
  66. if (instance != NULL)
  67. {
  68. for (FontFamilyMap::iterator i = instance->font_families.begin(); i != instance->font_families.end(); ++i)
  69. delete (*i).second;
  70. if (ft_library != NULL)
  71. {
  72. FT_Done_FreeType(ft_library);
  73. ft_library = NULL;
  74. }
  75. delete instance;
  76. }
  77. }
  78. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  79. bool FontDatabase::LoadFontFace(const String& file_name)
  80. {
  81. FT_Face ft_face = (FT_Face) instance->LoadFace(file_name);
  82. if (ft_face == NULL)
  83. {
  84. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.CString());
  85. return false;
  86. }
  87. Font::Style style = ft_face->style_flags & FT_STYLE_FLAG_ITALIC ? Font::STYLE_ITALIC : Font::STYLE_NORMAL;
  88. Font::Weight weight = ft_face->style_flags & FT_STYLE_FLAG_BOLD ? Font::WEIGHT_BOLD : Font::WEIGHT_NORMAL;
  89. if (instance->AddFace(ft_face, ft_face->family_name, style, weight, true))
  90. {
  91. Log::Message(Log::LT_INFO, "Loaded font face %s %s (from %s).", ft_face->family_name, ft_face->style_name, file_name.CString());
  92. return true;
  93. }
  94. else
  95. {
  96. Log::Message(Log::LT_ERROR, "Failed to load font face %s %s (from %s).", ft_face->family_name, ft_face->style_name, file_name.CString());
  97. return false;
  98. }
  99. }
  100. // Loads a new font face.
  101. bool FontDatabase::LoadFontFace(const String& file_name, const String& family, Font::Style style, Font::Weight weight)
  102. {
  103. FT_Face ft_face = (FT_Face) instance->LoadFace(file_name);
  104. if (ft_face == NULL)
  105. {
  106. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.CString());
  107. return false;
  108. }
  109. if (instance->AddFace(ft_face, family, style, weight, true))
  110. {
  111. Log::Message(Log::LT_INFO, "Loaded font face %s %s (from %s).", ft_face->family_name, ft_face->style_name, file_name.CString());
  112. return true;
  113. }
  114. else
  115. {
  116. Log::Message(Log::LT_ERROR, "Failed to load font face %s %s (from %s).", ft_face->family_name, ft_face->style_name, file_name.CString());
  117. return false;
  118. }
  119. }
  120. // Adds a new font face to the database, loading from memory.
  121. bool FontDatabase::LoadFontFace(const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  122. {
  123. FT_Face ft_face = (FT_Face) instance->LoadFace(data, data_length, "memory", false);
  124. if (ft_face == NULL)
  125. {
  126. Log::Message(Log::LT_ERROR, "Failed to load font face from byte stream.");
  127. return false;
  128. }
  129. if (instance->AddFace(ft_face, family, style, weight, false))
  130. {
  131. Log::Message(Log::LT_INFO, "Loaded font face %s %s (from byte stream).", ft_face->family_name, ft_face->style_name);
  132. return true;
  133. }
  134. else
  135. {
  136. Log::Message(Log::LT_ERROR, "Failed to load font face %s %s (from byte stream).", ft_face->family_name, ft_face->style_name);
  137. return false;
  138. }
  139. }
  140. // Returns a handle to a font face that can be used to position and render text.
  141. FontFaceHandle* FontDatabase::GetFontFaceHandle(const String& family, const String& charset, Font::Style style, Font::Weight weight, int size)
  142. {
  143. FontFamilyMap::iterator iterator = instance->font_families.find(family);
  144. if (iterator == instance->font_families.end())
  145. return NULL;
  146. return (*iterator).second->GetFaceHandle(charset, style, weight, size);
  147. }
  148. // Returns a font effect, either a newly-instanced effect from the factory or an identical shared
  149. // effect.
  150. FontEffect* FontDatabase::GetFontEffect(const String& name, const PropertyDictionary& properties)
  151. {
  152. // The caching here should be moved into the Factory for optimal behaviour. This system has a
  153. // few shortfalls:
  154. // * ignores default properties
  155. // * could be shared with decorators as well
  156. // Generate a key so we can distinguish unique property sets quickly.
  157. typedef std::list< std::pair< String, String > > PropertyList;
  158. PropertyList sorted_properties;
  159. for (PropertyMap::const_iterator property_iterator = properties.GetProperties().begin(); property_iterator != properties.GetProperties().end(); ++property_iterator)
  160. {
  161. // Skip the font-effect declaration.
  162. if (property_iterator->first == "font-effect")
  163. continue;
  164. PropertyList::iterator insert = sorted_properties.begin();
  165. while (insert != sorted_properties.end() &&
  166. insert->first < property_iterator->first)
  167. ++insert;
  168. sorted_properties.insert(insert, PropertyList::value_type(property_iterator->first, property_iterator->second.Get< String >()));
  169. }
  170. // Generate the font effect's key from the properties.
  171. String key = name + ";";
  172. for (PropertyList::iterator i = sorted_properties.begin(); i != sorted_properties.end(); ++i)
  173. key += i->first + ":" + i->second + ";";
  174. // Check if we have a previously instanced effect.
  175. FontEffectCache::iterator i = font_effect_cache.find(key);
  176. if (i != font_effect_cache.end())
  177. {
  178. FontEffect* effect = i->second;
  179. effect->AddReference();
  180. return effect;
  181. }
  182. FontEffect* font_effect = Factory::InstanceFontEffect(name, properties);
  183. if (font_effect == NULL)
  184. return NULL;
  185. font_effect_cache[key] = font_effect;
  186. return font_effect;
  187. }
  188. // Removes a font effect from the font database's cache.
  189. void FontDatabase::ReleaseFontEffect(const FontEffect* effect)
  190. {
  191. for (FontEffectCache::iterator i = font_effect_cache.begin(); i != font_effect_cache.end(); ++i)
  192. {
  193. if (i->second == effect)
  194. {
  195. font_effect_cache.erase(i);
  196. return;
  197. }
  198. }
  199. }
  200. // Adds a loaded face to the appropriate font family.
  201. bool FontDatabase::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
  202. {
  203. FontFamily* font_family = NULL;
  204. FontFamilyMap::iterator iterator = instance->font_families.find(family);
  205. if (iterator != instance->font_families.end())
  206. font_family = (*iterator).second;
  207. else
  208. {
  209. font_family = new FontFamily(family);
  210. instance->font_families[family] = font_family;
  211. }
  212. return font_family->AddFace((FT_Face) face, style, weight, release_stream);
  213. }
  214. // Loads a FreeType face.
  215. void* FontDatabase::LoadFace(const String& file_name)
  216. {
  217. FileInterface* file_interface = GetFileInterface();
  218. FileHandle handle = file_interface->Open(file_name);
  219. if (!handle)
  220. {
  221. return false;
  222. }
  223. // Seek to the end of the file so we can determine its size.
  224. if (!file_interface->Seek(handle, 0, SEEK_END))
  225. {
  226. file_interface->Close(handle);
  227. return false;
  228. }
  229. size_t length = file_interface->Tell(handle);
  230. file_interface->Seek(handle, 0, SEEK_SET);
  231. FT_Byte* buffer = new FT_Byte[length];
  232. file_interface->Read(buffer, length, handle);
  233. file_interface->Close(handle);
  234. return LoadFace(buffer, length, file_name, true);
  235. }
  236. // Loads a FreeType face from memory.
  237. void* FontDatabase::LoadFace(const byte* data, int data_length, const String& source, bool local_data)
  238. {
  239. FT_Face face = NULL;
  240. int error = FT_New_Memory_Face(ft_library, (const FT_Byte*) data, data_length, 0, &face);
  241. if (error != 0)
  242. {
  243. Log::Message(Log::LT_ERROR, "FreeType error %d while loading face from %s.", error, source.CString());
  244. if (local_data)
  245. delete[] data;
  246. return NULL;
  247. }
  248. // Initialise the character mapping on the face.
  249. if (face->charmap == NULL)
  250. {
  251. FT_Select_Charmap(face, FT_ENCODING_APPLE_ROMAN);
  252. if (face->charmap == NULL)
  253. {
  254. Log::Message(Log::LT_ERROR, "Font face (from %s) does not contain a Unicode or Apple Roman character map.", source.CString());
  255. FT_Done_Face(face);
  256. if (local_data)
  257. delete[] data;
  258. return NULL;
  259. }
  260. }
  261. return face;
  262. }
  263. }
  264. }