CTrueTypeFont.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * CTrueTypeFont.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 "Fonts.h"
  12. #include <SDL_ttf.h>
  13. #include "SDL_Extensions.h"
  14. #include "../../lib/JsonNode.h"
  15. #include "../../lib/vcmi_endian.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include "../../lib/CGeneralTextHandler.h"
  18. TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
  19. {
  20. int pointSize = static_cast<int>(config["size"].Float());
  21. if(!TTF_WasInit() && TTF_Init()==-1)
  22. throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
  23. return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), (int)data.second), 1, pointSize);
  24. }
  25. int CTrueTypeFont::getFontStyle(const JsonNode &config)
  26. {
  27. const JsonVector & names = config["style"].Vector();
  28. int ret = 0;
  29. for(const JsonNode & node : names)
  30. {
  31. if (node.String() == "bold")
  32. ret |= TTF_STYLE_BOLD;
  33. else if (node.String() == "italic")
  34. ret |= TTF_STYLE_ITALIC;
  35. }
  36. return ret;
  37. }
  38. CTrueTypeFont::CTrueTypeFont(const JsonNode & fontConfig):
  39. data(loadData(fontConfig)),
  40. font(loadFont(fontConfig), TTF_CloseFont),
  41. blended(fontConfig["blend"].Bool())
  42. {
  43. assert(font);
  44. TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
  45. }
  46. size_t CTrueTypeFont::getLineHeight() const
  47. {
  48. return TTF_FontHeight(font.get());
  49. }
  50. size_t CTrueTypeFont::getGlyphWidth(const char *data) const
  51. {
  52. return getStringWidth(std::string(data, Unicode::getCharacterSize(*data)));
  53. /*
  54. int advance;
  55. TTF_GlyphMetrics(font.get(), *data, nullptr, nullptr, nullptr, nullptr, &advance);
  56. return advance;
  57. */
  58. }
  59. size_t CTrueTypeFont::getStringWidth(const std::string & data) const
  60. {
  61. int width;
  62. TTF_SizeUTF8(font.get(), data.c_str(), &width, nullptr);
  63. return width;
  64. }
  65. void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  66. {
  67. if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
  68. renderText(surface, data, Colors::BLACK, pos + Point(1,1));
  69. if (!data.empty())
  70. {
  71. SDL_Surface * rendered;
  72. if (blended)
  73. rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), color);
  74. else
  75. rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), color);
  76. assert(rendered);
  77. CSDL_Ext::blitSurface(rendered, surface, pos);
  78. SDL_FreeSurface(rendered);
  79. }
  80. }