FontFace.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "FontFace.h"
  29. #include "FontFaceHandle.h"
  30. #include <Rocket/Core/Log.h>
  31. namespace Rocket {
  32. namespace Core {
  33. FontFace::FontFace(FT_Face _face, Font::Style _style, Font::Weight _weight, bool _release_stream)
  34. {
  35. face = _face;
  36. style = _style;
  37. weight = _weight;
  38. release_stream = _release_stream;
  39. }
  40. FontFace::~FontFace()
  41. {
  42. for (HandleMap::iterator iterator = handles.begin(); iterator != handles.end(); ++iterator)
  43. {
  44. HandleList& handle_list = (*iterator).second;
  45. for (size_t i = 0; i < handle_list.size(); ++i)
  46. handle_list[i]->RemoveReference();
  47. }
  48. ReleaseFace();
  49. }
  50. // Returns the style of the font face.
  51. Font::Style FontFace::GetStyle() const
  52. {
  53. return style;
  54. }
  55. // Returns the weight of the font face.
  56. Font::Weight FontFace::GetWeight() const
  57. {
  58. return weight;
  59. }
  60. // Returns a handle for positioning and rendering this face at the given size.
  61. FontFaceHandle* FontFace::GetHandle(const String& _raw_charset, int size)
  62. {
  63. UnicodeRangeList charset;
  64. HandleMap::iterator iterator = handles.find(size);
  65. if (iterator != handles.end())
  66. {
  67. const HandleList& handles = (*iterator).second;
  68. // Check all the handles if their charsets match the requested one exactly (ie, were specified by the same
  69. // string).
  70. String raw_charset(_raw_charset);
  71. for (size_t i = 0; i < handles.size(); ++i)
  72. {
  73. if (handles[i]->GetRawCharset() == _raw_charset)
  74. {
  75. handles[i]->AddReference();
  76. return handles[i];
  77. }
  78. }
  79. // Check all the handles if their charsets contain the requested charset.
  80. if (!UnicodeRange::BuildList(charset, raw_charset))
  81. {
  82. Log::Message(Log::LT_ERROR, "Invalid font charset '%s'.", _raw_charset.CString());
  83. return NULL;
  84. }
  85. for (size_t i = 0; i < handles.size(); ++i)
  86. {
  87. bool range_contained = true;
  88. const UnicodeRangeList& handle_charset = handles[i]->GetCharset();
  89. for (size_t j = 0; j < charset.size() && range_contained; ++j)
  90. {
  91. if (!charset[j].IsContained(handle_charset))
  92. range_contained = false;
  93. }
  94. if (range_contained)
  95. {
  96. handles[i]->AddReference();
  97. return handles[i];
  98. }
  99. }
  100. }
  101. // See if this face has been released.
  102. if (face == NULL)
  103. {
  104. Log::Message(Log::LT_WARNING, "Font face has been released, unable to generate new handle.");
  105. return NULL;
  106. }
  107. // Construct and initialise the new handle.
  108. FontFaceHandle* handle = new FontFaceHandle();
  109. if (!handle->Initialise(face, _raw_charset, size))
  110. {
  111. handle->RemoveReference();
  112. return NULL;
  113. }
  114. // Save the handle, and add a reference for the callee. The initial reference will be removed when the font face
  115. // releases it.
  116. if (iterator != handles.end())
  117. (*iterator).second.push_back(handle);
  118. else
  119. handles[size] = HandleList(1, handle);
  120. handle->AddReference();
  121. return handle;
  122. }
  123. // Releases the face's FreeType face structure.
  124. void FontFace::ReleaseFace()
  125. {
  126. if (face != NULL)
  127. {
  128. FT_Byte* face_memory = face->stream->base;
  129. FT_Done_Face(face);
  130. if (release_stream)
  131. delete[] face_memory;
  132. face = NULL;
  133. }
  134. }
  135. }
  136. }