FontDatabase.cpp 5.7 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 <RmlUi/Core/FontDatabase.h>
  30. #include <RmlUi/Core/FontFamily.h>
  31. #include <RmlUi/Core.h>
  32. #include <RmlUi/Core/FreeType/FontProvider.h>
  33. #include <RmlUi/Core/BitmapFont/FontProvider.h>
  34. namespace Rml {
  35. namespace Core {
  36. FontDatabase* FontDatabase::instance = nullptr;
  37. FontDatabase::FontProviderTable FontDatabase::font_provider_table;
  38. FontDatabase::FontDatabase()
  39. {
  40. RMLUI_ASSERT(instance == nullptr);
  41. instance = this;
  42. }
  43. FontDatabase::~FontDatabase()
  44. {
  45. RMLUI_ASSERT(instance == this);
  46. instance = nullptr;
  47. }
  48. bool FontDatabase::Initialise()
  49. {
  50. if (instance == nullptr)
  51. {
  52. new FontDatabase();
  53. if(!FreeType::FontProvider::Initialise())
  54. return false;
  55. if(!BitmapFont::FontProvider::Initialise())
  56. return false;
  57. }
  58. return true;
  59. }
  60. void FontDatabase::Shutdown()
  61. {
  62. if (instance != nullptr)
  63. {
  64. FreeType::FontProvider::Shutdown();
  65. BitmapFont::FontProvider::Shutdown();
  66. delete instance;
  67. }
  68. }
  69. // Loads a new font face.
  70. bool FontDatabase::LoadFontFace(const String& file_name)
  71. {
  72. FontProviderType font_provider_type = GetFontProviderType(file_name);
  73. switch(font_provider_type)
  74. {
  75. case FreeType:
  76. return FreeType::FontProvider::LoadFontFace(file_name);
  77. case BitmapFont:
  78. return BitmapFont::FontProvider::LoadFontFace(file_name);
  79. default:
  80. return false;
  81. }
  82. }
  83. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  84. bool FontDatabase::LoadFontFace(const String& file_name, const String& family, Style::FontStyle style, Style::FontWeight weight)
  85. {
  86. FontProviderType font_provider_type = GetFontProviderType(file_name);
  87. switch(font_provider_type)
  88. {
  89. case FreeType:
  90. return FreeType::FontProvider::LoadFontFace(file_name, family, style, weight);
  91. case BitmapFont:
  92. return BitmapFont::FontProvider::LoadFontFace(file_name, family, style, weight);
  93. default:
  94. return false;
  95. }
  96. }
  97. // Adds a new font face to the database, loading from memory.
  98. bool FontDatabase::LoadFontFace(FontProviderType font_provider_type, const byte* data, int data_length)
  99. {
  100. switch(font_provider_type)
  101. {
  102. case FreeType:
  103. return FreeType::FontProvider::LoadFontFace(data, data_length);
  104. case BitmapFont:
  105. return BitmapFont::FontProvider::LoadFontFace(data, data_length);
  106. default:
  107. return false;
  108. }
  109. }
  110. // Adds a new font face to the database, loading from memory, ignoring any family, style and weight information stored in the face itself.
  111. bool FontDatabase::LoadFontFace(FontProviderType font_provider_type, const byte* data, int data_length, const String& family, Style::FontStyle style, Style::FontWeight weight)
  112. {
  113. switch(font_provider_type)
  114. {
  115. case FreeType:
  116. return FreeType::FontProvider::LoadFontFace(data, data_length, family, style, weight);
  117. case BitmapFont:
  118. return BitmapFont::FontProvider::LoadFontFace(data, data_length, family, style, weight);
  119. default:
  120. return false;
  121. }
  122. }
  123. FontDatabase::FontProviderType FontDatabase::GetFontProviderType(const String& file_name)
  124. {
  125. if(file_name.find(".fnt") != String::npos)
  126. {
  127. return BitmapFont;
  128. }
  129. else
  130. {
  131. return FreeType;
  132. }
  133. }
  134. // Returns a handle to a font face that can be used to position and render text.
  135. SharedPtr<FontFaceHandle> FontDatabase::GetFontFaceHandle(const String& family, const String& charset, Style::FontStyle style, Style::FontWeight weight, int size)
  136. {
  137. size_t provider_count = font_provider_table.size();
  138. for(size_t provider_index = 0; provider_index < provider_count; ++provider_index)
  139. {
  140. SharedPtr<FontFaceHandle> face_handle = font_provider_table[ provider_index ]->GetFontFaceHandle(family, charset, style, weight, size);
  141. if(face_handle)
  142. return face_handle;
  143. }
  144. return nullptr;
  145. }
  146. void FontDatabase::AddFontProvider(FontProvider * provider)
  147. {
  148. instance->font_provider_table.push_back(provider);
  149. }
  150. void FontDatabase::RemoveFontProvider(FontProvider * provider)
  151. {
  152. for(FontProviderTable::iterator i = instance->font_provider_table.begin(); i != instance->font_provider_table.end(); ++i)
  153. {
  154. if(*i == provider)
  155. {
  156. instance->font_provider_table.erase(i);
  157. return;
  158. }
  159. }
  160. }
  161. }
  162. }