CBitmapFont.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "CBitmapFont.h"
  12. #include "SDL_Extensions.h"
  13. #include "../CGameInfo.h"
  14. #include "../render/Colors.h"
  15. #include "../../lib/CModHandler.h"
  16. #include "../../lib/Languages.h"
  17. #include "../../lib/Rect.h"
  18. #include "../../lib/TextOperations.h"
  19. #include "../../lib/filesystem/Filesystem.h"
  20. #include "../../lib/vcmi_endian.h"
  21. #include "../../lib/VCMI_Lib.h"
  22. #include <SDL_surface.h>
  23. void CBitmapFont::loadModFont(const std::string & modName, const ResourceID & resource)
  24. {
  25. auto data = CResourceHandler::get(modName)->load(resource)->readAll();
  26. std::string modLanguage = CGI->modh->getModLanguage(modName);
  27. std::string modEncoding = Languages::getLanguageOptions(modLanguage).encoding;
  28. uint32_t dataHeight = data.first[5];
  29. maxHeight = std::max(maxHeight, dataHeight);
  30. constexpr size_t symbolsInFile = 0x100;
  31. constexpr size_t baseIndex = 32;
  32. constexpr size_t offsetIndex = baseIndex + symbolsInFile*12;
  33. constexpr size_t dataIndex = offsetIndex + symbolsInFile*4;
  34. for (uint32_t charIndex = 0; charIndex < symbolsInFile; ++charIndex)
  35. {
  36. CodePoint codepoint = TextOperations::getUnicodeCodepoint(static_cast<char>(charIndex), modEncoding);
  37. BitmapChar symbol;
  38. symbol.leftOffset = read_le_u32(data.first.get() + baseIndex + charIndex * 12 + 0);
  39. symbol.width = read_le_u32(data.first.get() + baseIndex + charIndex * 12 + 4);
  40. symbol.rightOffset = read_le_u32(data.first.get() + baseIndex + charIndex * 12 + 8);
  41. symbol.height = dataHeight;
  42. uint32_t pixelDataOffset = read_le_u32(data.first.get() + offsetIndex + charIndex * 4);
  43. uint32_t pixelsCount = dataHeight * symbol.width;
  44. symbol.pixels.resize(pixelsCount);
  45. uint8_t * pixelData = data.first.get() + dataIndex + pixelDataOffset;
  46. std::copy_n(pixelData, pixelsCount, symbol.pixels.data() );
  47. chars[codepoint] = symbol;
  48. }
  49. }
  50. CBitmapFont::CBitmapFont(const std::string & filename):
  51. maxHeight(0)
  52. {
  53. ResourceID resource("data/" + filename, EResType::BMP_FONT);
  54. loadModFont("core", resource);
  55. for (auto const & modName : VLC->modh->getActiveMods())
  56. {
  57. if (CResourceHandler::get(modName)->existsResource(resource))
  58. loadModFont(modName, resource);
  59. }
  60. }
  61. size_t CBitmapFont::getLineHeight() const
  62. {
  63. return maxHeight;
  64. }
  65. size_t CBitmapFont::getGlyphWidth(const char * data) const
  66. {
  67. CodePoint localChar = TextOperations::getUnicodeCodepoint(data, 4);
  68. auto iter = chars.find(localChar);
  69. if (iter == chars.end())
  70. return 0;
  71. return iter->second.leftOffset + iter->second.width + iter->second.rightOffset;
  72. }
  73. void CBitmapFont::renderCharacter(SDL_Surface * surface, const BitmapChar & character, const SDL_Color & color, int &posX, int &posY) const
  74. {
  75. Rect clipRect;
  76. CSDL_Ext::getClipRect(surface, clipRect);
  77. posX += character.leftOffset;
  78. CSDL_Ext::TColorPutter colorPutter = CSDL_Ext::getPutterFor(surface, 0);
  79. uint8_t bpp = surface->format->BytesPerPixel;
  80. // start of line, may differ from 0 due to end of surface or clipped surface
  81. int lineBegin = std::max<int>(0, clipRect.y - posY);
  82. int lineEnd = std::min<int>(character.height, clipRect.y + clipRect.h - posY - 1);
  83. // start end end of each row, may differ from 0
  84. int rowBegin = std::max<int>(0, clipRect.x - posX);
  85. int rowEnd = std::min<int>(character.width, clipRect.x + clipRect.w - posX - 1);
  86. //for each line in symbol
  87. for(int dy = lineBegin; dy <lineEnd; dy++)
  88. {
  89. uint8_t *dstLine = (uint8_t*)surface->pixels;
  90. const uint8_t *srcLine = character.pixels.data();
  91. // shift source\destination pixels to current position
  92. dstLine += (posY+dy) * surface->pitch + posX * bpp;
  93. srcLine += dy * character.width;
  94. //for each column in line
  95. for(int dx = rowBegin; dx < rowEnd; dx++)
  96. {
  97. uint8_t* dstPixel = dstLine + dx*bpp;
  98. switch(srcLine[dx])
  99. {
  100. case 1: //black "shadow"
  101. colorPutter(dstPixel, 0, 0, 0);
  102. break;
  103. case 255: //text colour
  104. colorPutter(dstPixel, color.r, color.g, color.b);
  105. break;
  106. default :
  107. break; //transparency
  108. }
  109. }
  110. }
  111. posX += character.width;
  112. posX += character.rightOffset;
  113. }
  114. void CBitmapFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  115. {
  116. if (data.empty())
  117. return;
  118. assert(surface);
  119. int posX = pos.x;
  120. int posY = pos.y;
  121. // Should be used to detect incorrect text parsing. Disabled right now due to some old UI code (mostly pregame and battles)
  122. //assert(data[0] != '{');
  123. //assert(data[data.size()-1] != '}');
  124. SDL_LockSurface(surface);
  125. for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  126. {
  127. CodePoint codepoint = TextOperations::getUnicodeCodepoint(data.data() + i, data.size() - i);
  128. auto iter = chars.find(codepoint);
  129. if (iter != chars.end())
  130. renderCharacter(surface, iter->second, color, posX, posY);
  131. }
  132. SDL_UnlockSurface(surface);
  133. }