CTrueTypeFont.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "../render/Colors.h"
  13. #include "../renderSDL/SDL_Extensions.h"
  14. #include "../../lib/JsonNode.h"
  15. #include "../../lib/TextOperations.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include <SDL_ttf.h>
  18. std::pair<std::unique_ptr<ui8[]>, ui64> CTrueTypeFont::loadData(const JsonNode & config)
  19. {
  20. std::string filename = "Data/" + config["file"].String();
  21. return CResourceHandler::get()->load(ResourceID(filename, EResType::TTF_FONT))->readAll();
  22. }
  23. TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
  24. {
  25. int pointSize = static_cast<int>(config["size"].Float());
  26. if(!TTF_WasInit() && TTF_Init()==-1)
  27. throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
  28. return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), (int)data.second), 1, pointSize);
  29. }
  30. int CTrueTypeFont::getFontStyle(const JsonNode &config)
  31. {
  32. const JsonVector & names = config["style"].Vector();
  33. int ret = 0;
  34. for(const JsonNode & node : names)
  35. {
  36. if (node.String() == "bold")
  37. ret |= TTF_STYLE_BOLD;
  38. else if (node.String() == "italic")
  39. ret |= TTF_STYLE_ITALIC;
  40. }
  41. return ret;
  42. }
  43. CTrueTypeFont::CTrueTypeFont(const JsonNode & fontConfig):
  44. data(loadData(fontConfig)),
  45. font(loadFont(fontConfig), TTF_CloseFont),
  46. blended(fontConfig["blend"].Bool())
  47. {
  48. assert(font);
  49. TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
  50. }
  51. size_t CTrueTypeFont::getLineHeight() const
  52. {
  53. return TTF_FontHeight(font.get());
  54. }
  55. size_t CTrueTypeFont::getGlyphWidth(const char *data) const
  56. {
  57. return getStringWidth(std::string(data, TextOperations::getUnicodeCharacterSize(*data)));
  58. /*
  59. int advance;
  60. TTF_GlyphMetrics(font.get(), *data, nullptr, nullptr, nullptr, nullptr, &advance);
  61. return advance;
  62. */
  63. }
  64. size_t CTrueTypeFont::getStringWidth(const std::string & data) const
  65. {
  66. int width;
  67. TTF_SizeUTF8(font.get(), data.c_str(), &width, nullptr);
  68. return width;
  69. }
  70. void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
  71. {
  72. if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
  73. renderText(surface, data, Colors::BLACK, pos + Point(1,1));
  74. if (!data.empty())
  75. {
  76. SDL_Surface * rendered;
  77. if (blended)
  78. rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), color);
  79. else
  80. rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), color);
  81. assert(rendered);
  82. CSDL_Ext::blitSurface(rendered, surface, pos);
  83. SDL_FreeSurface(rendered);
  84. }
  85. }