CTrueTypeFont.cpp 3.5 KB

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