CBitmapFont.cpp 5.5 KB

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