Text3DFont.cpp 6.5 KB

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