CmTextUtility.cpp 11 KB

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