BsFont.cpp 2.8 KB

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