Font.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "Log.h"
  31. #include "MemoryBuffer.h"
  32. #include "Profiler.h"
  33. #include "ResourceCache.h"
  34. // #include "Texture2D.h"
  35. // #include "UI.h"
  36. #include "DebugNew.h"
  37. namespace Urho3D
  38. {
  39. static const int MIN_POINT_SIZE = 1;
  40. static const int MAX_POINT_SIZE = 96;
  41. Font::Font(Context* context) :
  42. Resource(context),
  43. fontDataSize_(0),
  44. fontType_(FONT_NONE)
  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::Load(Deserializer& source)
  58. {
  59. PROFILE(LoadFont);
  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. fontType_ = FONT_FREETYPE;
  81. else if (ext == ".xml" || ext == ".fnt")
  82. fontType_ = FONT_BITMAP;
  83. SetMemoryUse(fontDataSize_);
  84. return true;
  85. }
  86. bool Font::SaveXML(Serializer& dest, int pointSize, bool usedGlyphs)
  87. {
  88. FontFace* fontFace = GetFace(pointSize);
  89. if (!fontFace)
  90. return false;
  91. PROFILE(FontSaveXML);
  92. SharedPtr<FontFaceBitMap> packedFontFace(new FontFaceBitMap(this));
  93. if (!packedFontFace->Load(fontFace, usedGlyphs))
  94. return false;
  95. return packedFontFace->Save(dest, pointSize);
  96. }
  97. FontFace* Font::GetFace(int pointSize)
  98. {
  99. // In headless mode, always return null
  100. Graphics* graphics = GetSubsystem<Graphics>();
  101. if (!graphics)
  102. return 0;
  103. // For bitmap font type, always return the same font face provided by the font's bitmap file regardless of the actual requested point size
  104. if (fontType_ == FONT_BITMAP)
  105. pointSize = 0;
  106. else
  107. pointSize = Clamp(pointSize, MIN_POINT_SIZE, MAX_POINT_SIZE);
  108. HashMap<int, SharedPtr<FontFace> >::Iterator i = faces_.Find(pointSize);
  109. if (i != faces_.End())
  110. {
  111. if (!i->second_->IsDataLost())
  112. return i->second_;
  113. else
  114. {
  115. // Erase and reload face if texture data lost (OpenGL mode only)
  116. faces_.Erase(i);
  117. }
  118. }
  119. PROFILE(GetFontFace);
  120. switch (fontType_)
  121. {
  122. case FONT_FREETYPE:
  123. return GetFaceFreeType(pointSize);
  124. case FONT_BITMAP:
  125. return GetFaceBitmap(pointSize);
  126. default:
  127. return 0;
  128. }
  129. }
  130. void Font::ReleaseFaces()
  131. {
  132. faces_.Clear();
  133. }
  134. FontFace* Font::GetFaceFreeType(int pointSize)
  135. {
  136. SharedPtr<FontFace> newFace(new FontFaceFreeType(this));
  137. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  138. return 0;
  139. faces_[pointSize] = newFace;
  140. return newFace;
  141. }
  142. FontFace* Font::GetFaceBitmap(int pointSize)
  143. {
  144. SharedPtr<FontFace> newFace(new FontFaceBitMap(this));
  145. if (!newFace->Load(&fontData_[0], fontDataSize_, pointSize))
  146. return 0;
  147. faces_[pointSize] = newFace;
  148. return newFace;
  149. }
  150. }