Font.cpp 6.3 KB

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