Font.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "Deserializer.h"
  25. #include "FileSystem.h"
  26. #include "Font.h"
  27. #include "FontFaceBitmap.h"
  28. #include "FontFaceFreeType.h"
  29. #include "Graphics.h"
  30. #include "Profiler.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. static const int MIN_POINT_SIZE = 1;
  35. static const int MAX_POINT_SIZE = 96;
  36. Font::Font(Context* context) :
  37. Resource(context),
  38. fontDataSize_(0),
  39. fontType_(FONT_NONE),
  40. sdfFont_(false)
  41. {
  42. }
  43. Font::~Font()
  44. {
  45. // To ensure FreeType deallocates properly, first clear all faces, then release the raw font data
  46. ReleaseFaces();
  47. fontData_.Reset();
  48. }
  49. void Font::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<Font>();
  52. }
  53. bool Font::BeginLoad(Deserializer& source)
  54. {
  55. // In headless mode, do not actually load, just return success
  56. Graphics* graphics = GetSubsystem<Graphics>();
  57. if (!graphics)
  58. return true;
  59. fontType_ = FONT_NONE;
  60. faces_.Clear();
  61. fontDataSize_ = source.GetSize();
  62. if (fontDataSize_)
  63. {
  64. fontData_ = new unsigned char[fontDataSize_];
  65. if (source.Read(&fontData_[0], fontDataSize_) != fontDataSize_)
  66. return false;
  67. }
  68. else
  69. {
  70. fontData_.Reset();
  71. return false;
  72. }
  73. String ext = GetExtension(GetName());
  74. if (ext == ".ttf" || ext == ".otf" || ext == ".woff")
  75. fontType_ = FONT_FREETYPE;
  76. else if (ext == ".xml" || ext == ".fnt" || ext == ".sdf")
  77. fontType_ = FONT_BITMAP;
  78. sdfFont_ = ext == ".sdf";
  79. SetMemoryUse(fontDataSize_);
  80. return true;
  81. }
  82. bool Font::SaveXML(Serializer& dest, int pointSize, bool usedGlyphs)
  83. {
  84. FontFace* fontFace = GetFace(pointSize);
  85. if (!fontFace)
  86. return false;
  87. PROFILE(FontSaveXML);
  88. SharedPtr<FontFaceBitmap> packedFontFace(new FontFaceBitmap(this));
  89. if (!packedFontFace->Load(fontFace, usedGlyphs))
  90. return false;
  91. return packedFontFace->Save(dest, pointSize);
  92. }
  93. FontFace* Font::GetFace(int pointSize)
  94. {
  95. // In headless mode, always return null
  96. Graphics* graphics = GetSubsystem<Graphics>();
  97. if (!graphics)
  98. return 0;
  99. // For bitmap font type, always return the same font face provided by the font's bitmap file regardless of the actual requested point size
  100. if (fontType_ == FONT_BITMAP)
  101. pointSize = 0;
  102. else
  103. pointSize = Clamp(pointSize, MIN_POINT_SIZE, MAX_POINT_SIZE);
  104. HashMap<int, SharedPtr<FontFace> >::Iterator i = faces_.Find(pointSize);
  105. if (i != faces_.End())
  106. {
  107. if (!i->second_->IsDataLost())
  108. return i->second_;
  109. else
  110. {
  111. // Erase and reload face if texture data lost (OpenGL mode only)
  112. faces_.Erase(i);
  113. }
  114. }
  115. PROFILE(GetFontFace);
  116. switch (fontType_)
  117. {
  118. case FONT_FREETYPE:
  119. return GetFaceFreeType(pointSize);
  120. case FONT_BITMAP:
  121. return GetFaceBitmap(pointSize);
  122. default:
  123. return 0;
  124. }
  125. }
  126. void Font::ReleaseFaces()
  127. {
  128. faces_.Clear();
  129. }
  130. FontFace* Font::GetFaceFreeType(int pointSize)
  131. {
  132. SharedPtr<FontFace> newFace(new FontFaceFreeType(this));
  133. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  134. return 0;
  135. faces_[pointSize] = newFace;
  136. return newFace;
  137. }
  138. FontFace* Font::GetFaceBitmap(int pointSize)
  139. {
  140. SharedPtr<FontFace> newFace(new FontFaceBitmap(this));
  141. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  142. return 0;
  143. faces_[pointSize] = newFace;
  144. return newFace;
  145. }
  146. }