CTrueTypeFont.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. dropShadow(!fontConfig["noShadow"].Bool()),
  57. outline(fontConfig["outline"].Bool()),
  58. blended(true)
  59. {
  60. assert(font);
  61. TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
  62. TTF_SetFontHinting(font.get(),TTF_HINTING_MONO);
  63. }
  64. CTrueTypeFont::~CTrueTypeFont() = default;
  65. size_t CTrueTypeFont::getFontAscentScaled() const
  66. {
  67. return TTF_FontAscent(font.get());
  68. }
  69. size_t CTrueTypeFont::getLineHeightScaled() const
  70. {
  71. return TTF_FontHeight(font.get());
  72. }
  73. size_t CTrueTypeFont::getGlyphWidthScaled(const char *data) const
  74. {
  75. return getStringWidthScaled(std::string(data, TextOperations::getUnicodeCharacterSize(*data)));
  76. }
  77. bool CTrueTypeFont::canRepresentCharacter(const char * data) const
  78. {
  79. uint32_t codepoint = TextOperations::getUnicodeCodepoint(data, TextOperations::getUnicodeCharacterSize(*data));
  80. #if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
  81. return TTF_GlyphIsProvided32(font.get(), codepoint);
  82. #elif SDL_TTF_VERSION_ATLEAST(2, 0, 12)
  83. if (codepoint <= 0xffff)
  84. return TTF_GlyphIsProvided(font.get(), codepoint);
  85. return true;
  86. #else
  87. return true;
  88. #endif
  89. }
  90. size_t CTrueTypeFont::getStringWidthScaled(const std::string & data) const
  91. {
  92. int width;
  93. TTF_SizeUTF8(font.get(), data.c_str(), &width, nullptr);
  94. return width;
  95. }
  96. void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  97. {
  98. if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
  99. {
  100. if (outline)
  101. renderText(surface, data, Colors::BLACK, pos - Point(1,1) * getScalingFactor());
  102. if (dropShadow || outline)
  103. renderText(surface, data, Colors::BLACK, pos + Point(1,1) * getScalingFactor());
  104. }
  105. if (!data.empty())
  106. {
  107. SDL_Surface * rendered;
  108. if (blended)
  109. rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
  110. else
  111. rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
  112. assert(rendered);
  113. CSDL_Ext::blitSurface(rendered, surface, pos);
  114. SDL_FreeSurface(rendered);
  115. }
  116. }