text_metrics.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #ifndef TEXT_METRICS_H_HEADER_GUARD
  6. #define TEXT_METRICS_H_HEADER_GUARD
  7. #include "font_manager.h"
  8. class TextMetrics
  9. {
  10. public:
  11. TextMetrics(FontManager* _fontManager);
  12. /// Append an ASCII/utf-8 string to the metrics helper.
  13. void appendText(FontHandle _fontHandle, const char* _string);
  14. /// Append a wide char string to the metrics helper.
  15. void appendText(FontHandle _fontHandle, const wchar_t* _string);
  16. /// Return the width of the measured text.
  17. float getWidth() const { return m_width; }
  18. /// Return the height of the measured text.
  19. float getHeight() const { return m_height; }
  20. /// Clear the width and height of the measured text.
  21. void clearText();
  22. private:
  23. FontManager* m_fontManager;
  24. float m_width;
  25. float m_height;
  26. float m_x;
  27. float m_lineHeight;
  28. float m_lineGap;
  29. };
  30. /// Compute text crop area for text using a single font.
  31. class TextLineMetrics
  32. {
  33. public:
  34. TextLineMetrics(const FontInfo& _fontInfo);
  35. /// Return the height of a line of text using the given font.
  36. float getLineHeight() const { return m_lineHeight; }
  37. /// Return the number of text line in the given text.
  38. uint32_t getLineCount(const bx::StringView& _string) const;
  39. /// Return the first and last character visible in the [_firstLine, _lastLine] range.
  40. void getSubText(const bx::StringView& _str, uint32_t _firstLine, uint32_t _lastLine, const char*& _begin, const char*& _end);
  41. private:
  42. float m_lineHeight;
  43. };
  44. #endif // TEXT_METRICS_H_HEADER_GUARD