FontProvider.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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-2023 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 "FontProvider.h"
  29. #include "../../../Include/RmlUi/Core/Core.h"
  30. #include "../../../Include/RmlUi/Core/FileInterface.h"
  31. #include "../../../Include/RmlUi/Core/Log.h"
  32. #include "../../../Include/RmlUi/Core/Math.h"
  33. #include "../../../Include/RmlUi/Core/StringUtilities.h"
  34. #include "../ComputeProperty.h"
  35. #include "FontFace.h"
  36. #include "FontFamily.h"
  37. #include "FreeTypeInterface.h"
  38. #include <algorithm>
  39. namespace Rml {
  40. static FontProvider* g_font_provider = nullptr;
  41. FontProvider::FontProvider()
  42. {
  43. RMLUI_ASSERT(!g_font_provider);
  44. }
  45. FontProvider::~FontProvider()
  46. {
  47. RMLUI_ASSERT(g_font_provider == this);
  48. }
  49. bool FontProvider::Initialise()
  50. {
  51. RMLUI_ASSERT(!g_font_provider);
  52. if (!FreeType::Initialise())
  53. return false;
  54. g_font_provider = new FontProvider;
  55. return true;
  56. }
  57. void FontProvider::Shutdown()
  58. {
  59. RMLUI_ASSERT(g_font_provider);
  60. delete g_font_provider;
  61. g_font_provider = nullptr;
  62. FreeType::Shutdown();
  63. }
  64. FontProvider& FontProvider::Get()
  65. {
  66. RMLUI_ASSERT(g_font_provider);
  67. return *g_font_provider;
  68. }
  69. FontFaceHandleDefault* FontProvider::GetFontFaceHandle(const String& family, Style::FontStyle style, Style::FontWeight weight, int size)
  70. {
  71. RMLUI_ASSERTMSG(family == StringUtilities::ToLower(family), "Font family name must be converted to lowercase before entering here.");
  72. FontFamilyMap& families = Get().font_families;
  73. auto it = families.find(family);
  74. if (it == families.end())
  75. return nullptr;
  76. return it->second->GetFaceHandle(style, weight, size);
  77. }
  78. int FontProvider::CountFallbackFontFaces()
  79. {
  80. return (int)Get().fallback_font_faces.size();
  81. }
  82. FontFaceHandleDefault* FontProvider::GetFallbackFontFace(int index, int font_size)
  83. {
  84. auto& faces = FontProvider::Get().fallback_font_faces;
  85. if (index >= 0 && index < (int)faces.size())
  86. return faces[index]->GetHandle(font_size, false);
  87. return nullptr;
  88. }
  89. void FontProvider::ReleaseFontResources()
  90. {
  91. RMLUI_ASSERT(g_font_provider);
  92. for (auto& name_family : g_font_provider->font_families)
  93. name_family.second->ReleaseFontResources();
  94. }
  95. bool FontProvider::LoadFontFace(const String& file_name, bool fallback_face, Style::FontWeight weight)
  96. {
  97. FileInterface* file_interface = GetFileInterface();
  98. FileHandle handle = file_interface->Open(file_name);
  99. if (!handle)
  100. {
  101. Log::Message(Log::LT_ERROR, "Failed to load font face from %s, could not open file.", file_name.c_str());
  102. return false;
  103. }
  104. size_t length = file_interface->Length(handle);
  105. auto buffer_ptr = UniquePtr<byte[]>(new byte[length]);
  106. byte* buffer = buffer_ptr.get();
  107. file_interface->Read(buffer, length, handle);
  108. file_interface->Close(handle);
  109. bool result = Get().LoadFontFace(buffer, (int)length, fallback_face, std::move(buffer_ptr), file_name, {}, Style::FontStyle::Normal, weight);
  110. return result;
  111. }
  112. bool FontProvider::LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
  113. bool fallback_face)
  114. {
  115. const String source = "memory";
  116. bool result = Get().LoadFontFace(data, data_size, fallback_face, nullptr, source, font_family, style, weight);
  117. return result;
  118. }
  119. bool FontProvider::LoadFontFace(const byte* data, int data_size, bool fallback_face, UniquePtr<byte[]> face_memory, const String& source,
  120. String font_family, Style::FontStyle style, Style::FontWeight weight)
  121. {
  122. using Style::FontWeight;
  123. Vector<FaceVariation> face_variations;
  124. if (!FreeType::GetFaceVariations(data, data_size, face_variations))
  125. {
  126. Log::Message(Log::LT_ERROR, "Failed to load font face from '%s': Invalid or unsupported font face file format.", source.c_str());
  127. return false;
  128. }
  129. Vector<FaceVariation> load_variations;
  130. if (face_variations.empty())
  131. {
  132. load_variations.push_back(FaceVariation{Style::FontWeight::Auto, 0, 0});
  133. }
  134. else
  135. {
  136. // Iterate through all the face variations and pick the ones to load. The list is already sorted by (weight, width). When weight is set to
  137. // 'auto' we load all the weights of the face. However, we only want to load one width for each weight.
  138. for (auto it = face_variations.begin(); it != face_variations.end();)
  139. {
  140. if (weight != FontWeight::Auto && it->weight != weight)
  141. {
  142. ++it;
  143. continue;
  144. }
  145. // We don't currently have any way for users to select widths, so we search for a regular (medium) value here.
  146. constexpr int search_width = 100;
  147. const FontWeight current_weight = it->weight;
  148. int best_width_distance = Math::Absolute((int)it->width - search_width);
  149. auto it_best_width = it;
  150. // Search forward to find the best 'width' with the same weight.
  151. for (++it; it != face_variations.end(); ++it)
  152. {
  153. if (it->weight != current_weight)
  154. break;
  155. const int width_distance = Math::Absolute((int)it->width - search_width);
  156. if (width_distance < best_width_distance)
  157. {
  158. best_width_distance = width_distance;
  159. it_best_width = it;
  160. }
  161. }
  162. load_variations.push_back(*it_best_width);
  163. }
  164. }
  165. if (load_variations.empty())
  166. {
  167. Log::Message(Log::LT_ERROR, "Failed to load font face from '%s': Could not locate face with weight %d.", source.c_str(), (int)weight);
  168. return false;
  169. }
  170. for (const FaceVariation& variation : load_variations)
  171. {
  172. FontFaceHandleFreetype ft_face = FreeType::LoadFace(data, data_size, source, variation.named_instance_index);
  173. if (!ft_face)
  174. return false;
  175. if (font_family.empty())
  176. FreeType::GetFaceStyle(ft_face, &font_family, &style, nullptr);
  177. if (weight == FontWeight::Auto)
  178. FreeType::GetFaceStyle(ft_face, nullptr, nullptr, &weight);
  179. const FontWeight variation_weight = (variation.weight == FontWeight::Auto ? weight : variation.weight);
  180. const String font_face_description = GetFontFaceDescription(font_family, style, variation_weight);
  181. if (!AddFace(ft_face, font_family, style, variation_weight, fallback_face, std::move(face_memory)))
  182. {
  183. Log::Message(Log::LT_ERROR, "Failed to load font face %s from '%s'.", font_face_description.c_str(), source.c_str());
  184. return false;
  185. }
  186. Log::Message(Log::LT_INFO, "Loaded font face %s from '%s'.", font_face_description.c_str(), source.c_str());
  187. }
  188. return true;
  189. }
  190. bool FontProvider::AddFace(FontFaceHandleFreetype face, const String& family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face,
  191. UniquePtr<byte[]> face_memory)
  192. {
  193. if (family.empty() || weight == Style::FontWeight::Auto)
  194. return false;
  195. String family_lower = StringUtilities::ToLower(family);
  196. FontFamily* font_family = nullptr;
  197. auto it = font_families.find(family_lower);
  198. if (it != font_families.end())
  199. {
  200. font_family = (FontFamily*)it->second.get();
  201. }
  202. else
  203. {
  204. auto font_family_ptr = MakeUnique<FontFamily>(family_lower);
  205. font_family = font_family_ptr.get();
  206. font_families[family_lower] = std::move(font_family_ptr);
  207. }
  208. FontFace* font_face_result = font_family->AddFace(face, style, weight, std::move(face_memory));
  209. if (font_face_result && fallback_face)
  210. {
  211. auto it_fallback_face = std::find(fallback_font_faces.begin(), fallback_font_faces.end(), font_face_result);
  212. if (it_fallback_face == fallback_font_faces.end())
  213. {
  214. fallback_font_faces.push_back(font_face_result);
  215. }
  216. }
  217. return static_cast<bool>(font_face_result);
  218. }
  219. } // namespace Rml