CmTextUtility.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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), mLineHeight(lineHeight), mSpaceWidth(spaceWidth)
  64. {
  65. }
  66. TextUtility::TextLine::~TextLine()
  67. {
  68. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  69. CM_DELETE(*iter, TextWord, ScratchAlloc);
  70. }
  71. void TextUtility::TextLine::add(const CHAR_DESC& charDesc)
  72. {
  73. if(mLastWord == nullptr)
  74. {
  75. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(false);
  76. mLastWord = newWord;
  77. mWords.push_back(mLastWord);
  78. mLastWord->addChar(charDesc);
  79. }
  80. else
  81. {
  82. if(mLastWord->isSpacer())
  83. {
  84. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(false);
  85. mLastWord = newWord;
  86. mWords.push_back(mLastWord);
  87. }
  88. mLastWord->addChar(charDesc);
  89. }
  90. calculateBounds();
  91. }
  92. void TextUtility::TextLine::addSpace()
  93. {
  94. if(mLastWord == nullptr)
  95. {
  96. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(true);
  97. mLastWord = newWord;
  98. mWords.push_back(mLastWord);
  99. mLastWord->addSpace(mSpaceWidth);
  100. }
  101. else
  102. {
  103. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(true); // Each space is counted as its own word, to make certain operations easier
  104. mLastWord = newWord;
  105. mWords.push_back(mLastWord);
  106. mLastWord->addSpace(mSpaceWidth);
  107. }
  108. calculateBounds();
  109. }
  110. void TextUtility::TextLine::addWord(TextWord* word)
  111. {
  112. mWords.push_back(word);
  113. mLastWord = word;
  114. calculateBounds();
  115. }
  116. TextUtility::TextWord* TextUtility::TextLine::removeLastWord()
  117. {
  118. if(mWords.size() == 0)
  119. return nullptr;
  120. TextWord* word = mWords[mWords.size() - 1];
  121. mWords.erase(mWords.end() - 1);
  122. if(mWords.size() > 0)
  123. mLastWord = mWords[mWords.size() - 1];
  124. else
  125. mLastWord = nullptr;
  126. calculateBounds();
  127. return word;
  128. }
  129. std::vector<UINT32> TextUtility::TextLine::getNumQuadsPerPage() const
  130. {
  131. std::vector<UINT32> quadsPerPage;
  132. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  133. {
  134. if(!(*wordIter)->isSpacer())
  135. {
  136. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  137. UINT32 kerning = 0;
  138. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  139. {
  140. if(charIter->page > (UINT32)quadsPerPage.size())
  141. quadsPerPage.resize(charIter->page + 1);
  142. quadsPerPage[charIter->page]++;
  143. }
  144. }
  145. }
  146. return quadsPerPage;
  147. }
  148. UINT32 TextUtility::TextLine::fillBuffer(UINT32 page, Vector2* vertices, Vector2* uvs, UINT32* indexes, UINT32 offset, UINT32 size) const
  149. {
  150. UINT32 numQuads = 0;
  151. UINT32 penX = 0;
  152. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  153. {
  154. if((*wordIter)->isSpacer())
  155. {
  156. penX += mSpaceWidth;
  157. }
  158. else
  159. {
  160. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  161. UINT32 kerning = 0;
  162. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  163. {
  164. INT32 curX = penX + charIter->xOffset;
  165. INT32 curY = ((INT32)mBaselineOffset - charIter->yOffset);
  166. penX += charIter->xAdvance + kerning;
  167. kerning = 0;
  168. if((charIter + 1) != chars.end())
  169. {
  170. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  171. {
  172. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  173. {
  174. kerning = charIter->kerningPairs[j].amount;
  175. break;
  176. }
  177. }
  178. }
  179. if(charIter->page != page)
  180. continue;
  181. UINT32 curVert = offset * 4;
  182. UINT32 curIndex = offset * 6;
  183. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  184. vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  185. vertices[curVert + 2] = Vector2((float)curX, (float)curY + (float)charIter->height);
  186. vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)curY + (float)charIter->height);
  187. uvs[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  188. uvs[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  189. uvs[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  190. uvs[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  191. indexes[curIndex + 0] = curVert + 0;
  192. indexes[curIndex + 1] = curVert + 1;
  193. indexes[curIndex + 2] = curVert + 2;
  194. indexes[curIndex + 3] = curVert + 1;
  195. indexes[curIndex + 4] = curVert + 3;
  196. indexes[curIndex + 5] = curVert + 2;
  197. offset++;
  198. numQuads++;
  199. if(offset > size)
  200. CM_EXCEPT(InternalErrorException, "Out of buffer bounds. Buffer size: " + toString(size));
  201. }
  202. }
  203. }
  204. return numQuads;
  205. }
  206. void TextUtility::TextLine::calculateBounds()
  207. {
  208. mWidth = 0;
  209. mHeight = 0;
  210. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  211. {
  212. mWidth += (*iter)->getWidth();
  213. mHeight = std::max(mHeight, (*iter)->getHeight());
  214. }
  215. }
  216. TextUtility::TextData::~TextData()
  217. {
  218. for(size_t i = 0; i < mLines.size(); i++)
  219. CM_DELETE(mLines[i], TextLine, ScratchAlloc);
  220. }
  221. std::shared_ptr<TextUtility::TextData> TextUtility::getTextData(const String& text, const HFont& font, UINT32 fontSize, UINT32 width, UINT32 height, bool wordWrap)
  222. {
  223. const FontData* fontData = nullptr;
  224. if(font != nullptr)
  225. {
  226. UINT32 nearestSize = font->getClosestAvailableSize(fontSize);
  227. fontData = font->getFontDataForSize(nearestSize);
  228. }
  229. if(fontData == nullptr)
  230. return nullptr;
  231. if(fontData->size != fontSize)
  232. {
  233. LOGWRN("Unable to find font with specified size (" + toString(fontSize) + "). Using nearest available size: " + toString(fontData->size));
  234. }
  235. bool heightIsLimited = height > 0;
  236. bool widthIsLimited = width > 0;
  237. std::shared_ptr<TextUtility::TextData> textData(CM_NEW(TextData, PoolAlloc) TextData(), &MemAllocDeleter<TextData, PoolAlloc>::deleter);
  238. TextLine* curLine = CM_NEW(TextLine, ScratchAlloc) TextLine(fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth);
  239. textData->mLines.push_back(curLine);
  240. UINT32 curHeight = fontData->fontDesc.lineHeight;
  241. UINT32 charIdx = 0;
  242. while(true)
  243. {
  244. if(charIdx >= text.size())
  245. break;
  246. if(text[charIdx] == '\n')
  247. {
  248. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > height)
  249. break; // Max height reached
  250. curLine = CM_NEW(TextLine, ScratchAlloc) TextLine(fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth);
  251. textData->mLines.push_back(curLine);
  252. curHeight += fontData->fontDesc.lineHeight;
  253. charIdx++;
  254. continue;
  255. }
  256. UINT32 charId = text[charIdx];
  257. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  258. if(charId != SPACE_CHAR)
  259. {
  260. curLine->add(charDesc);
  261. if(charDesc.page >= (UINT32)textData->mQuadsPerPage.size())
  262. {
  263. textData->mQuadsPerPage.resize(charDesc.page + 1);
  264. textData->mTexturePages.resize(charDesc.page + 1);
  265. }
  266. textData->mQuadsPerPage[charDesc.page]++;
  267. if(textData->mTexturePages[charDesc.page] == nullptr)
  268. textData->mTexturePages[charDesc.page] = fontData->texturePages[charDesc.page];
  269. }
  270. else
  271. curLine->addSpace();
  272. if(widthIsLimited && curLine->getWidth() > width)
  273. {
  274. if(wordWrap)
  275. {
  276. TextWord* lastWord = curLine->removeLastWord();
  277. if(lastWord->isSpacer())
  278. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  279. // No more lines fit vertically so we're done
  280. if(heightIsLimited && curHeight > height)
  281. break;
  282. curLine = CM_NEW(TextLine, ScratchAlloc) TextLine(fontData->fontDesc.baselineOffset, fontData->fontDesc.lineHeight, fontData->fontDesc.spaceWidth);
  283. textData->mLines.push_back(curLine);
  284. curHeight += fontData->fontDesc.lineHeight;
  285. if(!lastWord->isSpacer())
  286. curLine->addWord(lastWord);
  287. }
  288. else
  289. {
  290. // Nothing else we can do, chars don't fit so we are done
  291. break;
  292. }
  293. }
  294. charIdx++;
  295. }
  296. return textData;
  297. }
  298. }