IFont.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * IFont.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 "IFont.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/IScreenHandler.h"
  14. #include "../../lib/Point.h"
  15. #include "../../lib/texts/TextOperations.h"
  16. int IFont::getScalingFactor() const
  17. {
  18. return GH.screenHandler().getScalingFactor();
  19. }
  20. size_t IFont::getLineHeight() const
  21. {
  22. return getLineHeightScaled() / getScalingFactor();
  23. }
  24. size_t IFont::getGlyphWidth(const char * data) const
  25. {
  26. return getGlyphWidthScaled(data) / getScalingFactor();
  27. }
  28. size_t IFont::getStringWidth(const std::string & data) const
  29. {
  30. return getStringWidthScaled(data) / getScalingFactor();
  31. }
  32. size_t IFont::getFontAscent() const
  33. {
  34. return getFontAscentScaled() / getScalingFactor();
  35. }
  36. size_t IFont::getStringWidthScaled(const std::string & data) const
  37. {
  38. size_t width = 0;
  39. for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  40. {
  41. width += getGlyphWidthScaled(data.data() + i);
  42. }
  43. return width;
  44. }
  45. void IFont::renderTextLeft(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  46. {
  47. renderText(surface, data, color, pos);
  48. }
  49. void IFont::renderTextRight(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  50. {
  51. Point size = Point(getStringWidth(data), getLineHeight()) * getScalingFactor();
  52. renderText(surface, data, color, pos - size);
  53. }
  54. void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  55. {
  56. Point size = Point(getStringWidth(data), getLineHeight()) * getScalingFactor();
  57. renderText(surface, data, color, pos - size / 2);
  58. }
  59. void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
  60. {
  61. Point currPos = pos;
  62. for(const std::string & line : data)
  63. {
  64. renderTextLeft(surface, line, color, currPos);
  65. currPos.y += getLineHeight() * getScalingFactor();
  66. }
  67. }
  68. void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
  69. {
  70. Point currPos = pos;
  71. currPos.y -= data.size() * getLineHeight() * getScalingFactor();
  72. for(const std::string & line : data)
  73. {
  74. renderTextRight(surface, line, color, currPos);
  75. currPos.y += getLineHeight() * getScalingFactor();
  76. }
  77. }
  78. void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
  79. {
  80. Point currPos = pos;
  81. currPos.y -= data.size() * getLineHeight() / 2 * getScalingFactor();
  82. for(const std::string & line : data)
  83. {
  84. renderTextCenter(surface, line, color, currPos);
  85. currPos.y += getLineHeight() * getScalingFactor();
  86. }
  87. }