BsFont.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsFont.h"
  5. #include "BsFontRTTI.h"
  6. #include "BsFontManager.h"
  7. #include "BsResources.h"
  8. namespace BansheeEngine
  9. {
  10. const CHAR_DESC& FontData::getCharDesc(UINT32 charId) const
  11. {
  12. auto iterFind = fontDesc.characters.find(charId);
  13. if(iterFind != fontDesc.characters.end())
  14. {
  15. return fontDesc.characters.at(charId);
  16. }
  17. return fontDesc.missingGlyph;
  18. }
  19. RTTITypeBase* FontData::getRTTIStatic()
  20. {
  21. return FontDataRTTI::instance();
  22. }
  23. RTTITypeBase* FontData::getRTTI() const
  24. {
  25. return FontData::getRTTIStatic();
  26. }
  27. Font::Font()
  28. :Resource(false)
  29. { }
  30. Font::~Font()
  31. { }
  32. void Font::initialize(const Vector<FontData>& fontData)
  33. {
  34. for(auto iter = fontData.begin(); iter != fontData.end(); ++iter)
  35. mFontDataPerSize[iter->size] = *iter;
  36. Resource::initialize();
  37. }
  38. const FontData* Font::getFontDataForSize(UINT32 size) const
  39. {
  40. auto iterFind = mFontDataPerSize.find(size);
  41. if(iterFind == mFontDataPerSize.end())
  42. return nullptr;
  43. return &iterFind->second;
  44. }
  45. INT32 Font::getClosestAvailableSize(UINT32 size) const
  46. {
  47. UINT32 minDiff = std::numeric_limits<UINT32>::max();
  48. UINT32 bestSize = size;
  49. for(auto iter = mFontDataPerSize.begin(); iter != mFontDataPerSize.end(); ++iter)
  50. {
  51. if(iter->first == size)
  52. return size;
  53. else if(iter->first > size)
  54. {
  55. UINT32 diff = iter->first - size;
  56. if(diff < minDiff)
  57. {
  58. minDiff = diff;
  59. bestSize = iter->first;
  60. }
  61. }
  62. else
  63. {
  64. UINT32 diff = size - iter->first;
  65. if(diff < minDiff)
  66. {
  67. minDiff = diff;
  68. bestSize = iter->first;
  69. }
  70. }
  71. }
  72. return bestSize;
  73. }
  74. HFont Font::create(const Vector<FontData>& fontData)
  75. {
  76. FontPtr newFont = _createPtr(fontData);
  77. return gResources()._createResourceHandle(newFont);
  78. }
  79. FontPtr Font::_createPtr(const Vector<FontData>& fontData)
  80. {
  81. return FontManager::instance().create(fontData);
  82. }
  83. RTTITypeBase* Font::getRTTIStatic()
  84. {
  85. return FontRTTI::instance();
  86. }
  87. RTTITypeBase* Font::getRTTI() const
  88. {
  89. return Font::getRTTIStatic();
  90. }
  91. }