FontProvider.cpp 5.8 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/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. // TODO: Loading from memory
  115. return false;
  116. }
  117. // Adds a new font face to the database, loading from memory.
  118. bool FontProvider::LoadFontFace(const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  119. {
  120. // TODO Loading from memory
  121. return false;
  122. }
  123. // Adds a loaded face to the appropriate font family.
  124. bool FontProvider::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
  125. {
  126. Rocket::Core::FontFamily* font_family = NULL;
  127. FontFamilyMap::iterator iterator = instance->font_families.find(family);
  128. if (iterator != instance->font_families.end())
  129. font_family = (*iterator).second;
  130. else
  131. {
  132. font_family = new FontFamily(family);
  133. instance->font_families[family] = font_family;
  134. }
  135. return font_family->AddFace((BitmapFontDefinitions *) face, style, weight, release_stream);
  136. return true;
  137. }
  138. // Loads a FreeType face.
  139. void* FontProvider::LoadFace(const String& file_name)
  140. {
  141. BitmapFontDefinitions *bm_face = new BitmapFontDefinitions();
  142. FontParser parser( bm_face );
  143. FileInterface* file_interface = GetFileInterface();
  144. FileHandle handle = file_interface->Open(file_name);
  145. if (!handle)
  146. {
  147. return NULL;
  148. }
  149. size_t length = file_interface->Length(handle);
  150. byte* buffer = new byte[length];
  151. file_interface->Read(buffer, length, handle);
  152. file_interface->Close(handle);
  153. StreamMemory* stream = new StreamMemory( buffer, length );
  154. stream->SetSourceURL( file_name );
  155. parser.Parse( stream );
  156. bm_face->Face.Source = file_name;
  157. return bm_face;
  158. }
  159. // Loads a FreeType face from memory.
  160. void* FontProvider::LoadFace(const byte* data, int data_length, const String& source, bool local_data)
  161. {
  162. URL file_url = source + ".fnt";
  163. BitmapFontDefinitions *bm_face = new BitmapFontDefinitions();
  164. FontParser parser( bm_face );
  165. StreamMemory* stream = new StreamMemory( data, data_length );
  166. stream->SetSourceURL( file_url );
  167. parser.Parse( stream );
  168. bm_face->Face.Source = file_url.GetPathedFileName();
  169. return bm_face;
  170. }
  171. }
  172. }
  173. }