IFont.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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::getStringWidth(const std::string & data) const
  21. {
  22. size_t width = 0;
  23. for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
  24. {
  25. width += getGlyphWidth(data.data() + i);
  26. }
  27. return width;
  28. }
  29. void IFont::renderTextLeft(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  30. {
  31. renderText(surface, data, color, pos);
  32. }
  33. void IFont::renderTextRight(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  34. {
  35. Point size = Point((int)getStringWidth(data), (int)getLineHeight()) * getScalingFactor();
  36. renderText(surface, data, color, pos - size);
  37. }
  38. void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
  39. {
  40. Point size = Point((int)getStringWidth(data), (int)getLineHeight()) * getScalingFactor();
  41. renderText(surface, data, color, pos - size / 2);
  42. }
  43. void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
  44. {
  45. Point currPos = pos;
  46. for(const std::string & line : data)
  47. {
  48. renderTextLeft(surface, line, color, currPos);
  49. currPos.y += getLineHeight() * getScalingFactor();
  50. }
  51. }
  52. void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
  53. {
  54. Point currPos = pos;
  55. currPos.y -= (int)data.size() * (int)getLineHeight() * getScalingFactor();
  56. for(const std::string & line : data)
  57. {
  58. renderTextRight(surface, line, color, currPos);
  59. currPos.y += getLineHeight() * getScalingFactor();
  60. }
  61. }
  62. void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
  63. {
  64. Point currPos = pos;
  65. currPos.y -= (int)data.size() * (int)getLineHeight() / 2 * getScalingFactor();
  66. for(const std::string & line : data)
  67. {
  68. renderTextCenter(surface, line, color, currPos);
  69. currPos.y += getLineHeight() * getScalingFactor();
  70. }
  71. }