Fonts.h 4.5 KB

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