CBitmapFont.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 height;
  36. uint32_t ascent;
  37. void loadFont(const ResourcePath & resource, std::unordered_map<CodePoint, EntryFNT> & loadedChars);
  38. void renderCharacter(SDL_Surface * surface, const BitmapChar & character, const ColorRGBA & color, int &posX, int &posY) const;
  39. void renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const override;
  40. public:
  41. explicit CBitmapFont(const std::string & filename);
  42. ~CBitmapFont();
  43. size_t getFontAscentScaled() const override;
  44. size_t getLineHeightScaled() const override;
  45. size_t getGlyphWidthScaled(const char * data) const override;
  46. /// returns true if this font contains provided utf-8 character
  47. bool canRepresentCharacter(const char * data) const override;
  48. bool canRepresentString(const std::string & data) const;
  49. friend class CBitmapHanFont;
  50. friend class CTrueTypeFont;
  51. };