IFont.cpp 2.2 KB

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