CmFont.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "CmFont.h"
  2. #include "CmFontRTTI.h"
  3. #include "CmFontManager.h"
  4. namespace CamelotFramework
  5. {
  6. const CHAR_DESC& FontData::getCharDesc(UINT32 charId) const
  7. {
  8. auto iterFind = fontDesc.characters.find(charId);
  9. if(iterFind != fontDesc.characters.end())
  10. {
  11. return fontDesc.characters.at(charId);
  12. }
  13. return fontDesc.missingGlyph;
  14. }
  15. RTTITypeBase* FontData::getRTTIStatic()
  16. {
  17. return FontDataRTTI::instance();
  18. }
  19. RTTITypeBase* FontData::getRTTI() const
  20. {
  21. return FontData::getRTTIStatic();
  22. }
  23. Font::Font()
  24. :Resource(false)
  25. { }
  26. Font::~Font()
  27. { }
  28. void Font::initialize(Vector<FontData>::type& fontData)
  29. {
  30. for(auto iter = fontData.begin(); iter != fontData.end(); ++iter)
  31. mFontDataPerSize[iter->size] = *iter;
  32. Resource::initialize();
  33. }
  34. const FontData* Font::getFontDataForSize(UINT32 size) const
  35. {
  36. auto iterFind = mFontDataPerSize.find(size);
  37. if(iterFind == mFontDataPerSize.end())
  38. return nullptr;
  39. return &iterFind->second;
  40. }
  41. INT32 Font::getClosestAvailableSize(UINT32 size) const
  42. {
  43. UINT32 minDiff = std::numeric_limits<UINT32>::max();
  44. UINT32 bestSize = size;
  45. for(auto iter = mFontDataPerSize.begin(); iter != mFontDataPerSize.end(); ++iter)
  46. {
  47. if(iter->first == size)
  48. return size;
  49. else if(iter->first > size)
  50. {
  51. UINT32 diff = iter->first - size;
  52. if(diff < minDiff)
  53. {
  54. minDiff = diff;
  55. bestSize = iter->first;
  56. }
  57. }
  58. else
  59. {
  60. UINT32 diff = size - iter->first;
  61. if(diff < minDiff)
  62. {
  63. minDiff = diff;
  64. bestSize = iter->first;
  65. }
  66. }
  67. }
  68. return bestSize;
  69. }
  70. HFont Font::create(Vector<FontData>::type& fontData)
  71. {
  72. FontPtr newFont = FontManager::instance().create(fontData);
  73. return Resource::_createResourceHandle(newFont);
  74. }
  75. RTTITypeBase* Font::getRTTIStatic()
  76. {
  77. return FontRTTI::instance();
  78. }
  79. RTTITypeBase* Font::getRTTI() const
  80. {
  81. return Font::getRTTIStatic();
  82. }
  83. }