Fonts.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "../Gfx/Basic.h"
  12. class JsonNode;
  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, Gfx::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, Gfx::Point pos) const;
  38. /// pos = center of the text
  39. void renderTextRight(SDL_Surface * surface, const std::string & data, const SDL_Color & color, Gfx::Point pos) const;
  40. /// pos = bottomright corner of the text
  41. void renderTextCenter(SDL_Surface * surface, const std::string & data, const SDL_Color & color, Gfx::Point pos) const;
  42. /// pos = topleft corner of the text
  43. void renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, Gfx::Point pos) const;
  44. /// pos = center of the text
  45. void renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, Gfx::Point pos) const;
  46. /// pos = bottomright corner of the text
  47. void renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, Gfx::Point pos) const;
  48. };
  49. class CBitmapFont : public IFont
  50. {
  51. static const size_t totalChars = 256;
  52. struct Char
  53. {
  54. si32 leftOffset;
  55. ui32 width;
  56. si32 rightOffset;
  57. ui8 *pixels; // pixels of this character, part of BitmapFont::data
  58. };
  59. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  60. const std::array<Char, totalChars> chars;
  61. const ui8 height;
  62. std::array<Char, totalChars> loadChars() const;
  63. void renderCharacter(SDL_Surface * surface, const Char & character, const SDL_Color & color, int &posX, int &posY) const;
  64. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, Gfx::Point pos) const;
  65. public:
  66. CBitmapFont(const std::string & filename);
  67. size_t getLineHeight() const;
  68. size_t getSymbolWidth(char data) const;
  69. size_t getStringWidth(const std::string & data) const;
  70. };
  71. class CTrueTypeFont : public IFont
  72. {
  73. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  74. const std::unique_ptr<TTF_Font, void (*)(TTF_Font*)> font;
  75. const bool blended;
  76. std::pair<std::unique_ptr<ui8[]>, ui64> loadData(const JsonNode & config);
  77. TTF_Font * loadFont(const JsonNode & config);
  78. int getFontStyle(const JsonNode & config);
  79. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, Gfx::Point pos) const;
  80. public:
  81. CTrueTypeFont(const JsonNode & fontConfig);
  82. size_t getLineHeight() const;
  83. size_t getSymbolWidth(char data) const;
  84. size_t getStringWidth(const std::string & data) const;
  85. };