CmTextUtility.cpp 9.4 KB

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