FontDatabase.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. namespace Rml {
  34. namespace Core {
  35. FontDatabase* FontDatabase::instance = nullptr;
  36. FontDatabase::FontProviderTable FontDatabase::font_provider_table;
  37. FontDatabase::FontDatabase()
  38. {
  39. RMLUI_ASSERT(instance == nullptr);
  40. instance = this;
  41. }
  42. FontDatabase::~FontDatabase()
  43. {
  44. RMLUI_ASSERT(instance == this);
  45. instance = nullptr;
  46. }
  47. bool FontDatabase::Initialise()
  48. {
  49. if (instance == nullptr)
  50. {
  51. new FontDatabase();
  52. if(!FreeType::FontProvider::Initialise())
  53. return false;
  54. }
  55. return true;
  56. }
  57. void FontDatabase::Shutdown()
  58. {
  59. if (instance != nullptr)
  60. {
  61. FreeType::FontProvider::Shutdown();
  62. delete instance;
  63. }
  64. }
  65. // Loads a new font face.
  66. bool FontDatabase::LoadFontFace(const String& file_name)
  67. {
  68. return FreeType::FontProvider::LoadFontFace(file_name);
  69. }
  70. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  71. bool FontDatabase::LoadFontFace(const String& file_name, const String& family, Style::FontStyle style, Style::FontWeight weight)
  72. {
  73. return FreeType::FontProvider::LoadFontFace(file_name, family, style, weight);
  74. }
  75. // Adds a new font face to the database, loading from memory.
  76. bool FontDatabase::LoadFontFace(const byte* data, int data_length)
  77. {
  78. return FreeType::FontProvider::LoadFontFace(data, data_length);
  79. }
  80. // Adds a new font face to the database, loading from memory, ignoring any family, style and weight information stored in the face itself.
  81. bool FontDatabase::LoadFontFace(const byte* data, int data_length, const String& family, Style::FontStyle style, Style::FontWeight weight)
  82. {
  83. return FreeType::FontProvider::LoadFontFace(data, data_length, family, style, weight);
  84. }
  85. // Returns a handle to a font face that can be used to position and render text.
  86. SharedPtr<FontFaceHandle> FontDatabase::GetFontFaceHandle(const String& family, const String& charset, Style::FontStyle style, Style::FontWeight weight, int size)
  87. {
  88. size_t provider_count = font_provider_table.size();
  89. for(size_t provider_index = 0; provider_index < provider_count; ++provider_index)
  90. {
  91. SharedPtr<FontFaceHandle> face_handle = font_provider_table[ provider_index ]->GetFontFaceHandle(family, charset, style, weight, size);
  92. if(face_handle)
  93. return face_handle;
  94. }
  95. return nullptr;
  96. }
  97. void FontDatabase::AddFontProvider(FontProvider * provider)
  98. {
  99. instance->font_provider_table.push_back(provider);
  100. }
  101. void FontDatabase::RemoveFontProvider(FontProvider * provider)
  102. {
  103. for(FontProviderTable::iterator i = instance->font_provider_table.begin(); i != instance->font_provider_table.end(); ++i)
  104. {
  105. if(*i == provider)
  106. {
  107. instance->font_provider_table.erase(i);
  108. return;
  109. }
  110. }
  111. }
  112. }
  113. }