FontProvider.cpp 8.6 KB

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