Font.h 1.4 KB

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