BsFont.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. HFont Font::create(const Vector<FontData>& fontData)
  72. {
  73. FontPtr newFont = _createPtr(fontData);
  74. return static_resource_cast<Font>(gResources()._createResourceHandle(newFont));
  75. }
  76. FontPtr Font::_createPtr(const Vector<FontData>& fontData)
  77. {
  78. return FontManager::instance().create(fontData);
  79. }
  80. RTTITypeBase* Font::getRTTIStatic()
  81. {
  82. return FontRTTI::instance();
  83. }
  84. RTTITypeBase* Font::getRTTI() const
  85. {
  86. return Font::getRTTIStatic();
  87. }
  88. }