CmTextSprite.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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), mSpaceWidth(0)
  16. { }
  17. void addChar(const CHAR_DESC& desc)
  18. {
  19. mChars.push_back(desc);
  20. calculateWidth();
  21. }
  22. void addSpace(UINT32 spaceWidth)
  23. {
  24. mSpaceWidth += spaceWidth;
  25. }
  26. void removeLastChar()
  27. {
  28. if(mChars.size() > 0)
  29. {
  30. mChars.erase(mChars.end() - 1);
  31. calculateWidth();
  32. }
  33. }
  34. UINT32 getWidth() const { return mWidth; }
  35. bool isSpacer() const { return mSpacer; }
  36. const vector<CHAR_DESC>::type& getChars() const { return mChars; }
  37. private:
  38. vector<CHAR_DESC>::type mChars;
  39. UINT32 mWidth;
  40. bool mSpacer;
  41. UINT32 mSpaceWidth;
  42. void calculateWidth()
  43. {
  44. if(isSpacer())
  45. {
  46. mWidth = mSpaceWidth;
  47. return;
  48. }
  49. if(mChars.size() == 0)
  50. {
  51. mWidth = 0;
  52. return;
  53. }
  54. mWidth = 0;
  55. UINT32 kerning = 0;
  56. for(size_t i = 0; i < mChars.size() - 1; i++)
  57. {
  58. mWidth += mChars[i].xAdvance + kerning;
  59. kerning = 0;
  60. for(size_t j = 0; j < mChars[i].kerningPairs.size(); j++)
  61. {
  62. if(mChars[i].kerningPairs[j].otherCharId == mChars[i + 1].charId)
  63. {
  64. kerning = mChars[i].kerningPairs[j].amount;
  65. break;
  66. }
  67. }
  68. }
  69. mWidth += mChars[mChars.size() - 1].xAdvance + kerning;
  70. }
  71. };
  72. class TextLine
  73. {
  74. public:
  75. TextLine()
  76. :mWidth(0), mLastWord(nullptr)
  77. {
  78. }
  79. ~TextLine()
  80. {
  81. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  82. CM_DELETE(*iter, TextWord, ScratchAlloc);
  83. }
  84. void add(const CHAR_DESC& charDesc)
  85. {
  86. if(mLastWord == nullptr)
  87. {
  88. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(false);
  89. mLastWord = newWord;
  90. mWords.push_back(mLastWord);
  91. mLastWord->addChar(charDesc);
  92. }
  93. else
  94. {
  95. if(mLastWord->isSpacer())
  96. {
  97. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(false);
  98. mLastWord = newWord;
  99. mWords.push_back(mLastWord);
  100. }
  101. mLastWord->addChar(charDesc);
  102. }
  103. calculateBounds();
  104. }
  105. void addSpace(UINT32 spaceWidth)
  106. {
  107. if(mLastWord == nullptr)
  108. {
  109. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(true);
  110. mLastWord = newWord;
  111. mWords.push_back(mLastWord);
  112. mLastWord->addSpace(spaceWidth);
  113. }
  114. else
  115. {
  116. TextWord* newWord = CM_NEW(TextWord, ScratchAlloc) TextWord(true); // Each space is counted as its own word, to make certain operations easier
  117. mLastWord = newWord;
  118. mWords.push_back(mLastWord);
  119. mLastWord->addSpace(spaceWidth);
  120. }
  121. calculateBounds();
  122. }
  123. void addWord(TextWord* word)
  124. {
  125. mWords.push_back(word);
  126. mLastWord = word;
  127. calculateBounds();
  128. }
  129. TextWord* removeLastWord()
  130. {
  131. if(mWords.size() == 0)
  132. return nullptr;
  133. TextWord* word = mWords[mWords.size() - 1];
  134. mWords.erase(mWords.end() - 1);
  135. if(mWords.size() > 0)
  136. mLastWord = mWords[mWords.size() - 1];
  137. else
  138. mLastWord = nullptr;
  139. calculateBounds();
  140. return word;
  141. }
  142. UINT32 getWidth() const { return mWidth; }
  143. Point getPosition() const { return mPosition; }
  144. void setPosition(const Point& pos) { mPosition = pos; }
  145. void fillBuffer(const vector<SpriteRenderElement>::type& renderElements, vector<UINT32>::type& faceOffsets, const FontData& fontData)
  146. {
  147. UINT32 penX = mPosition.x;
  148. UINT32 baselineY = mPosition.y + fontData.fontDesc.baselineOffset;
  149. for(auto wordIter = mWords.begin(); wordIter != mWords.end(); ++wordIter)
  150. {
  151. if((*wordIter)->isSpacer())
  152. {
  153. penX += fontData.fontDesc.spaceWidth;
  154. }
  155. else
  156. {
  157. const vector<CHAR_DESC>::type& chars = (*wordIter)->getChars();
  158. UINT32 kerning = 0;
  159. for(auto charIter = chars.begin(); charIter != chars.end(); ++charIter)
  160. {
  161. INT32 curX = penX + charIter->xOffset;
  162. INT32 curY = -((INT32)baselineY - charIter->yOffset);
  163. UINT32 curVert = faceOffsets[charIter->page] * 4;
  164. UINT32 curIndex = faceOffsets[charIter->page] * 6;
  165. const SpriteRenderElement& renderElem = renderElements[charIter->page];
  166. renderElem.vertices[curVert + 0] = Vector2((float)curX, (float)curY);
  167. renderElem.vertices[curVert + 1] = Vector2((float)(curX + charIter->width), (float)curY);
  168. renderElem.vertices[curVert + 2] = Vector2((float)curX, (float)curY - (float)charIter->height);
  169. renderElem.vertices[curVert + 3] = Vector2((float)(curX + charIter->width), (float)curY - (float)charIter->height);
  170. renderElem.uvs[curVert + 0] = Vector2(charIter->uvX, charIter->uvY);
  171. renderElem.uvs[curVert + 1] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY);
  172. renderElem.uvs[curVert + 2] = Vector2(charIter->uvX, charIter->uvY + charIter->uvHeight);
  173. renderElem.uvs[curVert + 3] = Vector2(charIter->uvX + charIter->uvWidth, charIter->uvY + charIter->uvHeight);
  174. renderElem.indexes[curIndex + 0] = curVert + 0;
  175. renderElem.indexes[curIndex + 1] = curVert + 1;
  176. renderElem.indexes[curIndex + 2] = curVert + 2;
  177. renderElem.indexes[curIndex + 3] = curVert + 1;
  178. renderElem.indexes[curIndex + 4] = curVert + 3;
  179. renderElem.indexes[curIndex + 5] = curVert + 2;
  180. penX += charIter->xAdvance + kerning;
  181. faceOffsets[charIter->page]++;
  182. kerning = 0;
  183. if((charIter + 1) != chars.end())
  184. {
  185. for(size_t j = 0; j < charIter->kerningPairs.size(); j++)
  186. {
  187. if(charIter->kerningPairs[j].otherCharId == (charIter + 1)->charId)
  188. {
  189. kerning = charIter->kerningPairs[j].amount;
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. private:
  199. UINT32 mWidth;
  200. vector<TextWord*>::type mWords;
  201. TextWord* mLastWord;
  202. Point mPosition;
  203. void calculateBounds()
  204. {
  205. mWidth = 0;
  206. for(auto iter = mWords.begin(); iter != mWords.end(); ++iter)
  207. {
  208. mWidth += (*iter)->getWidth();
  209. }
  210. }
  211. };
  212. TextSprite::TextSprite()
  213. :mFontSize(0), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  214. {
  215. }
  216. TextSprite::TextSprite(const String& text, HFont font, UINT32 fontSize)
  217. :mText(text), mFont(font), mFontSize(fontSize), mWordWrap(false), mHorzAlign(THA_Left), mVertAlign(TVA_Top)
  218. {
  219. }
  220. void TextSprite::updateMesh() const
  221. {
  222. const FontData* fontData = getFontData();
  223. if(fontData == nullptr)
  224. {
  225. clearMesh();
  226. return;
  227. }
  228. if(fontData->size != mFontSize)
  229. {
  230. LOGWRN("Unable to find font with specified size (" + toString(mFontSize) + "). Using nearest available size: " + toString(fontData->size));
  231. }
  232. bool heightIsLimited = mHeight > 0;
  233. bool widthIsLimited = mWidth > 0;
  234. TextLine* curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
  235. vector<TextLine*>::type textLines;
  236. textLines.push_back(curLine);
  237. UINT32 curHeight = fontData->fontDesc.lineHeight;
  238. UINT32 charIdx = 0;
  239. vector<UINT32>::type newRenderElemSizes;
  240. while(true)
  241. {
  242. if(charIdx >= mText.size())
  243. break;
  244. if(mText[charIdx] == '\n')
  245. {
  246. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  247. break; // Max height reached
  248. curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
  249. textLines.push_back(curLine);
  250. curHeight += fontData->fontDesc.lineHeight;
  251. charIdx++;
  252. continue;
  253. }
  254. UINT32 charId = mText[charIdx];
  255. const CHAR_DESC& charDesc = fontData->getCharDesc(charId);
  256. if(charId != SPACE_CHAR)
  257. {
  258. curLine->add(charDesc);
  259. if(charDesc.page >= (UINT32)newRenderElemSizes.size())
  260. newRenderElemSizes.resize(charDesc.page + 1);
  261. newRenderElemSizes[charDesc.page]++;
  262. }
  263. else
  264. curLine->addSpace(fontData->fontDesc.spaceWidth);
  265. if(widthIsLimited && curLine->getWidth() > mWidth)
  266. {
  267. if(mWordWrap)
  268. {
  269. TextWord* lastWord = curLine->removeLastWord();
  270. if(lastWord->isSpacer())
  271. curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
  272. // No more lines fit vertically so we're done
  273. if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
  274. break;
  275. curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
  276. textLines.push_back(curLine);
  277. curHeight += fontData->fontDesc.lineHeight;
  278. if(!lastWord->isSpacer())
  279. curLine->addWord(lastWord);
  280. }
  281. else
  282. {
  283. // Nothing else we can do, chars don't fit so we are done
  284. break;
  285. }
  286. }
  287. charIdx++;
  288. }
  289. // Calc vertical alignment offset
  290. UINT32 vertDiff = std::max(0U, mHeight - curHeight);
  291. UINT32 vertOffset = 0;
  292. switch(mVertAlign)
  293. {
  294. case TVA_Top:
  295. vertOffset = 0;
  296. break;
  297. case TVA_Bottom:
  298. vertOffset = std::max(0, (INT32)(mHeight - curHeight));
  299. break;
  300. case TVA_Center:
  301. vertOffset = std::max(0, (INT32)(mHeight - curHeight)) / 2;
  302. break;
  303. }
  304. // Calc horizontal alignment offset and set final line positions
  305. Point offset = mOffset + getAnchorOffset();
  306. UINT32 curY = 0;
  307. for(size_t i = 0; i < textLines.size(); i++)
  308. {
  309. UINT32 horzOffset = 0;
  310. switch(mHorzAlign)
  311. {
  312. case THA_Left:
  313. horzOffset = 0;
  314. break;
  315. case THA_Right:
  316. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth()));
  317. break;
  318. case THA_Center:
  319. horzOffset = std::max(0, (INT32)(mWidth - textLines[i]->getWidth())) / 2;
  320. break;
  321. }
  322. textLines[i]->setPosition(offset + Point(horzOffset, vertOffset + curY));
  323. curY += fontData->fontDesc.lineHeight;
  324. }
  325. // Actually generate a mesh
  326. if(mCachedRenderElements.size() < newRenderElemSizes.size())
  327. mCachedRenderElements.resize(newRenderElemSizes.size());
  328. UINT32 texPage = 0;
  329. for(auto& cachedElem : mCachedRenderElements)
  330. {
  331. UINT32 newNumQuads = newRenderElemSizes[texPage];
  332. if(newNumQuads != cachedElem.numQuads)
  333. {
  334. UINT32 oldVertexCount = cachedElem.numQuads * 4;
  335. UINT32 oldIndexCount = cachedElem.numQuads * 6;
  336. if(cachedElem.vertices != nullptr) CM_DELETE_ARRAY(cachedElem.vertices, Vector2, oldVertexCount, ScratchAlloc);
  337. if(cachedElem.uvs != nullptr) CM_DELETE_ARRAY(cachedElem.uvs, Vector2, oldVertexCount, ScratchAlloc);
  338. if(cachedElem.indexes != nullptr) CM_DELETE_ARRAY(cachedElem.indexes, UINT32, oldIndexCount, ScratchAlloc);
  339. cachedElem.vertices = CM_NEW_ARRAY(Vector2, newNumQuads * 4, ScratchAlloc);
  340. cachedElem.uvs = CM_NEW_ARRAY(Vector2, newNumQuads * 4, ScratchAlloc);
  341. cachedElem.indexes = CM_NEW_ARRAY(UINT32, newNumQuads * 6, ScratchAlloc);
  342. cachedElem.numQuads = newNumQuads;
  343. }
  344. HMaterial newMaterial = GUIMaterialManager::instance().requestTextMaterial(fontData->texturePages[texPage]);
  345. if(cachedElem.material != nullptr)
  346. GUIMaterialManager::instance().releaseMaterial(newMaterial);
  347. cachedElem.material = newMaterial;
  348. texPage++;
  349. }
  350. vector<UINT32>::type faceOffsets(mCachedRenderElements.size(), 0);
  351. for(size_t i = 0; i < textLines.size(); i++)
  352. textLines[i]->fillBuffer(mCachedRenderElements, faceOffsets, *fontData);
  353. for(size_t i = 0; i < textLines.size(); i++)
  354. CM_DELETE(textLines[i], TextLine, ScratchAlloc);
  355. if(isClipRectangleValid())
  356. {
  357. for(auto& renderElem : mCachedRenderElements)
  358. {
  359. clipToRect(renderElem.vertices, renderElem.uvs, renderElem.numQuads, mClipRect);
  360. }
  361. }
  362. }
  363. const FontData* TextSprite::getFontData() const
  364. {
  365. if(mFont == nullptr)
  366. return nullptr;
  367. UINT32 nearestSize = mFont->getClosestAvailableSize(mFontSize);
  368. return mFont->getFontDataForSize(nearestSize);
  369. }
  370. // This will only properly clip an array of rectangular quads
  371. // Vertices in the quad must be in a specific order: top left, top right, bottom left, bottom right
  372. // (0, 0) represents top left of the screen
  373. void TextSprite::clipToRect(Vector2* vertices, Vector2* uv, UINT32 numQuads, Rect clipRect) const
  374. {
  375. float left = (float)clipRect.x;
  376. float right = (float)clipRect.x + clipRect.width;
  377. float top = (float)clipRect.y;
  378. float bottom = (float)clipRect.y - clipRect.height;
  379. for(UINT32 i = 0; i < numQuads; i++)
  380. {
  381. UINT32 vertIdx = i * 4;
  382. // Attempt to skip those that are definitely not clipped
  383. if(vertices[vertIdx + 0].x >= left && vertices[vertIdx + 1].x <= right &&
  384. vertices[vertIdx + 0].y <= top && vertices[vertIdx + 2].y >= bottom)
  385. {
  386. continue;
  387. }
  388. float du = (uv[vertIdx + 1].x - uv[vertIdx + 0].x) / (vertices[vertIdx + 1].x - vertices[vertIdx + 0].x);
  389. float dv = (uv[vertIdx + 0].y - uv[vertIdx + 2].y) / (vertices[vertIdx + 0].y - vertices[vertIdx + 2].y);
  390. // Clip left
  391. float newLeft = Math::Clamp(vertices[vertIdx + 0].x, left, right);
  392. float uvLeftOffset = (newLeft - vertices[vertIdx + 0].x) * du;
  393. vertices[vertIdx + 0].x = newLeft;
  394. vertices[vertIdx + 2].x = newLeft;
  395. uv[vertIdx + 0].x += uvLeftOffset;
  396. uv[vertIdx + 2].x += uvLeftOffset;
  397. // Clip right
  398. float newRight = Math::Clamp(vertices[vertIdx + 1].x, left, right);
  399. float uvRightOffset = (vertices[vertIdx + 1].x - newRight) * du;
  400. vertices[vertIdx + 1].x = newRight;
  401. vertices[vertIdx + 3].x = newRight;
  402. uv[vertIdx + 1].x -= uvRightOffset;
  403. uv[vertIdx + 3].x -= uvRightOffset;
  404. // Clip top
  405. float newTop = Math::Clamp(vertices[vertIdx + 0].y, bottom, top);
  406. float uvTopOffset = (vertices[vertIdx + 0].y - newTop) * dv;
  407. vertices[vertIdx + 0].y = newTop;
  408. vertices[vertIdx + 1].y = newTop;
  409. uv[vertIdx + 0].y -= uvTopOffset;
  410. uv[vertIdx + 1].y -= uvTopOffset;
  411. // Clip bottom
  412. float newBottom = Math::Clamp(vertices[vertIdx + 2].y, bottom, top);
  413. float uvBottomOffset = (newBottom - vertices[vertIdx + 2].y) * dv;
  414. vertices[vertIdx + 2].y = newBottom;
  415. vertices[vertIdx + 3].y = newBottom;
  416. uv[vertIdx + 2].y += uvBottomOffset;
  417. uv[vertIdx + 3].y += uvBottomOffset;
  418. }
  419. }
  420. }