CmTextSprite.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #include "CmTextSprite.h"
  2. #include "CmDebug.h"
  3. #include "CmFontDesc.h"
  4. #include "CmFont.h"
  5. #include "CmVector2.h"
  6. #include "CmMath.h"
  7. #include "CmGUIMaterialManager.h"
  8. namespace CamelotEngine
  9. {
  10. const int SPACE_CHAR = 32;
  11. class TextWord
  12. {
  13. public:
  14. TextWord(bool spacer)
  15. :mWidth(0), mSpacer(spacer)
  16. { }
  17. void addChar(const CHAR_DESC& desc)
  18. {
  19. mChars.push_back(desc);
  20. calculateWidth();
  21. }
  22. void removeLastChar()
  23. {
  24. if(mChars.size() > 0)
  25. {
  26. mChars.erase(mChars.end() - 1);
  27. calculateWidth();
  28. }
  29. }
  30. UINT32 getWidth() const { return mWidth; }
  31. bool isSpacer() const { return mSpacer; }
  32. const vector<CHAR_DESC>::type& getChars() const { return mChars; }
  33. private:
  34. vector<CHAR_DESC>::type mChars;
  35. UINT32 mWidth;
  36. bool mSpacer;
  37. void calculateWidth()
  38. {
  39. if(mChars.size() == 0)
  40. {
  41. mWidth = 0;
  42. return;
  43. }
  44. mWidth = 0;
  45. UINT32 kerning = 0;
  46. for(size_t i = 0; i < mChars.size() - 1; i++)
  47. {
  48. mWidth += mChars[i].xAdvance + kerning;
  49. kerning = 0;
  50. for(size_t j = 0; j < mChars[i].kerningPairs.size(); j++)
  51. {
  52. if(mChars[i].kerningPairs[j].otherCharId == mChars[i + 1].charId)
  53. {
  54. kerning = mChars[i].kerningPairs[j].amount;
  55. break;
  56. }
  57. }
  58. }
  59. mWidth += mChars[mChars.size() - 1].xAdvance + kerning;
  60. }
  61. };
  62. class TextLine
  63. {
  64. public:
  65. TextLine()
  66. :mWidth(0), mLastWord(nullptr)
  67. {
  68. }
  69. ~TextLine()
  70. {
  71. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  72. delete *iter;
  73. }
  74. void add(const CHAR_DESC& charDesc)
  75. {
  76. if(mLastWord == nullptr)
  77. {
  78. TextWord* newWord = new TextWord(charDesc.charId == SPACE_CHAR);
  79. mLastWord = newWord;
  80. mWords.push_back(mLastWord);
  81. mLastWord->addChar(charDesc);
  82. }
  83. else
  84. {
  85. if(charDesc.charId != SPACE_CHAR)
  86. {
  87. if(mLastWord->isSpacer())
  88. {
  89. TextWord* newWord = new TextWord(false);
  90. mLastWord = newWord;
  91. mWords.push_back(mLastWord);
  92. }
  93. mLastWord->addChar(charDesc);
  94. }
  95. else
  96. {
  97. TextWord* newWord = new TextWord(true); // Each space is counted as its own word, to make certain operations easier
  98. mLastWord = newWord;
  99. mWords.push_back(mLastWord);
  100. mLastWord->addChar(charDesc);
  101. }
  102. }
  103. calculateBounds();
  104. }
  105. void addWord(TextWord* word)
  106. {
  107. mWords.push_back(word);
  108. mLastWord = word;
  109. calculateBounds();
  110. }
  111. TextWord* removeLastWord()
  112. {
  113. if(mWords.size() == 0)
  114. return nullptr;
  115. TextWord* word = mWords[mWords.size() - 1];
  116. mWords.erase(mWords.end() - 1);
  117. if(mWords.size() > 0)
  118. mLastWord = mWords[mWords.size() - 1];
  119. else
  120. mLastWord = nullptr;
  121. calculateBounds();
  122. return word;
  123. }
  124. UINT32 getWidth() const { return mWidth; }
  125. Point getPosition() const { return mPosition; }
  126. void setPosition(const Point& pos) { mPosition = pos; }
  127. void fillBuffer(const vector<SpriteRenderElement>::type& renderElements, vector<UINT32>::type& faceOffsets, const FontData& fontData)
  128. {
  129. UINT32 penX = mPosition.x;
  130. UINT32 baselineY = mPosition.y + fontData.fontDesc.baselineOffset;
  131. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  132. {
  133. if((*wordIter)->isSpacer())
  134. {
  135. penX += fontData.fontDesc.spaceWidth;
  136. }
  137. else
  138. {
  139. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  140. UINT32 kerning = 0;
  141. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  142. {
  143. INT32 curX = penX + charIter->xOffset;
  144. INT32 curY = -((INT32)baselineY - charIter->yOffset);
  145. UINT32 curVert = faceOffsets[charIter->page] * 4;
  146. UINT32 curIndex = faceOffsets[charIter->page] * 6;
  147. const SpriteRenderElement& renderElem = renderElements[charIter->page];
  148. renderElem.vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  149. renderElem.vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  150. renderElem.vertices[curVert + 2] = Vector2((float)curX, (float)curY - (float)charIter->height);
  151. renderElem.vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)curY - (float)charIter->height);
  152. renderElem.uvs[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  153. renderElem.uvs[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  154. renderElem.uvs[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  155. renderElem.uvs[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  156. renderElem.indexes[curIndex + 0] = curVert + 0;
  157. renderElem.indexes[curIndex + 1] = curVert + 1;
  158. renderElem.indexes[curIndex + 2] = curVert + 2;
  159. renderElem.indexes[curIndex + 3] = curVert + 1;
  160. renderElem.indexes[curIndex + 4] = curVert + 3;
  161. renderElem.indexes[curIndex + 5] = curVert + 2;
  162. penX += charIter->xAdvance + kerning;
  163. faceOffsets[charIter->page]++;
  164. kerning = 0;
  165. if((charIter + 1) != chars.end())
  166. {
  167. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  168. {
  169. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  170. {
  171. kerning = charIter->kerningPairs[j].amount;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. private:
  181. UINT32 mWidth;
  182. vector<TextWord*>::type mWords;
  183. TextWord* mLastWord;
  184. Point mPosition;
  185. void calculateBounds()
  186. {
  187. mWidth = 0;
  188. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  189. {
  190. mWidth += (*iter)->getWidth();
  191. }
  192. }
  193. };
  194. TextSprite::TextSprite()
  195. :mFontSize(0), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  196. {
  197. }
  198. TextSprite::TextSprite(const String& text, FontPtr font, UINT32 fontSize)
  199. :mText(text), mFont(font), mFontSize(fontSize), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  200. {
  201. }
  202. void TextSprite::updateMesh() const
  203. {
  204. const FontData* fontData = getFontData();
  205. clearMesh();
  206. if(fontData == nullptr)
  207. return;
  208. if(fontData->size != mFontSize)
  209. {
  210. LOGWRN("Unable to find font with specified size (" + toString(mFontSize) + "). Using nearest available size: " + toString(fontData->size));
  211. }
  212. bool heightIsLimited = mHeight > 0;
  213. bool widthIsLimited = mWidth > 0;
  214. TextLine* curLine = new TextLine();
  215. vector<TextLine*>::type textLines;
  216. textLines.push_back(curLine);
  217. UINT32 curHeight = fontData->fontDesc.lineHeight;
  218. UINT32 charIdx = 0;
  219. while(true)
  220. {
  221. if(charIdx >= mText.size())
  222. break;
  223. if(mText[charIdx] == '\n')
  224. {
  225. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  226. break; // Max height reached
  227. curLine = new TextLine();
  228. textLines.push_back(curLine);
  229. curHeight += fontData->fontDesc.lineHeight;
  230. charIdx++;
  231. continue;
  232. }
  233. UINT32 charId = mText[charIdx];
  234. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  235. curLine->add(charDesc);
  236. if(charDesc.charId != SPACE_CHAR)
  237. {
  238. if(charDesc.page >= (UINT32)mCachedRenderElements.size())
  239. mCachedRenderElements.resize(charDesc.page + 1);
  240. mCachedRenderElements[charDesc.page].numQuads++;
  241. }
  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. UINT32 texPage = 0;
  304. for(auto& renderElem : mCachedRenderElements)
  305. {
  306. renderElem.vertices = new Vector2[renderElem.numQuads * 4];
  307. renderElem.uvs = new Vector2[renderElem.numQuads * 4];
  308. renderElem.indexes = new UINT32[renderElem.numQuads * 6];
  309. renderElem.material = GUIMaterialManager::instance().requestTextMaterial(fontData->texturePages[texPage]);
  310. texPage++;
  311. }
  312. vector<UINT32>::type faceOffsets(mCachedRenderElements.size(), 0);
  313. for(size_t i = 0; i < textLines.size(); i++)
  314. textLines[i]->fillBuffer(mCachedRenderElements, faceOffsets, *fontData);
  315. for(size_t i = 0; i < textLines.size(); i++)
  316. delete textLines[i];
  317. if(isClipRectangleValid())
  318. {
  319. for(auto& renderElem : mCachedRenderElements)
  320. {
  321. clipToRect(renderElem.vertices, renderElem.uvs, renderElem.numQuads, mClipRect);
  322. }
  323. }
  324. }
  325. const FontData* TextSprite::getFontData() const
  326. {
  327. if(mFont == nullptr)
  328. return nullptr;
  329. UINT32 nearestSize = mFont->getClosestAvailableSize(mFontSize);
  330. return mFont->getFontDataForSize(nearestSize);
  331. }
  332. // This will only properly clip an array of rectangular quads
  333. // Vertices in the quad must be in a specific order: top left, top right, bottom left, bottom right
  334. // (0, 0) represents top left of the screen
  335. void TextSprite::clipToRect(Vector2* vertices, Vector2* uv, UINT32 numQuads, Rect clipRect) const
  336. {
  337. float left = (float)clipRect.x;
  338. float right = (float)clipRect.x + clipRect.width;
  339. float top = (float)clipRect.y;
  340. float bottom = (float)clipRect.y - clipRect.height;
  341. for(UINT32 i = 0; i < numQuads; i++)
  342. {
  343. UINT32 vertIdx = i * 4;
  344. // Attempt to skip those that are definitely not clipped
  345. if(vertices[vertIdx + 0].x >= left && vertices[vertIdx + 1].x <= right &&
  346. vertices[vertIdx + 0].y <= top && vertices[vertIdx + 2].y >= bottom)
  347. {
  348. continue;
  349. }
  350. float du = (uv[vertIdx + 1].x - uv[vertIdx + 0].x) / (vertices[vertIdx + 1].x - vertices[vertIdx + 0].x);
  351. float dv = (uv[vertIdx + 0].y - uv[vertIdx + 2].y) / (vertices[vertIdx + 0].y - vertices[vertIdx + 2].y);
  352. // Clip left
  353. float newLeft = Math::Clamp(vertices[vertIdx + 0].x, left, right);
  354. float uvLeftOffset = (newLeft - vertices[vertIdx + 0].x) * du;
  355. vertices[vertIdx + 0].x = newLeft;
  356. vertices[vertIdx + 2].x = newLeft;
  357. uv[vertIdx + 0].x += uvLeftOffset;
  358. uv[vertIdx + 2].x += uvLeftOffset;
  359. // Clip right
  360. float newRight = Math::Clamp(vertices[vertIdx + 1].x, left, right);
  361. float uvRightOffset = (vertices[vertIdx + 1].x - newRight) * du;
  362. vertices[vertIdx + 1].x = newRight;
  363. vertices[vertIdx + 3].x = newRight;
  364. uv[vertIdx + 1].x -= uvRightOffset;
  365. uv[vertIdx + 3].x -= uvRightOffset;
  366. // Clip top
  367. float newTop = Math::Clamp(vertices[vertIdx + 0].y, bottom, top);
  368. float uvTopOffset = (vertices[vertIdx + 0].y - newTop) * dv;
  369. vertices[vertIdx + 0].y = newTop;
  370. vertices[vertIdx + 1].y = newTop;
  371. uv[vertIdx + 0].y -= uvTopOffset;
  372. uv[vertIdx + 1].y -= uvTopOffset;
  373. // Clip bottom
  374. float newBottom = Math::Clamp(vertices[vertIdx + 2].y, bottom, top);
  375. float uvBottomOffset = (newBottom - vertices[vertIdx + 2].y) * dv;
  376. vertices[vertIdx + 2].y = newBottom;
  377. vertices[vertIdx + 3].y = newBottom;
  378. uv[vertIdx + 2].y += uvBottomOffset;
  379. uv[vertIdx + 3].y += uvBottomOffset;
  380. }
  381. }
  382. }