CmTextSprite.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. }
  80. else
  81. {
  82. if(charDesc.charId != SPACE_CHAR)
  83. {
  84. if(mLastWord->isSpacer())
  85. {
  86. TextWord* newWord = new TextWord(false);
  87. mLastWord = newWord;
  88. mWords.push_back(mLastWord);
  89. }
  90. mLastWord->addChar(charDesc);
  91. }
  92. else
  93. {
  94. TextWord* newWord = new TextWord(true); // Each space is counted as its own word, to make certain operations easier
  95. mLastWord = newWord;
  96. mWords.push_back(mLastWord);
  97. mLastWord->addChar(charDesc);
  98. }
  99. }
  100. calculateBounds();
  101. }
  102. void addWord(TextWord* word)
  103. {
  104. mWords.push_back(word);
  105. mLastWord = word;
  106. calculateBounds();
  107. }
  108. TextWord* removeLastWord()
  109. {
  110. if(mWords.size() == 0)
  111. return nullptr;
  112. TextWord* word = mWords[mWords.size() - 1];
  113. mWords.erase(mWords.end() - 1);
  114. if(mWords.size() > 0)
  115. mLastWord = mWords[mWords.size() - 1];
  116. else
  117. mLastWord = nullptr;
  118. calculateBounds();
  119. return word;
  120. }
  121. UINT32 getWidth() const { return mWidth; }
  122. Point getPosition() const { return mPosition; }
  123. void setPosition(const Point& pos) { mPosition = pos; }
  124. UINT32 fillBuffer(Vector2* vertices, Vector2* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads, const FontData& fontData)
  125. {
  126. UINT32 curVert = startingQuad * 4;
  127. UINT32 curIndex = startingQuad * 6;
  128. UINT32 maxVertIdx = maxNumQuads * 4;
  129. UINT32 maxIndexIdx = maxNumQuads * 6;
  130. UINT32 penX = mPosition.x;
  131. UINT32 baselineY = mPosition.y + fontData.fontDesc.baselineOffset;
  132. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  133. {
  134. if((*wordIter)->isSpacer())
  135. {
  136. penX += fontData.fontDesc.spaceWidth;
  137. }
  138. else
  139. {
  140. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  141. UINT32 kerning = 0;
  142. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  143. {
  144. assert(curVert < maxVertIdx);
  145. assert(curIndex < maxIndexIdx);
  146. UINT32 curX = penX + charIter->xOffset;
  147. UINT32 curY = baselineY - charIter->yOffset;
  148. vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  149. vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  150. vertices[curVert + 2] = Vector2((float)curX, (float)(curY + charIter->height));
  151. vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)(curY + charIter->height));
  152. uv[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  153. uv[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  154. uv[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  155. uv[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  156. indices[curIndex + 0] = 0;
  157. indices[curIndex + 1] = 2;
  158. indices[curIndex + 2] = 1;
  159. indices[curIndex + 3] = 1;
  160. indices[curIndex + 4] = 2;
  161. indices[curIndex + 5] = 3;
  162. penX += charIter->xAdvance + kerning;
  163. curVert += 4;
  164. curIndex += 6;
  165. kerning = 0;
  166. if((charIter + 1) != chars.end())
  167. {
  168. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  169. {
  170. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  171. {
  172. kerning = charIter->kerningPairs[j].amount;
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. return (curVert / 4) - startingQuad;
  181. }
  182. private:
  183. UINT32 mWidth;
  184. vector<TextWord*>::type mWords;
  185. TextWord* mLastWord;
  186. Point mPosition;
  187. void calculateBounds()
  188. {
  189. mWidth = 0;
  190. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  191. {
  192. mWidth += (*iter)->getWidth();
  193. }
  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()
  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. TextLine* curLine = new TextLine();
  212. vector<TextLine*>::type textLines;
  213. textLines.push_back(curLine);
  214. UINT32 curHeight = fontData->fontDesc.lineHeight;
  215. UINT32 charIdx = 0;
  216. mNumMeshQuads = 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. mNumMeshQuads++;
  236. if(curLine->getWidth() > mWidth)
  237. {
  238. if(mWordWrap)
  239. {
  240. TextWord* lastWord = curLine->removeLastWord();
  241. if(lastWord->isSpacer())
  242. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  243. // No more lines fit vertically so we're done
  244. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  245. break;
  246. curLine = new TextLine();
  247. textLines.push_back(curLine);
  248. curHeight += fontData->fontDesc.lineHeight;
  249. if(!lastWord->isSpacer())
  250. curLine->addWord(lastWord);
  251. }
  252. else
  253. {
  254. // Nothing else we can do, chars don't fit so we are done
  255. break;
  256. }
  257. }
  258. charIdx++;
  259. }
  260. // Calc vertical alignment offset
  261. UINT32 vertDiff = std::max(0U, mHeight - curHeight);
  262. UINT32 vertOffset = 0;
  263. switch(mVertAlign)
  264. {
  265. case TVA_Top:
  266. vertOffset = 0;
  267. break;
  268. case TVA_Bottom:
  269. vertOffset = std::max(0, (INT32)(mHeight - curHeight));
  270. break;
  271. case TVA_Center:
  272. vertOffset = std::max(0, (INT32)(mHeight - curHeight)) / 2;
  273. break;
  274. }
  275. // Calc horizontal alignment offset and set final line positions
  276. Point offset = mOffset + getAnchorOffset();
  277. UINT32 curY = 0;
  278. for(size_t i = 0; i < textLines.size(); i++)
  279. {
  280. UINT32 horzOffset = 0;
  281. switch(mHorzAlign)
  282. {
  283. case THA_Left:
  284. horzOffset = 0;
  285. break;
  286. case THA_Right:
  287. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth()));
  288. break;
  289. case THA_Center:
  290. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth())) / 2;
  291. break;
  292. }
  293. textLines[i]->setPosition(offset + Point(horzOffset, vertOffset + curY));
  294. curY += fontData->fontDesc.lineHeight;
  295. }
  296. // Actually generate a mesh
  297. mVertices = new Vector2[mNumMeshQuads * 4];
  298. mUVs = new Vector2[mNumMeshQuads * 4];
  299. mIndexes = new UINT32[mNumMeshQuads * 6];
  300. UINT32 curFace = 0;
  301. for(size_t i = 0; i < textLines.size(); i++)
  302. {
  303. UINT32 newFaces = textLines[i]->fillBuffer(mVertices, mUVs, mIndexes, curFace, mNumMeshQuads, *fontData);
  304. curFace += newFaces;
  305. }
  306. for(size_t i = 0; i < textLines.size(); i++)
  307. delete textLines[i];
  308. // TODO - Clip the mesh based on mWidth/mHeight
  309. // TODO - How do I implement a scrollable text area without a clip rect?
  310. }
  311. const FontData* TextSprite::getFontData() const
  312. {
  313. if(mFont == nullptr)
  314. return nullptr;
  315. UINT32 nearestSize = mFont->getClosestAvailableSize(mFontSize);
  316. return mFont->getFontDataForSize(nearestSize);
  317. }
  318. }