Font.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Core/Profiler.h"
  6. #include "../Graphics/Graphics.h"
  7. #include "../IO/Deserializer.h"
  8. #include "../IO/FileSystem.h"
  9. #include "../UI/Font.h"
  10. #include "../UI/FontFaceBitmap.h"
  11. #include "../UI/FontFaceFreeType.h"
  12. #include "../Resource/ResourceCache.h"
  13. #include "../Resource/XMLElement.h"
  14. #include "../Resource/XMLFile.h"
  15. #include "../DebugNew.h"
  16. namespace Urho3D
  17. {
  18. namespace
  19. {
  20. // Convert float to 26.6 fixed-point (as used internally by FreeType)
  21. inline int FloatToFixed(float value)
  22. {
  23. return (int)(value * 64);
  24. }
  25. }
  26. static const float MIN_POINT_SIZE = 1;
  27. static const float MAX_POINT_SIZE = 96;
  28. Font::Font(Context* context) :
  29. Resource(context),
  30. fontDataSize_(0),
  31. absoluteOffset_(IntVector2::ZERO),
  32. scaledOffset_(Vector2::ZERO),
  33. fontType_(FONT_NONE),
  34. sdfFont_(false)
  35. {
  36. }
  37. Font::~Font()
  38. {
  39. // To ensure FreeType deallocates properly, first clear all faces, then release the raw font data
  40. ReleaseFaces();
  41. fontData_.Reset();
  42. }
  43. void Font::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<Font>();
  46. }
  47. bool Font::BeginLoad(Deserializer& source)
  48. {
  49. // In headless mode, do not actually load, just return success
  50. auto* graphics = GetSubsystem<Graphics>();
  51. if (!graphics)
  52. return true;
  53. fontType_ = FONT_NONE;
  54. faces_.Clear();
  55. fontDataSize_ = source.GetSize();
  56. if (fontDataSize_)
  57. {
  58. fontData_ = new unsigned char[fontDataSize_];
  59. if (source.Read(&fontData_[0], fontDataSize_) != fontDataSize_)
  60. return false;
  61. }
  62. else
  63. {
  64. fontData_.Reset();
  65. return false;
  66. }
  67. String ext = GetExtension(GetName());
  68. if (ext == ".ttf" || ext == ".otf" || ext == ".woff")
  69. {
  70. fontType_ = FONT_FREETYPE;
  71. LoadParameters();
  72. }
  73. else if (ext == ".xml" || ext == ".fnt" || ext == ".sdf")
  74. fontType_ = FONT_BITMAP;
  75. sdfFont_ = ext == ".sdf";
  76. SetMemoryUse(fontDataSize_);
  77. return true;
  78. }
  79. bool Font::SaveXML(Serializer& dest, int pointSize, bool usedGlyphs, const String& indentation)
  80. {
  81. FontFace* fontFace = GetFace(pointSize);
  82. if (!fontFace)
  83. return false;
  84. URHO3D_PROFILE(FontSaveXML);
  85. SharedPtr<FontFaceBitmap> packedFontFace(new FontFaceBitmap(this));
  86. if (!packedFontFace->Load(fontFace, usedGlyphs))
  87. return false;
  88. return packedFontFace->Save(dest, pointSize, indentation);
  89. }
  90. void Font::SetAbsoluteGlyphOffset(const IntVector2& offset)
  91. {
  92. absoluteOffset_ = offset;
  93. }
  94. void Font::SetScaledGlyphOffset(const Vector2& offset)
  95. {
  96. scaledOffset_ = offset;
  97. }
  98. FontFace* Font::GetFace(float pointSize)
  99. {
  100. // In headless mode, always return null
  101. auto* graphics = GetSubsystem<Graphics>();
  102. if (!graphics)
  103. return nullptr;
  104. // For bitmap font type, always return the same font face provided by the font's bitmap file regardless of the actual requested point size
  105. if (fontType_ == FONT_BITMAP)
  106. pointSize = 0;
  107. else
  108. pointSize = Clamp(pointSize, MIN_POINT_SIZE, MAX_POINT_SIZE);
  109. // For outline fonts, we return the nearest size in 1/64th increments, as that's what FreeType supports.
  110. int key = FloatToFixed(pointSize);
  111. HashMap<int, SharedPtr<FontFace>>::Iterator i = faces_.Find(key);
  112. if (i != faces_.End())
  113. {
  114. if (!i->second_->IsDataLost())
  115. return i->second_;
  116. else
  117. {
  118. // Erase and reload face if texture data lost (OpenGL mode only)
  119. faces_.Erase(i);
  120. }
  121. }
  122. URHO3D_PROFILE(GetFontFace);
  123. switch (fontType_)
  124. {
  125. case FONT_FREETYPE:
  126. return GetFaceFreeType(pointSize);
  127. case FONT_BITMAP:
  128. return GetFaceBitmap(pointSize);
  129. default:
  130. return nullptr;
  131. }
  132. }
  133. IntVector2 Font::GetTotalGlyphOffset(float pointSize) const
  134. {
  135. Vector2 multipliedOffset = pointSize * scaledOffset_;
  136. return absoluteOffset_ + IntVector2(RoundToInt(multipliedOffset.x_), RoundToInt(multipliedOffset.y_));
  137. }
  138. void Font::ReleaseFaces()
  139. {
  140. faces_.Clear();
  141. }
  142. void Font::LoadParameters()
  143. {
  144. auto* cache = GetSubsystem<ResourceCache>();
  145. String xmlName = ReplaceExtension(GetName(), ".xml");
  146. SharedPtr<XMLFile> xml = cache->GetTempResource<XMLFile>(xmlName, false);
  147. if (!xml)
  148. return;
  149. XMLElement rootElem = xml->GetRoot();
  150. XMLElement absoluteElem = rootElem.GetChild("absoluteoffset");
  151. if (!absoluteElem)
  152. absoluteElem = rootElem.GetChild("absolute");
  153. if (absoluteElem)
  154. {
  155. absoluteOffset_.x_ = absoluteElem.GetI32("x");
  156. absoluteOffset_.y_ = absoluteElem.GetI32("y");
  157. }
  158. XMLElement scaledElem = rootElem.GetChild("scaledoffset");
  159. if (!scaledElem)
  160. scaledElem = rootElem.GetChild("scaled");
  161. if (scaledElem)
  162. {
  163. scaledOffset_.x_ = scaledElem.GetFloat("x");
  164. scaledOffset_.y_ = scaledElem.GetFloat("y");
  165. }
  166. }
  167. FontFace* Font::GetFaceFreeType(float pointSize)
  168. {
  169. SharedPtr<FontFace> newFace(new FontFaceFreeType(this));
  170. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  171. return nullptr;
  172. int key = FloatToFixed(pointSize);
  173. faces_[key] = newFace;
  174. return newFace;
  175. }
  176. FontFace* Font::GetFaceBitmap(float pointSize)
  177. {
  178. SharedPtr<FontFace> newFace(new FontFaceBitmap(this));
  179. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  180. return nullptr;
  181. int key = FloatToFixed(pointSize);
  182. faces_[key] = newFace;
  183. return newFace;
  184. }
  185. }