FontFace.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  30. #include "FontFace.h"
  31. #include "FontFaceHandle.h"
  32. #include <RmlUi/Core/Log.h>
  33. namespace Rml {
  34. namespace Core {
  35. BitmapFont::FontFace::FontFace(BitmapFontDefinitions *_face, Style::FontStyle _style, Style::FontWeight _weight, bool _release_stream) : Rml::Core::FontFace(_style, _weight, _release_stream)
  36. {
  37. face = _face;
  38. }
  39. BitmapFont::FontFace::~FontFace()
  40. {
  41. ReleaseFace();
  42. }
  43. // Returns a handle for positioning and rendering this face at the given size.
  44. SharedPtr<Rml::Core::FontFaceHandleDefault> BitmapFont::FontFace::GetHandle(const String& _raw_charset, int size)
  45. {
  46. UnicodeRangeList charset;
  47. HandleMap::iterator iterator = handles.find(size);
  48. if (iterator != handles.end())
  49. {
  50. const HandleList& handles = (*iterator).second;
  51. // Check all the handles if their charsets match the requested one exactly (ie, were specified by the same
  52. // string).
  53. String raw_charset(_raw_charset);
  54. for (size_t i = 0; i < handles.size(); ++i)
  55. {
  56. if (handles[i]->GetRawCharset() == _raw_charset)
  57. {
  58. return handles[i];
  59. }
  60. }
  61. // Check all the handles if their charsets contain the requested charset.
  62. if (!UnicodeRange::BuildList(charset, raw_charset))
  63. {
  64. Log::Message(Log::LT_ERROR, "Invalid font charset '%s'.", _raw_charset.c_str());
  65. return nullptr;
  66. }
  67. for (size_t i = 0; i < handles.size(); ++i)
  68. {
  69. bool range_contained = true;
  70. const UnicodeRangeList& handle_charset = handles[i]->GetCharset();
  71. for (size_t j = 0; j < charset.size() && range_contained; ++j)
  72. {
  73. if (!charset[j].IsContained(handle_charset))
  74. range_contained = false;
  75. }
  76. if (range_contained)
  77. {
  78. return handles[i];
  79. }
  80. }
  81. }
  82. // See if this face has been released.
  83. if (!face)
  84. {
  85. Log::Message(Log::LT_WARNING, "Font face has been released, unable to generate new handle.");
  86. return nullptr;
  87. }
  88. // Construct and initialise the new handle.
  89. auto handle = std::make_shared<BitmapFont::FontFaceHandle>();
  90. if (!handle->Initialise(face, _raw_charset, size))
  91. {
  92. return nullptr;
  93. }
  94. // Save the handle, and add a reference for the callee. The initial reference will be removed when the font face
  95. // releases it.
  96. if (iterator != handles.end())
  97. (*iterator).second.push_back(handle);
  98. else
  99. handles[size] = HandleList(1, handle);
  100. return handle;
  101. }
  102. // Releases the face's structure.
  103. void BitmapFont::FontFace::ReleaseFace()
  104. {
  105. delete face;
  106. }
  107. }
  108. }
  109. #endif