CmTextSprite.cpp 9.9 KB

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