2
0

CmTextUtility.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. UINT32 penX = 0;
  125. for(UINT32 i = mWordsStart; i <= mWordsEnd; i++)
  126. {
  127. const TextWord& word = mTextData->getWord(i);
  128. if(word.isSpacer())
  129. {
  130. // We store invisible space quads in the first page. Even though they aren't needed
  131. // for rendering and we could just leave an empty space, they are needed for intersection tests
  132. // for things like determining caret placement and selection areas
  133. if(page == 0)
  134. {
  135. INT32 curX = penX;
  136. INT32 curY = 0;
  137. UINT32 curVert = offset * 4;
  138. UINT32 curIndex = offset * 6;
  139. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  140. vertices[curVert + 1] = Vector2((float)(curX + mTextData->getSpaceWidth()), (float)curY);
  141. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)mTextData->getLineHeight());
  142. vertices[curVert + 3] = Vector2((float)(curX + mTextData->getSpaceWidth()), (float)curY + (float)mTextData->getLineHeight());
  143. if(uvs != nullptr)
  144. {
  145. uvs[curVert + 0] = Vector2(0.0f, 0.0f);
  146. uvs[curVert + 1] = Vector2(0.0f, 0.0f);
  147. uvs[curVert + 2] = Vector2(0.0f, 0.0f);
  148. uvs[curVert + 3] = Vector2(0.0f, 0.0f);
  149. }
  150. // Triangles are back-facing which makes them invisible
  151. if(indexes != nullptr)
  152. {
  153. indexes[curIndex + 0] = curVert + 0;
  154. indexes[curIndex + 1] = curVert + 2;
  155. indexes[curIndex + 2] = curVert + 1;
  156. indexes[curIndex + 3] = curVert + 1;
  157. indexes[curIndex + 4] = curVert + 2;
  158. indexes[curIndex + 5] = curVert + 3;
  159. }
  160. offset++;
  161. numQuads++;
  162. if(offset > size)
  163. CM_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  164. }
  165. penX += mTextData->getSpaceWidth();
  166. }
  167. else
  168. {
  169. UINT32 kerning = 0;
  170. for(UINT32 j = word.getCharsStart(); j <= word.getCharsEnd(); j++)
  171. {
  172. const CHAR_DESC& curChar = mTextData->getChar(j);
  173. INT32 curX = penX + curChar.xOffset;
  174. INT32 curY = ((INT32) mTextData->getBaselineOffset() - curChar.yOffset);
  175. penX += curChar.xAdvance + kerning;
  176. kerning = 0;
  177. if((j + 1) <= word.getCharsEnd())
  178. {
  179. const CHAR_DESC& nextChar = mTextData->getChar(j + 1);
  180. for(size_t j = 0; j < curChar.kerningPairs.size(); j++)
  181. {
  182. if(curChar.kerningPairs[j].otherCharId == nextChar.charId)
  183. {
  184. kerning = curChar.kerningPairs[j].amount;
  185. break;
  186. }
  187. }
  188. }
  189. if(curChar.page != page)
  190. continue;
  191. UINT32 curVert = offset * 4;
  192. UINT32 curIndex = offset * 6;
  193. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  194. vertices[curVert + 1] = Vector2((float)(curX + curChar.width), (float)curY);
  195. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)curChar.height);
  196. vertices[curVert + 3] = Vector2((float)(curX + curChar.width), (float)curY + (float)curChar.height);
  197. if(uvs != nullptr)
  198. {
  199. uvs[curVert + 0] = Vector2(curChar.uvX, curChar.uvY);
  200. uvs[curVert + 1] = Vector2(curChar.uvX + curChar.uvWidth, curChar.uvY);
  201. uvs[curVert + 2] = Vector2(curChar.uvX, curChar.uvY + curChar.uvHeight);
  202. uvs[curVert + 3] = Vector2(curChar.uvX + curChar.uvWidth, curChar.uvY + curChar.uvHeight);
  203. }
  204. if(indexes != nullptr)
  205. {
  206. indexes[curIndex + 0] = curVert + 0;
  207. indexes[curIndex + 1] = curVert + 1;
  208. indexes[curIndex + 2] = curVert + 2;
  209. indexes[curIndex + 3] = curVert + 1;
  210. indexes[curIndex + 4] = curVert + 3;
  211. indexes[curIndex + 5] = curVert + 2;
  212. }
  213. offset++;
  214. numQuads++;
  215. if(offset > size)
  216. CM_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  217. }
  218. }
  219. }
  220. return numQuads;
  221. }
  222. UINT32 TextUtility::TextLine::getNumChars() const
  223. {
  224. UINT32 numChars = 0;
  225. for(UINT32 i = mWordsStart; i <= mWordsEnd; i++)
  226. {
  227. TextWord& word = TextUtility::WordBuffer[i];
  228. if(word.isSpacer())
  229. numChars++;
  230. else
  231. numChars += (UINT32)word.getNumChars();
  232. }
  233. return numChars;
  234. }
  235. void TextUtility::TextLine::calculateBounds()
  236. {
  237. mWidth = 0;
  238. mHeight = 0;
  239. for(UINT32 i = mWordsStart; i <= mWordsEnd; i++)
  240. {
  241. TextWord& word = TextUtility::WordBuffer[i];
  242. mWidth += word.getWidth();
  243. mHeight = std::max(mHeight, word.getHeight());
  244. }
  245. }
  246. TextUtility::TextData::TextData(const HFont& font, INT32 baselineOffset, UINT32 lineHeight, UINT32 spaceWidth)
  247. :mFont(font), mBaselineOffset(baselineOffset), mLineHeight(lineHeight), mSpaceWidth(spaceWidth), mChars(nullptr),
  248. mNumChars(0), mWords(nullptr), mNumWords(0), mLines(nullptr), mNumLines(0), mPageInfos(nullptr), mNumPageInfos(0), mData(nullptr)
  249. {
  250. }
  251. TextUtility::TextData::~TextData()
  252. {
  253. if(mData != nullptr)
  254. cm_free(mData);
  255. }
  256. Vector<TextUtility::TextWord>::type TextUtility::WordBuffer = Vector<TextUtility::TextWord>::type(2000);
  257. UINT32 TextUtility::NextFreeWord = 0;
  258. Vector<TextUtility::TextLine>::type TextUtility::LineBuffer = Vector<TextUtility::TextLine>::type(500);
  259. UINT32 TextUtility::NextFreeLine = 0;
  260. Vector<TextUtility::PageInfo>::type TextUtility::PageBuffer = Vector<TextUtility::PageInfo>::type(20);
  261. UINT32 TextUtility::NextFreePageInfo = 0;
  262. UINT32 TextUtility::allocWord(bool spacer)
  263. {
  264. if(NextFreeWord >= WordBuffer.size())
  265. WordBuffer.resize(WordBuffer.size() * 2);
  266. WordBuffer[NextFreeWord].init(spacer);
  267. return NextFreeWord++;
  268. }
  269. UINT32 TextUtility::allocLine(TextData* textData)
  270. {
  271. if(NextFreeLine >= LineBuffer.size())
  272. LineBuffer.resize(LineBuffer.size() * 2);
  273. LineBuffer[NextFreeLine].init(textData);
  274. return NextFreeLine++;
  275. }
  276. void TextUtility::deallocAll()
  277. {
  278. NextFreeWord = 0;
  279. NextFreeLine = 0;
  280. NextFreePageInfo = 0;
  281. }
  282. std::shared_ptr<TextUtility::TextData> TextUtility::getTextData(const WString& text, const HFont& font, UINT32 fontSize, UINT32 width, UINT32 height, bool wordWrap)
  283. {
  284. // In order to reduce number of memory allocations algorithm first calculates data into temporary buffers and then copies the results
  285. const FontData* fontData = nullptr;
  286. if(font != nullptr)
  287. {
  288. UINT32 nearestSize = font->getClosestAvailableSize(fontSize);
  289. fontData = font->getFontDataForSize(nearestSize);
  290. }
  291. if(fontData == nullptr || fontData->texturePages.size() == 0)
  292. return nullptr;
  293. if(fontData->size != fontSize)
  294. {
  295. LOGWRN("Unable to find font with specified size (" + toString(fontSize) + "). Using nearest available size: " + toString(fontData->size));
  296. }
  297. bool widthIsLimited = width > 0;
  298. std::shared_ptr<TextUtility::TextData> textData = cm_shared_ptr<TextData, PoolAlloc>(font, fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth);
  299. UINT32 curLineIdx = allocLine(textData.get());
  300. UINT32 curHeight = fontData->fontDesc.lineHeight;
  301. UINT32 charIdx = 0;
  302. while(true)
  303. {
  304. if(charIdx >= text.size())
  305. break;
  306. UINT32 charId = text[charIdx];
  307. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  308. TextLine& curLine = LineBuffer[curLineIdx];
  309. if(text[charIdx] == '\n')
  310. {
  311. curLine.finalize(true);
  312. curLineIdx = allocLine(textData.get());
  313. curLine = LineBuffer[curLineIdx];
  314. curHeight += fontData->fontDesc.lineHeight;
  315. charIdx++;
  316. continue;
  317. }
  318. if(charId != SPACE_CHAR)
  319. {
  320. curLine.add(charIdx, charDesc);
  321. addCharToPage(charDesc.page, *fontData);
  322. }
  323. else
  324. {
  325. curLine.addSpace();
  326. addCharToPage(0, *fontData);
  327. }
  328. if(widthIsLimited && curLine.getWidth() > width)
  329. {
  330. if(wordWrap)
  331. {
  332. assert(!curLine.isEmpty());
  333. UINT32 lastWordIdx = curLine.removeLastWord();
  334. TextWord& lastWord = WordBuffer[lastWordIdx];
  335. if(lastWord.getWidth() <= width) // If the word fits, attempt to add it to a new line
  336. {
  337. curLine.finalize(false);
  338. curLineIdx = allocLine(textData.get());
  339. curLine = LineBuffer[curLineIdx];
  340. curHeight += fontData->fontDesc.lineHeight;
  341. }
  342. curLine.addWord(lastWordIdx, lastWord);
  343. }
  344. }
  345. charIdx++;
  346. }
  347. LineBuffer[curLineIdx].finalize(true);
  348. // Now that we have all the data we need, allocate the permanent buffers and copy the data
  349. textData->mNumChars = (UINT32)text.size();
  350. textData->mNumWords = NextFreeWord;
  351. textData->mNumLines = NextFreeLine;
  352. textData->mNumPageInfos = NextFreePageInfo;
  353. UINT32 charArraySize = textData->mNumChars * sizeof(const CHAR_DESC*);
  354. UINT32 wordArraySize = textData->mNumWords * sizeof(TextWord);
  355. UINT32 lineArraySize = textData->mNumLines * sizeof(TextLine);
  356. UINT32 pageInfoArraySize = textData->mNumPageInfos * sizeof(PageInfo);
  357. UINT32 totalBufferSize = charArraySize + wordArraySize + lineArraySize + pageInfoArraySize;
  358. textData->mData = cm_alloc(totalBufferSize);
  359. UINT8* dataPtr = (UINT8*)textData->mData;
  360. textData->mChars = (const CHAR_DESC**)dataPtr;
  361. for(UINT32 i = 0; i < textData->mNumChars; i++)
  362. {
  363. UINT32 charId = text[i];
  364. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  365. textData->mChars[i] = &charDesc;
  366. }
  367. dataPtr += charArraySize;
  368. textData->mWords = (TextWord*)dataPtr;
  369. memcpy(textData->mWords, &WordBuffer[0], wordArraySize);
  370. dataPtr += wordArraySize;
  371. textData->mLines = (TextLine*)dataPtr;
  372. memcpy(textData->mLines, &LineBuffer[0], lineArraySize);
  373. dataPtr += lineArraySize;
  374. textData->mPageInfos = (PageInfo*)dataPtr;
  375. memcpy(textData->mPageInfos, &PageBuffer[0], pageInfoArraySize);
  376. TextUtility::deallocAll();
  377. return textData;
  378. }
  379. void TextUtility::addCharToPage(UINT32 page, const FontData& fontData)
  380. {
  381. while(page >= NextFreePageInfo)
  382. {
  383. PageBuffer[NextFreePageInfo].numQuads = 0;
  384. PageBuffer[NextFreePageInfo].texture = HTexture();
  385. NextFreePageInfo++;
  386. }
  387. PageBuffer[page].numQuads++;
  388. if(PageBuffer[page].texture == nullptr)
  389. PageBuffer[page].texture = fontData.texturePages[page];
  390. }
  391. UINT32 TextUtility::TextData::getWidth() const
  392. {
  393. UINT32 width = 0;
  394. for(UINT32 i = 0; i < mNumLines; i++)
  395. width = std::max(width, mLines[i].getWidth());
  396. return width;
  397. }
  398. UINT32 TextUtility::TextData::getHeight() const
  399. {
  400. UINT32 height = 0;
  401. for(UINT32 i = 0; i < mNumLines; i++)
  402. height += mLines[i].getHeight();
  403. return height;
  404. }
  405. }