Fonts.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Fonts.h, 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. #pragma once
  11. class JsonNode;
  12. struct Point;
  13. struct SDL_Surface;
  14. struct SDL_Color;
  15. typedef struct _TTF_Font TTF_Font;
  16. class CBitmapFont;
  17. class CBitmapHanFont;
  18. class IFont
  19. {
  20. protected:
  21. /// Internal function to render font, see renderTextLeft
  22. virtual void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const = 0;
  23. public:
  24. virtual ~IFont()
  25. {}
  26. /// Returns height of font
  27. virtual size_t getLineHeight() const = 0;
  28. /// Returns width, in pixels of a character glyph. Pointer must contain at least characterSize valid bytes
  29. virtual size_t getGlyphWidth(const char * data) const = 0;
  30. /// Return width of the string
  31. virtual size_t getStringWidth(const std::string & data) const;
  32. /**
  33. * @param surface - destination to print text on
  34. * @param data - string to print
  35. * @param color - font color
  36. * @param pos - position of rendered font
  37. */
  38. /// pos = topleft corner of the text
  39. void renderTextLeft(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  40. /// pos = center of the text
  41. void renderTextRight(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  42. /// pos = bottomright corner of the text
  43. void renderTextCenter(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const;
  44. /// pos = topleft corner of the text
  45. void renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const;
  46. /// pos = center of the text
  47. void renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const;
  48. /// pos = bottomright corner of the text
  49. void renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const SDL_Color & color, const Point & pos) const;
  50. };
  51. class CBitmapFont : public IFont
  52. {
  53. static const size_t totalChars = 256;
  54. struct BitmapChar
  55. {
  56. si32 leftOffset;
  57. ui32 width;
  58. si32 rightOffset;
  59. ui8 *pixels; // pixels of this character, part of BitmapFont::data
  60. };
  61. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  62. const std::array<BitmapChar, totalChars> chars;
  63. const ui8 height;
  64. std::array<BitmapChar, totalChars> loadChars() const;
  65. void renderCharacter(SDL_Surface * surface, const BitmapChar & character, const SDL_Color & color, int &posX, int &posY) const;
  66. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const override;
  67. public:
  68. CBitmapFont(const std::string & filename);
  69. size_t getLineHeight() const override;
  70. size_t getGlyphWidth(const char * data) const override;
  71. friend class CBitmapHanFont;
  72. };
  73. /// supports multi-byte characters for such languages like Chinese
  74. class CBitmapHanFont : public IFont
  75. {
  76. std::unique_ptr<CBitmapFont> fallback;
  77. // data, directly copied from file
  78. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  79. // size of the font. Not available in file but needed for proper rendering
  80. const size_t size;
  81. size_t getCharacterDataOffset(size_t index) const;
  82. size_t getCharacterIndex(ui8 first, ui8 second) const;
  83. void renderCharacter(SDL_Surface * surface, int characterIndex, const SDL_Color & color, int &posX, int &posY) const;
  84. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const override;
  85. public:
  86. CBitmapHanFont(const JsonNode & config);
  87. size_t getLineHeight() const override;
  88. size_t getGlyphWidth(const char * data) const override;
  89. };
  90. class CTrueTypeFont : public IFont
  91. {
  92. const std::pair<std::unique_ptr<ui8[]>, ui64> data;
  93. const std::unique_ptr<TTF_Font, void (*)(TTF_Font*)> font;
  94. const bool blended;
  95. std::pair<std::unique_ptr<ui8[]>, ui64> loadData(const JsonNode & config);
  96. TTF_Font * loadFont(const JsonNode & config);
  97. int getFontStyle(const JsonNode & config);
  98. void renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const override;
  99. public:
  100. CTrueTypeFont(const JsonNode & fontConfig);
  101. size_t getLineHeight() const override;
  102. size_t getGlyphWidth(const char * data) const override;
  103. size_t getStringWidth(const std::string & data) const override;
  104. };