CmTextUtility.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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()
  63. :mWidth(0), mHeight(0), mLastWord(nullptr)
  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(UINT32 spaceWidth)
  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(spaceWidth);
  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(spaceWidth);
  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. void TextUtility::TextLine::fillBuffer(UINT32 page, Vector2* vertices, Vector2* uvs, UINT32* indexes, UINT32 offset, UINT32 size, const FontData& fontData) const
  149. {
  150. UINT32 penX = 0;
  151. UINT32 baselineY = fontData.fontDesc.baselineOffset;
  152. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  153. {
  154. if((*wordIter)->isSpacer())
  155. {
  156. penX += fontData.fontDesc.spaceWidth;
  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)baselineY - 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. }
  199. }
  200. }
  201. }
  202. void TextUtility::TextLine::calculateBounds()
  203. {
  204. mWidth = 0;
  205. mHeight = 0;
  206. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  207. {
  208. mWidth += (*iter)->getWidth();
  209. mHeight = std::max(mHeight, (*iter)->getHeight());
  210. }
  211. }
  212. TextUtility::TextData::~TextData()
  213. {
  214. for(size_t i = 0; i < mLines.size(); i++)
  215. CM_DELETE(mLines[i], TextLine, ScratchAlloc);
  216. }
  217. std::shared_ptr<TextUtility::TextData> TextUtility::getTextData(const String& text, const HFont& font, UINT32 fontSize, UINT32 width, UINT32 height, bool wordWrap)
  218. {
  219. const FontData* fontData = nullptr;
  220. if(font != nullptr)
  221. {
  222. UINT32 nearestSize = font->getClosestAvailableSize(fontSize);
  223. fontData = font->getFontDataForSize(nearestSize);
  224. }
  225. if(fontData == nullptr)
  226. return nullptr;
  227. if(fontData->size != fontSize)
  228. {
  229. LOGWRN("Unable to find font with specified size (" + toString(fontSize) + "). Using nearest available size: " + toString(fontData->size));
  230. }
  231. bool heightIsLimited = height > 0;
  232. bool widthIsLimited = width > 0;
  233. std::shared_ptr<TextUtility::TextData> textData(CM_NEW(TextData, PoolAlloc) TextData(), &MemAllocDeleter<TextData, PoolAlloc>::deleter);
  234. TextLine* curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
  235. textData->mLines.push_back(curLine);
  236. UINT32 curHeight = fontData->fontDesc.lineHeight;
  237. UINT32 charIdx = 0;
  238. while(true)
  239. {
  240. if(charIdx >= text.size())
  241. break;
  242. if(text[charIdx] == '\n')
  243. {
  244. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > height)
  245. break; // Max height reached
  246. curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
  247. textData->mLines.push_back(curLine);
  248. curHeight += fontData->fontDesc.lineHeight;
  249. charIdx++;
  250. continue;
  251. }
  252. UINT32 charId = text[charIdx];
  253. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  254. if(charId != SPACE_CHAR)
  255. {
  256. curLine->add(charDesc);
  257. if(charDesc.page >= (UINT32)textData->mQuadsPerPage.size())
  258. textData->mQuadsPerPage.resize(charDesc.page + 1);
  259. textData->mQuadsPerPage[charDesc.page]++;
  260. }
  261. else
  262. curLine->addSpace(fontData->fontDesc.spaceWidth);
  263. if(widthIsLimited && curLine->getWidth() > width)
  264. {
  265. if(wordWrap)
  266. {
  267. TextWord* lastWord = curLine->removeLastWord();
  268. if(lastWord->isSpacer())
  269. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  270. // No more lines fit vertically so we're done
  271. if(heightIsLimited && curHeight > height)
  272. break;
  273. curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
  274. textData->mLines.push_back(curLine);
  275. curHeight += fontData->fontDesc.lineHeight;
  276. if(!lastWord->isSpacer())
  277. curLine->addWord(lastWord);
  278. }
  279. else
  280. {
  281. // Nothing else we can do, chars don't fit so we are done
  282. break;
  283. }
  284. }
  285. charIdx++;
  286. }
  287. return textData;
  288. }
  289. }