text_metrics.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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. private:
  21. FontManager* m_fontManager;
  22. float m_width;
  23. float m_height;
  24. float m_x;
  25. float m_lineHeight;
  26. float m_lineGap;
  27. };
  28. /// Compute text crop area for text using a single font.
  29. class TextLineMetrics
  30. {
  31. public:
  32. TextLineMetrics(const FontInfo& _fontInfo);
  33. /// Return the height of a line of text using the given font.
  34. float getLineHeight() const { return m_lineHeight; }
  35. /// Return the number of text line in the given text.
  36. uint32_t getLineCount(const char* _string) const;
  37. /// Return the number of text line in the given text.
  38. uint32_t getLineCount(const wchar_t* _string) const;
  39. /// Return the first and last character visible in the [_firstLine, _lastLine] range.
  40. void getSubText(const char* _string, uint32_t _firstLine, uint32_t _lastLine, const char*& _begin, const char*& _end);
  41. /// Return the first and last character visible in the [_firstLine, _lastLine] range.
  42. void getSubText(const wchar_t* _string, uint32_t _firstLine, uint32_t _lastLine, const wchar_t*& _begin, const wchar_t*& _end);
  43. /// Return the first and last character visible in the [_top, _bottom] range,
  44. void getVisibleText(const char* _string, float _top, float _bottom, const char*& _begin, const char*& _end);
  45. /// Return the first and last character visible in the [_top, _bottom] range,
  46. void getVisibleText(const wchar_t* _string, float _top, float _bottom, const wchar_t*& _begin, const wchar_t*& _end);
  47. private:
  48. float m_lineHeight;
  49. };
  50. #endif // TEXT_METRICS_H_HEADER_GUARD