IFont.cpp 2.3 KB

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