text_metrics.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <wchar.h> // wcslen
  6. #include "text_metrics.h"
  7. #include "utf8.h"
  8. TextMetrics::TextMetrics(FontManager* _fontManager)
  9. : m_fontManager(_fontManager)
  10. {
  11. clearText();
  12. }
  13. void TextMetrics::clearText()
  14. {
  15. m_width = m_height = m_x = m_lineHeight = m_lineGap = 0;
  16. }
  17. void TextMetrics::appendText(FontHandle _fontHandle, const char* _string)
  18. {
  19. const FontInfo& font = m_fontManager->getFontInfo(_fontHandle);
  20. if (font.lineGap > m_lineGap)
  21. {
  22. m_lineGap = font.lineGap;
  23. }
  24. if ( (font.ascender - font.descender) > m_lineHeight)
  25. {
  26. m_height -= m_lineHeight;
  27. m_lineHeight = font.ascender - font.descender;
  28. m_height += m_lineHeight;
  29. }
  30. CodePoint codepoint = 0;
  31. uint32_t state = 0;
  32. for (; *_string; ++_string)
  33. {
  34. if (!utf8_decode(&state, (uint32_t*)&codepoint, *_string) )
  35. {
  36. const GlyphInfo* glyph = m_fontManager->getGlyphInfo(_fontHandle, codepoint);
  37. if (NULL != glyph)
  38. {
  39. if (codepoint == L'\n')
  40. {
  41. m_height += m_lineGap + font.ascender - font.descender;
  42. m_lineGap = font.lineGap;
  43. m_lineHeight = font.ascender - font.descender;
  44. m_x = 0;
  45. }
  46. m_x += glyph->advance_x;
  47. if(m_x > m_width)
  48. {
  49. m_width = m_x;
  50. }
  51. }
  52. else
  53. {
  54. BX_CHECK(false, "Glyph not found");
  55. }
  56. }
  57. }
  58. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  59. }
  60. void TextMetrics::appendText(FontHandle _fontHandle, const wchar_t* _string)
  61. {
  62. const FontInfo& font = m_fontManager->getFontInfo(_fontHandle);
  63. if (font.lineGap > m_lineGap)
  64. {
  65. m_lineGap = font.lineGap;
  66. }
  67. if ( (font.ascender - font.descender) > m_lineHeight)
  68. {
  69. m_height -= m_lineHeight;
  70. m_lineHeight = font.ascender - font.descender;
  71. m_height += m_lineHeight;
  72. }
  73. for (uint32_t ii = 0, end = (uint32_t)wcslen(_string); ii < end; ++ii)
  74. {
  75. uint32_t codepoint = _string[ii];
  76. const GlyphInfo* glyph = m_fontManager->getGlyphInfo(_fontHandle, codepoint);
  77. if (NULL != glyph)
  78. {
  79. if (codepoint == L'\n')
  80. {
  81. m_height += m_lineGap + font.ascender - font.descender;
  82. m_lineGap = font.lineGap;
  83. m_lineHeight = font.ascender - font.descender;
  84. m_x = 0;
  85. }
  86. m_x += glyph->advance_x;
  87. if(m_x > m_width)
  88. {
  89. m_width = m_x;
  90. }
  91. }
  92. else
  93. {
  94. BX_CHECK(false, "Glyph not found");
  95. }
  96. }
  97. }
  98. TextLineMetrics::TextLineMetrics(const FontInfo& _fontInfo)
  99. {
  100. m_lineHeight = _fontInfo.ascender - _fontInfo.descender + _fontInfo.lineGap;
  101. }
  102. uint32_t TextLineMetrics::getLineCount(const char* _string) const
  103. {
  104. CodePoint codepoint = 0;
  105. uint32_t state = 0;
  106. uint32_t lineCount = 1;
  107. for (; *_string; ++_string)
  108. {
  109. if (utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT)
  110. {
  111. if(codepoint == L'\n')
  112. {
  113. ++lineCount;
  114. }
  115. }
  116. }
  117. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  118. return lineCount;
  119. }
  120. uint32_t TextLineMetrics::getLineCount(const wchar_t* _string) const
  121. {
  122. uint32_t lineCount = 1;
  123. for ( ;*_string != L'\0'; ++_string)
  124. {
  125. if(*_string == L'\n')
  126. {
  127. ++lineCount;
  128. }
  129. }
  130. return lineCount;
  131. }
  132. void TextLineMetrics::getSubText(const char* _string, uint32_t _firstLine, uint32_t _lastLine, const char*& _begin, const char*& _end)
  133. {
  134. CodePoint codepoint = 0;
  135. uint32_t state = 0;
  136. // y is bottom of a text line
  137. uint32_t currentLine = 0;
  138. while(*_string && (currentLine < _firstLine) )
  139. {
  140. for (; *_string; ++_string)
  141. {
  142. if (utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT)
  143. {
  144. if (codepoint == L'\n')
  145. {
  146. ++currentLine;
  147. ++_string;
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  154. _begin = _string;
  155. while ( (*_string) && (currentLine < _lastLine) )
  156. {
  157. for (; *_string; ++_string)
  158. {
  159. if(utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT)
  160. {
  161. if(codepoint == L'\n')
  162. {
  163. ++currentLine;
  164. ++_string;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  171. _end = _string;
  172. }
  173. void TextLineMetrics::getSubText(const wchar_t* _string, uint32_t _firstLine, uint32_t _lastLine, const wchar_t*& _begin, const wchar_t*& _end)
  174. {
  175. uint32_t currentLine = 0;
  176. while ( (*_string != L'\0') && (currentLine < _firstLine) )
  177. {
  178. for ( ;*_string != L'\0'; ++_string)
  179. {
  180. if(*_string == L'\n')
  181. {
  182. ++currentLine;
  183. ++_string;
  184. break;
  185. }
  186. }
  187. }
  188. _begin = _string;
  189. while ( (*_string != L'\0') && (currentLine < _lastLine) )
  190. {
  191. for ( ;*_string != L'\0'; ++_string)
  192. {
  193. if(*_string == L'\n')
  194. {
  195. ++currentLine;
  196. ++_string;
  197. break;
  198. }
  199. }
  200. }
  201. _end = _string;
  202. }
  203. void TextLineMetrics::getVisibleText(const char* _string, float _top, float _bottom, const char*& _begin, const char*& _end)
  204. {
  205. CodePoint codepoint = 0;
  206. uint32_t state = 0;
  207. // y is bottom of a text line
  208. float y = m_lineHeight;
  209. while (*_string && (y < _top) )
  210. {
  211. for (; *_string; ++_string)
  212. {
  213. if(utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT)
  214. {
  215. if(codepoint == L'\n')
  216. {
  217. y += m_lineHeight;
  218. ++_string;
  219. break;
  220. }
  221. }
  222. }
  223. }
  224. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  225. _begin = _string;
  226. // y is now top of a text line
  227. y -= m_lineHeight;
  228. while ( (*_string) && (y < _bottom) )
  229. {
  230. for (; *_string; ++_string)
  231. {
  232. if(utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT)
  233. {
  234. if(codepoint == L'\n')
  235. {
  236. y += m_lineHeight;
  237. ++_string;
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  244. _end = _string;
  245. }
  246. void TextLineMetrics::getVisibleText(const wchar_t* _string, float _top, float _bottom, const wchar_t*& _begin, const wchar_t*& _end)
  247. {
  248. // y is bottom of a text line
  249. float y = m_lineHeight;
  250. const wchar_t* _textEnd = _string + wcslen(_string);
  251. while (y < _top)
  252. {
  253. for (const wchar_t* _current = _string; _current < _textEnd; ++_current)
  254. {
  255. if(*_current == L'\n')
  256. {
  257. y += m_lineHeight;
  258. ++_string;
  259. break;
  260. }
  261. }
  262. }
  263. _begin = _string;
  264. // y is now top of a text line
  265. y -= m_lineHeight;
  266. while (y < _bottom )
  267. {
  268. for (const wchar_t* _current = _string; _current < _textEnd; ++_current)
  269. {
  270. if(*_current == L'\n')
  271. {
  272. y += m_lineHeight;
  273. ++_string;
  274. break;
  275. }
  276. }
  277. }
  278. _end = _string;
  279. }