2
0

BsFont.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "BsFont.h"
  2. #include "BsFontRTTI.h"
  3. #include "BsFontManager.h"
  4. #include "BsResources.h"
  5. namespace BansheeEngine
  6. {
  7. const CHAR_DESC& FontBitmap::getCharDesc(UINT32 charId) const
  8. {
  9. auto iterFind = fontDesc.characters.find(charId);
  10. if(iterFind != fontDesc.characters.end())
  11. {
  12. return fontDesc.characters.at(charId);
  13. }
  14. return fontDesc.missingGlyph;
  15. }
  16. RTTITypeBase* FontBitmap::getRTTIStatic()
  17. {
  18. return FontBitmapRTTI::instance();
  19. }
  20. RTTITypeBase* FontBitmap::getRTTI() const
  21. {
  22. return FontBitmap::getRTTIStatic();
  23. }
  24. Font::Font()
  25. :Resource(false)
  26. { }
  27. Font::~Font()
  28. { }
  29. void Font::initialize(const Vector<SPtr<FontBitmap>>& fontData)
  30. {
  31. for(auto iter = fontData.begin(); iter != fontData.end(); ++iter)
  32. mFontDataPerSize[(*iter)->size] = *iter;
  33. Resource::initialize();
  34. }
  35. SPtr<const FontBitmap> Font::getBitmap(UINT32 size) const
  36. {
  37. auto iterFind = mFontDataPerSize.find(size);
  38. if(iterFind == mFontDataPerSize.end())
  39. return nullptr;
  40. return iterFind->second;
  41. }
  42. INT32 Font::getClosestSize(UINT32 size) const
  43. {
  44. UINT32 minDiff = std::numeric_limits<UINT32>::max();
  45. UINT32 bestSize = size;
  46. for(auto iter = mFontDataPerSize.begin(); iter != mFontDataPerSize.end(); ++iter)
  47. {
  48. if(iter->first == size)
  49. return size;
  50. else if(iter->first > size)
  51. {
  52. UINT32 diff = iter->first - size;
  53. if(diff < minDiff)
  54. {
  55. minDiff = diff;
  56. bestSize = iter->first;
  57. }
  58. }
  59. else
  60. {
  61. UINT32 diff = size - iter->first;
  62. if(diff < minDiff)
  63. {
  64. minDiff = diff;
  65. bestSize = iter->first;
  66. }
  67. }
  68. }
  69. return bestSize;
  70. }
  71. void Font::getResourceDependencies(FrameVector<HResource>& dependencies) const
  72. {
  73. for (auto& fontDataEntry : mFontDataPerSize)
  74. {
  75. for (auto& texture : fontDataEntry.second->texturePages)
  76. {
  77. if (texture != nullptr)
  78. dependencies.push_back(texture);
  79. }
  80. }
  81. }
  82. void Font::getCoreDependencies(Vector<CoreObject*>& dependencies)
  83. {
  84. for (auto& fontDataEntry : mFontDataPerSize)
  85. {
  86. for (auto& texture : fontDataEntry.second->texturePages)
  87. {
  88. if (texture.isLoaded())
  89. dependencies.push_back(texture.get());
  90. }
  91. }
  92. }
  93. HFont Font::create(const Vector<SPtr<FontBitmap>>& fontData)
  94. {
  95. FontPtr newFont = _createPtr(fontData);
  96. return static_resource_cast<Font>(gResources()._createResourceHandle(newFont));
  97. }
  98. FontPtr Font::_createPtr(const Vector<SPtr<FontBitmap>>& fontData)
  99. {
  100. return FontManager::instance().create(fontData);
  101. }
  102. RTTITypeBase* Font::getRTTIStatic()
  103. {
  104. return FontRTTI::instance();
  105. }
  106. RTTITypeBase* Font::getRTTI() const
  107. {
  108. return Font::getRTTIStatic();
  109. }
  110. }