Font.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Font.cpp
  3. */
  4. #include "Base.h"
  5. #include "Font.h"
  6. #include "Game.h"
  7. #include "FileSystem.h"
  8. #include "Package.h"
  9. // Default font vertex shader
  10. #define FONT_VSH \
  11. "uniform mat4 u_projectionMatrix;\n" \
  12. "attribute vec3 a_position;\n" \
  13. "attribute vec2 a_texcoord;\n" \
  14. "attribute vec4 a_color;\n" \
  15. "varying vec2 v_texcoord;\n" \
  16. "varying vec4 v_color;\n" \
  17. "void main()\n" \
  18. "{\n" \
  19. "gl_Position = u_projectionMatrix * vec4(a_position, 1);\n" \
  20. "v_texcoord = a_texcoord;\n" \
  21. "v_color = a_color;\n" \
  22. "}\n"
  23. // Default font fragment shader
  24. #define FONT_FSH \
  25. "#ifdef OPENGL_ES\n" \
  26. "precision highp float;\n" \
  27. "#endif\n" \
  28. "varying vec2 v_texcoord;\n" \
  29. "varying vec4 v_color;\n" \
  30. "uniform sampler2D u_texture;\n" \
  31. "void main()\n" \
  32. "{\n" \
  33. "gl_FragColor = v_color;\n" \
  34. "gl_FragColor.a = texture2D(u_texture, v_texcoord).a;\n" \
  35. "}"
  36. namespace gameplay
  37. {
  38. static std::vector<Font*> __fontCache;
  39. static Effect* __fontEffect = NULL;
  40. Font::Font() :
  41. _style(PLAIN), _size(0), _glyphs(NULL), _glyphCount(0), _texture(NULL), _batch(NULL)
  42. {
  43. }
  44. Font::Font(const Font& copy)
  45. {
  46. // hidden
  47. }
  48. Font::~Font()
  49. {
  50. // Remove this Font from the font cache.
  51. std::vector<Font*>::iterator itr = find(__fontCache.begin(), __fontCache.end(), this);
  52. if (itr != __fontCache.end())
  53. {
  54. __fontCache.erase(itr);
  55. }
  56. SAFE_DELETE(_batch);
  57. SAFE_DELETE_ARRAY(_glyphs);
  58. SAFE_RELEASE(_texture);
  59. }
  60. Font* Font::create(const char* path, const char* id)
  61. {
  62. // Search the font cache for a font with the given path and ID.
  63. for (unsigned int i = 0, count = __fontCache.size(); i < count; ++i)
  64. {
  65. Font* f = __fontCache[i];
  66. if (f->_path == path && (id == NULL || f->_id == id))
  67. {
  68. // Found a match.
  69. f->addRef();
  70. return f;
  71. }
  72. }
  73. // Load the package.
  74. Package* pkg = Package::create(path);
  75. if (pkg == NULL)
  76. {
  77. return NULL;
  78. }
  79. Font* font = NULL;
  80. if (id == NULL)
  81. {
  82. // Get the ID of the first/only object in the package (assume it's a Font).
  83. const char* id;
  84. if (pkg->getObjectCount() != 1 || (id = pkg->getObjectID(0)) == NULL)
  85. {
  86. return NULL;
  87. }
  88. // Load the font using the ID of the first object in the package.
  89. font = pkg->loadFont(pkg->getObjectID(0));
  90. }
  91. else
  92. {
  93. // Load the font with the given ID.
  94. font = pkg->loadFont(id);
  95. }
  96. if (font)
  97. {
  98. // Add this font to the cache.
  99. __fontCache.push_back(font);
  100. }
  101. SAFE_RELEASE(pkg);
  102. return font;
  103. }
  104. Font* Font::create(const char* family, Style style, unsigned int size, Glyph* glyphs, int glyphCount, Texture* texture)
  105. {
  106. // Create the effect for the font's sprite batch.
  107. if (__fontEffect == NULL)
  108. {
  109. __fontEffect = Effect::createFromSource(FONT_VSH, FONT_FSH);
  110. if (__fontEffect == NULL)
  111. {
  112. LOG_ERROR("Failed to create effect for font.");
  113. SAFE_RELEASE(texture);
  114. return NULL;
  115. }
  116. }
  117. else
  118. {
  119. __fontEffect->addRef();
  120. }
  121. // Create batch for the font.
  122. SpriteBatch* batch = SpriteBatch::create(texture, __fontEffect, 128);
  123. // Release __fontEffect since the SpriteBatch keeps a reference to it
  124. SAFE_RELEASE(__fontEffect);
  125. if (batch == NULL)
  126. {
  127. LOG_ERROR("Failed to create batch for font.");
  128. return NULL;
  129. }
  130. // Increase the ref count of the texture to retain it.
  131. texture->addRef();
  132. Font* font = new Font();
  133. font->_family = family;
  134. font->_style = style;
  135. font->_size = size;
  136. font->_texture = texture;
  137. font->_batch = batch;
  138. // Copy the glyphs array.
  139. font->_glyphs = new Glyph[glyphCount];
  140. memcpy(font->_glyphs, glyphs, sizeof(Glyph) * glyphCount);
  141. font->_glyphCount = glyphCount;
  142. return font;
  143. }
  144. void Font::begin()
  145. {
  146. _batch->begin();
  147. }
  148. void Font::drawText(const char* text, int x, int y, const Vector4& color, float scale, bool rightToLeft)
  149. {
  150. char* rightToLeftText = NULL;
  151. if (rightToLeft)
  152. {
  153. rightToLeftText = new char[strlen(text) + 1];
  154. strcpy(rightToLeftText, text);
  155. reverseLines(rightToLeftText);
  156. }
  157. const int length = strlen(text);
  158. const int size = (int)_size * scale;
  159. int xPos = x, yPos = y;
  160. for (int i = 0; i < length; ++i)
  161. {
  162. char c = 0;
  163. if (rightToLeft)
  164. {
  165. c = rightToLeftText[i];
  166. }
  167. else
  168. {
  169. c = text[i];
  170. }
  171. // Draw this character.
  172. switch (c)
  173. {
  174. case ' ':
  175. xPos += size>>1;
  176. break;
  177. case '\r':
  178. case '\n':
  179. yPos += size;
  180. xPos = x;
  181. break;
  182. case '\t':
  183. xPos += (size>>1)*4;
  184. break;
  185. default:
  186. int index = c - 32; // HACK for ASCII
  187. if (index >= 0 && index < (int)_glyphCount)
  188. {
  189. Glyph& g = _glyphs[index];
  190. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  191. xPos += g.width * scale + (size>>3);
  192. break;
  193. }
  194. }
  195. }
  196. SAFE_DELETE(rightToLeftText);
  197. }
  198. void Font::drawText(const char* text, const Rectangle& area, const Vector4& color, float scale, Justify justify, bool wrap, bool rightToLeft)
  199. {
  200. char* token = const_cast<char*>(text);
  201. const int length = strlen(text);
  202. const int fontSize = (int)(_size * scale);
  203. int yPos = area.y;
  204. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  205. if (vAlign == 0)
  206. {
  207. vAlign = ALIGN_TOP;
  208. }
  209. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  210. if (hAlign == 0)
  211. {
  212. hAlign = ALIGN_LEFT;
  213. }
  214. char* rightToLeftText = NULL;
  215. if (rightToLeft)
  216. {
  217. rightToLeftText = new char[strlen(text) + 1];
  218. strcpy(rightToLeftText, text);
  219. token = rightToLeftText;
  220. }
  221. else
  222. {
  223. token = const_cast<char*>(text);
  224. }
  225. // For alignments other than top-left, need to calculate the y position to begin drawing from
  226. // and the starting x position of each line. For right-to-left text that wraps, need to insert
  227. // newlines where lines wrap even for top-left alignment.
  228. std::vector<int> xPositions;
  229. if (vAlign != ALIGN_TOP || hAlign != ALIGN_LEFT || (rightToLeft && wrap))
  230. {
  231. int lineWidth = 0;
  232. int delimWidth = 0;
  233. if (wrap)
  234. {
  235. // Go a word at a time.
  236. bool reachedEOF = false;
  237. while (token[0] != 0)
  238. {
  239. unsigned int tokenWidth = 0;
  240. // Handle delimiters until next token.
  241. char delimiter = token[0];
  242. while (delimiter == ' ' ||
  243. delimiter == '\t' ||
  244. delimiter == '\r' ||
  245. delimiter == '\n' ||
  246. delimiter == 0)
  247. {
  248. switch (delimiter)
  249. {
  250. case ' ':
  251. delimWidth += fontSize>>1;
  252. break;
  253. case '\r':
  254. case '\n':
  255. yPos += fontSize;
  256. if (lineWidth > 0)
  257. {
  258. int hWhitespace = area.width - lineWidth;
  259. if (hAlign == ALIGN_HCENTER)
  260. {
  261. xPositions.push_back(area.x + hWhitespace / 2);
  262. }
  263. else if (hAlign == ALIGN_RIGHT)
  264. {
  265. xPositions.push_back(area.x + hWhitespace);
  266. }
  267. }
  268. lineWidth = 0;
  269. delimWidth = 0;
  270. break;
  271. case '\t':
  272. delimWidth += (fontSize>>1)*4;
  273. break;
  274. case 0:
  275. reachedEOF = true;
  276. break;
  277. }
  278. if (reachedEOF)
  279. {
  280. break;
  281. }
  282. token++;
  283. delimiter = token[0];
  284. }
  285. if (reachedEOF || token == NULL)
  286. {
  287. break;
  288. }
  289. unsigned int tokenLength = strcspn(token, " \r\n\t");
  290. tokenWidth += getTokenWidth(token, tokenLength, scale);
  291. // Wrap if necessary.
  292. if (lineWidth + tokenWidth + delimWidth > area.width)
  293. {
  294. yPos += fontSize;
  295. // Push position of current line.
  296. int hWhitespace = area.width - lineWidth;
  297. if (hAlign == ALIGN_HCENTER)
  298. {
  299. xPositions.push_back(area.x + hWhitespace / 2);
  300. }
  301. else if (hAlign == ALIGN_RIGHT)
  302. {
  303. xPositions.push_back(area.x + hWhitespace);
  304. }
  305. if (rightToLeft)
  306. {
  307. // Is this sane?
  308. token[-1] = '\n';
  309. }
  310. // Move token to the next line.
  311. lineWidth = 0;
  312. delimWidth = 0;
  313. }
  314. else
  315. {
  316. lineWidth += delimWidth;
  317. delimWidth = 0;
  318. }
  319. lineWidth += tokenWidth;
  320. token += tokenLength;
  321. }
  322. // Final calculation of vertical position.
  323. int textHeight = yPos - area.y;
  324. int vWhiteSpace = area.height - textHeight;
  325. if (vAlign == ALIGN_VCENTER)
  326. {
  327. yPos = area.y + vWhiteSpace / 2;
  328. }
  329. else if (vAlign == ALIGN_BOTTOM)
  330. {
  331. yPos = area.y + vWhiteSpace;
  332. }
  333. // Calculation of final horizontal position.
  334. int hWhitespace = area.width - lineWidth;
  335. if (hAlign == ALIGN_HCENTER)
  336. {
  337. xPositions.push_back(area.x + hWhitespace / 2);
  338. }
  339. else if (hAlign == ALIGN_RIGHT)
  340. {
  341. xPositions.push_back(area.x + hWhitespace);
  342. }
  343. }
  344. else
  345. {
  346. // Go a line at a time.
  347. while (token[0] != 0)
  348. {
  349. char delimiter = token[0];
  350. while (delimiter == '\n')
  351. {
  352. yPos += fontSize;
  353. ++token;
  354. delimiter = token[0];
  355. }
  356. unsigned int tokenLength = strcspn(token, "\n");
  357. if (tokenLength == 0)
  358. {
  359. tokenLength = strlen(token);
  360. }
  361. int lineWidth = getTokenWidth(token, tokenLength, scale);
  362. int whitespace = area.width - lineWidth;
  363. if (hAlign == ALIGN_HCENTER)
  364. {
  365. xPositions.push_back(area.x + whitespace / 2);
  366. }
  367. else if (hAlign == ALIGN_RIGHT)
  368. {
  369. xPositions.push_back(area.x + whitespace);
  370. }
  371. token += tokenLength;
  372. }
  373. int textHeight = yPos - area.y;
  374. int vWhiteSpace = area.height - textHeight;
  375. if (vAlign == ALIGN_VCENTER)
  376. {
  377. yPos = area.y + vWhiteSpace / 2;
  378. }
  379. else if (vAlign == ALIGN_BOTTOM)
  380. {
  381. yPos = area.y + vWhiteSpace;
  382. }
  383. }
  384. if (vAlign == ALIGN_TOP)
  385. {
  386. yPos = area.y;
  387. }
  388. }
  389. // Now we have the info we need in order to render.
  390. int xPos = area.x;
  391. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  392. if (xPositionsIt != xPositions.end())
  393. {
  394. xPos = *xPositionsIt++;
  395. }
  396. if (rightToLeft)
  397. {
  398. reverseLines(rightToLeftText);
  399. token = rightToLeftText;
  400. }
  401. else
  402. {
  403. token = const_cast<char*>(text);
  404. }
  405. while (token[0] != 0)
  406. {
  407. // Handle delimiters until next token.
  408. char delimiter = token[0];
  409. bool nextLine = true;
  410. bool reachedEOF = false;
  411. while (delimiter == ' ' ||
  412. delimiter == '\t' ||
  413. delimiter == '\r' ||
  414. delimiter == '\n' ||
  415. delimiter == 0)
  416. {
  417. switch (delimiter)
  418. {
  419. case ' ':
  420. xPos += fontSize>>1;
  421. break;
  422. case '\r':
  423. case '\n':
  424. yPos += fontSize;
  425. // Only use next xPos for first newline character (in case of multiple consecutive newlines).
  426. if (nextLine)
  427. {
  428. if (xPositionsIt != xPositions.end())
  429. {
  430. xPos = *xPositionsIt++;
  431. }
  432. else
  433. {
  434. xPos = area.x;
  435. }
  436. nextLine = false;
  437. }
  438. break;
  439. case '\t':
  440. xPos += (fontSize>>1)*4;
  441. break;
  442. case 0:
  443. reachedEOF = true;
  444. break;
  445. }
  446. if (reachedEOF)
  447. {
  448. break;
  449. }
  450. token++;
  451. delimiter = token[0];
  452. }
  453. if (reachedEOF || token == NULL)
  454. {
  455. break;
  456. }
  457. unsigned int tokenLength = strcspn(token, " \r\n\t");
  458. delimiter = token[tokenLength];
  459. bool truncated = false;
  460. unsigned int tokenWidth = getTokenWidth(token, tokenLength, scale);
  461. // Wrap if necessary.
  462. if (wrap &&
  463. xPos + tokenWidth > area.x + area.width)
  464. {
  465. yPos += fontSize;
  466. if (xPositionsIt != xPositions.end())
  467. {
  468. xPos = *xPositionsIt++;
  469. }
  470. else
  471. {
  472. xPos = area.x;
  473. }
  474. }
  475. bool draw = true;
  476. if (yPos < area.y)
  477. {
  478. // Skip drawing until linebreak or wrap.
  479. draw = false;
  480. }
  481. else if (yPos > area.y + area.height)
  482. {
  483. // Truncate below area's vertical limit.
  484. break;
  485. }
  486. for (unsigned int i = 0; i < tokenLength; ++i)
  487. {
  488. char c = token[i];
  489. int glyphIndex = c - 32; // HACK for ASCII
  490. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  491. {
  492. Glyph& g = _glyphs[glyphIndex];
  493. if (xPos + (int)(g.width*scale) >= area.x + area.width)
  494. {
  495. // Truncate this line and go on to the next one.
  496. truncated = true;
  497. break;
  498. }
  499. else if (xPos >= area.x)
  500. {
  501. // Draw this character.
  502. if (draw)
  503. {
  504. _batch->draw(xPos, yPos, g.width * scale, fontSize, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  505. }
  506. }
  507. xPos += g.width*scale + (fontSize>>3);
  508. }
  509. }
  510. if (!truncated)
  511. {
  512. // Get next token.
  513. token += tokenLength;
  514. }
  515. else
  516. {
  517. // Skip the rest of this line.
  518. unsigned int tokenLength = strcspn(token, "\n");
  519. if (tokenLength > 0)
  520. {
  521. // Get first token of next line.
  522. token += tokenLength;
  523. }
  524. }
  525. }
  526. SAFE_DELETE(rightToLeftText);
  527. }
  528. void Font::end()
  529. {
  530. _batch->end();
  531. }
  532. void Font::measureText(const char* text, unsigned int* width, unsigned int* height, float scale)
  533. {
  534. const int length = strlen(text);
  535. char* token = const_cast<char*>(text);
  536. unsigned int fontSize = _size * scale;
  537. *width = 0;
  538. *height = 0;
  539. // Measure a line at a time.
  540. while (token[0] != 0)
  541. {
  542. while (token[0] == '\n')
  543. {
  544. *height += fontSize;
  545. ++token;
  546. }
  547. unsigned int tokenLength = strcspn(token, "\n");
  548. unsigned int tokenWidth = getTokenWidth(token, tokenLength, scale);
  549. if (tokenWidth > *width)
  550. {
  551. *width = tokenWidth;
  552. }
  553. token += tokenLength;
  554. }
  555. }
  556. void Font::measureText(const char* text, Rectangle* out, const Rectangle& viewport, float scale, Justify justify, bool wrap, bool clipped)
  557. {
  558. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  559. if (vAlign == 0)
  560. {
  561. vAlign = ALIGN_TOP;
  562. }
  563. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  564. if (hAlign == 0)
  565. {
  566. hAlign = ALIGN_LEFT;
  567. }
  568. int fontSize = (int)(_size * scale);
  569. char* token = const_cast<char*>(text);
  570. std::vector<bool> emptyLines;
  571. std::vector<Vector2> lines;
  572. unsigned int lineWidth = 0;
  573. int yPos = viewport.y;
  574. if (wrap)
  575. {
  576. unsigned int delimWidth = 0;
  577. bool reachedEOF = false;
  578. while (token[0] != 0)
  579. {
  580. // Handle delimiters until next token.
  581. char delimiter = token[0];
  582. while (delimiter == ' ' ||
  583. delimiter == '\t' ||
  584. delimiter == '\r' ||
  585. delimiter == '\n' ||
  586. delimiter == 0)
  587. {
  588. switch (delimiter)
  589. {
  590. case ' ':
  591. delimWidth += fontSize>>1;
  592. break;
  593. case '\r':
  594. case '\n':
  595. // Add line-height to vertical cursor.
  596. yPos += fontSize;
  597. if (lineWidth > 0)
  598. {
  599. // Determine horizontal position and width.
  600. int hWhitespace = viewport.width - lineWidth;
  601. int xPos = viewport.x;
  602. if (hAlign == ALIGN_HCENTER)
  603. {
  604. xPos += hWhitespace / 2;
  605. }
  606. else if (hAlign == ALIGN_RIGHT)
  607. {
  608. xPos += hWhitespace;
  609. }
  610. // Record this line's size.
  611. emptyLines.push_back(false);
  612. lines.push_back(Vector2(xPos, lineWidth));
  613. }
  614. else
  615. {
  616. // Record the existence of an empty line.
  617. emptyLines.push_back(true);
  618. lines.push_back(Vector2(FLT_MAX, 0));
  619. }
  620. lineWidth = 0;
  621. delimWidth = 0;
  622. break;
  623. case '\t':
  624. delimWidth += (fontSize>>1)*4;
  625. break;
  626. case 0:
  627. reachedEOF = true;
  628. break;
  629. }
  630. if (reachedEOF)
  631. {
  632. break;
  633. }
  634. token++;
  635. delimiter = token[0];
  636. }
  637. if (reachedEOF)
  638. {
  639. break;
  640. }
  641. // Measure the next token.
  642. unsigned int tokenLength = strcspn(token, " \r\n\t");
  643. unsigned int tokenWidth = getTokenWidth(token, tokenLength, scale);
  644. // Wrap if necessary.
  645. if (lineWidth + tokenWidth + delimWidth > viewport.width)
  646. {
  647. // Add line-height to vertical cursor.
  648. yPos += fontSize;
  649. // Determine horizontal position and width.
  650. int hWhitespace = viewport.width - lineWidth;
  651. int xPos = viewport.x;
  652. if (hAlign == ALIGN_HCENTER)
  653. {
  654. xPos += hWhitespace / 2;
  655. }
  656. else if (hAlign == ALIGN_RIGHT)
  657. {
  658. xPos += hWhitespace;
  659. }
  660. // Record this line's size.
  661. emptyLines.push_back(false);
  662. lines.push_back(Vector2(xPos, lineWidth));
  663. lineWidth = 0;
  664. }
  665. else
  666. {
  667. lineWidth += delimWidth;
  668. }
  669. delimWidth = 0;
  670. lineWidth += tokenWidth;
  671. token += tokenLength;
  672. }
  673. }
  674. else
  675. {
  676. // Measure a whole line at a time.
  677. int emptyLinesCount = 0;
  678. while (token[0] != 0)
  679. {
  680. // Handle any number of consecutive newlines.
  681. bool nextLine = true;
  682. while (token[0] == '\n')
  683. {
  684. if (nextLine)
  685. {
  686. // Add line-height to vertical cursor.
  687. yPos += fontSize * (emptyLinesCount+1);
  688. nextLine = false;
  689. emptyLinesCount = 0;
  690. emptyLines.push_back(false);
  691. }
  692. else
  693. {
  694. // Record the existence of an empty line.
  695. ++emptyLinesCount;
  696. emptyLines.push_back(true);
  697. lines.push_back(Vector2(FLT_MAX, 0));
  698. }
  699. token++;
  700. }
  701. // Measure the next line.
  702. unsigned int tokenLength = strcspn(token, "\n");
  703. lineWidth = getTokenWidth(token, tokenLength, scale);
  704. // Determine horizontal position and width.
  705. int xPos = viewport.x;
  706. int hWhitespace = viewport.width - lineWidth;
  707. if (hAlign == ALIGN_HCENTER)
  708. {
  709. xPos += hWhitespace / 2;
  710. }
  711. else if (hAlign == ALIGN_RIGHT)
  712. {
  713. xPos += hWhitespace;
  714. }
  715. // Record this line's size.
  716. lines.push_back(Vector2(xPos, lineWidth));
  717. token += tokenLength;
  718. }
  719. yPos += fontSize;
  720. }
  721. if (wrap)
  722. {
  723. // Record the size of the last line.
  724. int hWhitespace = viewport.width - lineWidth;
  725. int xPos = viewport.x;
  726. if (hAlign == ALIGN_HCENTER)
  727. {
  728. xPos += hWhitespace / 2;
  729. }
  730. else if (hAlign == ALIGN_RIGHT)
  731. {
  732. xPos += hWhitespace;
  733. }
  734. lines.push_back(Vector2(xPos, lineWidth));
  735. }
  736. int x = INT_MAX;
  737. int y = viewport.y;
  738. unsigned int width = 0;
  739. int height = yPos - viewport.y;
  740. // Calculate top of text without clipping.
  741. int vWhitespace = viewport.height - height;
  742. if (vAlign == ALIGN_VCENTER)
  743. {
  744. y += vWhitespace / 2;
  745. }
  746. else if (vAlign == ALIGN_BOTTOM)
  747. {
  748. y += vWhitespace;
  749. }
  750. int clippedTop = 0;
  751. int clippedBottom = 0;
  752. if (clipped)
  753. {
  754. // Trim rect to fit text that would actually be drawn within the given viewport.
  755. if (y >= viewport.y)
  756. {
  757. // Text goes off the bottom of the viewport.
  758. clippedBottom = (height - viewport.height) / fontSize + 1;
  759. if (clippedBottom > 0)
  760. {
  761. // Also need to crop empty lines above non-empty lines that have been clipped.
  762. unsigned int emptyIndex = emptyLines.size() - clippedBottom;
  763. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  764. {
  765. height -= fontSize;
  766. emptyIndex++;
  767. }
  768. height -= fontSize * clippedBottom;
  769. }
  770. else
  771. {
  772. clippedBottom = 0;
  773. }
  774. }
  775. else
  776. {
  777. // Text goes above the top of the viewport.
  778. clippedTop = (viewport.y - y) / fontSize + 1;
  779. if (clippedTop < 0)
  780. {
  781. clippedTop = 0;
  782. }
  783. // Also need to crop empty lines below non-empty lines that have been clipped.
  784. unsigned int emptyIndex = clippedTop;
  785. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  786. {
  787. y += fontSize;
  788. height -= fontSize;
  789. emptyIndex++;
  790. }
  791. if (vAlign == ALIGN_VCENTER)
  792. {
  793. // In this case lines may be clipped off the bottom as well.
  794. clippedBottom = (height - viewport.height + vWhitespace/2 + 0.01) / fontSize + 1;
  795. if (clippedBottom > 0)
  796. {
  797. emptyIndex = emptyLines.size() - clippedBottom;
  798. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  799. {
  800. height -= fontSize;
  801. emptyIndex++;
  802. }
  803. height -= fontSize * clippedBottom;
  804. }
  805. else
  806. {
  807. clippedBottom = 0;
  808. }
  809. }
  810. y = y + fontSize * clippedTop;
  811. height = height - fontSize * clippedTop;
  812. }
  813. }
  814. // Determine left-most x coordinate and largest width out of lines that have not been clipped.
  815. for (unsigned int i = clippedTop; i < lines.size() - clippedBottom; ++i)
  816. {
  817. if (lines[i].x < x)
  818. {
  819. x = lines[i].x;
  820. }
  821. if (lines[i].y > width)
  822. {
  823. width = lines[i].y;
  824. }
  825. }
  826. if (clipped)
  827. {
  828. // Guarantee that the output rect will fit within the viewport.
  829. out->x = (x >= viewport.x)? x : viewport.x;
  830. out->y = (y >= viewport.y)? y : viewport.y;
  831. out->width = (width <= viewport.width)? width : viewport.width;
  832. out->height = (height <= viewport.height)? height : viewport.height;
  833. }
  834. else
  835. {
  836. out->x = x;
  837. out->y = y;
  838. out->width = width;
  839. out->height = height;
  840. }
  841. }
  842. unsigned int Font::getTokenWidth(const char* token, unsigned int length, float scale)
  843. {
  844. // Calculate width of word or line.
  845. unsigned int tokenWidth = 0;
  846. const int size = (int)(_size * scale);
  847. for (unsigned int i = 0; i < length; ++i)
  848. {
  849. char c = token[i];
  850. switch (c)
  851. {
  852. case ' ':
  853. tokenWidth += size>>1;
  854. break;
  855. case '\t':
  856. tokenWidth += (size>>1)*4;
  857. break;
  858. default:
  859. int glyphIndex = c - 32;
  860. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  861. {
  862. Glyph& g = _glyphs[glyphIndex];
  863. tokenWidth += g.width * scale + (size>>3);
  864. }
  865. break;
  866. }
  867. }
  868. return tokenWidth;
  869. }
  870. void Font::reverseLines(char* text)
  871. {
  872. // Naive approach: reverse each line, then render left-to-right as usual.
  873. while (text[0] != 0)
  874. {
  875. while (text[0] == '\n')
  876. {
  877. ++text;
  878. }
  879. unsigned int textLength = strcspn(text, "\n");
  880. std::string line = std::string(text, textLength);
  881. std::string reversedLine = std::string(line.rbegin(), line.rend());
  882. memcpy(text, reversedLine.c_str(), textLength);
  883. text += textLength;
  884. }
  885. }
  886. }