CmTextSprite.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include "CmTextSprite.h"
  2. #include "CmDebug.h"
  3. #include "CmFontDesc.h"
  4. #include "CmFont.h"
  5. #include "CmVector2.h"
  6. #include "CmGUIMaterialManager.h"
  7. namespace CamelotEngine
  8. {
  9. const int SPACE_CHAR = 32;
  10. class TextWord
  11. {
  12. public:
  13. TextWord(bool spacer)
  14. :mWidth(0), mSpacer(spacer)
  15. { }
  16. void addChar(const CHAR_DESC& desc)
  17. {
  18. mChars.push_back(desc);
  19. calculateWidth();
  20. }
  21. void removeLastChar()
  22. {
  23. if(mChars.size() > 0)
  24. {
  25. mChars.erase(mChars.end() - 1);
  26. calculateWidth();
  27. }
  28. }
  29. UINT32 getWidth() const { return mWidth; }
  30. bool isSpacer() const { return mSpacer; }
  31. const vector<CHAR_DESC>::type& getChars() const { return mChars; }
  32. private:
  33. vector<CHAR_DESC>::type mChars;
  34. UINT32 mWidth;
  35. bool mSpacer;
  36. void calculateWidth()
  37. {
  38. if(mChars.size() == 0)
  39. {
  40. mWidth = 0;
  41. return;
  42. }
  43. mWidth = 0;
  44. UINT32 kerning = 0;
  45. for(size_t i = 0; i < mChars.size() - 1; i++)
  46. {
  47. mWidth += mChars[i].xAdvance + kerning;
  48. kerning = 0;
  49. for(size_t j = 0; j < mChars[i].kerningPairs.size(); j++)
  50. {
  51. if(mChars[i].kerningPairs[j].otherCharId == mChars[i + 1].charId)
  52. {
  53. kerning = mChars[i].kerningPairs[j].amount;
  54. break;
  55. }
  56. }
  57. }
  58. mWidth += mChars[mChars.size() - 1].xAdvance + kerning;
  59. }
  60. };
  61. class TextLine
  62. {
  63. public:
  64. TextLine()
  65. :mWidth(0), mLastWord(nullptr)
  66. {
  67. }
  68. ~TextLine()
  69. {
  70. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  71. delete *iter;
  72. }
  73. void add(const CHAR_DESC& charDesc)
  74. {
  75. if(mLastWord == nullptr)
  76. {
  77. TextWord* newWord = new TextWord(charDesc.charId == SPACE_CHAR);
  78. mLastWord = newWord;
  79. mWords.push_back(mLastWord);
  80. mLastWord->addChar(charDesc);
  81. }
  82. else
  83. {
  84. if(charDesc.charId != SPACE_CHAR)
  85. {
  86. if(mLastWord->isSpacer())
  87. {
  88. TextWord* newWord = new TextWord(false);
  89. mLastWord = newWord;
  90. mWords.push_back(mLastWord);
  91. }
  92. mLastWord->addChar(charDesc);
  93. }
  94. else
  95. {
  96. TextWord* newWord = new TextWord(true); // Each space is counted as its own word, to make certain operations easier
  97. mLastWord = newWord;
  98. mWords.push_back(mLastWord);
  99. mLastWord->addChar(charDesc);
  100. }
  101. }
  102. calculateBounds();
  103. }
  104. void addWord(TextWord* word)
  105. {
  106. mWords.push_back(word);
  107. mLastWord = word;
  108. calculateBounds();
  109. }
  110. TextWord* 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. UINT32 getWidth() const { return mWidth; }
  124. Point getPosition() const { return mPosition; }
  125. void setPosition(const Point& pos) { mPosition = pos; }
  126. void fillBuffer(const vector<SpriteRenderElement>::type& renderElements, vector<UINT32>::type& faceOffsets, const FontData& fontData)
  127. {
  128. UINT32 penX = mPosition.x;
  129. UINT32 baselineY = mPosition.y + fontData.fontDesc.baselineOffset;
  130. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  131. {
  132. if((*wordIter)->isSpacer())
  133. {
  134. penX += fontData.fontDesc.spaceWidth;
  135. }
  136. else
  137. {
  138. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  139. UINT32 kerning = 0;
  140. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  141. {
  142. INT32 curX = penX + charIter->xOffset;
  143. INT32 curY = -((INT32)baselineY - charIter->yOffset);
  144. UINT32 curVert = faceOffsets[charIter->page] * 4;
  145. UINT32 curIndex = faceOffsets[charIter->page] * 6;
  146. const SpriteRenderElement& renderElem = renderElements[charIter->page];
  147. renderElem.vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  148. renderElem.vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  149. renderElem.vertices[curVert + 2] = Vector2((float)curX, (float)curY - (float)charIter->height);
  150. renderElem.vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)curY - (float)charIter->height);
  151. renderElem.uvs[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  152. renderElem.uvs[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  153. renderElem.uvs[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  154. renderElem.uvs[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  155. renderElem.indexes[curIndex + 0] = curVert + 0;
  156. renderElem.indexes[curIndex + 1] = curVert + 1;
  157. renderElem.indexes[curIndex + 2] = curVert + 2;
  158. renderElem.indexes[curIndex + 3] = curVert + 1;
  159. renderElem.indexes[curIndex + 4] = curVert + 3;
  160. renderElem.indexes[curIndex + 5] = curVert + 2;
  161. penX += charIter->xAdvance + kerning;
  162. faceOffsets[charIter->page]++;
  163. kerning = 0;
  164. if((charIter + 1) != chars.end())
  165. {
  166. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  167. {
  168. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  169. {
  170. kerning = charIter->kerningPairs[j].amount;
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. private:
  180. UINT32 mWidth;
  181. vector<TextWord*>::type mWords;
  182. TextWord* mLastWord;
  183. Point mPosition;
  184. void calculateBounds()
  185. {
  186. mWidth = 0;
  187. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  188. {
  189. mWidth += (*iter)->getWidth();
  190. }
  191. }
  192. };
  193. TextSprite::TextSprite()
  194. :mFontSize(0), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  195. {
  196. }
  197. TextSprite::TextSprite(const String& text, FontPtr font, UINT32 fontSize)
  198. :mText(text), mFont(font), mFontSize(fontSize), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  199. {
  200. }
  201. void TextSprite::updateMesh() const
  202. {
  203. const FontData* fontData = getFontData();
  204. clearMesh();
  205. if(fontData == nullptr)
  206. return;
  207. if(fontData->size != mFontSize)
  208. {
  209. LOGWRN("Unable to find font with specified size (" + toString(mFontSize) + "). Using nearest available size: " + toString(fontData->size));
  210. }
  211. bool heightIsLimited = mHeight > 0;
  212. bool widthIsLimited = mWidth > 0;
  213. TextLine* curLine = new TextLine();
  214. vector<TextLine*>::type textLines;
  215. textLines.push_back(curLine);
  216. UINT32 curHeight = fontData->fontDesc.lineHeight;
  217. UINT32 charIdx = 0;
  218. while(true)
  219. {
  220. if(charIdx >= mText.size())
  221. break;
  222. if(mText[charIdx] == '\n')
  223. {
  224. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  225. break; // Max height reached
  226. curLine = new TextLine();
  227. textLines.push_back(curLine);
  228. curHeight += fontData->fontDesc.lineHeight;
  229. charIdx++;
  230. continue;
  231. }
  232. UINT32 charId = mText[charIdx];
  233. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  234. curLine->add(charDesc);
  235. if(charDesc.charId != SPACE_CHAR)
  236. {
  237. if(charDesc.page >= (UINT32)mCachedRenderElements.size())
  238. mCachedRenderElements.resize(charDesc.page + 1);
  239. mCachedRenderElements[charDesc.page].numQuads++;
  240. }
  241. if(widthIsLimited && curLine->getWidth() > mWidth)
  242. {
  243. if(mWordWrap)
  244. {
  245. TextWord* lastWord = curLine->removeLastWord();
  246. if(lastWord->isSpacer())
  247. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  248. // No more lines fit vertically so we're done
  249. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  250. break;
  251. curLine = new TextLine();
  252. textLines.push_back(curLine);
  253. curHeight += fontData->fontDesc.lineHeight;
  254. if(!lastWord->isSpacer())
  255. curLine->addWord(lastWord);
  256. }
  257. else
  258. {
  259. // Nothing else we can do, chars don't fit so we are done
  260. break;
  261. }
  262. }
  263. charIdx++;
  264. }
  265. // Calc vertical alignment offset
  266. UINT32 vertDiff = std::max(0U, mHeight - curHeight);
  267. UINT32 vertOffset = 0;
  268. switch(mVertAlign)
  269. {
  270. case TVA_Top:
  271. vertOffset = 0;
  272. break;
  273. case TVA_Bottom:
  274. vertOffset = std::max(0, (INT32)(mHeight - curHeight));
  275. break;
  276. case TVA_Center:
  277. vertOffset = std::max(0, (INT32)(mHeight - curHeight)) / 2;
  278. break;
  279. }
  280. // Calc horizontal alignment offset and set final line positions
  281. Point offset = mOffset + getAnchorOffset();
  282. UINT32 curY = 0;
  283. for(size_t i = 0; i < textLines.size(); i++)
  284. {
  285. UINT32 horzOffset = 0;
  286. switch(mHorzAlign)
  287. {
  288. case THA_Left:
  289. horzOffset = 0;
  290. break;
  291. case THA_Right:
  292. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth()));
  293. break;
  294. case THA_Center:
  295. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth())) / 2;
  296. break;
  297. }
  298. textLines[i]->setPosition(offset + Point(horzOffset, vertOffset + curY));
  299. curY += fontData->fontDesc.lineHeight;
  300. }
  301. // Actually generate a mesh
  302. UINT32 texPage = 0;
  303. for(auto& renderElem : mCachedRenderElements)
  304. {
  305. renderElem.vertices = new Vector2[renderElem.numQuads * 4];
  306. renderElem.uvs = new Vector2[renderElem.numQuads * 4];
  307. renderElem.indexes = new UINT32[renderElem.numQuads * 6];
  308. renderElem.material = GUIMaterialManager::instance().requestTextMaterial(fontData->texturePages[texPage]);
  309. texPage++;
  310. }
  311. vector<UINT32>::type faceOffsets(mCachedRenderElements.size(), 0);
  312. for(size_t i = 0; i < textLines.size(); i++)
  313. textLines[i]->fillBuffer(mCachedRenderElements, faceOffsets, *fontData);
  314. for(size_t i = 0; i < textLines.size(); i++)
  315. delete textLines[i];
  316. // TODO - Clip the mesh based on mWidth/mHeight
  317. // TODO - How do I implement a scrollable text area without a clip rect?
  318. }
  319. const FontData* TextSprite::getFontData() const
  320. {
  321. if(mFont == nullptr)
  322. return nullptr;
  323. UINT32 nearestSize = mFont->getClosestAvailableSize(mFontSize);
  324. return mFont->getFontDataForSize(nearestSize);
  325. }
  326. }