CBitmapFont.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * CBitmapFont.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Fonts.h"
  12. #include <SDL_ttf.h>
  13. #include "SDL_Extensions.h"
  14. #include "../../lib/JsonNode.h"
  15. #include "../../lib/vcmi_endian.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include "../../lib/CGeneralTextHandler.h"
  18. std::array<CBitmapFont::BitmapChar, CBitmapFont::totalChars> CBitmapFont::loadChars() const
  19. {
  20. std::array<BitmapChar, totalChars> ret;
  21. size_t offset = 32;
  22. for (auto & elem : ret)
  23. {
  24. elem.leftOffset = read_le_u32(data.first.get() + offset); offset+=4;
  25. elem.width = read_le_u32(data.first.get() + offset); offset+=4;
  26. elem.rightOffset = read_le_u32(data.first.get() + offset); offset+=4;
  27. }
  28. for (auto & elem : ret)
  29. {
  30. int pixelOffset = read_le_u32(data.first.get() + offset); offset+=4;
  31. elem.pixels = data.first.get() + 4128 + pixelOffset;
  32. assert(pixelOffset + 4128 < data.second);
  33. }
  34. return ret;
  35. }
  36. CBitmapFont::CBitmapFont(const std::string & filename):
  37. data(CResourceHandler::get()->load(ResourceID("data/" + filename, EResType::BMP_FONT))->readAll()),
  38. chars(loadChars()),
  39. height(data.first.get()[5])
  40. {}
  41. size_t CBitmapFont::getLineHeight() const
  42. {
  43. return height;
  44. }
  45. size_t CBitmapFont::getGlyphWidth(const char * data) const
  46. {
  47. std::string localChar = Unicode::fromUnicode(std::string(data, Unicode::getCharacterSize(data[0])));
  48. if (localChar.size() == 1)
  49. {
  50. const BitmapChar & ch = chars[ui8(localChar[0])];
  51. return ch.leftOffset + ch.width + ch.rightOffset;
  52. }
  53. return 0;
  54. }
  55. void CBitmapFont::renderCharacter(SDL_Surface * surface, const BitmapChar & character, const SDL_Color & color, int &posX, int &posY) const
  56. {
  57. Rect clipRect;
  58. CSDL_Ext::getClipRect(surface, clipRect);
  59. posX += character.leftOffset;
  60. CSDL_Ext::TColorPutter colorPutter = CSDL_Ext::getPutterFor(surface, 0);
  61. uint8_t bpp = surface->format->BytesPerPixel;
  62. // start of line, may differ from 0 due to end of surface or clipped surface
  63. int lineBegin = std::max<int>(0, clipRect.y - posY);
  64. int lineEnd = std::min<int>(height, clipRect.y + clipRect.h - posY - 1);
  65. // start end end of each row, may differ from 0
  66. int rowBegin = std::max<int>(0, clipRect.x - posX);
  67. int rowEnd = std::min<int>(character.width, clipRect.x + clipRect.w - posX - 1);
  68. //for each line in symbol
  69. for(int dy = lineBegin; dy <lineEnd; dy++)
  70. {
  71. uint8_t *dstLine = (uint8_t*)surface->pixels;
  72. uint8_t *srcLine = character.pixels;
  73. // shift source\destination pixels to current position
  74. dstLine += (posY+dy) * surface->pitch + posX * bpp;
  75. srcLine += dy * character.width;
  76. //for each column in line
  77. for(int dx = rowBegin; dx < rowEnd; dx++)
  78. {
  79. uint8_t* dstPixel = dstLine + dx*bpp;
  80. switch(srcLine[dx])
  81. {
  82. case 1: //black "shadow"
  83. colorPutter(dstPixel, 0, 0, 0);
  84. break;
  85. case 255: //text colour
  86. colorPutter(dstPixel, color.r, color.g, color.b);
  87. break;
  88. default :
  89. break; //transparency
  90. }
  91. }
  92. }
  93. posX += character.width;
  94. posX += character.rightOffset;
  95. }
  96. void CBitmapFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  97. {
  98. if (data.empty())
  99. return;
  100. assert(surface);
  101. int posX = pos.x;
  102. int posY = pos.y;
  103. // Should be used to detect incorrect text parsing. Disabled right now due to some old UI code (mostly pregame and battles)
  104. //assert(data[0] != '{');
  105. //assert(data[data.size()-1] != '}');
  106. SDL_LockSurface(surface);
  107. for(size_t i=0; i<data.size(); i += Unicode::getCharacterSize(data[i]))
  108. {
  109. std::string localChar = Unicode::fromUnicode(data.substr(i, Unicode::getCharacterSize(data[i])));
  110. if (localChar.size() == 1)
  111. renderCharacter(surface, chars[ui8(localChar[0])], color, posX, posY);
  112. }
  113. SDL_UnlockSurface(surface);
  114. }