CTrueTypeFont.cpp 3.4 KB

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