BsTextData.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. #include "BsTextData.h"
  2. #include "BsFont.h"
  3. #include "BsVector2.h"
  4. #include "BsDebug.h"
  5. namespace BansheeEngine
  6. {
  7. const int SPACE_CHAR = 32;
  8. void TextData::TextWord::init(bool spacer)
  9. {
  10. mWidth = mHeight = 0;
  11. mSpacer = spacer;
  12. mSpaceWidth = 0;
  13. mCharsStart = 0;
  14. mCharsEnd = 0;
  15. mLastChar = nullptr;
  16. }
  17. // Assumes charIdx is an index right after last char in the list (if any). All chars need to be sequential.
  18. UINT32 TextData::TextWord::addChar(UINT32 charIdx, const CHAR_DESC& desc)
  19. {
  20. UINT32 charWidth = calcCharWidth(mLastChar, desc);
  21. mWidth += charWidth;
  22. mHeight = std::max(mHeight, desc.height);
  23. if(mLastChar == nullptr) // First char
  24. mCharsStart = mCharsEnd = charIdx;
  25. else
  26. mCharsEnd = charIdx;
  27. mLastChar = &desc;
  28. return charWidth;
  29. }
  30. UINT32 TextData::TextWord::calcWidthWithChar(const CHAR_DESC& desc)
  31. {
  32. return mWidth + calcCharWidth(mLastChar, desc);
  33. }
  34. UINT32 TextData::TextWord::calcCharWidth(const CHAR_DESC* prevDesc, const CHAR_DESC& desc)
  35. {
  36. UINT32 charWidth = desc.xAdvance;
  37. if (prevDesc != nullptr)
  38. {
  39. UINT32 kerning = 0;
  40. for (size_t j = 0; j < prevDesc->kerningPairs.size(); j++)
  41. {
  42. if (prevDesc->kerningPairs[j].otherCharId == desc.charId)
  43. {
  44. kerning = prevDesc->kerningPairs[j].amount;
  45. break;
  46. }
  47. }
  48. charWidth += kerning;
  49. }
  50. return charWidth;
  51. }
  52. void TextData::TextWord::addSpace(UINT32 spaceWidth)
  53. {
  54. mSpaceWidth += spaceWidth;
  55. mWidth = mSpaceWidth;
  56. mHeight = 0;
  57. }
  58. void TextData::TextLine::init(TextData* textData)
  59. {
  60. mWidth = 0;
  61. mHeight = 0;
  62. mIsEmpty = true;
  63. mTextData = textData;
  64. mWordsStart = mWordsEnd = 0;
  65. }
  66. void TextData::TextLine::finalize(bool hasNewlineChar)
  67. {
  68. mHasNewline = hasNewlineChar;
  69. }
  70. void TextData::TextLine::add(UINT32 charIdx, const CHAR_DESC& charDesc)
  71. {
  72. UINT32 charWidth = 0;
  73. if(mIsEmpty)
  74. {
  75. mWordsStart = mWordsEnd = allocWord(false);
  76. mIsEmpty = false;
  77. }
  78. else
  79. {
  80. if(TextData::WordBuffer[mWordsEnd].isSpacer())
  81. mWordsEnd = allocWord(false);
  82. }
  83. TextWord& lastWord = TextData::WordBuffer[mWordsEnd];
  84. charWidth = lastWord.addChar(charIdx, charDesc);
  85. mWidth += charWidth;
  86. mHeight = std::max(mHeight, lastWord.getHeight());
  87. }
  88. void TextData::TextLine::addSpace()
  89. {
  90. if(mIsEmpty)
  91. {
  92. mWordsStart = mWordsEnd = allocWord(true);
  93. mIsEmpty = false;
  94. }
  95. else
  96. mWordsEnd = allocWord(true); // Each space is counted as its own word, to make certain operations easier
  97. TextWord& lastWord = TextData::WordBuffer[mWordsEnd];
  98. lastWord.addSpace(mTextData->getSpaceWidth());
  99. mWidth += mTextData->getSpaceWidth();
  100. }
  101. // Assumes wordIdx is an index right after last word in the list (if any). All words need to be sequential.
  102. void TextData::TextLine::addWord(UINT32 wordIdx, const TextWord& word)
  103. {
  104. if(mIsEmpty)
  105. {
  106. mWordsStart = mWordsEnd = wordIdx;
  107. mIsEmpty = false;
  108. }
  109. else
  110. mWordsEnd = wordIdx;
  111. mWidth += word.getWidth();
  112. mHeight = std::max(mHeight, word.getHeight());
  113. }
  114. UINT32 TextData::TextLine::removeLastWord()
  115. {
  116. if(mIsEmpty)
  117. {
  118. assert(false);
  119. return 0;
  120. }
  121. UINT32 lastWord = mWordsEnd--;
  122. if(mWordsStart == lastWord)
  123. {
  124. mIsEmpty = true;
  125. mWordsStart = mWordsEnd = 0;
  126. }
  127. calculateBounds();
  128. return lastWord;
  129. }
  130. UINT32 TextData::TextLine::calcWidthWithChar(const CHAR_DESC& desc, bool space)
  131. {
  132. UINT32 charWidth = 0;
  133. if (space)
  134. charWidth = mTextData->getSpaceWidth();
  135. else
  136. {
  137. UINT32 word = mWordsEnd;
  138. if (!mIsEmpty)
  139. {
  140. TextWord& lastWord = TextData::WordBuffer[mWordsEnd];
  141. if (lastWord.isSpacer())
  142. charWidth = TextWord::calcCharWidth(nullptr, desc);
  143. else
  144. charWidth = lastWord.calcWidthWithChar(desc) - lastWord.getWidth();
  145. }
  146. else
  147. {
  148. charWidth = TextWord::calcCharWidth(nullptr, desc);
  149. }
  150. }
  151. return mWidth + charWidth;
  152. }
  153. bool TextData::TextLine::isAtWordBoundary() const
  154. {
  155. return mIsEmpty || TextData::WordBuffer[mWordsEnd].isSpacer();
  156. }
  157. UINT32 TextData::TextLine::fillBuffer(UINT32 page, Vector2* vertices, Vector2* uvs, UINT32* indexes, UINT32 offset, UINT32 size) const
  158. {
  159. UINT32 numQuads = 0;
  160. if(mIsEmpty)
  161. return numQuads;
  162. UINT32 penX = 0;
  163. for(UINT32 i = mWordsStart; i <= mWordsEnd; i++)
  164. {
  165. const TextWord& word = mTextData->getWord(i);
  166. if(word.isSpacer())
  167. {
  168. // We store invisible space quads in the first page. Even though they aren't needed
  169. // for rendering and we could just leave an empty space, they are needed for intersection tests
  170. // for things like determining caret placement and selection areas
  171. if(page == 0)
  172. {
  173. INT32 curX = penX;
  174. INT32 curY = 0;
  175. UINT32 curVert = offset * 4;
  176. UINT32 curIndex = offset * 6;
  177. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  178. vertices[curVert + 1] = Vector2((float)(curX + mTextData->getSpaceWidth()), (float)curY);
  179. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)mTextData->getLineHeight());
  180. vertices[curVert + 3] = Vector2((float)(curX + mTextData->getSpaceWidth()), (float)curY + (float)mTextData->getLineHeight());
  181. if(uvs != nullptr)
  182. {
  183. uvs[curVert + 0] = Vector2(0.0f, 0.0f);
  184. uvs[curVert + 1] = Vector2(0.0f, 0.0f);
  185. uvs[curVert + 2] = Vector2(0.0f, 0.0f);
  186. uvs[curVert + 3] = Vector2(0.0f, 0.0f);
  187. }
  188. // Triangles are back-facing which makes them invisible
  189. if(indexes != nullptr)
  190. {
  191. indexes[curIndex + 0] = curVert + 0;
  192. indexes[curIndex + 1] = curVert + 2;
  193. indexes[curIndex + 2] = curVert + 1;
  194. indexes[curIndex + 3] = curVert + 1;
  195. indexes[curIndex + 4] = curVert + 2;
  196. indexes[curIndex + 5] = curVert + 3;
  197. }
  198. offset++;
  199. numQuads++;
  200. if(offset > size)
  201. BS_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  202. }
  203. penX += mTextData->getSpaceWidth();
  204. }
  205. else
  206. {
  207. UINT32 kerning = 0;
  208. for(UINT32 j = word.getCharsStart(); j <= word.getCharsEnd(); j++)
  209. {
  210. const CHAR_DESC& curChar = mTextData->getChar(j);
  211. INT32 curX = penX + curChar.xOffset;
  212. INT32 curY = ((INT32) mTextData->getBaselineOffset() - curChar.yOffset);
  213. penX += curChar.xAdvance + kerning;
  214. kerning = 0;
  215. if((j + 1) <= word.getCharsEnd())
  216. {
  217. const CHAR_DESC& nextChar = mTextData->getChar(j + 1);
  218. for(size_t j = 0; j < curChar.kerningPairs.size(); j++)
  219. {
  220. if(curChar.kerningPairs[j].otherCharId == nextChar.charId)
  221. {
  222. kerning = curChar.kerningPairs[j].amount;
  223. break;
  224. }
  225. }
  226. }
  227. if(curChar.page != page)
  228. continue;
  229. UINT32 curVert = offset * 4;
  230. UINT32 curIndex = offset * 6;
  231. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  232. vertices[curVert + 1] = Vector2((float)(curX + curChar.width), (float)curY);
  233. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)curChar.height);
  234. vertices[curVert + 3] = Vector2((float)(curX + curChar.width), (float)curY + (float)curChar.height);
  235. if(uvs != nullptr)
  236. {
  237. uvs[curVert + 0] = Vector2(curChar.uvX, curChar.uvY);
  238. uvs[curVert + 1] = Vector2(curChar.uvX + curChar.uvWidth, curChar.uvY);
  239. uvs[curVert + 2] = Vector2(curChar.uvX, curChar.uvY + curChar.uvHeight);
  240. uvs[curVert + 3] = Vector2(curChar.uvX + curChar.uvWidth, curChar.uvY + curChar.uvHeight);
  241. }
  242. if(indexes != nullptr)
  243. {
  244. indexes[curIndex + 0] = curVert + 0;
  245. indexes[curIndex + 1] = curVert + 1;
  246. indexes[curIndex + 2] = curVert + 2;
  247. indexes[curIndex + 3] = curVert + 1;
  248. indexes[curIndex + 4] = curVert + 3;
  249. indexes[curIndex + 5] = curVert + 2;
  250. }
  251. offset++;
  252. numQuads++;
  253. if(offset > size)
  254. BS_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  255. }
  256. }
  257. }
  258. return numQuads;
  259. }
  260. UINT32 TextData::TextLine::getNumChars() const
  261. {
  262. if(mIsEmpty)
  263. return 0;
  264. UINT32 numChars = 0;
  265. for(UINT32 i = mWordsStart; i <= mWordsEnd; i++)
  266. {
  267. TextWord& word = TextData::WordBuffer[i];
  268. if(word.isSpacer())
  269. numChars++;
  270. else
  271. numChars += (UINT32)word.getNumChars();
  272. }
  273. return numChars;
  274. }
  275. void TextData::TextLine::calculateBounds()
  276. {
  277. mWidth = 0;
  278. mHeight = 0;
  279. if(mIsEmpty)
  280. return;
  281. for(UINT32 i = mWordsStart; i <= mWordsEnd; i++)
  282. {
  283. TextWord& word = TextData::WordBuffer[i];
  284. mWidth += word.getWidth();
  285. mHeight = std::max(mHeight, word.getHeight());
  286. }
  287. }
  288. TextData::TextData(const WString& text, const HFont& font, UINT32 fontSize, UINT32 width, UINT32 height, bool wordWrap, bool wordBreak)
  289. :mFont(font), mChars(nullptr), mFontData(nullptr),
  290. mNumChars(0), mWords(nullptr), mNumWords(0), mLines(nullptr), mNumLines(0), mPageInfos(nullptr), mNumPageInfos(0), mData(nullptr)
  291. {
  292. // In order to reduce number of memory allocations algorithm first calculates data into temporary buffers and then copies the results
  293. initAlloc();
  294. if(font != nullptr)
  295. {
  296. UINT32 nearestSize = font->getClosestAvailableSize(fontSize);
  297. mFontData = font->getFontDataForSize(nearestSize);
  298. }
  299. if(mFontData == nullptr || mFontData->texturePages.size() == 0)
  300. return;
  301. if(mFontData->size != fontSize)
  302. {
  303. LOGWRN("Unable to find font with specified size (" + toString(fontSize) + "). Using nearest available size: " + toString(mFontData->size));
  304. }
  305. bool widthIsLimited = width > 0;
  306. mFont = font;
  307. UINT32 curLineIdx = allocLine(this);
  308. UINT32 curHeight = mFontData->fontDesc.lineHeight;
  309. UINT32 charIdx = 0;
  310. while(true)
  311. {
  312. if(charIdx >= text.size())
  313. break;
  314. UINT32 charId = text[charIdx];
  315. const CHAR_DESC& charDesc = mFontData->getCharDesc(charId);
  316. TextLine* curLine = &LineBuffer[curLineIdx];
  317. if(text[charIdx] == '\n')
  318. {
  319. curLine->finalize(true);
  320. curLineIdx = allocLine(this);
  321. curLine = &LineBuffer[curLineIdx];
  322. curHeight += mFontData->fontDesc.lineHeight;
  323. charIdx++;
  324. continue;
  325. }
  326. if (widthIsLimited && wordWrap)
  327. {
  328. if (curLine->calcWidthWithChar(charDesc, charId == SPACE_CHAR) > width && !curLine->isEmpty())
  329. {
  330. bool atWordBoundary = charId == SPACE_CHAR || curLine->isAtWordBoundary();
  331. if (!atWordBoundary) // Need to break word into multiple pieces, or move it to next line
  332. {
  333. if (wordBreak)
  334. {
  335. curLine->finalize(false);
  336. curLineIdx = allocLine(this);
  337. curLine = &LineBuffer[curLineIdx];
  338. curHeight += mFontData->fontDesc.lineHeight;
  339. }
  340. else
  341. {
  342. UINT32 lastWordIdx = curLine->removeLastWord();
  343. TextWord& lastWord = WordBuffer[lastWordIdx];
  344. if (lastWord.calcWidthWithChar(charDesc) <= width) // If the word fits, attempt to add it to a new line
  345. {
  346. curLine->finalize(false);
  347. curLineIdx = allocLine(this);
  348. curLine = &LineBuffer[curLineIdx];
  349. curHeight += mFontData->fontDesc.lineHeight;
  350. }
  351. curLine->addWord(lastWordIdx, lastWord);
  352. }
  353. }
  354. }
  355. }
  356. if(charId != SPACE_CHAR)
  357. {
  358. curLine->add(charIdx, charDesc);
  359. addCharToPage(charDesc.page, *mFontData);
  360. }
  361. else
  362. {
  363. curLine->addSpace();
  364. addCharToPage(0, *mFontData);
  365. }
  366. charIdx++;
  367. }
  368. LineBuffer[curLineIdx].finalize(true);
  369. // Now that we have all the data we need, allocate the permanent buffers and copy the data
  370. mNumChars = (UINT32)text.size();
  371. mNumWords = NextFreeWord;
  372. mNumLines = NextFreeLine;
  373. mNumPageInfos = NextFreePageInfo;
  374. UINT32 charArraySize = mNumChars * sizeof(const CHAR_DESC*);
  375. UINT32 wordArraySize = mNumWords * sizeof(TextWord);
  376. UINT32 lineArraySize = mNumLines * sizeof(TextLine);
  377. UINT32 pageInfoArraySize = mNumPageInfos * sizeof(PageInfo);
  378. UINT32 totalBufferSize = charArraySize + wordArraySize + lineArraySize + pageInfoArraySize;
  379. mData = bs_alloc(totalBufferSize);
  380. UINT8* dataPtr = (UINT8*)mData;
  381. mChars = (const CHAR_DESC**)dataPtr;
  382. for(UINT32 i = 0; i < mNumChars; i++)
  383. {
  384. UINT32 charId = text[i];
  385. const CHAR_DESC& charDesc = mFontData->getCharDesc(charId);
  386. mChars[i] = &charDesc;
  387. }
  388. dataPtr += charArraySize;
  389. mWords = (TextWord*)dataPtr;
  390. memcpy(mWords, &WordBuffer[0], wordArraySize);
  391. dataPtr += wordArraySize;
  392. mLines = (TextLine*)dataPtr;
  393. memcpy(mLines, &LineBuffer[0], lineArraySize);
  394. dataPtr += lineArraySize;
  395. mPageInfos = (PageInfo*)dataPtr;
  396. memcpy(mPageInfos, &PageBuffer[0], pageInfoArraySize);
  397. TextData::deallocAll();
  398. }
  399. TextData::~TextData()
  400. {
  401. if(mData != nullptr)
  402. bs_free(mData);
  403. }
  404. const HTexture& TextData::getTextureForPage(UINT32 page) const
  405. {
  406. return mFontData->texturePages[page];
  407. }
  408. INT32 TextData::getBaselineOffset() const
  409. {
  410. return mFontData->fontDesc.baselineOffset;
  411. }
  412. UINT32 TextData::getLineHeight() const
  413. {
  414. return mFontData->fontDesc.lineHeight;
  415. }
  416. UINT32 TextData::getSpaceWidth() const
  417. {
  418. return mFontData->fontDesc.spaceWidth;
  419. }
  420. bool TextData::BuffersInitialized = false;
  421. TextData::TextWord* TextData::WordBuffer = nullptr;
  422. UINT32 TextData::NextFreeWord = 0;
  423. UINT32 TextData::WordBufferSize = 0;
  424. TextData::TextLine* TextData::LineBuffer = nullptr;
  425. UINT32 TextData::NextFreeLine = 0;
  426. UINT32 TextData::LineBufferSize = 0;
  427. TextData::PageInfo* TextData::PageBuffer = nullptr;
  428. UINT32 TextData::NextFreePageInfo = 0;
  429. UINT32 TextData::PageBufferSize = 0;
  430. void TextData::initAlloc()
  431. {
  432. if(!BuffersInitialized)
  433. {
  434. WordBufferSize = 2000;
  435. LineBufferSize = 500;
  436. PageBufferSize = 20;
  437. WordBuffer = bs_newN<TextWord>(WordBufferSize);
  438. LineBuffer = bs_newN<TextLine>(LineBufferSize);
  439. PageBuffer = bs_newN<PageInfo>(PageBufferSize);
  440. BuffersInitialized = true;
  441. }
  442. }
  443. UINT32 TextData::allocWord(bool spacer)
  444. {
  445. if(NextFreeWord >= WordBufferSize)
  446. {
  447. UINT32 newBufferSize = WordBufferSize * 2;
  448. TextWord* newBuffer = bs_newN<TextWord>(newBufferSize);
  449. memcpy(WordBuffer, newBuffer, WordBufferSize);
  450. bs_deleteN(WordBuffer, WordBufferSize);
  451. WordBuffer = newBuffer;
  452. WordBufferSize = newBufferSize;
  453. }
  454. WordBuffer[NextFreeWord].init(spacer);
  455. return NextFreeWord++;
  456. }
  457. UINT32 TextData::allocLine(TextData* textData)
  458. {
  459. if(NextFreeLine >= LineBufferSize)
  460. {
  461. UINT32 newBufferSize = LineBufferSize * 2;
  462. TextLine* newBuffer = bs_newN<TextLine>(newBufferSize);
  463. memcpy(LineBuffer, newBuffer, LineBufferSize);
  464. bs_deleteN(LineBuffer, LineBufferSize);
  465. LineBuffer = newBuffer;
  466. LineBufferSize = newBufferSize;
  467. }
  468. LineBuffer[NextFreeLine].init(textData);
  469. return NextFreeLine++;
  470. }
  471. void TextData::deallocAll()
  472. {
  473. NextFreeWord = 0;
  474. NextFreeLine = 0;
  475. NextFreePageInfo = 0;
  476. }
  477. void TextData::addCharToPage(UINT32 page, const FontData& fontData)
  478. {
  479. if(NextFreePageInfo >= PageBufferSize)
  480. {
  481. UINT32 newBufferSize = PageBufferSize * 2;
  482. PageInfo* newBuffer = bs_newN<PageInfo>(newBufferSize);
  483. memcpy(PageBuffer, newBuffer, PageBufferSize);
  484. bs_deleteN(PageBuffer, PageBufferSize);
  485. PageBuffer = newBuffer;
  486. PageBufferSize = newBufferSize;
  487. }
  488. while(page >= NextFreePageInfo)
  489. {
  490. PageBuffer[NextFreePageInfo].numQuads = 0;
  491. NextFreePageInfo++;
  492. }
  493. PageBuffer[page].numQuads++;
  494. }
  495. UINT32 TextData::getWidth() const
  496. {
  497. UINT32 width = 0;
  498. for(UINT32 i = 0; i < mNumLines; i++)
  499. width = std::max(width, mLines[i].getWidth());
  500. return width;
  501. }
  502. UINT32 TextData::getHeight() const
  503. {
  504. UINT32 height = 0;
  505. for(UINT32 i = 0; i < mNumLines; i++)
  506. height += mLines[i].getHeight();
  507. return height;
  508. }
  509. }