FontProvider.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "FontProvider.h"
  29. #include "FontFace.h"
  30. #include "FontFamily.h"
  31. #include "FreeTypeInterface.h"
  32. #include "../LayoutInlineBoxText.h"
  33. #include "../../../Include/RmlUi/Core/Core.h"
  34. #include "../../../Include/RmlUi/Core/FileInterface.h"
  35. #include "../../../Include/RmlUi/Core/Log.h"
  36. #include "../../../Include/RmlUi/Core/StringUtilities.h"
  37. #include <algorithm>
  38. namespace Rml {
  39. static FontProvider* g_font_provider = nullptr;
  40. FontProvider::FontProvider()
  41. {
  42. RMLUI_ASSERT(!g_font_provider);
  43. }
  44. FontProvider::~FontProvider()
  45. {
  46. RMLUI_ASSERT(g_font_provider == this);
  47. }
  48. bool FontProvider::Initialise()
  49. {
  50. RMLUI_ASSERT(!g_font_provider);
  51. if (!FreeType::Initialise())
  52. return false;
  53. g_font_provider = new FontProvider;
  54. return true;
  55. }
  56. void FontProvider::Shutdown()
  57. {
  58. RMLUI_ASSERT(g_font_provider);
  59. delete g_font_provider;
  60. g_font_provider = nullptr;
  61. FreeType::Shutdown();
  62. }
  63. FontProvider& FontProvider::Get()
  64. {
  65. RMLUI_ASSERT(g_font_provider);
  66. return *g_font_provider;
  67. }
  68. FontFaceHandleDefault* FontProvider::GetFontFaceHandle(const String& family, Style::FontStyle style, Style::FontWeight weight, int size)
  69. {
  70. RMLUI_ASSERTMSG(family == StringUtilities::ToLower(family), "Font family name must be converted to lowercase before entering here.");
  71. FontFamilyMap& families = Get().font_families;
  72. auto it = families.find(family);
  73. if (it == families.end())
  74. return nullptr;
  75. return it->second->GetFaceHandle(style, weight, size);
  76. }
  77. int FontProvider::CountFallbackFontFaces()
  78. {
  79. return (int)Get().fallback_font_faces.size();
  80. }
  81. FontFaceHandleDefault* FontProvider::GetFallbackFontFace(int index, int font_size)
  82. {
  83. auto& faces = FontProvider::Get().fallback_font_faces;
  84. if (index >= 0 && index < (int)faces.size())
  85. return faces[index]->GetHandle(font_size, true);
  86. return nullptr;
  87. }
  88. bool FontProvider::LoadFontFace(const String& file_name, bool fallback_face)
  89. {
  90. FileInterface* file_interface = GetFileInterface();
  91. FileHandle handle = file_interface->Open(file_name);
  92. if (!handle)
  93. {
  94. Log::Message(Log::LT_ERROR, "Failed to load font face from %s, could not open file.", file_name.c_str());
  95. return false;
  96. }
  97. size_t length = file_interface->Length(handle);
  98. auto buffer_ptr = UniquePtr<byte[]>(new byte[length]);
  99. byte* buffer = buffer_ptr.get();
  100. file_interface->Read(buffer, length, handle);
  101. file_interface->Close(handle);
  102. bool result = Get().LoadFontFace(buffer, (int)length, fallback_face, std::move(buffer_ptr), file_name);
  103. return result;
  104. }
  105. bool FontProvider::LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  106. {
  107. const String source = "memory";
  108. bool result = Get().LoadFontFace(data, data_size, fallback_face, nullptr, source, font_family, style, weight);
  109. return result;
  110. }
  111. bool FontProvider::LoadFontFace(const byte* data, int data_size, bool fallback_face, UniquePtr<byte[]> face_memory, const String& source,
  112. String font_family, Style::FontStyle style, Style::FontWeight weight)
  113. {
  114. FontFaceHandleFreetype ft_face = FreeType::LoadFace(data, data_size, source);
  115. if (!ft_face)
  116. {
  117. Log::Message(Log::LT_ERROR, "Failed to load font face %s from '%s'.", font_family.c_str(), source.c_str());
  118. return false;
  119. }
  120. if (font_family.empty())
  121. {
  122. FreeType::GetFaceStyle(ft_face, font_family, style, weight);
  123. }
  124. const String font_face_description = FontFaceDescription(font_family, style, weight);
  125. if (!AddFace(ft_face, font_family, style, weight, fallback_face, std::move(face_memory)))
  126. {
  127. Log::Message(Log::LT_ERROR, "Failed to load font face %s from '%s'.", font_face_description.c_str(), source.c_str());
  128. return false;
  129. }
  130. Log::Message(Log::LT_INFO, "Loaded font face %s from '%s'.", font_face_description.c_str(), source.c_str());
  131. return true;
  132. }
  133. bool FontProvider::AddFace(FontFaceHandleFreetype face, const String& family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face, UniquePtr<byte[]> face_memory)
  134. {
  135. String family_lower = StringUtilities::ToLower(family);
  136. FontFamily* font_family = nullptr;
  137. auto it = font_families.find(family_lower);
  138. if (it != font_families.end())
  139. {
  140. font_family = (FontFamily*)it->second.get();
  141. }
  142. else
  143. {
  144. auto font_family_ptr = MakeUnique<FontFamily>(family_lower);
  145. font_family = font_family_ptr.get();
  146. font_families[family_lower] = std::move(font_family_ptr);
  147. }
  148. FontFace* font_face_result = font_family->AddFace(face, style, weight, std::move(face_memory));
  149. if (font_face_result && fallback_face)
  150. {
  151. auto it_fallback_face = std::find(fallback_font_faces.begin(), fallback_font_faces.end(), font_face_result);
  152. if (it_fallback_face == fallback_font_faces.end())
  153. {
  154. fallback_font_faces.push_back(font_face_result);
  155. }
  156. }
  157. return static_cast<bool>(font_face_result);
  158. }
  159. } // namespace Rml