FontProvider.cpp 5.8 KB

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