Font.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include "anki/ui/UiInterface.h"
  7. namespace anki {
  8. /// @addtogroup ui
  9. /// @{
  10. class FontCharInfo
  11. {
  12. public:
  13. UVec2 m_advance; ///< Char advance.
  14. Rect m_imageRect; ///< The rect inside the image atlas.
  15. IVec2 m_offset; ///< Left and top bearing.
  16. };
  17. /// Font class.
  18. class Font
  19. {
  20. friend class IntrusivePtr<Font>;
  21. friend IntrusivePtr<Font>::Deleter;
  22. public:
  23. Font(UiInterface* interface)
  24. : m_interface(interface)
  25. {}
  26. ~Font();
  27. /// Initialize the font.
  28. ANKI_USE_RESULT Error init(const CString& filename, U32 fontHeight);
  29. anki_internal:
  30. /// Get info for a character.
  31. const FontCharInfo& getCharInfo(char c) const
  32. {
  33. ANKI_ASSERT(c >= FIRST_CHAR);
  34. return m_chars[c - ' '];
  35. }
  36. /// Get font image atlas.
  37. const UiImagePtr& getImage() const
  38. {
  39. return m_img;
  40. }
  41. private:
  42. static const char FIRST_CHAR = ' ';
  43. static const char LAST_CHAR = 127;
  44. static const U CHAR_COUNT = LAST_CHAR - FIRST_CHAR + 1;
  45. WeakPtr<UiInterface> m_interface;
  46. Atomic<I32> m_refcount = {0};
  47. UiImagePtr m_img;
  48. Array<FontCharInfo, CHAR_COUNT> m_chars;
  49. Atomic<I32>& getRefcount()
  50. {
  51. return m_refcount;
  52. }
  53. UiAllocator getAllocator() const
  54. {
  55. return m_interface->getAllocator();
  56. }
  57. };
  58. using FontPtr = IntrusivePtr<Font>;
  59. /// @}
  60. } // end namespace anki