CTrueTypeFont.cpp 3.7 KB

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