CTrueTypeFont.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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["blend"].Bool()),
  55. blended(fontConfig["blend"].Bool())
  56. {
  57. assert(font);
  58. TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
  59. std::string fallbackName = fontConfig["fallback"].String();
  60. if (!fallbackName.empty())
  61. fallbackFont = std::make_unique<CBitmapFont>(fallbackName);
  62. }
  63. CTrueTypeFont::~CTrueTypeFont() = default;
  64. size_t CTrueTypeFont::getLineHeight() const
  65. {
  66. if (fallbackFont)
  67. return fallbackFont->getLineHeight();
  68. return TTF_FontHeight(font.get()) / getScalingFactor();
  69. }
  70. size_t CTrueTypeFont::getGlyphWidth(const char *data) const
  71. {
  72. if (fallbackFont && fallbackFont->canRepresentCharacter(data))
  73. return fallbackFont->getGlyphWidth(data);
  74. return getStringWidth(std::string(data, TextOperations::getUnicodeCharacterSize(*data)));
  75. }
  76. size_t CTrueTypeFont::getStringWidth(const std::string & data) const
  77. {
  78. if (fallbackFont && fallbackFont->canRepresentString(data))
  79. return fallbackFont->getStringWidth(data) / getScalingFactor();
  80. int width;
  81. TTF_SizeUTF8(font.get(), data.c_str(), &width, nullptr);
  82. return width / getScalingFactor();
  83. }
  84. void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  85. {
  86. if (fallbackFont && fallbackFont->canRepresentString(data))
  87. {
  88. fallbackFont->renderText(surface, data, color, pos);
  89. return;
  90. }
  91. if (dropShadow && color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
  92. renderText(surface, data, Colors::BLACK, pos + Point(1,1) * getScalingFactor());
  93. if (!data.empty())
  94. {
  95. SDL_Surface * rendered;
  96. if (blended)
  97. rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
  98. else
  99. rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
  100. assert(rendered);
  101. CSDL_Ext::blitSurface(rendered, surface, pos);
  102. SDL_FreeSurface(rendered);
  103. }
  104. }