CBitmapFont.cpp 4.9 KB

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