CTrueTypeFont.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/CConfigHandler.h"
  16. #include "../../lib/json/JsonNode.h"
  17. #include "../../lib/filesystem/Filesystem.h"
  18. #include "../../lib/texts/TextOperations.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. float fontScale = settings["video"]["fontScalingFactor"].Float();
  27. int scalingFactor = getScalingFactor();
  28. if (config.isNumber())
  29. return std::round(config.Integer() * scalingFactor * fontScale);
  30. else
  31. return std::round(config[scalingFactor-1].Integer() * fontScale);
  32. }
  33. TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
  34. {
  35. if(!TTF_WasInit() && TTF_Init()==-1)
  36. throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
  37. return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), data.second), 1, getPointSize(config["size"]));
  38. }
  39. int CTrueTypeFont::getFontStyle(const JsonNode &config) const
  40. {
  41. const JsonVector & names = config["style"].Vector();
  42. int ret = 0;
  43. for(const JsonNode & node : names)
  44. {
  45. if (node.String() == "bold")
  46. ret |= TTF_STYLE_BOLD;
  47. else if (node.String() == "italic")
  48. ret |= TTF_STYLE_ITALIC;
  49. }
  50. return ret;
  51. }
  52. CTrueTypeFont::CTrueTypeFont(const JsonNode & fontConfig):
  53. data(loadData(fontConfig)),
  54. font(loadFont(fontConfig), TTF_CloseFont),
  55. blended(true),
  56. outline(fontConfig["outline"].Bool()),
  57. dropShadow(!fontConfig["noShadow"].Bool())
  58. {
  59. assert(font);
  60. TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
  61. TTF_SetFontHinting(font.get(),TTF_HINTING_MONO);
  62. logGlobal->debug("Loaded TTF font: '%s', point size %d, height %d, ascent %d, descent %d, line skip %d",
  63. fontConfig["file"].String(),
  64. getPointSize(fontConfig["size"]),
  65. TTF_FontHeight(font.get()),
  66. TTF_FontAscent(font.get()),
  67. TTF_FontDescent(font.get()),
  68. TTF_FontLineSkip(font.get())
  69. );
  70. }
  71. CTrueTypeFont::~CTrueTypeFont() = default;
  72. size_t CTrueTypeFont::getFontAscentScaled() const
  73. {
  74. return TTF_FontAscent(font.get());
  75. }
  76. size_t CTrueTypeFont::getLineHeightScaled() const
  77. {
  78. return TTF_FontHeight(font.get());
  79. }
  80. size_t CTrueTypeFont::getGlyphWidthScaled(const char *text) const
  81. {
  82. return getStringWidthScaled(std::string(text, TextOperations::getUnicodeCharacterSize(*text)));
  83. }
  84. bool CTrueTypeFont::canRepresentCharacter(const char * text) const
  85. {
  86. uint32_t codepoint = TextOperations::getUnicodeCodepoint(text, TextOperations::getUnicodeCharacterSize(*text));
  87. #if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
  88. return TTF_GlyphIsProvided32(font.get(), codepoint);
  89. #elif SDL_TTF_VERSION_ATLEAST(2, 0, 12)
  90. if (codepoint <= 0xffff)
  91. return TTF_GlyphIsProvided(font.get(), codepoint);
  92. return true;
  93. #else
  94. return true;
  95. #endif
  96. }
  97. size_t CTrueTypeFont::getStringWidthScaled(const std::string & text) const
  98. {
  99. int width;
  100. TTF_SizeUTF8(font.get(), text.c_str(), &width, nullptr);
  101. if (outline)
  102. width += getScalingFactor();
  103. if (dropShadow || outline)
  104. width += getScalingFactor();
  105. return width;
  106. }
  107. void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  108. {
  109. if (data.empty())
  110. return;
  111. if (outline)
  112. renderTextImpl(surface, data, Colors::BLACK, pos - Point(1,1) * getScalingFactor());
  113. if (dropShadow || outline)
  114. renderTextImpl(surface, data, Colors::BLACK, pos + Point(1,1) * getScalingFactor());
  115. renderTextImpl(surface, data, color, pos);
  116. }
  117. void CTrueTypeFont::renderTextImpl(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  118. {
  119. SDL_Surface * rendered;
  120. if (blended)
  121. rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
  122. else
  123. rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
  124. if (!rendered)
  125. throw std::runtime_error("Failed to render text '" + data + "'. Reason: '" + TTF_GetError() + "'");
  126. CSDL_Ext::blitSurface(rendered, surface, pos);
  127. SDL_FreeSurface(rendered);
  128. }