FontDatabase.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. // Loads a new font face.
  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. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  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)
  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. Font::Style style = ft_face->style_flags & FT_STYLE_FLAG_ITALIC ? Font::STYLE_ITALIC : Font::STYLE_NORMAL;
  130. Font::Weight weight = ft_face->style_flags & FT_STYLE_FLAG_BOLD ? Font::WEIGHT_BOLD : Font::WEIGHT_NORMAL;
  131. if (instance->AddFace(ft_face, ft_face->family_name, style, weight, false))
  132. {
  133. Log::Message(Log::LT_INFO, "Loaded font face %s %s (from byte stream).", ft_face->family_name, ft_face->style_name);
  134. return true;
  135. }
  136. else
  137. {
  138. Log::Message(Log::LT_ERROR, "Failed to load font face %s %s (from byte stream).", ft_face->family_name, ft_face->style_name);
  139. return false;
  140. }
  141. }
  142. // Adds a new font face to the database, loading from memory, ignoring any family, style and weight information stored in the face itself.
  143. bool FontDatabase::LoadFontFace(const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  144. {
  145. FT_Face ft_face = (FT_Face) instance->LoadFace(data, data_length, "memory", false);
  146. if (ft_face == NULL)
  147. {
  148. Log::Message(Log::LT_ERROR, "Failed to load font face from byte stream.");
  149. return false;
  150. }
  151. if (instance->AddFace(ft_face, family, style, weight, false))
  152. {
  153. Log::Message(Log::LT_INFO, "Loaded font face %s %s (from byte stream).", ft_face->family_name, ft_face->style_name);
  154. return true;
  155. }
  156. else
  157. {
  158. Log::Message(Log::LT_ERROR, "Failed to load font face %s %s (from byte stream).", ft_face->family_name, ft_face->style_name);
  159. return false;
  160. }
  161. }
  162. // Returns a handle to a font face that can be used to position and render text.
  163. FontFaceHandle* FontDatabase::GetFontFaceHandle(const String& family, const String& charset, Font::Style style, Font::Weight weight, int size)
  164. {
  165. FontFamilyMap::iterator iterator = instance->font_families.find(family);
  166. if (iterator == instance->font_families.end())
  167. return NULL;
  168. return (*iterator).second->GetFaceHandle(charset, style, weight, size);
  169. }
  170. // Returns a font effect, either a newly-instanced effect from the factory or an identical shared
  171. // effect.
  172. FontEffect* FontDatabase::GetFontEffect(const String& name, const PropertyDictionary& properties)
  173. {
  174. // The caching here should be moved into the Factory for optimal behaviour. This system has a
  175. // few shortfalls:
  176. // * ignores default properties
  177. // * could be shared with decorators as well
  178. // Generate a key so we can distinguish unique property sets quickly.
  179. typedef std::list< std::pair< String, String > > PropertyList;
  180. PropertyList sorted_properties;
  181. for (PropertyMap::const_iterator property_iterator = properties.GetProperties().begin(); property_iterator != properties.GetProperties().end(); ++property_iterator)
  182. {
  183. // Skip the font-effect declaration.
  184. if (property_iterator->first == "font-effect")
  185. continue;
  186. PropertyList::iterator insert = sorted_properties.begin();
  187. while (insert != sorted_properties.end() &&
  188. insert->first < property_iterator->first)
  189. ++insert;
  190. sorted_properties.insert(insert, PropertyList::value_type(property_iterator->first, property_iterator->second.Get< String >()));
  191. }
  192. // Generate the font effect's key from the properties.
  193. String key = name + ";";
  194. for (PropertyList::iterator i = sorted_properties.begin(); i != sorted_properties.end(); ++i)
  195. key += i->first + ":" + i->second + ";";
  196. // Check if we have a previously instanced effect.
  197. FontEffectCache::iterator i = font_effect_cache.find(key);
  198. if (i != font_effect_cache.end())
  199. {
  200. FontEffect* effect = i->second;
  201. effect->AddReference();
  202. return effect;
  203. }
  204. FontEffect* font_effect = Factory::InstanceFontEffect(name, properties);
  205. if (font_effect == NULL)
  206. return NULL;
  207. font_effect_cache[key] = font_effect;
  208. return font_effect;
  209. }
  210. // Removes a font effect from the font database's cache.
  211. void FontDatabase::ReleaseFontEffect(const FontEffect* effect)
  212. {
  213. for (FontEffectCache::iterator i = font_effect_cache.begin(); i != font_effect_cache.end(); ++i)
  214. {
  215. if (i->second == effect)
  216. {
  217. font_effect_cache.erase(i);
  218. return;
  219. }
  220. }
  221. }
  222. // Adds a loaded face to the appropriate font family.
  223. bool FontDatabase::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
  224. {
  225. FontFamily* font_family = NULL;
  226. FontFamilyMap::iterator iterator = instance->font_families.find(family);
  227. if (iterator != instance->font_families.end())
  228. font_family = (*iterator).second;
  229. else
  230. {
  231. font_family = new FontFamily(family);
  232. instance->font_families[family] = font_family;
  233. }
  234. return font_family->AddFace((FT_Face) face, style, weight, release_stream);
  235. }
  236. // Loads a FreeType face.
  237. void* FontDatabase::LoadFace(const String& file_name)
  238. {
  239. FileInterface* file_interface = GetFileInterface();
  240. FileHandle handle = file_interface->Open(file_name);
  241. if (!handle)
  242. {
  243. return false;
  244. }
  245. size_t length = file_interface->Length(handle);
  246. FT_Byte* buffer = new FT_Byte[length];
  247. file_interface->Read(buffer, length, handle);
  248. file_interface->Close(handle);
  249. return LoadFace(buffer, length, file_name, true);
  250. }
  251. // Loads a FreeType face from memory.
  252. void* FontDatabase::LoadFace(const byte* data, int data_length, const String& source, bool local_data)
  253. {
  254. FT_Face face = NULL;
  255. int error = FT_New_Memory_Face(ft_library, (const FT_Byte*) data, data_length, 0, &face);
  256. if (error != 0)
  257. {
  258. Log::Message(Log::LT_ERROR, "FreeType error %d while loading face from %s.", error, source.CString());
  259. if (local_data)
  260. delete[] data;
  261. return NULL;
  262. }
  263. // Initialise the character mapping on the face.
  264. if (face->charmap == NULL)
  265. {
  266. FT_Select_Charmap(face, FT_ENCODING_APPLE_ROMAN);
  267. if (face->charmap == NULL)
  268. {
  269. Log::Message(Log::LT_ERROR, "Font face (from %s) does not contain a Unicode or Apple Roman character map.", source.CString());
  270. FT_Done_Face(face);
  271. if (local_data)
  272. delete[] data;
  273. return NULL;
  274. }
  275. }
  276. return face;
  277. }
  278. }
  279. }