CmTextUtility.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "CmTextUtility.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 TextUtility::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 TextUtility::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 TextUtility::TextWord::addSpace(UINT32 spaceWidth)
  44. {
  45. mSpaceWidth += spaceWidth;
  46. mWidth = mSpaceWidth;
  47. mHeight = 0;
  48. }
  49. void TextUtility::TextLine::init(TextData* textData)
  50. {
  51. mWidth = 0;
  52. mHeight = 0;
  53. mIsEmpty = true;
  54. mTextData = textData;
  55. mWordsStart = mWordsEnd = 0;
  56. }
  57. void TextUtility::TextLine::finalize(bool hasNewlineChar)
  58. {
  59. mHasNewline = hasNewlineChar;
  60. }
  61. void TextUtility::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(TextUtility::WordBuffer[mWordsEnd].isSpacer())
  72. mWordsEnd = allocWord(false);
  73. }
  74. TextWord& lastWord = TextUtility::WordBuffer[mWordsEnd];
  75. charWidth = lastWord.addChar(charIdx, charDesc);
  76. mWidth += charWidth;
  77. mHeight = std::max(mHeight, lastWord.getHeight());
  78. }
  79. void TextUtility::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 = TextUtility::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 TextUtility::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 TextUtility::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 TextUtility::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 TextUtility::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 = TextUtility::WordBuffer[i];
  232. if(word.isSpacer())
  233. numChars++;
  234. else
  235. numChars += (UINT32)word.getNumChars();
  236. }
  237. return numChars;
  238. }
  239. void TextUtility::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 = TextUtility::WordBuffer[i];
  248. mWidth += word.getWidth();
  249. mHeight = std::max(mHeight, word.getHeight());
  250. }
  251. }
  252. TextUtility::TextData::TextData(const HFont& font, INT32 baselineOffset, UINT32 lineHeight, UINT32 spaceWidth)
  253. :mFont(font), mBaselineOffset(baselineOffset), mLineHeight(lineHeight), mSpaceWidth(spaceWidth), mChars(nullptr),
  254. mNumChars(0), mWords(nullptr), mNumWords(0), mLines(nullptr), mNumLines(0), mPageInfos(nullptr), mNumPageInfos(0), mData(nullptr)
  255. {
  256. }
  257. TextUtility::TextData::~TextData()
  258. {
  259. if(mData != nullptr)
  260. cm_free(mData);
  261. }
  262. Vector<TextUtility::TextWord>::type TextUtility::WordBuffer = Vector<TextUtility::TextWord>::type(2000);
  263. UINT32 TextUtility::NextFreeWord = 0;
  264. Vector<TextUtility::TextLine>::type TextUtility::LineBuffer = Vector<TextUtility::TextLine>::type(500);
  265. UINT32 TextUtility::NextFreeLine = 0;
  266. Vector<TextUtility::PageInfo>::type TextUtility::PageBuffer = Vector<TextUtility::PageInfo>::type(20);
  267. UINT32 TextUtility::NextFreePageInfo = 0;
  268. UINT32 TextUtility::allocWord(bool spacer)
  269. {
  270. if(NextFreeWord >= WordBuffer.size())
  271. WordBuffer.resize(WordBuffer.size() * 2);
  272. WordBuffer[NextFreeWord].init(spacer);
  273. return NextFreeWord++;
  274. }
  275. UINT32 TextUtility::allocLine(TextData* textData)
  276. {
  277. if(NextFreeLine >= LineBuffer.size())
  278. LineBuffer.resize(LineBuffer.size() * 2);
  279. LineBuffer[NextFreeLine].init(textData);
  280. return NextFreeLine++;
  281. }
  282. void TextUtility::deallocAll()
  283. {
  284. NextFreeWord = 0;
  285. NextFreeLine = 0;
  286. NextFreePageInfo = 0;
  287. }
  288. std::shared_ptr<TextUtility::TextData> TextUtility::getTextData(const WString& text, const HFont& font, UINT32 fontSize, UINT32 width, UINT32 height, bool wordWrap)
  289. {
  290. // In order to reduce number of memory allocations algorithm first calculates data into temporary buffers and then copies the results
  291. const FontData* fontData = nullptr;
  292. if(font != nullptr)
  293. {
  294. UINT32 nearestSize = font->getClosestAvailableSize(fontSize);
  295. fontData = font->getFontDataForSize(nearestSize);
  296. }
  297. if(fontData == nullptr || fontData->texturePages.size() == 0)
  298. return nullptr;
  299. if(fontData->size != fontSize)
  300. {
  301. LOGWRN("Unable to find font with specified size (" + toString(fontSize) + "). Using nearest available size: " + toString(fontData->size));
  302. }
  303. bool widthIsLimited = width > 0;
  304. std::shared_ptr<TextUtility::TextData> textData = cm_shared_ptr<TextData, PoolAlloc>(font, fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth);
  305. UINT32 curLineIdx = allocLine(textData.get());
  306. UINT32 curHeight = fontData->fontDesc.lineHeight;
  307. UINT32 charIdx = 0;
  308. while(true)
  309. {
  310. if(charIdx >= text.size())
  311. break;
  312. UINT32 charId = text[charIdx];
  313. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  314. TextLine* curLine = &LineBuffer[curLineIdx];
  315. if(text[charIdx] == '\n')
  316. {
  317. curLine->finalize(true);
  318. curLineIdx = allocLine(textData.get());
  319. curLine = &LineBuffer[curLineIdx];
  320. curHeight += fontData->fontDesc.lineHeight;
  321. charIdx++;
  322. continue;
  323. }
  324. if(charId != SPACE_CHAR)
  325. {
  326. curLine->add(charIdx, charDesc);
  327. addCharToPage(charDesc.page, *fontData);
  328. }
  329. else
  330. {
  331. curLine->addSpace();
  332. addCharToPage(0, *fontData);
  333. }
  334. if(widthIsLimited && curLine->getWidth() > width)
  335. {
  336. if(wordWrap)
  337. {
  338. assert(!curLine->isEmpty());
  339. UINT32 lastWordIdx = curLine->removeLastWord();
  340. TextWord& lastWord = WordBuffer[lastWordIdx];
  341. if(lastWord.getWidth() <= width) // If the word fits, attempt to add it to a new line
  342. {
  343. curLine->finalize(false);
  344. curLineIdx = allocLine(textData.get());
  345. curLine = &LineBuffer[curLineIdx];
  346. curHeight += fontData->fontDesc.lineHeight;
  347. }
  348. curLine->addWord(lastWordIdx, lastWord);
  349. }
  350. }
  351. charIdx++;
  352. }
  353. LineBuffer[curLineIdx].finalize(true);
  354. // Now that we have all the data we need, allocate the permanent buffers and copy the data
  355. textData->mNumChars = (UINT32)text.size();
  356. textData->mNumWords = NextFreeWord;
  357. textData->mNumLines = NextFreeLine;
  358. textData->mNumPageInfos = NextFreePageInfo;
  359. UINT32 charArraySize = textData->mNumChars * sizeof(const CHAR_DESC*);
  360. UINT32 wordArraySize = textData->mNumWords * sizeof(TextWord);
  361. UINT32 lineArraySize = textData->mNumLines * sizeof(TextLine);
  362. UINT32 pageInfoArraySize = textData->mNumPageInfos * sizeof(PageInfo);
  363. UINT32 totalBufferSize = charArraySize + wordArraySize + lineArraySize + pageInfoArraySize;
  364. textData->mData = cm_alloc(totalBufferSize);
  365. UINT8* dataPtr = (UINT8*)textData->mData;
  366. textData->mChars = (const CHAR_DESC**)dataPtr;
  367. for(UINT32 i = 0; i < textData->mNumChars; i++)
  368. {
  369. UINT32 charId = text[i];
  370. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  371. textData->mChars[i] = &charDesc;
  372. }
  373. dataPtr += charArraySize;
  374. textData->mWords = (TextWord*)dataPtr;
  375. memcpy(textData->mWords, &WordBuffer[0], wordArraySize);
  376. dataPtr += wordArraySize;
  377. textData->mLines = (TextLine*)dataPtr;
  378. memcpy(textData->mLines, &LineBuffer[0], lineArraySize);
  379. dataPtr += lineArraySize;
  380. textData->mPageInfos = (PageInfo*)dataPtr;
  381. memcpy(textData->mPageInfos, &PageBuffer[0], pageInfoArraySize);
  382. TextUtility::deallocAll();
  383. return textData;
  384. }
  385. void TextUtility::addCharToPage(UINT32 page, const FontData& fontData)
  386. {
  387. while(page >= NextFreePageInfo)
  388. {
  389. PageBuffer[NextFreePageInfo].numQuads = 0;
  390. PageBuffer[NextFreePageInfo].texture = HTexture();
  391. NextFreePageInfo++;
  392. }
  393. PageBuffer[page].numQuads++;
  394. if(PageBuffer[page].texture == nullptr)
  395. PageBuffer[page].texture = fontData.texturePages[page];
  396. }
  397. UINT32 TextUtility::TextData::getWidth() const
  398. {
  399. UINT32 width = 0;
  400. for(UINT32 i = 0; i < mNumLines; i++)
  401. width = std::max(width, mLines[i].getWidth());
  402. return width;
  403. }
  404. UINT32 TextUtility::TextData::getHeight() const
  405. {
  406. UINT32 height = 0;
  407. for(UINT32 i = 0; i < mNumLines; i++)
  408. height += mLines[i].getHeight();
  409. return height;
  410. }
  411. }