CBitmapFont.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * CBitmapFont.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. #include "../render/IFont.h"
  12. #include "../../lib/Rect.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class ResourcePath;
  15. VCMI_LIB_NAMESPACE_END
  16. class CBitmapFont final : public IFont
  17. {
  18. SDL_Surface * atlasImage;
  19. using CodePoint = uint32_t;
  20. struct EntryFNT
  21. {
  22. int32_t leftOffset;
  23. uint32_t width;
  24. uint32_t height;
  25. int32_t rightOffset;
  26. std::vector<uint8_t> pixels;
  27. };
  28. struct BitmapChar
  29. {
  30. Rect positionInAtlas;
  31. int32_t leftOffset;
  32. int32_t rightOffset;
  33. };
  34. std::unordered_map<CodePoint, BitmapChar> chars;
  35. uint32_t maxHeight;
  36. void loadModFont(const std::string & modName, const ResourcePath & resource, std::unordered_map<CodePoint, EntryFNT> & loadedChars);
  37. void renderCharacter(SDL_Surface * surface, const BitmapChar & character, const ColorRGBA & color, int &posX, int &posY) const;
  38. void renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const override;
  39. public:
  40. explicit CBitmapFont(const std::string & filename);
  41. ~CBitmapFont();
  42. size_t getLineHeight() const override;
  43. size_t getGlyphWidth(const char * data) const override;
  44. /// returns true if this font contains provided utf-8 character
  45. bool canRepresentCharacter(const char * data) const;
  46. bool canRepresentString(const std::string & data) const;
  47. friend class CBitmapHanFont;
  48. friend class CTrueTypeFont;
  49. };