text_buffer_manager.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "../common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bgfx/embedded_shader.h>
  8. #include <stddef.h> // offsetof
  9. #include <wchar.h> // wcslen
  10. #include "text_buffer_manager.h"
  11. #include "utf8.h"
  12. #include "../cube_atlas.h"
  13. #include "vs_font_basic.bin.h"
  14. #include "fs_font_basic.bin.h"
  15. #include "vs_font_distance_field.bin.h"
  16. #include "fs_font_distance_field.bin.h"
  17. #include "vs_font_distance_field_subpixel.bin.h"
  18. #include "fs_font_distance_field_subpixel.bin.h"
  19. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  20. {
  21. BGFX_EMBEDDED_SHADER(vs_font_basic),
  22. BGFX_EMBEDDED_SHADER(fs_font_basic),
  23. BGFX_EMBEDDED_SHADER(vs_font_distance_field),
  24. BGFX_EMBEDDED_SHADER(fs_font_distance_field),
  25. BGFX_EMBEDDED_SHADER(vs_font_distance_field_subpixel),
  26. BGFX_EMBEDDED_SHADER(fs_font_distance_field_subpixel),
  27. BGFX_EMBEDDED_SHADER_END()
  28. };
  29. #define MAX_BUFFERED_CHARACTERS (8192 - 5)
  30. class TextBuffer
  31. {
  32. public:
  33. /// TextBuffer is bound to a fontManager for glyph retrieval
  34. /// @remark the ownership of the manager is not taken
  35. TextBuffer(FontManager* _fontManager);
  36. ~TextBuffer();
  37. void setStyle(uint32_t _flags = STYLE_NORMAL)
  38. {
  39. m_styleFlags = _flags;
  40. }
  41. void setTextColor(uint32_t _rgba = 0x000000FF)
  42. {
  43. m_textColor = toABGR(_rgba);
  44. }
  45. void setBackgroundColor(uint32_t _rgba = 0x000000FF)
  46. {
  47. m_backgroundColor = toABGR(_rgba);
  48. }
  49. void setOverlineColor(uint32_t _rgba = 0x000000FF)
  50. {
  51. m_overlineColor = toABGR(_rgba);
  52. }
  53. void setUnderlineColor(uint32_t _rgba = 0x000000FF)
  54. {
  55. m_underlineColor = toABGR(_rgba);
  56. }
  57. void setStrikeThroughColor(uint32_t _rgba = 0x000000FF)
  58. {
  59. m_strikeThroughColor = toABGR(_rgba);
  60. }
  61. void setPenPosition(float _x, float _y)
  62. {
  63. m_penX = _x; m_penY = _y;
  64. }
  65. /// Append an ASCII/utf-8 string to the buffer using current pen
  66. /// position and color.
  67. void appendText(FontHandle _fontHandle, const char* _string, const char* _end = NULL);
  68. /// Append a wide char unicode string to the buffer using current pen
  69. /// position and color.
  70. void appendText(FontHandle _fontHandle, const wchar_t* _string, const wchar_t* _end = NULL);
  71. /// Append a whole face of the atlas cube, mostly used for debugging
  72. /// and visualizing atlas.
  73. void appendAtlasFace(uint16_t _faceIndex);
  74. /// Clear the text buffer and reset its state (pen/color)
  75. void clearTextBuffer();
  76. /// Get pointer to the vertex buffer to submit it to the graphic card.
  77. const uint8_t* getVertexBuffer()
  78. {
  79. return (uint8_t*) m_vertexBuffer;
  80. }
  81. /// Number of vertex in the vertex buffer.
  82. uint32_t getVertexCount() const
  83. {
  84. return m_vertexCount;
  85. }
  86. /// Size in bytes of a vertex.
  87. uint32_t getVertexSize() const
  88. {
  89. return sizeof(TextVertex);
  90. }
  91. /// get a pointer to the index buffer to submit it to the graphic
  92. const uint16_t* getIndexBuffer() const
  93. {
  94. return m_indexBuffer;
  95. }
  96. /// number of index in the index buffer
  97. uint32_t getIndexCount() const
  98. {
  99. return m_indexCount;
  100. }
  101. /// Size in bytes of an index.
  102. uint32_t getIndexSize() const
  103. {
  104. return sizeof(uint16_t);
  105. }
  106. uint32_t getTextColor() const
  107. {
  108. return toABGR(m_textColor);
  109. }
  110. TextRectangle getRectangle() const
  111. {
  112. return m_rectangle;
  113. }
  114. private:
  115. void appendGlyph(FontHandle _handle, CodePoint _codePoint);
  116. void verticalCenterLastLine(float _txtDecalY, float _top, float _bottom);
  117. static uint32_t toABGR(uint32_t _rgba)
  118. {
  119. return ( ( (_rgba >> 0) & 0xff) << 24)
  120. | ( ( (_rgba >> 8) & 0xff) << 16)
  121. | ( ( (_rgba >> 16) & 0xff) << 8)
  122. | ( ( (_rgba >> 24) & 0xff) << 0)
  123. ;
  124. }
  125. uint32_t m_styleFlags;
  126. // color states
  127. uint32_t m_textColor;
  128. uint32_t m_backgroundColor;
  129. uint32_t m_overlineColor;
  130. uint32_t m_underlineColor;
  131. uint32_t m_strikeThroughColor;
  132. //position states
  133. float m_penX;
  134. float m_penY;
  135. float m_originX;
  136. float m_originY;
  137. float m_lineAscender;
  138. float m_lineDescender;
  139. float m_lineGap;
  140. TextRectangle m_rectangle;
  141. FontManager* m_fontManager;
  142. void setVertex(uint32_t _i, float _x, float _y, uint32_t _rgba, uint8_t _style = STYLE_NORMAL)
  143. {
  144. m_vertexBuffer[_i].x = _x;
  145. m_vertexBuffer[_i].y = _y;
  146. m_vertexBuffer[_i].rgba = _rgba;
  147. m_styleBuffer[_i] = _style;
  148. }
  149. struct TextVertex
  150. {
  151. float x, y;
  152. int16_t u, v, w, t;
  153. uint32_t rgba;
  154. };
  155. TextVertex* m_vertexBuffer;
  156. uint16_t* m_indexBuffer;
  157. uint8_t* m_styleBuffer;
  158. uint32_t m_indexCount;
  159. uint32_t m_lineStartIndex;
  160. uint16_t m_vertexCount;
  161. };
  162. TextBuffer::TextBuffer(FontManager* _fontManager)
  163. : m_styleFlags(STYLE_NORMAL)
  164. , m_textColor(0xffffffff)
  165. , m_backgroundColor(0xffffffff)
  166. , m_overlineColor(0xffffffff)
  167. , m_underlineColor(0xffffffff)
  168. , m_strikeThroughColor(0xffffffff)
  169. , m_penX(0)
  170. , m_penY(0)
  171. , m_originX(0)
  172. , m_originY(0)
  173. , m_lineAscender(0)
  174. , m_lineDescender(0)
  175. , m_lineGap(0)
  176. , m_fontManager(_fontManager)
  177. , m_vertexBuffer(new TextVertex[MAX_BUFFERED_CHARACTERS * 4])
  178. , m_indexBuffer(new uint16_t[MAX_BUFFERED_CHARACTERS * 6])
  179. , m_styleBuffer(new uint8_t[MAX_BUFFERED_CHARACTERS * 4])
  180. , m_indexCount(0)
  181. , m_lineStartIndex(0)
  182. , m_vertexCount(0)
  183. {
  184. m_rectangle.width = 0;
  185. m_rectangle.height = 0;
  186. }
  187. TextBuffer::~TextBuffer()
  188. {
  189. delete [] m_vertexBuffer;
  190. delete [] m_indexBuffer;
  191. delete [] m_styleBuffer;
  192. }
  193. void TextBuffer::appendText(FontHandle _fontHandle, const char* _string, const char* _end)
  194. {
  195. if (m_vertexCount == 0)
  196. {
  197. m_originX = m_penX;
  198. m_originY = m_penY;
  199. m_lineDescender = 0;
  200. m_lineAscender = 0;
  201. m_lineGap = 0;
  202. }
  203. CodePoint codepoint = 0;
  204. uint32_t state = 0;
  205. if (_end == NULL)
  206. {
  207. _end = _string + bx::strLen(_string);
  208. }
  209. BX_CHECK(_end >= _string);
  210. for (; *_string && _string < _end ; ++_string)
  211. {
  212. if (utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT )
  213. {
  214. appendGlyph(_fontHandle, codepoint);
  215. }
  216. }
  217. BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
  218. }
  219. void TextBuffer::appendText(FontHandle _fontHandle, const wchar_t* _string, const wchar_t* _end)
  220. {
  221. if (m_vertexCount == 0)
  222. {
  223. m_originX = m_penX;
  224. m_originY = m_penY;
  225. m_lineDescender = 0;
  226. m_lineAscender = 0;
  227. m_lineGap = 0;
  228. }
  229. if (_end == NULL)
  230. {
  231. _end = _string + wcslen(_string);
  232. }
  233. BX_CHECK(_end >= _string);
  234. for (const wchar_t* _current = _string; _current < _end; ++_current)
  235. {
  236. uint32_t _codePoint = *_current;
  237. appendGlyph(_fontHandle, _codePoint);
  238. }
  239. }
  240. void TextBuffer::appendAtlasFace(uint16_t _faceIndex)
  241. {
  242. if( m_vertexCount/4 >= MAX_BUFFERED_CHARACTERS)
  243. {
  244. return;
  245. }
  246. float x0 = m_penX;
  247. float y0 = m_penY;
  248. float x1 = x0 + (float)m_fontManager->getAtlas()->getTextureSize();
  249. float y1 = y0 + (float)m_fontManager->getAtlas()->getTextureSize();
  250. m_fontManager->getAtlas()->packFaceLayerUV(_faceIndex
  251. , (uint8_t*)m_vertexBuffer
  252. , sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u)
  253. , sizeof(TextVertex)
  254. );
  255. setVertex(m_vertexCount + 0, x0, y0, m_backgroundColor);
  256. setVertex(m_vertexCount + 1, x0, y1, m_backgroundColor);
  257. setVertex(m_vertexCount + 2, x1, y1, m_backgroundColor);
  258. setVertex(m_vertexCount + 3, x1, y0, m_backgroundColor);
  259. m_indexBuffer[m_indexCount + 0] = m_vertexCount + 0;
  260. m_indexBuffer[m_indexCount + 1] = m_vertexCount + 1;
  261. m_indexBuffer[m_indexCount + 2] = m_vertexCount + 2;
  262. m_indexBuffer[m_indexCount + 3] = m_vertexCount + 0;
  263. m_indexBuffer[m_indexCount + 4] = m_vertexCount + 2;
  264. m_indexBuffer[m_indexCount + 5] = m_vertexCount + 3;
  265. m_vertexCount += 4;
  266. m_indexCount += 6;
  267. }
  268. void TextBuffer::clearTextBuffer()
  269. {
  270. m_penX = 0;
  271. m_penY = 0;
  272. m_originX = 0;
  273. m_originY = 0;
  274. m_vertexCount = 0;
  275. m_indexCount = 0;
  276. m_lineStartIndex = 0;
  277. m_lineAscender = 0;
  278. m_lineDescender = 0;
  279. m_lineGap = 0;
  280. m_rectangle.width = 0;
  281. m_rectangle.height = 0;
  282. }
  283. void TextBuffer::appendGlyph(FontHandle _handle, CodePoint _codePoint)
  284. {
  285. if (_codePoint == L'\t')
  286. {
  287. for (uint32_t ii = 0; ii < 4; ++ii)
  288. {
  289. appendGlyph(_handle, L' ');
  290. }
  291. return;
  292. }
  293. const GlyphInfo* glyph = m_fontManager->getGlyphInfo(_handle, _codePoint);
  294. BX_WARN(NULL != glyph, "Glyph not found (font handle %d, code point %d)", _handle.idx, _codePoint);
  295. if (NULL == glyph)
  296. {
  297. return;
  298. }
  299. if( m_vertexCount/4 >= MAX_BUFFERED_CHARACTERS)
  300. {
  301. return;
  302. }
  303. const FontInfo& font = m_fontManager->getFontInfo(_handle);
  304. if (_codePoint == L'\n')
  305. {
  306. m_penX = m_originX;
  307. m_penY += m_lineGap + m_lineAscender -m_lineDescender;
  308. m_lineGap = font.lineGap;
  309. m_lineDescender = font.descender;
  310. m_lineAscender = font.ascender;
  311. m_lineStartIndex = m_vertexCount;
  312. return;
  313. }
  314. //is there a change of font size that require the text on the left to be centered again ?
  315. if (font.ascender > m_lineAscender
  316. || (font.descender < m_lineDescender) )
  317. {
  318. if (font.descender < m_lineDescender)
  319. {
  320. m_lineDescender = font.descender;
  321. m_lineGap = font.lineGap;
  322. }
  323. float txtDecals = (font.ascender - m_lineAscender);
  324. m_lineAscender = font.ascender;
  325. m_lineGap = font.lineGap;
  326. verticalCenterLastLine( (txtDecals), (m_penY - m_lineAscender), (m_penY + m_lineAscender - m_lineDescender + m_lineGap) );
  327. }
  328. float kerning = 0 * font.scale;
  329. m_penX += kerning;
  330. const GlyphInfo& blackGlyph = m_fontManager->getBlackGlyph();
  331. const Atlas* atlas = m_fontManager->getAtlas();
  332. if (m_styleFlags & STYLE_BACKGROUND
  333. && m_backgroundColor & 0xff000000)
  334. {
  335. float x0 = (m_penX - kerning);
  336. float y0 = (m_penY);
  337. float x1 = ( (float)x0 + (glyph->advance_x) );
  338. float y1 = (m_penY + m_lineAscender - m_lineDescender + m_lineGap);
  339. atlas->packUV(blackGlyph.regionIndex
  340. , (uint8_t*)m_vertexBuffer
  341. , sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u)
  342. , sizeof(TextVertex)
  343. );
  344. const uint16_t vertexCount = m_vertexCount;
  345. setVertex(vertexCount + 0, x0, y0, m_backgroundColor, STYLE_BACKGROUND);
  346. setVertex(vertexCount + 1, x0, y1, m_backgroundColor, STYLE_BACKGROUND);
  347. setVertex(vertexCount + 2, x1, y1, m_backgroundColor, STYLE_BACKGROUND);
  348. setVertex(vertexCount + 3, x1, y0, m_backgroundColor, STYLE_BACKGROUND);
  349. m_indexBuffer[m_indexCount + 0] = vertexCount + 0;
  350. m_indexBuffer[m_indexCount + 1] = vertexCount + 1;
  351. m_indexBuffer[m_indexCount + 2] = vertexCount + 2;
  352. m_indexBuffer[m_indexCount + 3] = vertexCount + 0;
  353. m_indexBuffer[m_indexCount + 4] = vertexCount + 2;
  354. m_indexBuffer[m_indexCount + 5] = vertexCount + 3;
  355. m_vertexCount += 4;
  356. m_indexCount += 6;
  357. }
  358. if (m_styleFlags & STYLE_UNDERLINE
  359. && m_underlineColor & 0xFF000000)
  360. {
  361. float x0 = (m_penX - kerning);
  362. float y0 = (m_penY + m_lineAscender - m_lineDescender * 0.5f);
  363. float x1 = ( (float)x0 + (glyph->advance_x) );
  364. float y1 = y0 + font.underlineThickness;
  365. atlas->packUV(blackGlyph.regionIndex
  366. , (uint8_t*)m_vertexBuffer
  367. , sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u)
  368. , sizeof(TextVertex)
  369. );
  370. setVertex(m_vertexCount + 0, x0, y0, m_underlineColor, STYLE_UNDERLINE);
  371. setVertex(m_vertexCount + 1, x0, y1, m_underlineColor, STYLE_UNDERLINE);
  372. setVertex(m_vertexCount + 2, x1, y1, m_underlineColor, STYLE_UNDERLINE);
  373. setVertex(m_vertexCount + 3, x1, y0, m_underlineColor, STYLE_UNDERLINE);
  374. m_indexBuffer[m_indexCount + 0] = m_vertexCount + 0;
  375. m_indexBuffer[m_indexCount + 1] = m_vertexCount + 1;
  376. m_indexBuffer[m_indexCount + 2] = m_vertexCount + 2;
  377. m_indexBuffer[m_indexCount + 3] = m_vertexCount + 0;
  378. m_indexBuffer[m_indexCount + 4] = m_vertexCount + 2;
  379. m_indexBuffer[m_indexCount + 5] = m_vertexCount + 3;
  380. m_vertexCount += 4;
  381. m_indexCount += 6;
  382. }
  383. if (m_styleFlags & STYLE_OVERLINE
  384. && m_overlineColor & 0xFF000000)
  385. {
  386. float x0 = (m_penX - kerning);
  387. float y0 = (m_penY);
  388. float x1 = ( (float)x0 + (glyph->advance_x) );
  389. float y1 = y0 + font.underlineThickness;
  390. m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex
  391. , (uint8_t*)m_vertexBuffer
  392. , sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u)
  393. , sizeof(TextVertex)
  394. );
  395. setVertex(m_vertexCount + 0, x0, y0, m_overlineColor, STYLE_OVERLINE);
  396. setVertex(m_vertexCount + 1, x0, y1, m_overlineColor, STYLE_OVERLINE);
  397. setVertex(m_vertexCount + 2, x1, y1, m_overlineColor, STYLE_OVERLINE);
  398. setVertex(m_vertexCount + 3, x1, y0, m_overlineColor, STYLE_OVERLINE);
  399. m_indexBuffer[m_indexCount + 0] = m_vertexCount + 0;
  400. m_indexBuffer[m_indexCount + 1] = m_vertexCount + 1;
  401. m_indexBuffer[m_indexCount + 2] = m_vertexCount + 2;
  402. m_indexBuffer[m_indexCount + 3] = m_vertexCount + 0;
  403. m_indexBuffer[m_indexCount + 4] = m_vertexCount + 2;
  404. m_indexBuffer[m_indexCount + 5] = m_vertexCount + 3;
  405. m_vertexCount += 4;
  406. m_indexCount += 6;
  407. }
  408. if (m_styleFlags & STYLE_STRIKE_THROUGH
  409. && m_strikeThroughColor & 0xFF000000)
  410. {
  411. float x0 = (m_penX - kerning);
  412. float y0 = (m_penY + 0.666667f * font.ascender);
  413. float x1 = ( (float)x0 + (glyph->advance_x) );
  414. float y1 = y0 + font.underlineThickness;
  415. atlas->packUV(blackGlyph.regionIndex
  416. , (uint8_t*)m_vertexBuffer
  417. , sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u)
  418. , sizeof(TextVertex)
  419. );
  420. setVertex(m_vertexCount + 0, x0, y0, m_strikeThroughColor, STYLE_STRIKE_THROUGH);
  421. setVertex(m_vertexCount + 1, x0, y1, m_strikeThroughColor, STYLE_STRIKE_THROUGH);
  422. setVertex(m_vertexCount + 2, x1, y1, m_strikeThroughColor, STYLE_STRIKE_THROUGH);
  423. setVertex(m_vertexCount + 3, x1, y0, m_strikeThroughColor, STYLE_STRIKE_THROUGH);
  424. m_indexBuffer[m_indexCount + 0] = m_vertexCount + 0;
  425. m_indexBuffer[m_indexCount + 1] = m_vertexCount + 1;
  426. m_indexBuffer[m_indexCount + 2] = m_vertexCount + 2;
  427. m_indexBuffer[m_indexCount + 3] = m_vertexCount + 0;
  428. m_indexBuffer[m_indexCount + 4] = m_vertexCount + 2;
  429. m_indexBuffer[m_indexCount + 5] = m_vertexCount + 3;
  430. m_vertexCount += 4;
  431. m_indexCount += 6;
  432. }
  433. float x0 = m_penX + (glyph->offset_x);
  434. float y0 = (m_penY + m_lineAscender + (glyph->offset_y) );
  435. float x1 = (x0 + glyph->width);
  436. float y1 = (y0 + glyph->height);
  437. atlas->packUV(glyph->regionIndex
  438. , (uint8_t*)m_vertexBuffer
  439. , sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u)
  440. , sizeof(TextVertex)
  441. );
  442. setVertex(m_vertexCount + 0, x0, y0, m_textColor);
  443. setVertex(m_vertexCount + 1, x0, y1, m_textColor);
  444. setVertex(m_vertexCount + 2, x1, y1, m_textColor);
  445. setVertex(m_vertexCount + 3, x1, y0, m_textColor);
  446. m_indexBuffer[m_indexCount + 0] = m_vertexCount + 0;
  447. m_indexBuffer[m_indexCount + 1] = m_vertexCount + 1;
  448. m_indexBuffer[m_indexCount + 2] = m_vertexCount + 2;
  449. m_indexBuffer[m_indexCount + 3] = m_vertexCount + 0;
  450. m_indexBuffer[m_indexCount + 4] = m_vertexCount + 2;
  451. m_indexBuffer[m_indexCount + 5] = m_vertexCount + 3;
  452. m_vertexCount += 4;
  453. m_indexCount += 6;
  454. m_penX += glyph->advance_x;
  455. if (m_penX > m_rectangle.width)
  456. {
  457. m_rectangle.width = m_penX;
  458. }
  459. if ( (m_penY +m_lineAscender - m_lineDescender+m_lineGap) > m_rectangle.height)
  460. {
  461. m_rectangle.height = (m_penY +m_lineAscender - m_lineDescender+m_lineGap);
  462. }
  463. }
  464. void TextBuffer::verticalCenterLastLine(float _dy, float _top, float _bottom)
  465. {
  466. for (uint32_t ii = m_lineStartIndex; ii < m_vertexCount; ii += 4)
  467. {
  468. if (m_styleBuffer[ii] == STYLE_BACKGROUND)
  469. {
  470. m_vertexBuffer[ii + 0].y = _top;
  471. m_vertexBuffer[ii + 1].y = _bottom;
  472. m_vertexBuffer[ii + 2].y = _bottom;
  473. m_vertexBuffer[ii + 3].y = _top;
  474. }
  475. else
  476. {
  477. m_vertexBuffer[ii + 0].y += _dy;
  478. m_vertexBuffer[ii + 1].y += _dy;
  479. m_vertexBuffer[ii + 2].y += _dy;
  480. m_vertexBuffer[ii + 3].y += _dy;
  481. }
  482. }
  483. }
  484. TextBufferManager::TextBufferManager(FontManager* _fontManager)
  485. : m_fontManager(_fontManager)
  486. {
  487. m_textBuffers = new BufferCache[MAX_TEXT_BUFFER_COUNT];
  488. bgfx::RendererType::Enum type = bgfx::getRendererType();
  489. m_basicProgram = bgfx::createProgram(
  490. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_font_basic")
  491. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_font_basic")
  492. , true
  493. );
  494. m_distanceProgram = bgfx::createProgram(
  495. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_font_distance_field")
  496. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_font_distance_field")
  497. , true
  498. );
  499. m_distanceSubpixelProgram = bgfx::createProgram(
  500. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_font_distance_field_subpixel")
  501. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_font_distance_field_subpixel")
  502. , true
  503. );
  504. m_vertexLayout
  505. .begin()
  506. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  507. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Int16, true)
  508. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  509. .end();
  510. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  511. }
  512. TextBufferManager::~TextBufferManager()
  513. {
  514. BX_CHECK(m_textBufferHandles.getNumHandles() == 0, "All the text buffers must be destroyed before destroying the manager");
  515. delete [] m_textBuffers;
  516. bgfx::destroy(s_texColor);
  517. bgfx::destroy(m_basicProgram);
  518. bgfx::destroy(m_distanceProgram);
  519. bgfx::destroy(m_distanceSubpixelProgram);
  520. }
  521. TextBufferHandle TextBufferManager::createTextBuffer(uint32_t _type, BufferType::Enum _bufferType)
  522. {
  523. uint16_t textIdx = m_textBufferHandles.alloc();
  524. BufferCache& bc = m_textBuffers[textIdx];
  525. bc.textBuffer = new TextBuffer(m_fontManager);
  526. bc.fontType = _type;
  527. bc.bufferType = _bufferType;
  528. bc.indexBufferHandleIdx = bgfx::kInvalidHandle;
  529. bc.vertexBufferHandleIdx = bgfx::kInvalidHandle;
  530. TextBufferHandle ret = {textIdx};
  531. return ret;
  532. }
  533. void TextBufferManager::destroyTextBuffer(TextBufferHandle _handle)
  534. {
  535. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  536. BufferCache& bc = m_textBuffers[_handle.idx];
  537. m_textBufferHandles.free(_handle.idx);
  538. delete bc.textBuffer;
  539. bc.textBuffer = NULL;
  540. if (bc.vertexBufferHandleIdx == bgfx::kInvalidHandle)
  541. {
  542. return;
  543. }
  544. switch (bc.bufferType)
  545. {
  546. case BufferType::Static:
  547. {
  548. bgfx::IndexBufferHandle ibh;
  549. bgfx::VertexBufferHandle vbh;
  550. ibh.idx = bc.indexBufferHandleIdx;
  551. vbh.idx = bc.vertexBufferHandleIdx;
  552. bgfx::destroy(ibh);
  553. bgfx::destroy(vbh);
  554. }
  555. break;
  556. case BufferType::Dynamic:
  557. bgfx::DynamicIndexBufferHandle ibh;
  558. bgfx::DynamicVertexBufferHandle vbh;
  559. ibh.idx = bc.indexBufferHandleIdx;
  560. vbh.idx = bc.vertexBufferHandleIdx;
  561. bgfx::destroy(ibh);
  562. bgfx::destroy(vbh);
  563. break;
  564. case BufferType::Transient: // destroyed every frame
  565. break;
  566. }
  567. }
  568. void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, bgfx::ViewId _id, int32_t _depth)
  569. {
  570. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  571. BufferCache& bc = m_textBuffers[_handle.idx];
  572. uint32_t indexSize = bc.textBuffer->getIndexCount() * bc.textBuffer->getIndexSize();
  573. uint32_t vertexSize = bc.textBuffer->getVertexCount() * bc.textBuffer->getVertexSize();
  574. if (0 == indexSize || 0 == vertexSize)
  575. {
  576. return;
  577. }
  578. bgfx::setTexture(0, s_texColor, m_fontManager->getAtlas()->getTextureHandle() );
  579. bgfx::ProgramHandle program = BGFX_INVALID_HANDLE;
  580. switch (bc.fontType)
  581. {
  582. case FONT_TYPE_ALPHA:
  583. program = m_basicProgram;
  584. bgfx::setState(0
  585. | BGFX_STATE_WRITE_RGB
  586. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  587. );
  588. break;
  589. case FONT_TYPE_DISTANCE:
  590. program = m_distanceProgram;
  591. bgfx::setState(0
  592. | BGFX_STATE_WRITE_RGB
  593. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  594. );
  595. break;
  596. case FONT_TYPE_DISTANCE_SUBPIXEL:
  597. program = m_distanceSubpixelProgram;
  598. bgfx::setState(0
  599. | BGFX_STATE_WRITE_RGB
  600. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_FACTOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
  601. , bc.textBuffer->getTextColor()
  602. );
  603. break;
  604. }
  605. switch (bc.bufferType)
  606. {
  607. case BufferType::Static:
  608. {
  609. bgfx::IndexBufferHandle ibh;
  610. bgfx::VertexBufferHandle vbh;
  611. if (bgfx::kInvalidHandle == bc.vertexBufferHandleIdx)
  612. {
  613. ibh = bgfx::createIndexBuffer(
  614. bgfx::copy(bc.textBuffer->getIndexBuffer(), indexSize)
  615. );
  616. vbh = bgfx::createVertexBuffer(
  617. bgfx::copy(bc.textBuffer->getVertexBuffer(), vertexSize)
  618. , m_vertexLayout
  619. );
  620. bc.vertexBufferHandleIdx = vbh.idx;
  621. bc.indexBufferHandleIdx = ibh.idx;
  622. }
  623. else
  624. {
  625. vbh.idx = bc.vertexBufferHandleIdx;
  626. ibh.idx = bc.indexBufferHandleIdx;
  627. }
  628. bgfx::setVertexBuffer(0, vbh, 0, bc.textBuffer->getVertexCount() );
  629. bgfx::setIndexBuffer(ibh, 0, bc.textBuffer->getIndexCount() );
  630. }
  631. break;
  632. case BufferType::Dynamic:
  633. {
  634. bgfx::DynamicIndexBufferHandle ibh;
  635. bgfx::DynamicVertexBufferHandle vbh;
  636. if (bgfx::kInvalidHandle == bc.vertexBufferHandleIdx )
  637. {
  638. ibh = bgfx::createDynamicIndexBuffer(
  639. bgfx::copy(bc.textBuffer->getIndexBuffer(), indexSize)
  640. );
  641. vbh = bgfx::createDynamicVertexBuffer(
  642. bgfx::copy(bc.textBuffer->getVertexBuffer(), vertexSize)
  643. , m_vertexLayout
  644. );
  645. bc.indexBufferHandleIdx = ibh.idx;
  646. bc.vertexBufferHandleIdx = vbh.idx;
  647. }
  648. else
  649. {
  650. ibh.idx = bc.indexBufferHandleIdx;
  651. vbh.idx = bc.vertexBufferHandleIdx;
  652. bgfx::update(
  653. ibh
  654. , 0
  655. , bgfx::copy(bc.textBuffer->getIndexBuffer(), indexSize)
  656. );
  657. bgfx::update(
  658. vbh
  659. , 0
  660. , bgfx::copy(bc.textBuffer->getVertexBuffer(), vertexSize)
  661. );
  662. }
  663. bgfx::setVertexBuffer(0, vbh, 0, bc.textBuffer->getVertexCount() );
  664. bgfx::setIndexBuffer(ibh, 0, bc.textBuffer->getIndexCount() );
  665. }
  666. break;
  667. case BufferType::Transient:
  668. {
  669. bgfx::TransientIndexBuffer tib;
  670. bgfx::TransientVertexBuffer tvb;
  671. bgfx::allocTransientIndexBuffer(&tib, bc.textBuffer->getIndexCount() );
  672. bgfx::allocTransientVertexBuffer(&tvb, bc.textBuffer->getVertexCount(), m_vertexLayout);
  673. bx::memCopy(tib.data, bc.textBuffer->getIndexBuffer(), indexSize);
  674. bx::memCopy(tvb.data, bc.textBuffer->getVertexBuffer(), vertexSize);
  675. bgfx::setVertexBuffer(0, &tvb, 0, bc.textBuffer->getVertexCount() );
  676. bgfx::setIndexBuffer(&tib, 0, bc.textBuffer->getIndexCount() );
  677. }
  678. break;
  679. }
  680. bgfx::submit(_id, program, _depth);
  681. }
  682. void TextBufferManager::setStyle(TextBufferHandle _handle, uint32_t _flags)
  683. {
  684. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  685. BufferCache& bc = m_textBuffers[_handle.idx];
  686. bc.textBuffer->setStyle(_flags);
  687. }
  688. void TextBufferManager::setTextColor(TextBufferHandle _handle, uint32_t _rgba)
  689. {
  690. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  691. BufferCache& bc = m_textBuffers[_handle.idx];
  692. bc.textBuffer->setTextColor(_rgba);
  693. }
  694. void TextBufferManager::setBackgroundColor(TextBufferHandle _handle, uint32_t _rgba)
  695. {
  696. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  697. BufferCache& bc = m_textBuffers[_handle.idx];
  698. bc.textBuffer->setBackgroundColor(_rgba);
  699. }
  700. void TextBufferManager::setOverlineColor(TextBufferHandle _handle, uint32_t _rgba)
  701. {
  702. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  703. BufferCache& bc = m_textBuffers[_handle.idx];
  704. bc.textBuffer->setOverlineColor(_rgba);
  705. }
  706. void TextBufferManager::setUnderlineColor(TextBufferHandle _handle, uint32_t _rgba)
  707. {
  708. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  709. BufferCache& bc = m_textBuffers[_handle.idx];
  710. bc.textBuffer->setUnderlineColor(_rgba);
  711. }
  712. void TextBufferManager::setStrikeThroughColor(TextBufferHandle _handle, uint32_t _rgba)
  713. {
  714. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  715. BufferCache& bc = m_textBuffers[_handle.idx];
  716. bc.textBuffer->setStrikeThroughColor(_rgba);
  717. }
  718. void TextBufferManager::setPenPosition(TextBufferHandle _handle, float _x, float _y)
  719. {
  720. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  721. BufferCache& bc = m_textBuffers[_handle.idx];
  722. bc.textBuffer->setPenPosition(_x, _y);
  723. }
  724. void TextBufferManager::appendText(TextBufferHandle _handle, FontHandle _fontHandle, const char* _string, const char* _end)
  725. {
  726. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  727. BufferCache& bc = m_textBuffers[_handle.idx];
  728. bc.textBuffer->appendText(_fontHandle, _string, _end);
  729. }
  730. void TextBufferManager::appendText(TextBufferHandle _handle, FontHandle _fontHandle, const wchar_t* _string, const wchar_t* _end)
  731. {
  732. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  733. BufferCache& bc = m_textBuffers[_handle.idx];
  734. bc.textBuffer->appendText(_fontHandle, _string, _end);
  735. }
  736. void TextBufferManager::appendAtlasFace(TextBufferHandle _handle, uint16_t _faceIndex)
  737. {
  738. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  739. BufferCache& bc = m_textBuffers[_handle.idx];
  740. bc.textBuffer->appendAtlasFace(_faceIndex);
  741. }
  742. void TextBufferManager::clearTextBuffer(TextBufferHandle _handle)
  743. {
  744. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  745. BufferCache& bc = m_textBuffers[_handle.idx];
  746. bc.textBuffer->clearTextBuffer();
  747. }
  748. TextRectangle TextBufferManager::getRectangle(TextBufferHandle _handle) const
  749. {
  750. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  751. BufferCache& bc = m_textBuffers[_handle.idx];
  752. return bc.textBuffer->getRectangle();
  753. }