FontProvider.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/BitmapFont/FontProvider.h>
  29. #include "../FontFaceHandle.h"
  30. #include <Rocket/Core/FontDatabase.h>
  31. #include <Rocket/Core/StreamMemory.h>
  32. #include "FontFamily.h"
  33. #include <Rocket/Core.h>
  34. #include "BitmapFontDefinitions.h"
  35. #include "FontParser.h"
  36. namespace Rocket {
  37. namespace Core {
  38. namespace BitmapFont {
  39. FontProvider* FontProvider::instance = NULL;
  40. FontProvider::FontProvider()
  41. {
  42. ROCKET_ASSERT(instance == NULL);
  43. instance = this;
  44. }
  45. FontProvider::~FontProvider()
  46. {
  47. ROCKET_ASSERT(instance == this);
  48. instance = NULL;
  49. }
  50. bool FontProvider::Initialise()
  51. {
  52. if (instance == NULL)
  53. {
  54. new FontProvider();
  55. FontDatabase::AddFontProvider(instance);
  56. }
  57. return true;
  58. }
  59. void FontProvider::Shutdown()
  60. {
  61. if (instance != NULL)
  62. {
  63. FontDatabase::RemoveFontProvider(instance);
  64. delete instance;
  65. instance = NULL;
  66. }
  67. }
  68. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  69. bool FontProvider::LoadFontFace(const String& file_name)
  70. {
  71. BitmapFontDefinitions *bm_font = (BitmapFontDefinitions*) instance->LoadFace(file_name);
  72. if (bm_font == NULL)
  73. {
  74. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.CString());
  75. return false;
  76. }
  77. Font::Style style = bm_font->Face.Style;
  78. Font::Weight weight = bm_font->Face.Weight;
  79. if (instance->AddFace(bm_font, bm_font->Face.FamilyName, style, weight, true))
  80. {
  81. Log::Message(Log::LT_INFO, "Loaded font face %s (from %s).", bm_font->Face.FamilyName.CString(), file_name.CString());
  82. return true;
  83. }
  84. else
  85. {
  86. Log::Message(Log::LT_ERROR, "Failed to load font face %s (from %s).", bm_font->Face.FamilyName.CString(), file_name.CString());
  87. return false;
  88. }
  89. return true;
  90. }
  91. // Loads a new font face.
  92. bool FontProvider::LoadFontFace(const String& file_name, const String& family, Font::Style style, Font::Weight weight)
  93. {
  94. BitmapFontDefinitions *bm_font = (BitmapFontDefinitions*) instance->LoadFace(file_name);
  95. if (bm_font == NULL)
  96. {
  97. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.CString());
  98. return false;
  99. }
  100. if (instance->AddFace(bm_font, family, style, weight, true))
  101. {
  102. Log::Message(Log::LT_INFO, "Loaded font face %s (from %s).", bm_font->Face.FamilyName.CString(), file_name.CString());
  103. return true;
  104. }
  105. else
  106. {
  107. Log::Message(Log::LT_ERROR, "Failed to load font face %s (from %s).", bm_font->Face.FamilyName.CString(), file_name.CString());
  108. return false;
  109. }
  110. return true;
  111. }
  112. bool FontProvider::LoadFontFace(const byte* data, int data_length)
  113. {
  114. // BM_Font *bm_font = (BM_Font*) instance->LoadFace(data, data_length, family, false);
  115. // if (bm_font == NULL)
  116. // {
  117. // Log::Message(Log::LT_ERROR, "Failed to load font face from byte stream.");
  118. // return false;
  119. // }
  120. // if (instance->AddFace(bm_font, bm_font->Face.FamilyName, style, weight, false))
  121. // {
  122. // Log::Message(Log::LT_INFO, "Loaded font face %s (from byte stream).", bm_font->Face.FamilyName.CString());
  123. // return true;
  124. // }
  125. // else
  126. // {
  127. // Log::Message(Log::LT_ERROR, "Failed to load font face %s (from byte stream).", bm_font->Face.FamilyName.CString());
  128. // return false;
  129. // }
  130. return false;
  131. }
  132. // Adds a new font face to the database, loading from memory.
  133. bool FontProvider::LoadFontFace(const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  134. {
  135. // BM_Font *bm_font = (BM_Font*) instance->LoadFace(data, data_length, family, false);
  136. // if (bm_font == NULL)
  137. // {
  138. // Log::Message(Log::LT_ERROR, "Failed to load font face from byte stream.");
  139. // return false;
  140. // }
  141. // if (instance->AddFace(bm_font, family, style, weight, false))
  142. // {
  143. // Log::Message(Log::LT_INFO, "Loaded font face %s (from byte stream).", bm_font->Face.FamilyName.CString());
  144. // return true;
  145. // }
  146. // else
  147. // {
  148. // Log::Message(Log::LT_ERROR, "Failed to load font face %s (from byte stream).", bm_font->Face.FamilyName.CString());
  149. // return false;
  150. // }
  151. }
  152. // Adds a loaded face to the appropriate font family.
  153. bool FontProvider::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
  154. {
  155. Rocket::Core::FontFamily* font_family = NULL;
  156. FontFamilyMap::iterator iterator = instance->font_families.find(family);
  157. if (iterator != instance->font_families.end())
  158. font_family = (*iterator).second;
  159. else
  160. {
  161. font_family = new FontFamily(family);
  162. instance->font_families[family] = font_family;
  163. }
  164. return font_family->AddFace((BitmapFontDefinitions *) face, style, weight, release_stream);
  165. return true;
  166. }
  167. // Loads a FreeType face.
  168. void* FontProvider::LoadFace(const String& file_name)
  169. {
  170. BitmapFontDefinitions *bm_face = new BitmapFontDefinitions();
  171. FontParser parser( bm_face );
  172. FileInterface* file_interface = GetFileInterface();
  173. FileHandle handle = file_interface->Open(file_name);
  174. if (!handle)
  175. {
  176. return NULL;
  177. }
  178. size_t length = file_interface->Length(handle);
  179. byte* buffer = new byte[length];
  180. file_interface->Read(buffer, length, handle);
  181. file_interface->Close(handle);
  182. StreamMemory* stream = new StreamMemory( buffer, length );
  183. stream->SetSourceURL( file_name );
  184. parser.Parse( stream );
  185. bm_face->Face.Source = file_name;
  186. return bm_face;
  187. }
  188. // Loads a FreeType face from memory.
  189. void* FontProvider::LoadFace(const byte* data, int data_length, const String& source, bool local_data)
  190. {
  191. URL file_url = source + ".fnt";
  192. BitmapFontDefinitions *bm_face = new BitmapFontDefinitions();
  193. FontParser parser( bm_face );
  194. StreamMemory* stream = new StreamMemory( data, data_length );
  195. stream->SetSourceURL( file_url );
  196. parser.Parse( stream );
  197. bm_face->Face.Source = file_url.GetPathedFileName();
  198. return bm_face;
  199. }
  200. }
  201. }
  202. }