text_metrics.cpp 6.2 KB

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