IFont.cpp 2.2 KB

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