BsTextData.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 TextDataBase::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 TextDataBase::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 TextDataBase::TextWord::calcWidthWithChar(const CHAR_DESC& desc)
  31. {
  32. return mWidth + calcCharWidth(mLastChar, desc);
  33. }
  34. UINT32 TextDataBase::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 TextDataBase::TextWord::addSpace(UINT32 spaceWidth)
  53. {
  54. mSpaceWidth += spaceWidth;
  55. mWidth = mSpaceWidth;
  56. mHeight = 0;
  57. }
  58. void TextDataBase::TextLine::init(TextDataBase* textData)
  59. {
  60. mWidth = 0;
  61. mHeight = 0;
  62. mIsEmpty = true;
  63. mTextData = textData;
  64. mWordsStart = mWordsEnd = 0;
  65. }
  66. void TextDataBase::TextLine::finalize(bool hasNewlineChar)
  67. {
  68. mHasNewline = hasNewlineChar;
  69. }
  70. void TextDataBase::TextLine::add(UINT32 charIdx, const CHAR_DESC& charDesc)
  71. {
  72. UINT32 charWidth = 0;
  73. if(mIsEmpty)
  74. {
  75. mWordsStart = mWordsEnd = MemBuffer->allocWord(false);
  76. mIsEmpty = false;
  77. }
  78. else
  79. {
  80. if(MemBuffer->WordBuffer[mWordsEnd].isSpacer())
  81. mWordsEnd = MemBuffer->allocWord(false);
  82. }
  83. TextWord& lastWord = MemBuffer->WordBuffer[mWordsEnd];
  84. charWidth = lastWord.addChar(charIdx, charDesc);
  85. mWidth += charWidth;
  86. mHeight = std::max(mHeight, lastWord.getHeight());
  87. }
  88. void TextDataBase::TextLine::addSpace()
  89. {
  90. if(mIsEmpty)
  91. {
  92. mWordsStart = mWordsEnd = MemBuffer->allocWord(true);
  93. mIsEmpty = false;
  94. }
  95. else
  96. mWordsEnd = MemBuffer->allocWord(true); // Each space is counted as its own word, to make certain operations easier
  97. TextWord& lastWord = MemBuffer->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 TextDataBase::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 TextDataBase::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 TextDataBase::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 = MemBuffer->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 TextDataBase::TextLine::isAtWordBoundary() const
  154. {
  155. return mIsEmpty || MemBuffer->WordBuffer[mWordsEnd].isSpacer();
  156. }
  157. UINT32 TextDataBase::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 TextDataBase::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 = MemBuffer->WordBuffer[i];
  268. if(word.isSpacer())
  269. numChars++;
  270. else
  271. numChars += (UINT32)word.getNumChars();
  272. }
  273. return numChars;
  274. }
  275. void TextDataBase::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 = MemBuffer->WordBuffer[i];
  284. mWidth += word.getWidth();
  285. mHeight = std::max(mHeight, word.getHeight());
  286. }
  287. }
  288. TextDataBase::TextDataBase(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)
  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 = MemBuffer->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 = &MemBuffer->LineBuffer[curLineIdx];
  317. if(text[charIdx] == '\n')
  318. {
  319. curLine->finalize(true);
  320. curLineIdx = MemBuffer->allocLine(this);
  321. curLine = &MemBuffer->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. UINT32 lastWordIdx = curLine->removeLastWord();
  334. TextWord& lastWord = MemBuffer->WordBuffer[lastWordIdx];
  335. bool wordFits = lastWord.calcWidthWithChar(charDesc) <= width;
  336. if (wordFits)
  337. {
  338. curLine->finalize(false);
  339. curLineIdx = MemBuffer->allocLine(this);
  340. curLine = &MemBuffer->LineBuffer[curLineIdx];
  341. curHeight += mFontData->fontDesc.lineHeight;
  342. curLine->addWord(lastWordIdx, lastWord);
  343. }
  344. else
  345. {
  346. if (wordBreak)
  347. {
  348. curLine->addWord(lastWordIdx, lastWord);
  349. curLine->finalize(false);
  350. curLineIdx = MemBuffer->allocLine(this);
  351. curLine = &MemBuffer->LineBuffer[curLineIdx];
  352. curHeight += mFontData->fontDesc.lineHeight;
  353. }
  354. else
  355. {
  356. if (!curLine->isEmpty()) // Add new line unless current line is empty (to avoid constantly moving the word to new lines)
  357. {
  358. curLine->finalize(false);
  359. curLineIdx = MemBuffer->allocLine(this);
  360. curLine = &MemBuffer->LineBuffer[curLineIdx];
  361. curHeight += mFontData->fontDesc.lineHeight;
  362. }
  363. curLine->addWord(lastWordIdx, lastWord);
  364. }
  365. }
  366. }
  367. else
  368. {
  369. curLine->finalize(false);
  370. curLineIdx = MemBuffer->allocLine(this);
  371. curLine = &MemBuffer->LineBuffer[curLineIdx];
  372. curHeight += mFontData->fontDesc.lineHeight;
  373. }
  374. }
  375. }
  376. if(charId != SPACE_CHAR)
  377. {
  378. curLine->add(charIdx, charDesc);
  379. MemBuffer->addCharToPage(charDesc.page, *mFontData);
  380. }
  381. else
  382. {
  383. curLine->addSpace();
  384. MemBuffer->addCharToPage(0, *mFontData);
  385. }
  386. charIdx++;
  387. }
  388. MemBuffer->LineBuffer[curLineIdx].finalize(true);
  389. // Now that we have all the data we need, allocate the permanent buffers and copy the data
  390. mNumChars = (UINT32)text.size();
  391. mNumWords = MemBuffer->NextFreeWord;
  392. mNumLines = MemBuffer->NextFreeLine;
  393. mNumPageInfos = MemBuffer->NextFreePageInfo;
  394. }
  395. void TextDataBase::generatePersistentData(const WString& text, UINT8* buffer, UINT32& size, bool freeTemporary)
  396. {
  397. UINT32 charArraySize = mNumChars * sizeof(const CHAR_DESC*);
  398. UINT32 wordArraySize = mNumWords * sizeof(TextWord);
  399. UINT32 lineArraySize = mNumLines * sizeof(TextLine);
  400. UINT32 pageInfoArraySize = mNumPageInfos * sizeof(PageInfo);
  401. if (buffer == nullptr)
  402. {
  403. size = charArraySize + wordArraySize + lineArraySize + pageInfoArraySize;;
  404. return;
  405. }
  406. UINT8* dataPtr = (UINT8*)buffer;
  407. mChars = (const CHAR_DESC**)dataPtr;
  408. for (UINT32 i = 0; i < mNumChars; i++)
  409. {
  410. UINT32 charId = text[i];
  411. const CHAR_DESC& charDesc = mFontData->getCharDesc(charId);
  412. mChars[i] = &charDesc;
  413. }
  414. dataPtr += charArraySize;
  415. mWords = (TextWord*)dataPtr;
  416. memcpy(mWords, &MemBuffer->WordBuffer[0], wordArraySize);
  417. dataPtr += wordArraySize;
  418. mLines = (TextLine*)dataPtr;
  419. memcpy(mLines, &MemBuffer->LineBuffer[0], lineArraySize);
  420. dataPtr += lineArraySize;
  421. mPageInfos = (PageInfo*)dataPtr;
  422. memcpy(mPageInfos, &MemBuffer->PageBuffer[0], pageInfoArraySize);
  423. if (freeTemporary)
  424. MemBuffer->deallocAll();
  425. }
  426. const HTexture& TextDataBase::getTextureForPage(UINT32 page) const
  427. {
  428. return mFontData->texturePages[page];
  429. }
  430. INT32 TextDataBase::getBaselineOffset() const
  431. {
  432. return mFontData->fontDesc.baselineOffset;
  433. }
  434. UINT32 TextDataBase::getLineHeight() const
  435. {
  436. return mFontData->fontDesc.lineHeight;
  437. }
  438. UINT32 TextDataBase::getSpaceWidth() const
  439. {
  440. return mFontData->fontDesc.spaceWidth;
  441. }
  442. void TextDataBase::initAlloc()
  443. {
  444. if (MemBuffer == nullptr)
  445. MemBuffer = bs_new<BufferData>();
  446. }
  447. TextDataBase::BufferData* TextDataBase::MemBuffer = nullptr;
  448. TextDataBase::BufferData::BufferData()
  449. {
  450. WordBufferSize = 2000;
  451. LineBufferSize = 500;
  452. PageBufferSize = 20;
  453. WordBuffer = bs_newN<TextWord>(WordBufferSize);
  454. LineBuffer = bs_newN<TextLine>(LineBufferSize);
  455. PageBuffer = bs_newN<PageInfo>(PageBufferSize);
  456. }
  457. TextDataBase::BufferData::~BufferData()
  458. {
  459. bs_deleteN(WordBuffer, WordBufferSize);
  460. bs_deleteN(LineBuffer, LineBufferSize);
  461. bs_deleteN(PageBuffer, PageBufferSize);
  462. }
  463. UINT32 TextDataBase::BufferData::allocWord(bool spacer)
  464. {
  465. if(NextFreeWord >= WordBufferSize)
  466. {
  467. UINT32 newBufferSize = WordBufferSize * 2;
  468. TextWord* newBuffer = bs_newN<TextWord>(newBufferSize);
  469. memcpy(WordBuffer, newBuffer, WordBufferSize);
  470. bs_deleteN(WordBuffer, WordBufferSize);
  471. WordBuffer = newBuffer;
  472. WordBufferSize = newBufferSize;
  473. }
  474. WordBuffer[NextFreeWord].init(spacer);
  475. return NextFreeWord++;
  476. }
  477. UINT32 TextDataBase::BufferData::allocLine(TextDataBase* textData)
  478. {
  479. if(NextFreeLine >= LineBufferSize)
  480. {
  481. UINT32 newBufferSize = LineBufferSize * 2;
  482. TextLine* newBuffer = bs_newN<TextLine>(newBufferSize);
  483. memcpy(LineBuffer, newBuffer, LineBufferSize);
  484. bs_deleteN(LineBuffer, LineBufferSize);
  485. LineBuffer = newBuffer;
  486. LineBufferSize = newBufferSize;
  487. }
  488. LineBuffer[NextFreeLine].init(textData);
  489. return NextFreeLine++;
  490. }
  491. void TextDataBase::BufferData::deallocAll()
  492. {
  493. NextFreeWord = 0;
  494. NextFreeLine = 0;
  495. NextFreePageInfo = 0;
  496. }
  497. void TextDataBase::BufferData::addCharToPage(UINT32 page, const FontData& fontData)
  498. {
  499. if(NextFreePageInfo >= PageBufferSize)
  500. {
  501. UINT32 newBufferSize = PageBufferSize * 2;
  502. PageInfo* newBuffer = bs_newN<PageInfo>(newBufferSize);
  503. memcpy(PageBuffer, newBuffer, PageBufferSize);
  504. bs_deleteN(PageBuffer, PageBufferSize);
  505. PageBuffer = newBuffer;
  506. PageBufferSize = newBufferSize;
  507. }
  508. while(page >= NextFreePageInfo)
  509. {
  510. PageBuffer[NextFreePageInfo].numQuads = 0;
  511. NextFreePageInfo++;
  512. }
  513. PageBuffer[page].numQuads++;
  514. }
  515. UINT32 TextDataBase::getWidth() const
  516. {
  517. UINT32 width = 0;
  518. for(UINT32 i = 0; i < mNumLines; i++)
  519. width = std::max(width, mLines[i].getWidth());
  520. return width;
  521. }
  522. UINT32 TextDataBase::getHeight() const
  523. {
  524. UINT32 height = 0;
  525. for(UINT32 i = 0; i < mNumLines; i++)
  526. height += mLines[i].getHeight();
  527. return height;
  528. }
  529. }