FontChain.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * FontChain.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 "FontChain.h"
  12. #include "CTrueTypeFont.h"
  13. #include "CBitmapFont.h"
  14. #include "../CGameInfo.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include "../../lib/modding/CModHandler.h"
  17. #include "../../lib/texts/TextOperations.h"
  18. #include "../../lib/texts/CGeneralTextHandler.h"
  19. #include "../../lib/texts/Languages.h"
  20. void FontChain::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  21. {
  22. auto chunks = splitTextToChunks(data);
  23. int maxAscent = getFontAscentScaled();
  24. Point currentPos = pos;
  25. for (auto const & chunk : chunks)
  26. {
  27. Point chunkPos = currentPos;
  28. int currAscent = chunk.font->getFontAscentScaled();
  29. chunkPos.y += maxAscent - currAscent;
  30. chunk.font->renderText(surface, chunk.text, color, chunkPos);
  31. currentPos.x += chunk.font->getStringWidthScaled(chunk.text);
  32. }
  33. }
  34. size_t FontChain::getFontAscentScaled() const
  35. {
  36. size_t maxHeight = 0;
  37. for(const auto & font : chain)
  38. maxHeight = std::max(maxHeight, font->getFontAscentScaled());
  39. return maxHeight;
  40. }
  41. bool FontChain::bitmapFontsPrioritized(const std::string & bitmapFontName) const
  42. {
  43. const std::string & fontType = settings["video"]["fontsType"].String();
  44. if (fontType == "original")
  45. return true;
  46. if (fontType == "scalable")
  47. return false;
  48. // else - autoselection.
  49. if (getScalingFactor() != 1)
  50. return false; // If xbrz in use ttf/scalable fonts are preferred
  51. if (!vstd::isAlmostEqual(1.0, settings["video"]["fontScalingFactor"].Float()))
  52. return false; // If player requested non-100% scaling - use scalable fonts
  53. std::string modName = CGI->modh->findResourceOrigin(ResourcePath("data/" + bitmapFontName, EResType::BMP_FONT));
  54. std::string fontLanguage = CGI->modh->getModLanguage(modName);
  55. std::string gameLanguage = CGI->generaltexth->getPreferredLanguage();
  56. std::string fontEncoding = Languages::getLanguageOptions(fontLanguage).encoding;
  57. std::string gameEncoding = Languages::getLanguageOptions(gameLanguage).encoding;
  58. // player uses language with different encoding than his bitmap fonts
  59. // for example, Polish language with English fonts or Chinese language which can't use H3 fonts at all
  60. // this may result in unintended mixing of ttf and bitmap fonts, which may have a bit different look
  61. // so in this case prefer ttf fonts that are likely to cover target language better than H3 fonts
  62. if (fontEncoding != gameEncoding)
  63. return false;
  64. return true; // else - use original bitmap fonts
  65. }
  66. void FontChain::addTrueTypeFont(const JsonNode & trueTypeConfig)
  67. {
  68. chain.insert(chain.begin(), std::make_unique<CTrueTypeFont>(trueTypeConfig));
  69. }
  70. void FontChain::addBitmapFont(const std::string & bitmapFilename)
  71. {
  72. if (bitmapFontsPrioritized(bitmapFilename))
  73. chain.insert(chain.begin(), std::make_unique<CBitmapFont>(bitmapFilename));
  74. else
  75. chain.push_back(std::make_unique<CBitmapFont>(bitmapFilename));
  76. }
  77. bool FontChain::canRepresentCharacter(const char * data) const
  78. {
  79. for(const auto & font : chain)
  80. if (font->canRepresentCharacter(data))
  81. return true;
  82. return false;
  83. }
  84. size_t FontChain::getLineHeightScaled() const
  85. {
  86. size_t maxHeight = 0;
  87. for(const auto & font : chain)
  88. maxHeight = std::max(maxHeight, font->getLineHeightScaled());
  89. return maxHeight;
  90. }
  91. size_t FontChain::getGlyphWidthScaled(const char * data) const
  92. {
  93. for(const auto & font : chain)
  94. if (font->canRepresentCharacter(data))
  95. return font->getGlyphWidthScaled(data);
  96. return 0;
  97. }
  98. std::vector<FontChain::TextChunk> FontChain::splitTextToChunks(const std::string & data) const
  99. {
  100. std::vector<TextChunk> chunks;
  101. for (size_t i = 0; i < data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  102. {
  103. const IFont * currentFont = nullptr;
  104. for(const auto & font : chain)
  105. {
  106. if (font->canRepresentCharacter(data.data() + i))
  107. {
  108. currentFont = font.get();
  109. break;
  110. }
  111. }
  112. if (currentFont == nullptr)
  113. continue; // not representable
  114. std::string symbol = data.substr(i, TextOperations::getUnicodeCharacterSize(data[i]));
  115. if (chunks.empty() || chunks.back().font != currentFont)
  116. chunks.push_back({currentFont, symbol});
  117. else
  118. chunks.back().text += symbol;
  119. }
  120. return chunks;
  121. }
  122. size_t FontChain::getStringWidthScaled(const std::string & data) const
  123. {
  124. size_t result = 0;
  125. auto chunks = splitTextToChunks(data);
  126. for (auto const & chunk : chunks)
  127. result += chunk.font->getStringWidthScaled(chunk.text);
  128. return result;
  129. }