FontChain.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 gameLanguage = CGI->generaltexth->getPreferredLanguage();
  54. std::string gameEncoding = Languages::getLanguageOptions(gameLanguage).encoding;
  55. std::string fontEncoding = CGI->modh->findResourceEncoding(ResourcePath("data/" + bitmapFontName, EResType::BMP_FONT));
  56. // player uses language with different encoding than his bitmap fonts
  57. // for example, Polish language with English fonts or Chinese language which can't use H3 fonts at all
  58. // this may result in unintended mixing of ttf and bitmap fonts, which may have a bit different look
  59. // so in this case prefer ttf fonts that are likely to cover target language better than H3 fonts
  60. if (fontEncoding != gameEncoding)
  61. return false;
  62. return true; // else - use original bitmap fonts
  63. }
  64. void FontChain::addTrueTypeFont(const JsonNode & trueTypeConfig)
  65. {
  66. chain.insert(chain.begin(), std::make_unique<CTrueTypeFont>(trueTypeConfig));
  67. }
  68. void FontChain::addBitmapFont(const std::string & bitmapFilename)
  69. {
  70. if (bitmapFontsPrioritized(bitmapFilename))
  71. chain.insert(chain.begin(), std::make_unique<CBitmapFont>(bitmapFilename));
  72. else
  73. chain.push_back(std::make_unique<CBitmapFont>(bitmapFilename));
  74. }
  75. bool FontChain::canRepresentCharacter(const char * data) const
  76. {
  77. for(const auto & font : chain)
  78. if (font->canRepresentCharacter(data))
  79. return true;
  80. return false;
  81. }
  82. size_t FontChain::getLineHeightScaled() const
  83. {
  84. size_t maxHeight = 0;
  85. for(const auto & font : chain)
  86. maxHeight = std::max(maxHeight, font->getLineHeightScaled());
  87. return maxHeight;
  88. }
  89. size_t FontChain::getGlyphWidthScaled(const char * data) const
  90. {
  91. for(const auto & font : chain)
  92. if (font->canRepresentCharacter(data))
  93. return font->getGlyphWidthScaled(data);
  94. return 0;
  95. }
  96. std::vector<FontChain::TextChunk> FontChain::splitTextToChunks(const std::string & data) const
  97. {
  98. // U+FFFD - replacement character (question mark in rhombus)
  99. static const std::string replacementCharacter = u8"�";
  100. std::vector<TextChunk> chunks;
  101. const auto & selectFont = [this](const char * characterPtr) -> const IFont *
  102. {
  103. for(const auto & font : chain)
  104. if (font->canRepresentCharacter(characterPtr))
  105. return font.get();
  106. return nullptr;
  107. };
  108. for (size_t i = 0; i < data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  109. {
  110. std::string symbol = data.substr(i, TextOperations::getUnicodeCharacterSize(data[i]));
  111. const IFont * currentFont = selectFont(symbol.data());
  112. if (currentFont == nullptr)
  113. {
  114. symbol = replacementCharacter;
  115. currentFont = selectFont(symbol.data());
  116. }
  117. if (currentFont == nullptr)
  118. continue; // Still nothing - neither desired character nor fallback can be rendered
  119. if (chunks.empty() || chunks.back().font != currentFont)
  120. chunks.push_back({currentFont, symbol});
  121. else
  122. chunks.back().text += symbol;
  123. }
  124. return chunks;
  125. }
  126. size_t FontChain::getStringWidthScaled(const std::string & data) const
  127. {
  128. size_t result = 0;
  129. auto chunks = splitTextToChunks(data);
  130. for (auto const & chunk : chunks)
  131. result += chunk.font->getStringWidthScaled(chunk.text);
  132. return result;
  133. }