text_metrics.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "text_metrics.h"
  6. #include "utf8.h"
  7. TextMetrics::TextMetrics(FontManager* _fontManager)
  8. : m_fontManager(_fontManager)
  9. {
  10. clearText();
  11. }
  12. void TextMetrics::clearText()
  13. {
  14. m_width = m_height = m_x = m_lineHeight = m_lineGap = 0;
  15. }
  16. void TextMetrics::appendText(FontHandle _fontHandle, const char* _string)
  17. {
  18. const FontInfo& font = m_fontManager->getFontInfo(_fontHandle);
  19. if (font.lineGap > m_lineGap)
  20. {
  21. m_lineGap = font.lineGap;
  22. }
  23. if ( (font.ascender - font.descender) > m_lineHeight)
  24. {
  25. m_height -= m_lineHeight;
  26. m_lineHeight = font.ascender - font.descender;
  27. m_height += m_lineHeight;
  28. }
  29. CodePoint codepoint = 0;
  30. uint32_t state = 0;
  31. for (; *_string; ++_string)
  32. {
  33. if (!utf8_decode(&state, (uint32_t*)&codepoint, *_string) )
  34. {
  35. const GlyphInfo* glyph = m_fontManager->getGlyphInfo(_fontHandle, codepoint);
  36. if (NULL != glyph)
  37. {
  38. if (codepoint == L'\n')
  39. {
  40. m_height += m_lineGap + font.ascender - font.descender;
  41. m_lineGap = font.lineGap;
  42. m_lineHeight = font.ascender - font.descender;
  43. m_x = 0;
  44. }
  45. m_x += glyph->advance_x;
  46. if(m_x > m_width)
  47. {
  48. m_width = m_x;
  49. }
  50. }
  51. else
  52. {
  53. BX_ASSERT(false, "Glyph not found");
  54. }
  55. }
  56. }
  57. BX_ASSERT(state == UTF8_ACCEPT, "The string is not well-formed");
  58. }
  59. TextLineMetrics::TextLineMetrics(const FontInfo& _fontInfo)
  60. {
  61. m_lineHeight = _fontInfo.ascender - _fontInfo.descender + _fontInfo.lineGap;
  62. }
  63. uint32_t TextLineMetrics::getLineCount(const bx::StringView& _str) const
  64. {
  65. CodePoint codepoint = 0;
  66. uint32_t state = 0;
  67. uint32_t lineCount = 1;
  68. for (const char* ptr = _str.getPtr(); ptr != _str.getTerm(); ++ptr)
  69. {
  70. if (utf8_decode(&state, (uint32_t*)&codepoint, *ptr) == UTF8_ACCEPT)
  71. {
  72. if (codepoint == L'\n')
  73. {
  74. ++lineCount;
  75. }
  76. }
  77. }
  78. BX_ASSERT(state == UTF8_ACCEPT, "The string is not well-formed");
  79. return lineCount;
  80. }
  81. void TextLineMetrics::getSubText(const bx::StringView& _str, uint32_t _firstLine, uint32_t _lastLine, const char*& _begin, const char*& _end)
  82. {
  83. CodePoint codepoint = 0;
  84. uint32_t state = 0;
  85. // y is bottom of a text line
  86. uint32_t currentLine = 0;
  87. const char* ptr = _str.getPtr();
  88. while (ptr != _str.getTerm()
  89. && (currentLine < _firstLine) )
  90. {
  91. for (; ptr != _str.getTerm(); ++ptr)
  92. {
  93. if (utf8_decode(&state, (uint32_t*)&codepoint, *ptr) == UTF8_ACCEPT)
  94. {
  95. if (codepoint == L'\n')
  96. {
  97. ++currentLine;
  98. ++ptr;
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. BX_ASSERT(state == UTF8_ACCEPT, "The string is not well-formed");
  105. _begin = ptr;
  106. while (ptr != _str.getTerm()
  107. && (currentLine < _lastLine) )
  108. {
  109. for (; ptr != _str.getTerm(); ++ptr)
  110. {
  111. if(utf8_decode(&state, (uint32_t*)&codepoint, *ptr) == UTF8_ACCEPT)
  112. {
  113. if(codepoint == L'\n')
  114. {
  115. ++currentLine;
  116. ++ptr;
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. BX_ASSERT(state == UTF8_ACCEPT, "The string is not well-formed");
  123. _end = ptr;
  124. }