BsFont.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "BsFont.h"
  2. #include "BsFontRTTI.h"
  3. #include "BsFontManager.h"
  4. #include "BsResources.h"
  5. namespace BansheeEngine
  6. {
  7. const CHAR_DESC& FontData::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* FontData::getRTTIStatic()
  17. {
  18. return FontDataRTTI::instance();
  19. }
  20. RTTITypeBase* FontData::getRTTI() const
  21. {
  22. return FontData::getRTTIStatic();
  23. }
  24. Font::Font()
  25. :Resource(false)
  26. { }
  27. Font::~Font()
  28. { }
  29. void Font::initialize(const Vector<FontData>& fontData)
  30. {
  31. for(auto iter = fontData.begin(); iter != fontData.end(); ++iter)
  32. mFontDataPerSize[iter->size] = *iter;
  33. Resource::initialize();
  34. }
  35. const FontData* Font::getFontDataForSize(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::getClosestAvailableSize(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::getCoreDependencies(Vector<SPtr<CoreObject>>& dependencies)
  72. {
  73. for (auto& fontDataEntry : mFontDataPerSize)
  74. {
  75. for (auto& texture : fontDataEntry.second.texturePages)
  76. {
  77. if (texture.isLoaded())
  78. dependencies.push_back(texture.getInternalPtr());
  79. }
  80. }
  81. }
  82. HFont Font::create(const Vector<FontData>& fontData)
  83. {
  84. FontPtr newFont = _createPtr(fontData);
  85. return static_resource_cast<Font>(gResources()._createResourceHandle(newFont));
  86. }
  87. FontPtr Font::_createPtr(const Vector<FontData>& fontData)
  88. {
  89. return FontManager::instance().create(fontData);
  90. }
  91. RTTITypeBase* Font::getRTTIStatic()
  92. {
  93. return FontRTTI::instance();
  94. }
  95. RTTITypeBase* Font::getRTTI() const
  96. {
  97. return Font::getRTTIStatic();
  98. }
  99. }