FontProvider.cpp 5.6 KB

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