Font.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Core/Context.h"
  23. #include "../../Core/Profiler.h"
  24. #include "../../Graphics/Graphics.h"
  25. #include "../../IO/Deserializer.h"
  26. #include "../../IO/FileSystem.h"
  27. #include "Font.h"
  28. #include "FontFaceBitmap.h"
  29. #include "FontFaceFreeType.h"
  30. #include "../../Resource/ResourceCache.h"
  31. #include "../../Resource/XMLElement.h"
  32. #include "../../Resource/XMLFile.h"
  33. #include "../../DebugNew.h"
  34. namespace Atomic
  35. {
  36. static const int MIN_POINT_SIZE = 1;
  37. static const int MAX_POINT_SIZE = 96;
  38. Font::Font(Context* context) :
  39. Resource(context),
  40. fontDataSize_(0),
  41. absoluteOffset_(IntVector2::ZERO),
  42. scaledOffset_(Vector2::ZERO),
  43. fontType_(FONT_NONE),
  44. sdfFont_(false)
  45. {
  46. }
  47. Font::~Font()
  48. {
  49. // To ensure FreeType deallocates properly, first clear all faces, then release the raw font data
  50. ReleaseFaces();
  51. fontData_.Reset();
  52. }
  53. void Font::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<Font>();
  56. }
  57. bool Font::BeginLoad(Deserializer& source)
  58. {
  59. // In headless mode, do not actually load, just return success
  60. Graphics* graphics = GetSubsystem<Graphics>();
  61. if (!graphics)
  62. return true;
  63. fontType_ = FONT_NONE;
  64. faces_.Clear();
  65. fontDataSize_ = source.GetSize();
  66. if (fontDataSize_)
  67. {
  68. fontData_ = new unsigned char[fontDataSize_];
  69. if (source.Read(&fontData_[0], fontDataSize_) != fontDataSize_)
  70. return false;
  71. }
  72. else
  73. {
  74. fontData_.Reset();
  75. return false;
  76. }
  77. String ext = GetExtension(GetName());
  78. if (ext == ".ttf" || ext == ".otf" || ext == ".woff")
  79. {
  80. fontType_ = FONT_FREETYPE;
  81. LoadParameters();
  82. }
  83. else if (ext == ".xml" || ext == ".fnt" || ext == ".sdf")
  84. fontType_ = FONT_BITMAP;
  85. sdfFont_ = ext == ".sdf";
  86. SetMemoryUse(fontDataSize_);
  87. return true;
  88. }
  89. bool Font::SaveXML(Serializer& dest, int pointSize, bool usedGlyphs, const String& indentation)
  90. {
  91. FontFace* fontFace = GetFace(pointSize);
  92. if (!fontFace)
  93. return false;
  94. PROFILE(FontSaveXML);
  95. SharedPtr<FontFaceBitmap> packedFontFace(new FontFaceBitmap(this));
  96. if (!packedFontFace->Load(fontFace, usedGlyphs))
  97. return false;
  98. return packedFontFace->Save(dest, pointSize, indentation);
  99. }
  100. void Font::SetAbsoluteGlyphOffset(const IntVector2& offset)
  101. {
  102. absoluteOffset_ = offset;
  103. }
  104. void Font::SetScaledGlyphOffset(const Vector2& offset)
  105. {
  106. scaledOffset_ = offset;
  107. }
  108. FontFace* Font::GetFace(int pointSize)
  109. {
  110. // In headless mode, always return null
  111. Graphics* graphics = GetSubsystem<Graphics>();
  112. if (!graphics)
  113. return 0;
  114. // For bitmap font type, always return the same font face provided by the font's bitmap file regardless of the actual requested point size
  115. if (fontType_ == FONT_BITMAP)
  116. pointSize = 0;
  117. else
  118. pointSize = Clamp(pointSize, MIN_POINT_SIZE, MAX_POINT_SIZE);
  119. HashMap<int, SharedPtr<FontFace> >::Iterator i = faces_.Find(pointSize);
  120. if (i != faces_.End())
  121. {
  122. if (!i->second_->IsDataLost())
  123. return i->second_;
  124. else
  125. {
  126. // Erase and reload face if texture data lost (OpenGL mode only)
  127. faces_.Erase(i);
  128. }
  129. }
  130. PROFILE(GetFontFace);
  131. switch (fontType_)
  132. {
  133. case FONT_FREETYPE:
  134. return GetFaceFreeType(pointSize);
  135. case FONT_BITMAP:
  136. return GetFaceBitmap(pointSize);
  137. default:
  138. return 0;
  139. }
  140. }
  141. IntVector2 Font::GetTotalGlyphOffset(int pointSize) const
  142. {
  143. Vector2 multipliedOffset = (float)pointSize * scaledOffset_;
  144. return absoluteOffset_ + IntVector2((int)multipliedOffset.x_, (int)multipliedOffset.y_);
  145. }
  146. void Font::ReleaseFaces()
  147. {
  148. faces_.Clear();
  149. }
  150. void Font::LoadParameters()
  151. {
  152. ResourceCache* cache = GetSubsystem<ResourceCache>();
  153. String xmlName = ReplaceExtension(GetName(), ".xml");
  154. SharedPtr<XMLFile> xml = cache->GetTempResource<XMLFile>(xmlName, false);
  155. if (!xml)
  156. return;
  157. XMLElement rootElem = xml->GetRoot();
  158. XMLElement absoluteElem = rootElem.GetChild("absoluteoffset");
  159. if (!absoluteElem)
  160. absoluteElem = rootElem.GetChild("absolute");
  161. if (absoluteElem)
  162. {
  163. absoluteOffset_.x_ = absoluteElem.GetInt("x");
  164. absoluteOffset_.y_ = absoluteElem.GetInt("y");
  165. }
  166. XMLElement scaledElem = rootElem.GetChild("scaledoffset");
  167. if (!scaledElem)
  168. scaledElem = rootElem.GetChild("scaled");
  169. if (scaledElem)
  170. {
  171. scaledOffset_.x_ = scaledElem.GetFloat("x");
  172. scaledOffset_.y_ = scaledElem.GetFloat("y");
  173. }
  174. }
  175. FontFace* Font::GetFaceFreeType(int pointSize)
  176. {
  177. SharedPtr<FontFace> newFace(new FontFaceFreeType(this));
  178. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  179. return 0;
  180. faces_[pointSize] = newFace;
  181. return newFace;
  182. }
  183. FontFace* Font::GetFaceBitmap(int pointSize)
  184. {
  185. SharedPtr<FontFace> newFace(new FontFaceBitmap(this));
  186. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  187. return 0;
  188. faces_[pointSize] = newFace;
  189. return newFace;
  190. }
  191. }