Fonts.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. /*
  3. * Fonts.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. class JsonNode;
  12. struct Point;
  13. struct SDL_Surface;
  14. struct SDL_Color;
  15. typedef struct _TTF_Font TTF_Font;
  16. class IFont
  17. {
  18. protected:
  19. /// Internal function to render font, see renderTextLeft
  20. virtual void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const = 0;
  21. public:
  22. virtual ~IFont()
  23. {}
  24. /// Returns height of font
  25. virtual size_t getLineHeight() const = 0;
  26. /// Returns width of a single symbol
  27. virtual size_t getSymbolWidth(char data) const = 0;
  28. /// Return width of the string
  29. virtual size_t getStringWidth(const std::string & data) const = 0;
  30. /**
  31. * @param surface - destination to print text on
  32. * @param data - string to print
  33. * @param color - font color
  34. * @param pos - position of rendered font
  35. */
  36. /// pos = topleft corner of the text
  37. void renderTextLeft(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  38. /// pos = center of the text
  39. void renderTextRight(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  40. /// pos = bottomright corner of the text
  41. void renderTextCenter(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  42. /**
  43. * @param maxWidth - max width in pixels of one line
  44. */
  45. /// pos = topleft corner of the text
  46. void renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const;
  47. /// pos = center of the text
  48. void renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const;
  49. /// pos = bottomright corner of the text
  50. void renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const;
  51. };
  52. class CBitmapFont : public IFont
  53. {
  54. static const size_t totalChars = 256;
  55. struct Char
  56. {
  57. si32 leftOffset;
  58. ui32 width;
  59. si32 rightOffset;
  60. ui8 *pixels; // pixels of this character, part of BitmapFont::data
  61. };
  62. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  63. const std::array<Char, totalChars> chars;
  64. const ui8 height;
  65. std::array<Char, totalChars> loadChars() const;
  66. void renderCharacter(SDL_Surface * surface, const Char & character, const SDL_Color & color, int &posX, int &posY) const;
  67. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  68. public:
  69. CBitmapFont(const std::string & filename);
  70. size_t getLineHeight() const;
  71. size_t getSymbolWidth(char data) const;
  72. size_t getStringWidth(const std::string & data) const;
  73. };
  74. class CTrueTypeFont : public IFont
  75. {
  76. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  77. const std::unique_ptr<TTF_Font, void (*)(TTF_Font*)> font;
  78. const bool blended;
  79. std::pair<std::unique_ptr<ui8[]>, ui64> loadData(const JsonNode & config);
  80. TTF_Font * loadFont(const JsonNode & config);
  81. int getFontStyle(const JsonNode & config);
  82. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  83. public:
  84. CTrueTypeFont(const JsonNode & fontConfig);
  85. size_t getLineHeight() const;
  86. size_t getSymbolWidth(char data) const;
  87. size_t getStringWidth(const std::string & data) const;
  88. };