CmTextSprite.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. UINT32 fillBuffer(Vector2* vertices, Vector2* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads, const FontData& fontData)
  126. {
  127. UINT32 curVert = startingQuad * 4;
  128. UINT32 curIndex = startingQuad * 6;
  129. UINT32 maxVertIdx = maxNumQuads * 4;
  130. UINT32 maxIndexIdx = maxNumQuads * 6;
  131. UINT32 penX = mPosition.x;
  132. UINT32 baselineY = mPosition.y + fontData.fontDesc.baselineOffset;
  133. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  134. {
  135. if((*wordIter)->isSpacer())
  136. {
  137. penX += fontData.fontDesc.spaceWidth;
  138. }
  139. else
  140. {
  141. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  142. UINT32 kerning = 0;
  143. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  144. {
  145. assert(curVert < maxVertIdx);
  146. assert(curIndex < maxIndexIdx);
  147. UINT32 curX = penX + charIter->xOffset;
  148. UINT32 curY = baselineY - charIter->yOffset;
  149. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  150. vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  151. vertices[curVert + 2] = Vector2((float)curX, (float)curY - (float)charIter->height);
  152. vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)curY - (float)charIter->height);
  153. uv[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  154. uv[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  155. uv[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  156. uv[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  157. indices[curIndex + 0] = curVert + 0;
  158. indices[curIndex + 1] = curVert + 1;
  159. indices[curIndex + 2] = curVert + 2;
  160. indices[curIndex + 3] = curVert + 1;
  161. indices[curIndex + 4] = curVert + 3;
  162. indices[curIndex + 5] = curVert + 2;
  163. penX += charIter->xAdvance + kerning;
  164. curVert += 4;
  165. curIndex += 6;
  166. kerning = 0;
  167. if((charIter + 1) != chars.end())
  168. {
  169. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  170. {
  171. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  172. {
  173. kerning = charIter->kerningPairs[j].amount;
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. return (curVert / 4) - startingQuad;
  182. }
  183. private:
  184. UINT32 mWidth;
  185. vector<TextWord*>::type mWords;
  186. TextWord* mLastWord;
  187. Point mPosition;
  188. void calculateBounds()
  189. {
  190. mWidth = 0;
  191. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  192. {
  193. mWidth += (*iter)->getWidth();
  194. }
  195. }
  196. };
  197. TextSprite::TextSprite()
  198. :mFontSize(0), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  199. {
  200. }
  201. TextSprite::TextSprite(const String& text, FontPtr font, UINT32 fontSize)
  202. :mText(text), mFont(font), mFontSize(fontSize), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  203. {
  204. }
  205. void TextSprite::updateMesh()
  206. {
  207. const FontData* fontData = getFontData();
  208. clearMesh();
  209. if(fontData == nullptr)
  210. return;
  211. if(fontData->size != mFontSize)
  212. {
  213. LOGWRN("Unable to find font with specified size (" + toString(mFontSize) + "). Using nearest available size: " + toString(fontData->size));
  214. }
  215. bool heightIsLimited = mHeight > 0;
  216. bool widthIsLimited = mWidth > 0;
  217. TextLine* curLine = new TextLine();
  218. vector<TextLine*>::type textLines;
  219. textLines.push_back(curLine);
  220. UINT32 curHeight = fontData->fontDesc.lineHeight;
  221. UINT32 charIdx = 0;
  222. mNumMeshQuads = 0;
  223. while(true)
  224. {
  225. if(charIdx >= mText.size())
  226. break;
  227. if(mText[charIdx] == '\n')
  228. {
  229. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  230. break; // Max height reached
  231. curLine = new TextLine();
  232. textLines.push_back(curLine);
  233. curHeight += fontData->fontDesc.lineHeight;
  234. charIdx++;
  235. continue;
  236. }
  237. UINT32 charId = mText[charIdx];
  238. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  239. curLine->add(charDesc);
  240. if(charDesc.charId != SPACE_CHAR)
  241. mNumMeshQuads++;
  242. if(widthIsLimited && curLine->getWidth() > mWidth)
  243. {
  244. if(mWordWrap)
  245. {
  246. TextWord* lastWord = curLine->removeLastWord();
  247. if(lastWord->isSpacer())
  248. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  249. // No more lines fit vertically so we're done
  250. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  251. break;
  252. curLine = new TextLine();
  253. textLines.push_back(curLine);
  254. curHeight += fontData->fontDesc.lineHeight;
  255. if(!lastWord->isSpacer())
  256. curLine->addWord(lastWord);
  257. }
  258. else
  259. {
  260. // Nothing else we can do, chars don't fit so we are done
  261. break;
  262. }
  263. }
  264. charIdx++;
  265. }
  266. // Calc vertical alignment offset
  267. UINT32 vertDiff = std::max(0U, mHeight - curHeight);
  268. UINT32 vertOffset = 0;
  269. switch(mVertAlign)
  270. {
  271. case TVA_Top:
  272. vertOffset = 0;
  273. break;
  274. case TVA_Bottom:
  275. vertOffset = std::max(0, (INT32)(mHeight - curHeight));
  276. break;
  277. case TVA_Center:
  278. vertOffset = std::max(0, (INT32)(mHeight - curHeight)) / 2;
  279. break;
  280. }
  281. // Calc horizontal alignment offset and set final line positions
  282. Point offset = mOffset + getAnchorOffset();
  283. UINT32 curY = 0;
  284. for(size_t i = 0; i < textLines.size(); i++)
  285. {
  286. UINT32 horzOffset = 0;
  287. switch(mHorzAlign)
  288. {
  289. case THA_Left:
  290. horzOffset = 0;
  291. break;
  292. case THA_Right:
  293. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth()));
  294. break;
  295. case THA_Center:
  296. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth())) / 2;
  297. break;
  298. }
  299. textLines[i]->setPosition(offset + Point(horzOffset, vertOffset + curY));
  300. curY += fontData->fontDesc.lineHeight;
  301. }
  302. // Actually generate a mesh
  303. mVertices = new Vector2[mNumMeshQuads * 4];
  304. mUVs = new Vector2[mNumMeshQuads * 4];
  305. mIndexes = new UINT32[mNumMeshQuads * 6];
  306. UINT32 curFace = 0;
  307. for(size_t i = 0; i < textLines.size(); i++)
  308. {
  309. UINT32 newFaces = textLines[i]->fillBuffer(mVertices, mUVs, mIndexes, curFace, mNumMeshQuads, *fontData);
  310. curFace += newFaces;
  311. }
  312. for(size_t i = 0; i < textLines.size(); i++)
  313. delete textLines[i];
  314. // TODO - Clip the mesh based on mWidth/mHeight
  315. // TODO - How do I implement a scrollable text area without a clip rect?
  316. }
  317. const FontData* TextSprite::getFontData() const
  318. {
  319. if(mFont == nullptr)
  320. return nullptr;
  321. UINT32 nearestSize = mFont->getClosestAvailableSize(mFontSize);
  322. return mFont->getFontDataForSize(nearestSize);
  323. }
  324. }