CBitmapHanFont.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * CBitmapHanFont.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 "CBitmapHanFont.h"
  12. #include "CBitmapFont.h"
  13. #include "SDL_Extensions.h"
  14. #include "../../lib/filesystem/Filesystem.h"
  15. #include "../../lib/json/JsonNode.h"
  16. #include "../../lib/Rect.h"
  17. #include "../../lib/texts/TextOperations.h"
  18. #include <SDL_surface.h>
  19. size_t CBitmapHanFont::getCharacterDataOffset(size_t index) const
  20. {
  21. size_t rowSize = (size + 7) / 8; // 1 bit per pixel, rounded up
  22. size_t charSize = rowSize * size; // glyph contains "size" rows
  23. return index * charSize;
  24. }
  25. size_t CBitmapHanFont::getCharacterIndex(ui8 first, ui8 second) const
  26. {
  27. if (second > 0x7f )
  28. second--;
  29. return (first - 0x81) * (12*16 - 2) + (second - 0x40);
  30. }
  31. void CBitmapHanFont::renderCharacter(SDL_Surface * surface, int characterIndex, const ColorRGBA & color, int &posX, int &posY) const
  32. {
  33. //TODO: somewhat duplicated with CBitmapFont::renderCharacter();
  34. Rect clipRect;
  35. CSDL_Ext::getClipRect(surface, clipRect);
  36. CSDL_Ext::TColorPutter colorPutter = CSDL_Ext::getPutterFor(surface);
  37. uint8_t bpp = surface->format->BytesPerPixel;
  38. // start of line, may differ from 0 due to end of surface or clipped surface
  39. int lineBegin = std::max<int>(0, clipRect.y - posY);
  40. int lineEnd = std::min((int)size, clipRect.y + clipRect.h - posY);
  41. // start end end of each row, may differ from 0
  42. int rowBegin = std::max<int>(0, clipRect.x - posX);
  43. int rowEnd = std::min<int>((int)size, clipRect.x + clipRect.w - posX);
  44. //for each line in symbol
  45. for(int dy = lineBegin; dy <lineEnd; dy++)
  46. {
  47. uint8_t *dstLine = (uint8_t*)surface->pixels;
  48. uint8_t *source = data.first.get() + getCharacterDataOffset(characterIndex);
  49. // shift source\destination pixels to current position
  50. dstLine += (posY+dy) * surface->pitch + posX * bpp;
  51. source += ((size + 7) / 8) * dy;
  52. //for each column in line
  53. for(int dx = rowBegin; dx < rowEnd; dx++)
  54. {
  55. // select current bit in bitmap
  56. int bit = (source[dx / 8] << (dx % 8)) & 0x80;
  57. uint8_t* dstPixel = dstLine + dx*bpp;
  58. if (bit != 0)
  59. colorPutter(dstPixel, color.r, color.g, color.b);
  60. }
  61. }
  62. posX += (int)size + 1;
  63. }
  64. void CBitmapHanFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  65. {
  66. int posX = pos.x;
  67. int posY = pos.y;
  68. SDL_LockSurface(surface);
  69. for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  70. {
  71. std::string localChar = TextOperations::fromUnicode(data.substr(i, TextOperations::getUnicodeCharacterSize(data[i])), "GBK");
  72. if (localChar.size() == 1)
  73. fallback->renderCharacter(surface, fallback->chars[ui8(localChar[0])], color, posX, posY);
  74. if (localChar.size() == 2)
  75. renderCharacter(surface, (int)getCharacterIndex(localChar[0], localChar[1]), color, posX, posY);
  76. }
  77. SDL_UnlockSurface(surface);
  78. }
  79. CBitmapHanFont::CBitmapHanFont(const JsonNode &config):
  80. fallback(new CBitmapFont(config["fallback"].String())),
  81. data(CResourceHandler::get()->load(ResourcePath("data/" + config["name"].String(), EResType::OTHER))->readAll()),
  82. size((size_t)config["size"].Float())
  83. {
  84. // basic tests to make sure that fonts are OK
  85. // 1) fonts must contain 190 "sections", 126 symbols each.
  86. assert(getCharacterIndex(0xfe, 0xff) == 190*126);
  87. // 2) ensure that font size is correct - enough to fit all possible symbols
  88. assert(getCharacterDataOffset(getCharacterIndex(0xfe, 0xff)) == data.second);
  89. }
  90. size_t CBitmapHanFont::getLineHeight() const
  91. {
  92. return std::max(size + 1, fallback->getLineHeight());
  93. }
  94. size_t CBitmapHanFont::getGlyphWidth(const char * data) const
  95. {
  96. std::string localChar = TextOperations::fromUnicode(std::string(data, TextOperations::getUnicodeCharacterSize(data[0])), "GBK");
  97. if (localChar.size() == 1)
  98. return fallback->getGlyphWidth(data);
  99. if (localChar.size() == 2)
  100. return size + 1;
  101. return 0;
  102. }