Fonts.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "StdInc.h"
  2. #include "Fonts.h"
  3. #include <SDL_ttf.h>
  4. #include "SDL_Pixels.h"
  5. #include "../../lib/JsonNode.h"
  6. #include "../../lib/vcmi_endian.h"
  7. #include "../../lib/filesystem/CResourceLoader.h"
  8. /*
  9. * Fonts.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. void IFont::renderTextLeft(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  18. {
  19. renderText(surface, data, color, pos);
  20. }
  21. void IFont::renderTextRight(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  22. {
  23. Point size(getStringWidth(data), getLineHeight());
  24. renderText(surface, data, color, pos - size);
  25. }
  26. void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  27. {
  28. Point size(getStringWidth(data), getLineHeight());
  29. renderText(surface, data, color, pos - size / 2);
  30. }
  31. void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const
  32. {
  33. Point currPos = pos;
  34. BOOST_FOREACH(const std::string & line, data)
  35. {
  36. renderTextLeft(surface, line, color, currPos);
  37. currPos.y += getLineHeight();
  38. }
  39. }
  40. void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const
  41. {
  42. Point currPos = pos;
  43. currPos.y -= data.size() * getLineHeight();
  44. BOOST_FOREACH(const std::string & line, data)
  45. {
  46. renderTextRight(surface, line, color, currPos);
  47. currPos.y += getLineHeight();
  48. }
  49. }
  50. void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const
  51. {
  52. Point currPos = pos;
  53. currPos.y -= data.size() * getLineHeight()/2;
  54. BOOST_FOREACH(const std::string & line, data)
  55. {
  56. renderTextCenter(surface, line, color, currPos);
  57. currPos.y += getLineHeight();
  58. }
  59. }
  60. std::array<CBitmapFont::Char, CBitmapFont::totalChars> CBitmapFont::loadChars() const
  61. {
  62. std::array<Char, totalChars> ret;
  63. size_t offset = 32;
  64. for (size_t i=0; i< ret.size(); i++)
  65. {
  66. ret[i].leftOffset = read_le_u32(data.first.get() + offset); offset+=4;
  67. ret[i].width = read_le_u32(data.first.get() + offset); offset+=4;
  68. ret[i].rightOffset = read_le_u32(data.first.get() + offset); offset+=4;
  69. }
  70. for (size_t i=0; i< ret.size(); i++)
  71. {
  72. int pixelOffset = read_le_u32(data.first.get() + offset); offset+=4;
  73. ret[i].pixels = data.first.get() + 4128 + pixelOffset;
  74. assert(pixelOffset + 4128 < data.second);
  75. }
  76. return ret;
  77. }
  78. CBitmapFont::CBitmapFont(const std::string & filename):
  79. data(CResourceHandler::get()->loadData(ResourceID("data/" + filename, EResType::BMP_FONT))),
  80. chars(loadChars()),
  81. height(data.first.get()[5])
  82. {}
  83. size_t CBitmapFont::getLineHeight() const
  84. {
  85. return height;
  86. }
  87. size_t CBitmapFont::getSymbolWidth(char data) const
  88. {
  89. const Char & ch = chars[ui8(data)];
  90. return ch.leftOffset + ch.width + ch.rightOffset;
  91. }
  92. size_t CBitmapFont::getStringWidth(const std::string & data) const
  93. {
  94. size_t width = 0;
  95. BOOST_FOREACH(auto & ch, data)
  96. {
  97. width += getSymbolWidth(ch);
  98. }
  99. return width;
  100. }
  101. void CBitmapFont::renderCharacter(SDL_Surface * surface, const Char & character, const SDL_Color & color, int &posX, int &posY) const
  102. {
  103. Rect clipRect;
  104. SDL_GetClipRect(surface, &clipRect);
  105. posX += character.leftOffset;
  106. TColorPutter colorPutter = CSDL_Ext::getPutterFor(surface, 0);
  107. Uint8 bpp = surface->format->BytesPerPixel;
  108. // start of line, may differ from 0 due to end of surface or clipped surface
  109. int lineBegin = std::max<int>(0, clipRect.y - posY);
  110. int lineEnd = std::min<int>(height, clipRect.y + clipRect.h - posY - 1);
  111. // start end end of each row, may differ from 0
  112. int rowBegin = std::max<int>(0, clipRect.x - posX);
  113. int rowEnd = std::min<int>(character.width, clipRect.x + clipRect.w - posX - 1);
  114. //for each line in symbol
  115. for(int dy = lineBegin; dy <lineEnd; dy++)
  116. {
  117. Uint8 *dstLine = (Uint8*)surface->pixels;
  118. Uint8 *srcLine = character.pixels;
  119. // shift source\destination pixels to current position
  120. dstLine += (posY+dy) * surface->pitch + posX * bpp;
  121. srcLine += dy * character.width;
  122. //for each column in line
  123. for(int dx = rowBegin; dx < rowEnd; dx++)
  124. {
  125. Uint8* dstPixel = dstLine + dx*bpp;
  126. switch(srcLine[dx])
  127. {
  128. case 1: //black "shadow"
  129. std::fill(dstPixel, dstPixel + bpp, 0);
  130. break;
  131. case 255: //text colour
  132. colorPutter(dstPixel, color.r, color.g, color.b);
  133. break;
  134. default :
  135. break; //transparency
  136. }
  137. }
  138. }
  139. posX += character.width;
  140. posX += character.rightOffset;
  141. }
  142. void CBitmapFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  143. {
  144. if (data.empty())
  145. return;
  146. assert(surface);
  147. int posX = pos.x;
  148. int posY = pos.y;
  149. // Should be used to detect incorrect text parsing. Disabled right now due to some old UI code (mostly pregame and battles)
  150. //assert(data[0] != '{');
  151. //assert(data[data.size()-1] != '}');
  152. SDL_LockSurface(surface);
  153. // for each symbol
  154. for(size_t index = 0; index < data.size(); index++)
  155. {
  156. renderCharacter(surface, chars[ui8(data[index])], color, posX, posY);
  157. }
  158. SDL_UnlockSurface(surface);
  159. }
  160. std::pair<std::unique_ptr<ui8[]>, ui64> CTrueTypeFont::loadData(const JsonNode & config)
  161. {
  162. std::string filename = "Data/" + config["file"].String();
  163. return CResourceHandler::get()->loadData(ResourceID(filename, EResType::TTF_FONT));
  164. }
  165. TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
  166. {
  167. int pointSize = config["size"].Float();
  168. if(!TTF_WasInit() && TTF_Init()==-1)
  169. throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
  170. return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), data.second), 1, pointSize);
  171. }
  172. int CTrueTypeFont::getFontStyle(const JsonNode &config)
  173. {
  174. const JsonVector & names = config["style"].Vector();
  175. int ret = 0;
  176. BOOST_FOREACH(const JsonNode & node, names)
  177. {
  178. if (node.String() == "bold")
  179. ret |= TTF_STYLE_BOLD;
  180. else if (node.String() == "italic")
  181. ret |= TTF_STYLE_ITALIC;
  182. }
  183. return ret;
  184. }
  185. CTrueTypeFont::CTrueTypeFont(const JsonNode & fontConfig):
  186. data(loadData(fontConfig)),
  187. font(loadFont(fontConfig), TTF_CloseFont),
  188. blended(fontConfig["blend"].Bool())
  189. {
  190. assert(font);
  191. TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
  192. }
  193. size_t CTrueTypeFont::getLineHeight() const
  194. {
  195. return TTF_FontHeight(font.get());
  196. }
  197. size_t CTrueTypeFont::getSymbolWidth(char data) const
  198. {
  199. int advance;
  200. TTF_GlyphMetrics(font.get(), data, NULL, NULL, NULL, NULL, &advance);
  201. return advance;
  202. }
  203. size_t CTrueTypeFont::getStringWidth(const std::string & data) const
  204. {
  205. int width;
  206. TTF_SizeText(font.get(), data.c_str(), &width, NULL);
  207. return width;
  208. }
  209. void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  210. {
  211. if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
  212. {
  213. SDL_Color black = { 0, 0, 0, SDL_ALPHA_OPAQUE};
  214. renderText(surface, data, black, Point(pos.x + 1, pos.y + 1));
  215. }
  216. if (!data.empty())
  217. {
  218. SDL_Surface * rendered;
  219. if (blended)
  220. rendered = TTF_RenderText_Blended(font.get(), data.c_str(), color);
  221. else
  222. rendered = TTF_RenderText_Solid(font.get(), data.c_str(), color);
  223. assert(rendered);
  224. Rect rect(pos.x, pos.y, rendered->w, rendered->h);
  225. SDL_BlitSurface(rendered, NULL, surface, &rect);
  226. SDL_FreeSurface(rendered);
  227. }
  228. }