FontProvider.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include <RmlUi/Core/BitmapFont/FontProvider.h>
  30. #include "../FontFaceHandle.h"
  31. #include <RmlUi/Core/FontDatabase.h>
  32. #include <RmlUi/Core/StreamMemory.h>
  33. #include "FontFamily.h"
  34. #include <RmlUi/Core.h>
  35. #include "BitmapFontDefinitions.h"
  36. #include "FontParser.h"
  37. namespace Rml {
  38. namespace Core {
  39. namespace BitmapFont {
  40. FontProvider* FontProvider::instance = NULL;
  41. FontProvider::FontProvider()
  42. {
  43. RMLUI_ASSERT(instance == NULL);
  44. instance = this;
  45. }
  46. FontProvider::~FontProvider()
  47. {
  48. RMLUI_ASSERT(instance == this);
  49. instance = NULL;
  50. }
  51. bool FontProvider::Initialise()
  52. {
  53. if (instance == NULL)
  54. {
  55. new FontProvider();
  56. FontDatabase::AddFontProvider(instance);
  57. }
  58. return true;
  59. }
  60. void FontProvider::Shutdown()
  61. {
  62. if (instance != NULL)
  63. {
  64. FontDatabase::RemoveFontProvider(instance);
  65. delete instance;
  66. instance = NULL;
  67. }
  68. }
  69. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  70. bool FontProvider::LoadFontFace(const String& file_name)
  71. {
  72. BitmapFontDefinitions *bm_font = (BitmapFontDefinitions*) instance->LoadFace(file_name);
  73. if (bm_font == NULL)
  74. {
  75. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.c_str());
  76. return false;
  77. }
  78. Font::Style style = bm_font->Face.Style;
  79. Font::Weight weight = bm_font->Face.Weight;
  80. if (instance->AddFace(bm_font, bm_font->Face.FamilyName, style, weight, true))
  81. {
  82. Log::Message(Log::LT_INFO, "Loaded font face %s (from %s).", bm_font->Face.FamilyName.c_str(), file_name.c_str());
  83. return true;
  84. }
  85. else
  86. {
  87. Log::Message(Log::LT_ERROR, "Failed to load font face %s (from %s).", bm_font->Face.FamilyName.c_str(), file_name.c_str());
  88. return false;
  89. }
  90. return true;
  91. }
  92. // Loads a new font face.
  93. bool FontProvider::LoadFontFace(const String& file_name, const String& family, Font::Style style, Font::Weight weight)
  94. {
  95. BitmapFontDefinitions *bm_font = (BitmapFontDefinitions*) instance->LoadFace(file_name);
  96. if (bm_font == NULL)
  97. {
  98. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.c_str());
  99. return false;
  100. }
  101. if (instance->AddFace(bm_font, family, style, weight, true))
  102. {
  103. Log::Message(Log::LT_INFO, "Loaded font face %s (from %s).", bm_font->Face.FamilyName.c_str(), file_name.c_str());
  104. return true;
  105. }
  106. else
  107. {
  108. Log::Message(Log::LT_ERROR, "Failed to load font face %s (from %s).", bm_font->Face.FamilyName.c_str(), file_name.c_str());
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool FontProvider::LoadFontFace(const byte* data, int data_length)
  114. {
  115. // TODO: Loading from memory
  116. return false;
  117. }
  118. // Adds a new font face to the database, loading from memory.
  119. bool FontProvider::LoadFontFace(const byte* data, int data_length, const String& family, Font::Style style, Font::Weight weight)
  120. {
  121. // TODO Loading from memory
  122. return false;
  123. }
  124. // Adds a loaded face to the appropriate font family.
  125. bool FontProvider::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
  126. {
  127. String family_lower = ToLower(family);
  128. Rml::Core::FontFamily* font_family = NULL;
  129. FontFamilyMap::iterator iterator = instance->font_families.find(family_lower);
  130. if (iterator != instance->font_families.end())
  131. font_family = (*iterator).second;
  132. else
  133. {
  134. font_family = new FontFamily(family_lower);
  135. instance->font_families[family_lower] = font_family;
  136. }
  137. return font_family->AddFace((BitmapFontDefinitions *) face, style, weight, release_stream);
  138. return true;
  139. }
  140. // Loads a FreeType face.
  141. void* FontProvider::LoadFace(const String& file_name)
  142. {
  143. BitmapFontDefinitions *bm_face = new BitmapFontDefinitions();
  144. FontParser parser( bm_face );
  145. FileInterface* file_interface = GetFileInterface();
  146. FileHandle handle = file_interface->Open(file_name);
  147. if (!handle)
  148. {
  149. return NULL;
  150. }
  151. size_t length = file_interface->Length(handle);
  152. byte* buffer = new byte[length];
  153. file_interface->Read(buffer, length, handle);
  154. file_interface->Close(handle);
  155. StreamMemory* stream = new StreamMemory( buffer, length );
  156. stream->SetSourceURL( file_name );
  157. parser.Parse( stream );
  158. bm_face->Face.Source = file_name;
  159. return bm_face;
  160. }
  161. // Loads a FreeType face from memory.
  162. void* FontProvider::LoadFace(const byte* data, int data_length, const String& source, bool local_data)
  163. {
  164. URL file_url = source + ".fnt";
  165. BitmapFontDefinitions *bm_face = new BitmapFontDefinitions();
  166. FontParser parser( bm_face );
  167. StreamMemory* stream = new StreamMemory( data, data_length );
  168. stream->SetSourceURL( file_url );
  169. parser.Parse( stream );
  170. bm_face->Face.Source = file_url.GetPathedFileName();
  171. return bm_face;
  172. }
  173. }
  174. }
  175. }