CmTextUtility.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. TextUtility::TextWord::TextWord(bool spacer)
  9. :mWidth(0), mHeight(0), mSpacer(spacer), mSpaceWidth(0)
  10. { }
  11. void TextUtility::TextWord::addChar(const CHAR_DESC& desc)
  12. {
  13. mChars.push_back(desc);
  14. calculateWidth();
  15. }
  16. void TextUtility::TextWord::addSpace(UINT32 spaceWidth)
  17. {
  18. mSpaceWidth += spaceWidth;
  19. calculateWidth();
  20. }
  21. void TextUtility::TextWord::removeLastChar()
  22. {
  23. if(mChars.size() > 0)
  24. {
  25. mChars.erase(mChars.end() - 1);
  26. calculateWidth();
  27. }
  28. }
  29. void TextUtility::TextWord::calculateWidth()
  30. {
  31. if(isSpacer())
  32. {
  33. mWidth = mSpaceWidth;
  34. mHeight = 0;
  35. return;
  36. }
  37. if(mChars.size() == 0)
  38. {
  39. mWidth = 0;
  40. mHeight = 0;
  41. return;
  42. }
  43. mWidth = 0;
  44. mHeight = 0;
  45. UINT32 kerning = 0;
  46. for(size_t i = 0; i < mChars.size() - 1; i++)
  47. {
  48. mWidth += mChars[i].xAdvance + kerning;
  49. mHeight = std::max(mHeight, mChars[i].height);
  50. kerning = 0;
  51. for(size_t j = 0; j < mChars[i].kerningPairs.size(); j++)
  52. {
  53. if(mChars[i].kerningPairs[j].otherCharId == mChars[i + 1].charId)
  54. {
  55. kerning = mChars[i].kerningPairs[j].amount;
  56. break;
  57. }
  58. }
  59. }
  60. mWidth += mChars[mChars.size() - 1].xAdvance + kerning;
  61. }
  62. TextUtility::TextLine::TextLine(UINT32 baselineOffset, UINT32 lineHeight, UINT32 spaceWidth)
  63. :mWidth(0), mHeight(0), mLastWord(nullptr), mBaselineOffset(baselineOffset),
  64. mLineHeight(lineHeight), mSpaceWidth(spaceWidth)
  65. {
  66. }
  67. TextUtility::TextLine::~TextLine()
  68. {
  69. }
  70. void TextUtility::TextLine::finalize(bool hasNewlineChar)
  71. {
  72. mHasNewline = hasNewlineChar;
  73. }
  74. void TextUtility::TextLine::add(const CHAR_DESC& charDesc)
  75. {
  76. if(mLastWord == nullptr)
  77. {
  78. mWords.push_back(TextWord(false));
  79. mLastWord = &mWords.back();
  80. mLastWord->addChar(charDesc);
  81. }
  82. else
  83. {
  84. if(mLastWord->isSpacer())
  85. {
  86. mWords.push_back(TextWord(false));
  87. mLastWord = &mWords.back();
  88. }
  89. mLastWord->addChar(charDesc);
  90. }
  91. calculateBounds();
  92. }
  93. void TextUtility::TextLine::addSpace()
  94. {
  95. if(mLastWord == nullptr)
  96. {
  97. mWords.push_back(TextWord(true));
  98. mLastWord = &mWords.back();
  99. mLastWord->addSpace(mSpaceWidth);
  100. }
  101. else
  102. {
  103. mWords.push_back(TextWord(true)); // Each space is counted as its own word, to make certain operations easier
  104. mLastWord = &mWords.back();
  105. mLastWord->addSpace(mSpaceWidth);
  106. }
  107. calculateBounds();
  108. }
  109. void TextUtility::TextLine::addWord(const TextWord& word)
  110. {
  111. mWords.push_back(word);
  112. mLastWord = &mWords.back();
  113. calculateBounds();
  114. }
  115. TextUtility::TextWord TextUtility::TextLine::removeLastWord()
  116. {
  117. if(mWords.size() == 0)
  118. return nullptr;
  119. TextWord word = mWords[mWords.size() - 1];
  120. mWords.erase(mWords.end() - 1);
  121. if(mWords.size() > 0)
  122. mLastWord = &mWords[mWords.size() - 1];
  123. else
  124. mLastWord = nullptr;
  125. calculateBounds();
  126. return word;
  127. }
  128. Vector<UINT32>::type TextUtility::TextLine::getNumQuadsPerPage() const
  129. {
  130. Vector<UINT32>::type quadsPerPage;
  131. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  132. {
  133. if(!wordIter->isSpacer())
  134. {
  135. const Vector<CHAR_DESC>::type& chars = wordIter->getChars();
  136. UINT32 kerning = 0;
  137. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  138. {
  139. if(charIter->page > (UINT32)quadsPerPage.size())
  140. quadsPerPage.resize(charIter->page + 1);
  141. quadsPerPage[charIter->page]++;
  142. }
  143. }
  144. else
  145. quadsPerPage[0]++;
  146. }
  147. return quadsPerPage;
  148. }
  149. UINT32 TextUtility::TextLine::fillBuffer(UINT32 page, Vector2* vertices, Vector2* uvs, UINT32* indexes, UINT32 offset, UINT32 size) const
  150. {
  151. UINT32 numQuads = 0;
  152. UINT32 penX = 0;
  153. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  154. {
  155. if(wordIter->isSpacer())
  156. {
  157. // We store invisible space quads in the first page. Even though they aren't needed
  158. // for rendering and we could just leave an empty space, they are needed for intersection tests
  159. // for things like determining caret placement and selection areas
  160. if(page == 0)
  161. {
  162. INT32 curX = penX;
  163. INT32 curY = 0;
  164. UINT32 curVert = offset * 4;
  165. UINT32 curIndex = offset * 6;
  166. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  167. vertices[curVert + 1] = Vector2((float)(curX + mSpaceWidth), (float)curY);
  168. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)mLineHeight);
  169. vertices[curVert + 3] = Vector2((float)(curX + mSpaceWidth), (float)curY + (float)mLineHeight);
  170. if(uvs != nullptr)
  171. {
  172. uvs[curVert + 0] = Vector2(0.0f, 0.0f);
  173. uvs[curVert + 1] = Vector2(0.0f, 0.0f);
  174. uvs[curVert + 2] = Vector2(0.0f, 0.0f);
  175. uvs[curVert + 3] = Vector2(0.0f, 0.0f);
  176. }
  177. // Triangles are back-facing which makes them invisible
  178. if(indexes != nullptr)
  179. {
  180. indexes[curIndex + 0] = curVert + 0;
  181. indexes[curIndex + 1] = curVert + 2;
  182. indexes[curIndex + 2] = curVert + 1;
  183. indexes[curIndex + 3] = curVert + 1;
  184. indexes[curIndex + 4] = curVert + 2;
  185. indexes[curIndex + 5] = curVert + 3;
  186. }
  187. offset++;
  188. numQuads++;
  189. if(offset > size)
  190. CM_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  191. }
  192. penX += mSpaceWidth;
  193. }
  194. else
  195. {
  196. const Vector<CHAR_DESC>::type& chars = wordIter->getChars();
  197. UINT32 kerning = 0;
  198. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  199. {
  200. INT32 curX = penX + charIter->xOffset;
  201. INT32 curY = ((INT32)mBaselineOffset - charIter->yOffset);
  202. penX += charIter->xAdvance + kerning;
  203. kerning = 0;
  204. if((charIter + 1) != chars.end())
  205. {
  206. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  207. {
  208. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  209. {
  210. kerning = charIter->kerningPairs[j].amount;
  211. break;
  212. }
  213. }
  214. }
  215. if(charIter->page != page)
  216. continue;
  217. UINT32 curVert = offset * 4;
  218. UINT32 curIndex = offset * 6;
  219. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  220. vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  221. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)charIter->height);
  222. vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)curY + (float)charIter->height);
  223. if(uvs != nullptr)
  224. {
  225. uvs[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  226. uvs[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  227. uvs[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  228. uvs[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  229. }
  230. if(indexes != nullptr)
  231. {
  232. indexes[curIndex + 0] = curVert + 0;
  233. indexes[curIndex + 1] = curVert + 1;
  234. indexes[curIndex + 2] = curVert + 2;
  235. indexes[curIndex + 3] = curVert + 1;
  236. indexes[curIndex + 4] = curVert + 3;
  237. indexes[curIndex + 5] = curVert + 2;
  238. }
  239. offset++;
  240. numQuads++;
  241. if(offset > size)
  242. CM_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  243. }
  244. }
  245. }
  246. return numQuads;
  247. }
  248. UINT32 TextUtility::TextLine::getNumChars() const
  249. {
  250. UINT32 numChars = 0;
  251. for(auto& word : mWords)
  252. {
  253. if(word.isSpacer())
  254. numChars++;
  255. else
  256. numChars += (UINT32)word.getChars().size();
  257. }
  258. return numChars;
  259. }
  260. void TextUtility::TextLine::calculateBounds()
  261. {
  262. mWidth = 0;
  263. mHeight = 0;
  264. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  265. {
  266. mWidth += iter->getWidth();
  267. mHeight = std::max(mHeight, iter->getHeight());
  268. }
  269. }
  270. TextUtility::TextData::~TextData()
  271. {
  272. }
  273. std::shared_ptr<TextUtility::TextData> TextUtility::getTextData(const WString& text, const HFont& font, UINT32 fontSize, UINT32 width, UINT32 height, bool wordWrap)
  274. {
  275. const FontData* fontData = nullptr;
  276. if(font != nullptr)
  277. {
  278. UINT32 nearestSize = font->getClosestAvailableSize(fontSize);
  279. fontData = font->getFontDataForSize(nearestSize);
  280. }
  281. if(fontData == nullptr || fontData->texturePages.size() == 0)
  282. return nullptr;
  283. if(fontData->size != fontSize)
  284. {
  285. LOGWRN("Unable to find font with specified size (" + toString(fontSize) + "). Using nearest available size: " + toString(fontData->size));
  286. }
  287. bool widthIsLimited = width > 0;
  288. std::shared_ptr<TextUtility::TextData> textData = cm_shared_ptr<TextData, PoolAlloc>();
  289. textData->mLines.push_back(TextLine(fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth));
  290. TextLine* curLine = &textData->mLines.back();
  291. UINT32 curHeight = fontData->fontDesc.lineHeight;
  292. UINT32 charIdx = 0;
  293. while(true)
  294. {
  295. if(charIdx >= text.size())
  296. break;
  297. UINT32 charId = text[charIdx];
  298. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  299. if(text[charIdx] == '\n')
  300. {
  301. curLine->finalize(true);
  302. textData->mLines.push_back(TextLine(fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth));
  303. curLine = &textData->mLines.back();
  304. curHeight += fontData->fontDesc.lineHeight;
  305. charIdx++;
  306. continue;
  307. }
  308. if(charId != SPACE_CHAR)
  309. {
  310. curLine->add(charDesc);
  311. addCharToPage(*textData, charDesc.page, *fontData);
  312. }
  313. else
  314. {
  315. curLine->addSpace();
  316. addCharToPage(*textData, 0, *fontData);
  317. }
  318. if(widthIsLimited && curLine->getWidth() > width)
  319. {
  320. if(wordWrap)
  321. {
  322. TextWord lastWord = curLine->removeLastWord();
  323. if(lastWord.getWidth() <= width) // If the world fits, attempt to add it to a new line
  324. {
  325. curLine->finalize(false);
  326. textData->mLines.push_back(TextLine(fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth));
  327. curLine = &textData->mLines.back();
  328. curHeight += fontData->fontDesc.lineHeight;
  329. }
  330. curLine->addWord(lastWord);
  331. }
  332. }
  333. charIdx++;
  334. }
  335. curLine->finalize(true);
  336. return textData;
  337. }
  338. void TextUtility::addCharToPage(TextUtility::TextData& data, UINT32 page, const FontData& fontData)
  339. {
  340. if(page >= (UINT32)data.mQuadsPerPage.size())
  341. {
  342. data.mQuadsPerPage.resize(page + 1);
  343. data.mTexturePages.resize(page + 1);
  344. }
  345. data.mQuadsPerPage[page]++;
  346. if(data.mTexturePages[page] == nullptr)
  347. data.mTexturePages[page] = fontData.texturePages[page];
  348. }
  349. UINT32 TextUtility::TextData::getWidth() const
  350. {
  351. UINT32 width = 0;
  352. for(auto& line : mLines)
  353. width = std::max(width, line.getWidth());
  354. return width;
  355. }
  356. UINT32 TextUtility::TextData::getHeight() const
  357. {
  358. UINT32 height = 0;
  359. for(auto& line : mLines)
  360. height += line.getHeight();
  361. return height;
  362. }
  363. }