CmTextData.cpp 14 KB

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