CTrueTypeFont.cpp 4.3 KB

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