CmTextSprite.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. if(fontData == nullptr)
  206. {
  207. clearMesh();
  208. return;
  209. }
  210. if(fontData->size != mFontSize)
  211. {
  212. LOGWRN("Unable to find font with specified size (" + toString(mFontSize) + "). Using nearest available size: " + toString(fontData->size));
  213. }
  214. bool heightIsLimited = mHeight > 0;
  215. bool widthIsLimited = mWidth > 0;
  216. TextLine* curLine = new TextLine();
  217. vector<TextLine*>::type textLines;
  218. textLines.push_back(curLine);
  219. UINT32 curHeight = fontData->fontDesc.lineHeight;
  220. UINT32 charIdx = 0;
  221. vector<UINT32>::type newRenderElemSizes;
  222. while(true)
  223. {
  224. if(charIdx >= mText.size())
  225. break;
  226. if(mText[charIdx] == '\n')
  227. {
  228. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  229. break; // Max height reached
  230. curLine = new TextLine();
  231. textLines.push_back(curLine);
  232. curHeight += fontData->fontDesc.lineHeight;
  233. charIdx++;
  234. continue;
  235. }
  236. UINT32 charId = mText[charIdx];
  237. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  238. curLine->add(charDesc);
  239. if(charDesc.charId != SPACE_CHAR)
  240. {
  241. if(charDesc.page >= (UINT32)newRenderElemSizes.size())
  242. newRenderElemSizes.resize(charDesc.page + 1);
  243. newRenderElemSizes[charDesc.page]++;
  244. }
  245. if(widthIsLimited && curLine->getWidth() > mWidth)
  246. {
  247. if(mWordWrap)
  248. {
  249. TextWord* lastWord = curLine->removeLastWord();
  250. if(lastWord->isSpacer())
  251. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  252. // No more lines fit vertically so we're done
  253. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  254. break;
  255. curLine = new TextLine();
  256. textLines.push_back(curLine);
  257. curHeight += fontData->fontDesc.lineHeight;
  258. if(!lastWord->isSpacer())
  259. curLine->addWord(lastWord);
  260. }
  261. else
  262. {
  263. // Nothing else we can do, chars don't fit so we are done
  264. break;
  265. }
  266. }
  267. charIdx++;
  268. }
  269. // Calc vertical alignment offset
  270. UINT32 vertDiff = std::max(0U, mHeight - curHeight);
  271. UINT32 vertOffset = 0;
  272. switch(mVertAlign)
  273. {
  274. case TVA_Top:
  275. vertOffset = 0;
  276. break;
  277. case TVA_Bottom:
  278. vertOffset = std::max(0, (INT32)(mHeight - curHeight));
  279. break;
  280. case TVA_Center:
  281. vertOffset = std::max(0, (INT32)(mHeight - curHeight)) / 2;
  282. break;
  283. }
  284. // Calc horizontal alignment offset and set final line positions
  285. Point offset = mOffset + getAnchorOffset();
  286. UINT32 curY = 0;
  287. for(size_t i = 0; i < textLines.size(); i++)
  288. {
  289. UINT32 horzOffset = 0;
  290. switch(mHorzAlign)
  291. {
  292. case THA_Left:
  293. horzOffset = 0;
  294. break;
  295. case THA_Right:
  296. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth()));
  297. break;
  298. case THA_Center:
  299. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth())) / 2;
  300. break;
  301. }
  302. textLines[i]->setPosition(offset + Point(horzOffset, vertOffset + curY));
  303. curY += fontData->fontDesc.lineHeight;
  304. }
  305. // Actually generate a mesh
  306. if(mCachedRenderElements.size() < newRenderElemSizes.size())
  307. mCachedRenderElements.resize(newRenderElemSizes.size());
  308. UINT32 texPage = 0;
  309. for(auto& cachedElem : mCachedRenderElements)
  310. {
  311. UINT32 newNumQuads = newRenderElemSizes[texPage];
  312. if(newNumQuads != cachedElem.numQuads)
  313. {
  314. if(cachedElem.vertices != nullptr) delete[] cachedElem.vertices;
  315. if(cachedElem.uvs != nullptr) delete[] cachedElem.uvs;
  316. if(cachedElem.indexes != nullptr) delete[] cachedElem.indexes;
  317. cachedElem.vertices = new Vector2[newNumQuads * 4];
  318. cachedElem.uvs = new Vector2[newNumQuads * 4];
  319. cachedElem.indexes = new UINT32[newNumQuads * 6];
  320. cachedElem.numQuads = newNumQuads;
  321. }
  322. MaterialHandle newMaterial = GUIMaterialManager::instance().requestTextMaterial(fontData->texturePages[texPage]);
  323. if(cachedElem.material != nullptr)
  324. GUIMaterialManager::instance().releaseMaterial(newMaterial);
  325. cachedElem.material = newMaterial;
  326. texPage++;
  327. }
  328. vector<UINT32>::type faceOffsets(mCachedRenderElements.size(), 0);
  329. for(size_t i = 0; i < textLines.size(); i++)
  330. textLines[i]->fillBuffer(mCachedRenderElements, faceOffsets, *fontData);
  331. for(size_t i = 0; i < textLines.size(); i++)
  332. delete textLines[i];
  333. if(isClipRectangleValid())
  334. {
  335. for(auto& renderElem : mCachedRenderElements)
  336. {
  337. clipToRect(renderElem.vertices, renderElem.uvs, renderElem.numQuads, mClipRect);
  338. }
  339. }
  340. }
  341. const FontData* TextSprite::getFontData() const
  342. {
  343. if(mFont == nullptr)
  344. return nullptr;
  345. UINT32 nearestSize = mFont->getClosestAvailableSize(mFontSize);
  346. return mFont->getFontDataForSize(nearestSize);
  347. }
  348. // This will only properly clip an array of rectangular quads
  349. // Vertices in the quad must be in a specific order: top left, top right, bottom left, bottom right
  350. // (0, 0) represents top left of the screen
  351. void TextSprite::clipToRect(Vector2* vertices, Vector2* uv, UINT32 numQuads, Rect clipRect) const
  352. {
  353. float left = (float)clipRect.x;
  354. float right = (float)clipRect.x + clipRect.width;
  355. float top = (float)clipRect.y;
  356. float bottom = (float)clipRect.y - clipRect.height;
  357. for(UINT32 i = 0; i < numQuads; i++)
  358. {
  359. UINT32 vertIdx = i * 4;
  360. // Attempt to skip those that are definitely not clipped
  361. if(vertices[vertIdx + 0].x >= left && vertices[vertIdx + 1].x <= right &&
  362. vertices[vertIdx + 0].y <= top && vertices[vertIdx + 2].y >= bottom)
  363. {
  364. continue;
  365. }
  366. float du = (uv[vertIdx + 1].x - uv[vertIdx + 0].x) / (vertices[vertIdx + 1].x - vertices[vertIdx + 0].x);
  367. float dv = (uv[vertIdx + 0].y - uv[vertIdx + 2].y) / (vertices[vertIdx + 0].y - vertices[vertIdx + 2].y);
  368. // Clip left
  369. float newLeft = Math::Clamp(vertices[vertIdx + 0].x, left, right);
  370. float uvLeftOffset = (newLeft - vertices[vertIdx + 0].x) * du;
  371. vertices[vertIdx + 0].x = newLeft;
  372. vertices[vertIdx + 2].x = newLeft;
  373. uv[vertIdx + 0].x += uvLeftOffset;
  374. uv[vertIdx + 2].x += uvLeftOffset;
  375. // Clip right
  376. float newRight = Math::Clamp(vertices[vertIdx + 1].x, left, right);
  377. float uvRightOffset = (vertices[vertIdx + 1].x - newRight) * du;
  378. vertices[vertIdx + 1].x = newRight;
  379. vertices[vertIdx + 3].x = newRight;
  380. uv[vertIdx + 1].x -= uvRightOffset;
  381. uv[vertIdx + 3].x -= uvRightOffset;
  382. // Clip top
  383. float newTop = Math::Clamp(vertices[vertIdx + 0].y, bottom, top);
  384. float uvTopOffset = (vertices[vertIdx + 0].y - newTop) * dv;
  385. vertices[vertIdx + 0].y = newTop;
  386. vertices[vertIdx + 1].y = newTop;
  387. uv[vertIdx + 0].y -= uvTopOffset;
  388. uv[vertIdx + 1].y -= uvTopOffset;
  389. // Clip bottom
  390. float newBottom = Math::Clamp(vertices[vertIdx + 2].y, bottom, top);
  391. float uvBottomOffset = (newBottom - vertices[vertIdx + 2].y) * dv;
  392. vertices[vertIdx + 2].y = newBottom;
  393. vertices[vertIdx + 3].y = newBottom;
  394. uv[vertIdx + 2].y += uvBottomOffset;
  395. uv[vertIdx + 3].y += uvBottomOffset;
  396. }
  397. }
  398. }