CTrueTypeFont.cpp 2.7 KB

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