FontChain.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, bool begin)
  65. {
  66. if(begin)
  67. chain.insert(chain.begin(), std::make_unique<CTrueTypeFont>(trueTypeConfig));
  68. else
  69. chain.push_back(std::make_unique<CTrueTypeFont>(trueTypeConfig));
  70. }
  71. void FontChain::addBitmapFont(const std::string & bitmapFilename)
  72. {
  73. if (bitmapFontsPrioritized(bitmapFilename))
  74. chain.insert(chain.begin(), std::make_unique<CBitmapFont>(bitmapFilename));
  75. else
  76. chain.push_back(std::make_unique<CBitmapFont>(bitmapFilename));
  77. }
  78. bool FontChain::canRepresentCharacter(const char * data) const
  79. {
  80. for(const auto & font : chain)
  81. if (font->canRepresentCharacter(data))
  82. return true;
  83. return false;
  84. }
  85. size_t FontChain::getLineHeightScaled() const
  86. {
  87. size_t maxHeight = 0;
  88. for(const auto & font : chain)
  89. maxHeight = std::max(maxHeight, font->getLineHeightScaled());
  90. return maxHeight;
  91. }
  92. size_t FontChain::getGlyphWidthScaled(const char * data) const
  93. {
  94. for(const auto & font : chain)
  95. if (font->canRepresentCharacter(data))
  96. return font->getGlyphWidthScaled(data);
  97. return 0;
  98. }
  99. std::vector<FontChain::TextChunk> FontChain::splitTextToChunks(const std::string & data) const
  100. {
  101. // U+FFFD - replacement character (question mark in rhombus)
  102. static const std::string replacementCharacter = u8"�";
  103. std::vector<TextChunk> chunks;
  104. const auto & selectFont = [this](const char * characterPtr) -> const IFont *
  105. {
  106. for(const auto & font : chain)
  107. if (font->canRepresentCharacter(characterPtr))
  108. return font.get();
  109. return nullptr;
  110. };
  111. for (size_t i = 0; i < data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  112. {
  113. std::string symbol = data.substr(i, TextOperations::getUnicodeCharacterSize(data[i]));
  114. const IFont * currentFont = selectFont(symbol.data());
  115. if (currentFont == nullptr)
  116. {
  117. symbol = replacementCharacter;
  118. currentFont = selectFont(symbol.data());
  119. }
  120. if (currentFont == nullptr)
  121. continue; // Still nothing - neither desired character nor fallback can be rendered
  122. if (chunks.empty() || chunks.back().font != currentFont)
  123. chunks.push_back({currentFont, symbol});
  124. else
  125. chunks.back().text += symbol;
  126. }
  127. return chunks;
  128. }
  129. size_t FontChain::getStringWidthScaled(const std::string & data) const
  130. {
  131. size_t result = 0;
  132. auto chunks = splitTextToChunks(data);
  133. for (auto const & chunk : chunks)
  134. result += chunk.font->getStringWidthScaled(chunk.text);
  135. return result;
  136. }