Font.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. #include "Base.h"
  2. #include "Font.h"
  3. #include "Game.h"
  4. #include "FileSystem.h"
  5. #include "Package.h"
  6. // Default font vertex shader
  7. #define FONT_VSH \
  8. "uniform mat4 u_projectionMatrix;\n" \
  9. "attribute vec3 a_position;\n" \
  10. "attribute vec2 a_texCoord;\n" \
  11. "attribute vec4 a_color;\n" \
  12. "varying vec2 v_texCoord;\n" \
  13. "varying vec4 v_color;\n" \
  14. "void main()\n" \
  15. "{\n" \
  16. "gl_Position = u_projectionMatrix * vec4(a_position, 1);\n" \
  17. "v_texCoord = a_texCoord;\n" \
  18. "v_color = a_color;\n" \
  19. "}\n"
  20. // Default font fragment shader
  21. #define FONT_FSH \
  22. "#ifdef OPENGL_ES\n" \
  23. "precision highp float;\n" \
  24. "#endif\n" \
  25. "varying vec2 v_texCoord;\n" \
  26. "varying vec4 v_color;\n" \
  27. "uniform sampler2D u_texture;\n" \
  28. "void main()\n" \
  29. "{\n" \
  30. "gl_FragColor = v_color;\n" \
  31. "gl_FragColor.a = texture2D(u_texture, v_texCoord).a;\n" \
  32. "}"
  33. namespace gameplay
  34. {
  35. static std::vector<Font*> __fontCache;
  36. static Effect* __fontEffect = NULL;
  37. Font::Font() :
  38. _style(PLAIN), _size(0), _glyphs(NULL), _glyphCount(0), _texture(NULL), _batch(NULL)
  39. {
  40. }
  41. Font::Font(const Font& copy)
  42. {
  43. // hidden
  44. }
  45. Font::~Font()
  46. {
  47. // Remove this Font from the font cache.
  48. std::vector<Font*>::iterator itr = std::find(__fontCache.begin(), __fontCache.end(), this);
  49. if (itr != __fontCache.end())
  50. {
  51. __fontCache.erase(itr);
  52. }
  53. SAFE_DELETE(_batch);
  54. SAFE_DELETE_ARRAY(_glyphs);
  55. SAFE_RELEASE(_texture);
  56. }
  57. Font* Font::create(const char* path, const char* id)
  58. {
  59. // Search the font cache for a font with the given path and ID.
  60. for (unsigned int i = 0, count = __fontCache.size(); i < count; ++i)
  61. {
  62. Font* f = __fontCache[i];
  63. if (f->_path == path && (id == NULL || f->_id == id))
  64. {
  65. // Found a match.
  66. f->addRef();
  67. return f;
  68. }
  69. }
  70. // Load the package.
  71. Package* pkg = Package::create(path);
  72. if (pkg == NULL)
  73. {
  74. return NULL;
  75. }
  76. Font* font = NULL;
  77. if (id == NULL)
  78. {
  79. // Get the ID of the first/only object in the package (assume it's a Font).
  80. const char* id;
  81. if (pkg->getObjectCount() != 1 || (id = pkg->getObjectID(0)) == NULL)
  82. {
  83. return NULL;
  84. }
  85. // Load the font using the ID of the first object in the package.
  86. font = pkg->loadFont(pkg->getObjectID(0));
  87. }
  88. else
  89. {
  90. // Load the font with the given ID.
  91. font = pkg->loadFont(id);
  92. }
  93. if (font)
  94. {
  95. // Add this font to the cache.
  96. __fontCache.push_back(font);
  97. }
  98. SAFE_RELEASE(pkg);
  99. return font;
  100. }
  101. Font* Font::create(const char* family, Style style, unsigned int size, Glyph* glyphs, int glyphCount, Texture* texture)
  102. {
  103. // Create the effect for the font's sprite batch.
  104. if (__fontEffect == NULL)
  105. {
  106. __fontEffect = Effect::createFromSource(FONT_VSH, FONT_FSH);
  107. if (__fontEffect == NULL)
  108. {
  109. LOG_ERROR("Failed to create effect for font.");
  110. SAFE_RELEASE(texture);
  111. return NULL;
  112. }
  113. }
  114. else
  115. {
  116. __fontEffect->addRef();
  117. }
  118. // Create batch for the font.
  119. SpriteBatch* batch = SpriteBatch::create(texture, __fontEffect, 128);
  120. // Release __fontEffect since the SpriteBatch keeps a reference to it
  121. SAFE_RELEASE(__fontEffect);
  122. if (batch == NULL)
  123. {
  124. LOG_ERROR("Failed to create batch for font.");
  125. return NULL;
  126. }
  127. // Increase the ref count of the texture to retain it.
  128. texture->addRef();
  129. Font* font = new Font();
  130. font->_family = family;
  131. font->_style = style;
  132. font->_size = size;
  133. font->_texture = texture;
  134. font->_batch = batch;
  135. // Copy the glyphs array.
  136. font->_glyphs = new Glyph[glyphCount];
  137. memcpy(font->_glyphs, glyphs, sizeof(Glyph) * glyphCount);
  138. font->_glyphCount = glyphCount;
  139. return font;
  140. }
  141. unsigned int Font::getSize()
  142. {
  143. return _size;
  144. }
  145. void Font::begin()
  146. {
  147. _batch->begin();
  148. }
  149. void Font::drawText(const char* text, int x, int y, const Vector4& color, unsigned int size, bool rightToLeft)
  150. {
  151. if (size == 0)
  152. size = _size;
  153. float scale = (float)size / _size;
  154. const char* cursor = NULL;
  155. if (rightToLeft)
  156. {
  157. cursor = text;
  158. }
  159. int xPos = x, yPos = y;
  160. bool done = false;
  161. while (!done)
  162. {
  163. int length;
  164. int startIndex;
  165. int iteration;
  166. if (rightToLeft)
  167. {
  168. char delimiter = cursor[0];
  169. while (!done &&
  170. (delimiter == ' ' ||
  171. delimiter == '\t' ||
  172. delimiter == '\r' ||
  173. delimiter == '\n' ||
  174. delimiter == 0))
  175. {
  176. switch (delimiter)
  177. {
  178. case ' ':
  179. xPos += (float)size*0.5f;
  180. break;
  181. case '\r':
  182. case '\n':
  183. yPos += size;
  184. xPos = x;
  185. break;
  186. case '\t':
  187. xPos += ((float)size*0.5f)*4;
  188. break;
  189. case 0:
  190. done = true;
  191. break;
  192. }
  193. if (!done)
  194. {
  195. ++cursor;
  196. delimiter = cursor[0];
  197. }
  198. }
  199. length = strcspn(cursor, "\r\n");
  200. startIndex = length - 1;
  201. iteration = -1;
  202. }
  203. else
  204. {
  205. length = strlen(text);
  206. startIndex = 0;
  207. iteration = 1;
  208. }
  209. for (int i = startIndex; i < length && i >= 0; i += iteration)
  210. {
  211. char c = 0;
  212. if (rightToLeft)
  213. {
  214. c = cursor[i];
  215. }
  216. else
  217. {
  218. c = text[i];
  219. }
  220. // Draw this character.
  221. switch (c)
  222. {
  223. case ' ':
  224. xPos += (float)size*0.5f;
  225. break;
  226. case '\r':
  227. case '\n':
  228. yPos += size;
  229. xPos = x;
  230. break;
  231. case '\t':
  232. xPos += ((float)size*0.5f)*4;
  233. break;
  234. default:
  235. int index = c - 32; // HACK for ASCII
  236. if (index >= 0 && index < (int)_glyphCount)
  237. {
  238. Glyph& g = _glyphs[index];
  239. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  240. xPos += g.width * scale + ((float)size*0.125f);
  241. break;
  242. }
  243. }
  244. }
  245. if (rightToLeft)
  246. {
  247. cursor += length;
  248. }
  249. else
  250. {
  251. done = true;
  252. }
  253. }
  254. }
  255. void Font::drawText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size, Justify justify, bool wrap, bool rightToLeft)
  256. {
  257. if (size == 0)
  258. size = _size;
  259. float scale = (float)size / _size;
  260. const char* token = text;
  261. const int length = strlen(text);
  262. int yPos = area.y;
  263. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  264. if (vAlign == 0)
  265. {
  266. vAlign = ALIGN_TOP;
  267. }
  268. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  269. if (hAlign == 0)
  270. {
  271. hAlign = ALIGN_LEFT;
  272. }
  273. token = text;
  274. // For alignments other than top-left, need to calculate the y position to begin drawing from
  275. // and the starting x position of each line. For right-to-left text, need to determine
  276. // the number of characters on each line.
  277. std::vector<int> xPositions;
  278. std::vector<unsigned int> lineLengths;
  279. if (vAlign != ALIGN_TOP || hAlign != ALIGN_LEFT || rightToLeft)
  280. {
  281. int lineWidth = 0;
  282. int delimWidth = 0;
  283. if (wrap)
  284. {
  285. // Go a word at a time.
  286. bool reachedEOF = false;
  287. unsigned int lineLength = 0;
  288. while (token[0] != 0)
  289. {
  290. unsigned int tokenWidth = 0;
  291. // Handle delimiters until next token.
  292. char delimiter = token[0];
  293. while (delimiter == ' ' ||
  294. delimiter == '\t' ||
  295. delimiter == '\r' ||
  296. delimiter == '\n' ||
  297. delimiter == 0)
  298. {
  299. switch (delimiter)
  300. {
  301. case ' ':
  302. delimWidth += (float)size*0.5f;
  303. lineLength++;
  304. break;
  305. case '\r':
  306. case '\n':
  307. yPos += size;
  308. if (lineWidth > 0)
  309. {
  310. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  311. }
  312. lineWidth = 0;
  313. lineLength = 0;
  314. delimWidth = 0;
  315. break;
  316. case '\t':
  317. delimWidth += ((float)size*0.5f)*4;
  318. lineLength++;
  319. break;
  320. case 0:
  321. reachedEOF = true;
  322. break;
  323. }
  324. if (reachedEOF)
  325. {
  326. break;
  327. }
  328. token++;
  329. delimiter = token[0];
  330. }
  331. if (reachedEOF || token == NULL)
  332. {
  333. break;
  334. }
  335. unsigned int tokenLength = strcspn(token, " \r\n\t");
  336. tokenWidth += getTokenWidth(token, tokenLength, size, scale);
  337. // Wrap if necessary.
  338. if (lineWidth + tokenWidth + delimWidth > area.width)
  339. {
  340. yPos += size;
  341. // Push position of current line.
  342. if (lineLength)
  343. {
  344. addLineInfo(area, lineWidth, lineLength-1, hAlign, &xPositions, &lineLengths, rightToLeft);
  345. }
  346. else
  347. {
  348. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  349. }
  350. // Move token to the next line.
  351. lineWidth = 0;
  352. lineLength = 0;
  353. delimWidth = 0;
  354. }
  355. else
  356. {
  357. lineWidth += delimWidth;
  358. delimWidth = 0;
  359. }
  360. lineWidth += tokenWidth;
  361. lineLength += tokenLength;
  362. token += tokenLength;
  363. }
  364. // Final calculation of vertical position.
  365. int textHeight = yPos - area.y;
  366. int vWhiteSpace = area.height - textHeight;
  367. if (vAlign == ALIGN_VCENTER)
  368. {
  369. yPos = area.y + vWhiteSpace / 2;
  370. }
  371. else if (vAlign == ALIGN_BOTTOM)
  372. {
  373. yPos = area.y + vWhiteSpace;
  374. }
  375. // Calculation of final horizontal position.
  376. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  377. }
  378. else
  379. {
  380. // Go a line at a time.
  381. while (token[0] != 0)
  382. {
  383. char delimiter = token[0];
  384. while (delimiter == '\n')
  385. {
  386. yPos += size;
  387. ++token;
  388. delimiter = token[0];
  389. }
  390. unsigned int tokenLength = strcspn(token, "\n");
  391. if (tokenLength == 0)
  392. {
  393. tokenLength = strlen(token);
  394. }
  395. int lineWidth = getTokenWidth(token, tokenLength, size, scale);
  396. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  397. token += tokenLength;
  398. }
  399. int textHeight = yPos - area.y;
  400. int vWhiteSpace = area.height - textHeight;
  401. if (vAlign == ALIGN_VCENTER)
  402. {
  403. yPos = area.y + vWhiteSpace / 2;
  404. }
  405. else if (vAlign == ALIGN_BOTTOM)
  406. {
  407. yPos = area.y + vWhiteSpace;
  408. }
  409. }
  410. if (vAlign == ALIGN_TOP)
  411. {
  412. yPos = area.y;
  413. }
  414. }
  415. // Now we have the info we need in order to render.
  416. int xPos = area.x;
  417. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  418. if (xPositionsIt != xPositions.end())
  419. {
  420. xPos = *xPositionsIt++;
  421. }
  422. token = text;
  423. int iteration = 1;
  424. unsigned int lineLength;
  425. unsigned int currentLineLength = 0;
  426. const char* lineStart;
  427. std::vector<unsigned int>::const_iterator lineLengthsIt;
  428. if (rightToLeft)
  429. {
  430. lineStart = token;
  431. lineLengthsIt = lineLengths.begin();
  432. lineLength = *lineLengthsIt++;
  433. token += lineLength - 1;
  434. iteration = -1;
  435. }
  436. while (token[0] != 0)
  437. {
  438. // Handle delimiters until next token.
  439. if (!handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  440. {
  441. break;
  442. }
  443. bool truncated = false;
  444. unsigned int tokenLength;
  445. unsigned int tokenWidth;
  446. unsigned int startIndex;
  447. if (rightToLeft)
  448. {
  449. tokenLength = getReversedTokenLength(token, text);
  450. currentLineLength += tokenLength;
  451. token -= (tokenLength - 1);
  452. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  453. iteration = -1;
  454. startIndex = tokenLength - 1;
  455. }
  456. else
  457. {
  458. tokenLength = strcspn(token, " \r\n\t");
  459. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  460. iteration = 1;
  461. startIndex = 0;
  462. }
  463. // Wrap if necessary.
  464. if (wrap &&
  465. xPos + tokenWidth > area.x + area.width ||
  466. (rightToLeft && currentLineLength > lineLength))
  467. {
  468. yPos += size;
  469. currentLineLength = tokenLength;
  470. if (xPositionsIt != xPositions.end())
  471. {
  472. xPos = *xPositionsIt++;
  473. }
  474. else
  475. {
  476. xPos = area.x;
  477. }
  478. }
  479. bool draw = true;
  480. if (yPos < area.y)
  481. {
  482. // Skip drawing until line break or wrap.
  483. draw = false;
  484. }
  485. else if (yPos > area.y + area.height)
  486. {
  487. // Truncate below area's vertical limit.
  488. break;
  489. }
  490. for (int i = startIndex; i < (int)tokenLength && i >= 0; i += iteration)
  491. {
  492. char c = token[i];
  493. int glyphIndex = c - 32; // HACK for ASCII
  494. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  495. {
  496. Glyph& g = _glyphs[glyphIndex];
  497. if (xPos + (int)(g.width*scale) > area.x + area.width)
  498. {
  499. // Truncate this line and go on to the next one.
  500. truncated = true;
  501. break;
  502. }
  503. else if (xPos >= area.x)
  504. {
  505. // Draw this character.
  506. if (draw)
  507. {
  508. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  509. }
  510. }
  511. xPos += g.width*scale + ((float)size*0.125f);
  512. }
  513. }
  514. if (!truncated)
  515. {
  516. if (rightToLeft)
  517. {
  518. if (token == lineStart)
  519. {
  520. token += lineLength;
  521. // Now handle delimiters going forwards.
  522. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  523. {
  524. break;
  525. }
  526. if (lineLengthsIt != lineLengths.end())
  527. {
  528. lineLength = *lineLengthsIt++;
  529. }
  530. lineStart = token;
  531. token += lineLength-1;
  532. }
  533. else
  534. {
  535. token--;
  536. }
  537. }
  538. else
  539. {
  540. token += tokenLength;
  541. }
  542. }
  543. else
  544. {
  545. if (rightToLeft)
  546. {
  547. token = lineStart + lineLength;
  548. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  549. {
  550. break;
  551. }
  552. if (lineLengthsIt != lineLengths.end())
  553. {
  554. lineLength = *lineLengthsIt++;
  555. }
  556. lineStart = token;
  557. token += lineLength-1;
  558. }
  559. else
  560. {
  561. // Skip the rest of this line.
  562. unsigned int tokenLength = strcspn(token, "\n");
  563. if (tokenLength > 0)
  564. {
  565. // Get first token of next line.
  566. token += tokenLength;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. void Font::end()
  573. {
  574. _batch->end();
  575. }
  576. void Font::measureText(const char* text, unsigned int size, unsigned int* width, unsigned int* height)
  577. {
  578. float scale = (float)size / _size;
  579. const int length = strlen(text);
  580. const char* token = text;
  581. *width = 0;
  582. *height = 0;
  583. // Measure a line at a time.
  584. while (token[0] != 0)
  585. {
  586. while (token[0] == '\n')
  587. {
  588. *height += size;
  589. ++token;
  590. }
  591. unsigned int tokenLength = strcspn(token, "\n");
  592. unsigned int tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  593. if (tokenWidth > *width)
  594. {
  595. *width = tokenWidth;
  596. }
  597. token += tokenLength;
  598. }
  599. }
  600. void Font::measureText(const char* text, const Rectangle& viewport, unsigned int size, Rectangle* out, Justify justify, bool wrap, bool ignoreClip)
  601. {
  602. float scale = (float)size / _size;
  603. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  604. if (vAlign == 0)
  605. {
  606. vAlign = ALIGN_TOP;
  607. }
  608. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  609. if (hAlign == 0)
  610. {
  611. hAlign = ALIGN_LEFT;
  612. }
  613. const char* token = text;
  614. std::vector<bool> emptyLines;
  615. std::vector<Vector2> lines;
  616. unsigned int lineWidth = 0;
  617. int yPos = viewport.y;
  618. if (wrap)
  619. {
  620. unsigned int delimWidth = 0;
  621. bool reachedEOF = false;
  622. while (token[0] != 0)
  623. {
  624. // Handle delimiters until next token.
  625. char delimiter = token[0];
  626. while (delimiter == ' ' ||
  627. delimiter == '\t' ||
  628. delimiter == '\r' ||
  629. delimiter == '\n' ||
  630. delimiter == 0)
  631. {
  632. switch (delimiter)
  633. {
  634. case ' ':
  635. delimWidth += (float)size*0.5f;
  636. break;
  637. case '\r':
  638. case '\n':
  639. // Add line-height to vertical cursor.
  640. yPos += size;
  641. if (lineWidth > 0)
  642. {
  643. // Determine horizontal position and width.
  644. int hWhitespace = viewport.width - lineWidth;
  645. int xPos = viewport.x;
  646. if (hAlign == ALIGN_HCENTER)
  647. {
  648. xPos += hWhitespace / 2;
  649. }
  650. else if (hAlign == ALIGN_RIGHT)
  651. {
  652. xPos += hWhitespace;
  653. }
  654. // Record this line's size.
  655. emptyLines.push_back(false);
  656. lines.push_back(Vector2(xPos, lineWidth));
  657. }
  658. else
  659. {
  660. // Record the existence of an empty line.
  661. emptyLines.push_back(true);
  662. lines.push_back(Vector2(FLT_MAX, 0));
  663. }
  664. lineWidth = 0;
  665. delimWidth = 0;
  666. break;
  667. case '\t':
  668. delimWidth += ((float)size*0.5f)*4;
  669. break;
  670. case 0:
  671. reachedEOF = true;
  672. break;
  673. }
  674. if (reachedEOF)
  675. {
  676. break;
  677. }
  678. token++;
  679. delimiter = token[0];
  680. }
  681. if (reachedEOF)
  682. {
  683. break;
  684. }
  685. // Measure the next token.
  686. unsigned int tokenLength = strcspn(token, " \r\n\t");
  687. unsigned int tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  688. // Wrap if necessary.
  689. if (lineWidth + tokenWidth + delimWidth > viewport.width)
  690. {
  691. // Add line-height to vertical cursor.
  692. yPos += size;
  693. // Determine horizontal position and width.
  694. int hWhitespace = viewport.width - lineWidth;
  695. int xPos = viewport.x;
  696. if (hAlign == ALIGN_HCENTER)
  697. {
  698. xPos += hWhitespace / 2;
  699. }
  700. else if (hAlign == ALIGN_RIGHT)
  701. {
  702. xPos += hWhitespace;
  703. }
  704. // Record this line's size.
  705. emptyLines.push_back(false);
  706. lines.push_back(Vector2(xPos, lineWidth));
  707. lineWidth = 0;
  708. }
  709. else
  710. {
  711. lineWidth += delimWidth;
  712. }
  713. delimWidth = 0;
  714. lineWidth += tokenWidth;
  715. token += tokenLength;
  716. }
  717. }
  718. else
  719. {
  720. // Measure a whole line at a time.
  721. int emptyLinesCount = 0;
  722. while (token[0] != 0)
  723. {
  724. // Handle any number of consecutive newlines.
  725. bool nextLine = true;
  726. while (token[0] == '\n')
  727. {
  728. if (nextLine)
  729. {
  730. // Add line-height to vertical cursor.
  731. yPos += size * (emptyLinesCount+1);
  732. nextLine = false;
  733. emptyLinesCount = 0;
  734. emptyLines.push_back(false);
  735. }
  736. else
  737. {
  738. // Record the existence of an empty line.
  739. ++emptyLinesCount;
  740. emptyLines.push_back(true);
  741. lines.push_back(Vector2(FLT_MAX, 0));
  742. }
  743. token++;
  744. }
  745. // Measure the next line.
  746. unsigned int tokenLength = strcspn(token, "\n");
  747. lineWidth = getTokenWidth(token, tokenLength, size, scale);
  748. // Determine horizontal position and width.
  749. int xPos = viewport.x;
  750. int hWhitespace = viewport.width - lineWidth;
  751. if (hAlign == ALIGN_HCENTER)
  752. {
  753. xPos += hWhitespace / 2;
  754. }
  755. else if (hAlign == ALIGN_RIGHT)
  756. {
  757. xPos += hWhitespace;
  758. }
  759. // Record this line's size.
  760. lines.push_back(Vector2(xPos, lineWidth));
  761. token += tokenLength;
  762. }
  763. yPos += size;
  764. }
  765. if (wrap)
  766. {
  767. // Record the size of the last line.
  768. int hWhitespace = viewport.width - lineWidth;
  769. int xPos = viewport.x;
  770. if (hAlign == ALIGN_HCENTER)
  771. {
  772. xPos += hWhitespace / 2;
  773. }
  774. else if (hAlign == ALIGN_RIGHT)
  775. {
  776. xPos += hWhitespace;
  777. }
  778. lines.push_back(Vector2(xPos, lineWidth));
  779. }
  780. int x = INT_MAX;
  781. int y = viewport.y;
  782. unsigned int width = 0;
  783. int height = yPos - viewport.y;
  784. // Calculate top of text without clipping.
  785. int vWhitespace = viewport.height - height;
  786. if (vAlign == ALIGN_VCENTER)
  787. {
  788. y += vWhitespace / 2;
  789. }
  790. else if (vAlign == ALIGN_BOTTOM)
  791. {
  792. y += vWhitespace;
  793. }
  794. int clippedTop = 0;
  795. int clippedBottom = 0;
  796. if (!ignoreClip)
  797. {
  798. // Trim rect to fit text that would actually be drawn within the given viewport.
  799. if (y >= viewport.y)
  800. {
  801. // Text goes off the bottom of the viewport.
  802. clippedBottom = (height - viewport.height) / size + 1;
  803. if (clippedBottom > 0)
  804. {
  805. // Also need to crop empty lines above non-empty lines that have been clipped.
  806. unsigned int emptyIndex = emptyLines.size() - clippedBottom;
  807. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  808. {
  809. height -= size;
  810. emptyIndex++;
  811. }
  812. height -= size * clippedBottom;
  813. }
  814. else
  815. {
  816. clippedBottom = 0;
  817. }
  818. }
  819. else
  820. {
  821. // Text goes above the top of the viewport.
  822. clippedTop = (viewport.y - y) / size + 1;
  823. if (clippedTop < 0)
  824. {
  825. clippedTop = 0;
  826. }
  827. // Also need to crop empty lines below non-empty lines that have been clipped.
  828. unsigned int emptyIndex = clippedTop;
  829. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  830. {
  831. y += size;
  832. height -= size;
  833. emptyIndex++;
  834. }
  835. if (vAlign == ALIGN_VCENTER)
  836. {
  837. // In this case lines may be clipped off the bottom as well.
  838. clippedBottom = (height - viewport.height + vWhitespace/2 + 0.01) / size + 1;
  839. if (clippedBottom > 0)
  840. {
  841. emptyIndex = emptyLines.size() - clippedBottom;
  842. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  843. {
  844. height -= size;
  845. emptyIndex++;
  846. }
  847. height -= size * clippedBottom;
  848. }
  849. else
  850. {
  851. clippedBottom = 0;
  852. }
  853. }
  854. y = y + size * clippedTop;
  855. height = height - size * clippedTop;
  856. }
  857. }
  858. // Determine left-most x coordinate and largest width out of lines that have not been clipped.
  859. for (int i = clippedTop; i < (int)lines.size() - clippedBottom; ++i)
  860. {
  861. if (lines[i].x < x)
  862. {
  863. x = lines[i].x;
  864. }
  865. if (lines[i].y > width)
  866. {
  867. width = lines[i].y;
  868. }
  869. }
  870. if (!ignoreClip)
  871. {
  872. // Guarantee that the output rect will fit within the viewport.
  873. out->x = (x >= viewport.x)? x : viewport.x;
  874. out->y = (y >= viewport.y)? y : viewport.y;
  875. out->width = (width <= viewport.width)? width : viewport.width;
  876. out->height = (height <= viewport.height)? height : viewport.height;
  877. }
  878. else
  879. {
  880. out->x = x;
  881. out->y = y;
  882. out->width = width;
  883. out->height = height;
  884. }
  885. }
  886. unsigned int Font::getIndexAtLocation(const char* text, const Rectangle& area, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  887. Justify justify, bool wrap, bool rightToLeft)
  888. {
  889. return getIndexOrLocation(text, area, size, inLocation, outLocation, -1, justify, wrap, rightToLeft);
  890. }
  891. void Font::getLocationAtIndex(const char* text, const Rectangle& clip, unsigned int size, Vector2* outLocation, const unsigned int destIndex,
  892. Justify justify, bool wrap, bool rightToLeft)
  893. {
  894. getIndexOrLocation(text, clip, size, *outLocation, outLocation, (const int)destIndex, justify, wrap, rightToLeft);
  895. }
  896. unsigned int Font::getIndexOrLocation(const char* text, const Rectangle& area, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  897. const int destIndex, Justify justify, bool wrap, bool rightToLeft)
  898. {
  899. unsigned int charIndex = 0;
  900. // Essentially need to measure text until we reach inLocation.
  901. float scale = (float)size / _size;
  902. const char* token = text;
  903. const int length = strlen(text);
  904. int yPos = area.y;
  905. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  906. if (vAlign == 0)
  907. {
  908. vAlign = ALIGN_TOP;
  909. }
  910. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  911. if (hAlign == 0)
  912. {
  913. hAlign = ALIGN_LEFT;
  914. }
  915. token = text;
  916. // For alignments other than top-left, need to calculate the y position to begin drawing from
  917. // and the starting x position of each line. For right-to-left text, need to determine
  918. // the number of characters on each line.
  919. std::vector<int> xPositions;
  920. std::vector<unsigned int> lineLengths;
  921. if (vAlign != ALIGN_TOP || hAlign != ALIGN_LEFT || rightToLeft)
  922. {
  923. int lineWidth = 0;
  924. int delimWidth = 0;
  925. if (wrap)
  926. {
  927. // Go a word at a time.
  928. bool reachedEOF = false;
  929. unsigned int lineLength = 0;
  930. while (token[0] != 0)
  931. {
  932. unsigned int tokenWidth = 0;
  933. // Handle delimiters until next token.
  934. char delimiter = token[0];
  935. while (delimiter == ' ' ||
  936. delimiter == '\t' ||
  937. delimiter == '\r' ||
  938. delimiter == '\n' ||
  939. delimiter == 0)
  940. {
  941. switch (delimiter)
  942. {
  943. case ' ':
  944. delimWidth += (float)size*0.5f;
  945. lineLength++;
  946. break;
  947. case '\r':
  948. case '\n':
  949. yPos += size;
  950. if (lineWidth > 0)
  951. {
  952. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  953. }
  954. lineWidth = 0;
  955. lineLength = 0;
  956. delimWidth = 0;
  957. break;
  958. case '\t':
  959. delimWidth += ((float)size*0.5f)*4;
  960. lineLength++;
  961. break;
  962. case 0:
  963. reachedEOF = true;
  964. break;
  965. }
  966. if (reachedEOF)
  967. {
  968. break;
  969. }
  970. token++;
  971. delimiter = token[0];
  972. }
  973. if (reachedEOF || token == NULL)
  974. {
  975. break;
  976. }
  977. unsigned int tokenLength = strcspn(token, " \r\n\t");
  978. tokenWidth += getTokenWidth(token, tokenLength, size, scale);
  979. // Wrap if necessary.
  980. if (lineWidth + tokenWidth + delimWidth > area.width)
  981. {
  982. yPos += size;
  983. // Push position of current line.
  984. if (lineLength)
  985. {
  986. addLineInfo(area, lineWidth, lineLength-1, hAlign, &xPositions, &lineLengths, rightToLeft);
  987. }
  988. else
  989. {
  990. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  991. }
  992. // Move token to the next line.
  993. lineWidth = 0;
  994. lineLength = 0;
  995. delimWidth = 0;
  996. }
  997. else
  998. {
  999. lineWidth += delimWidth;
  1000. delimWidth = 0;
  1001. }
  1002. lineWidth += tokenWidth;
  1003. lineLength += tokenLength;
  1004. token += tokenLength;
  1005. }
  1006. // Final calculation of vertical position.
  1007. int textHeight = yPos - area.y;
  1008. int vWhiteSpace = area.height - textHeight;
  1009. if (vAlign == ALIGN_VCENTER)
  1010. {
  1011. yPos = area.y + vWhiteSpace / 2;
  1012. }
  1013. else if (vAlign == ALIGN_BOTTOM)
  1014. {
  1015. yPos = area.y + vWhiteSpace;
  1016. }
  1017. // Calculation of final horizontal position.
  1018. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  1019. }
  1020. else
  1021. {
  1022. // Go a line at a time.
  1023. while (token[0] != 0)
  1024. {
  1025. char delimiter = token[0];
  1026. while (delimiter == '\n')
  1027. {
  1028. yPos += size;
  1029. ++token;
  1030. delimiter = token[0];
  1031. }
  1032. unsigned int tokenLength = strcspn(token, "\n");
  1033. if (tokenLength == 0)
  1034. {
  1035. tokenLength = strlen(token);
  1036. }
  1037. int lineWidth = getTokenWidth(token, tokenLength, size, scale);
  1038. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  1039. token += tokenLength;
  1040. }
  1041. int textHeight = yPos - area.y;
  1042. int vWhiteSpace = area.height - textHeight;
  1043. if (vAlign == ALIGN_VCENTER)
  1044. {
  1045. yPos = area.y + vWhiteSpace / 2;
  1046. }
  1047. else if (vAlign == ALIGN_BOTTOM)
  1048. {
  1049. yPos = area.y + vWhiteSpace;
  1050. }
  1051. }
  1052. if (vAlign == ALIGN_TOP)
  1053. {
  1054. yPos = area.y;
  1055. }
  1056. }
  1057. // Now we have the info we need in order to render.
  1058. int xPos = area.x;
  1059. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  1060. if (xPositionsIt != xPositions.end())
  1061. {
  1062. xPos = *xPositionsIt++;
  1063. }
  1064. token = text;
  1065. int iteration = 1;
  1066. unsigned int lineLength;
  1067. unsigned int currentLineLength = 0;
  1068. const char* lineStart;
  1069. std::vector<unsigned int>::const_iterator lineLengthsIt;
  1070. if (rightToLeft)
  1071. {
  1072. lineStart = token;
  1073. lineLengthsIt = lineLengths.begin();
  1074. lineLength = *lineLengthsIt++;
  1075. token += lineLength - 1;
  1076. iteration = -1;
  1077. }
  1078. while (token[0] != 0)
  1079. {
  1080. // Handle delimiters until next token.
  1081. unsigned int delimLength = 0;
  1082. int result;
  1083. if (destIndex == -1)
  1084. {
  1085. result = handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &delimLength, &xPositionsIt, xPositions.end(), &charIndex, &inLocation);
  1086. }
  1087. else
  1088. {
  1089. result = handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &delimLength, &xPositionsIt, xPositions.end(), &charIndex, NULL, charIndex, destIndex);
  1090. }
  1091. currentLineLength += delimLength;
  1092. if (result == 0)
  1093. {
  1094. break;
  1095. }
  1096. else if (result == 2)
  1097. {
  1098. outLocation->x = xPos;
  1099. outLocation->y = yPos;
  1100. return charIndex;
  1101. }
  1102. if (destIndex == (int)charIndex ||
  1103. (destIndex == -1 &&
  1104. inLocation.x >= xPos && inLocation.x < floor(xPos + ((float)size*0.125f)) &&
  1105. inLocation.y >= yPos && inLocation.y < yPos + size))
  1106. {
  1107. outLocation->x = xPos;
  1108. outLocation->y = yPos;
  1109. return charIndex;
  1110. }
  1111. bool truncated = false;
  1112. unsigned int tokenLength;
  1113. unsigned int tokenWidth;
  1114. unsigned int startIndex;
  1115. if (rightToLeft)
  1116. {
  1117. tokenLength = getReversedTokenLength(token, text);
  1118. currentLineLength += tokenLength;
  1119. charIndex += tokenLength;
  1120. token -= (tokenLength - 1);
  1121. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  1122. iteration = -1;
  1123. startIndex = tokenLength - 1;
  1124. }
  1125. else
  1126. {
  1127. tokenLength = strcspn(token, " \r\n\t");
  1128. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  1129. iteration = 1;
  1130. startIndex = 0;
  1131. }
  1132. // Wrap if necessary.
  1133. if (wrap &&
  1134. xPos + tokenWidth > area.x + area.width ||
  1135. (rightToLeft && currentLineLength > lineLength))
  1136. {
  1137. yPos += size;
  1138. currentLineLength = tokenLength;
  1139. if (xPositionsIt != xPositions.end())
  1140. {
  1141. xPos = *xPositionsIt++;
  1142. }
  1143. else
  1144. {
  1145. xPos = area.x;
  1146. }
  1147. }
  1148. if (yPos > area.y + area.height)
  1149. {
  1150. // Truncate below area's vertical limit.
  1151. break;
  1152. }
  1153. for (int i = startIndex; i < (int)tokenLength && i >= 0; i += iteration)
  1154. {
  1155. char c = token[i];
  1156. int glyphIndex = c - 32; // HACK for ASCII
  1157. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  1158. {
  1159. Glyph& g = _glyphs[glyphIndex];
  1160. if (xPos + (int)(g.width*scale) > area.x + area.width)
  1161. {
  1162. // Truncate this line and go on to the next one.
  1163. truncated = true;
  1164. break;
  1165. }
  1166. // Check against inLocation.
  1167. if (destIndex == (int)charIndex ||
  1168. (destIndex == -1 &&
  1169. inLocation.x >= xPos && inLocation.x < floor(xPos + g.width*scale + ((float)size*0.125f)) &&
  1170. inLocation.y >= yPos && inLocation.y < yPos + size))
  1171. {
  1172. outLocation->x = xPos;
  1173. outLocation->y = yPos;
  1174. return charIndex;
  1175. }
  1176. xPos += g.width*scale + ((float)size*0.125f);
  1177. charIndex++;
  1178. }
  1179. }
  1180. if (!truncated)
  1181. {
  1182. if (rightToLeft)
  1183. {
  1184. if (token == lineStart)
  1185. {
  1186. token += lineLength;
  1187. // Now handle delimiters going forwards.
  1188. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  1189. {
  1190. break;
  1191. }
  1192. charIndex += currentLineLength;
  1193. if (lineLengthsIt != lineLengths.end())
  1194. {
  1195. lineLength = *lineLengthsIt++;
  1196. }
  1197. lineStart = token;
  1198. token += lineLength-1;
  1199. charIndex += tokenLength;
  1200. }
  1201. else
  1202. {
  1203. token--;
  1204. }
  1205. }
  1206. else
  1207. {
  1208. token += tokenLength;
  1209. }
  1210. }
  1211. else
  1212. {
  1213. if (rightToLeft)
  1214. {
  1215. token = lineStart + lineLength;
  1216. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  1217. {
  1218. break;
  1219. }
  1220. if (lineLengthsIt != lineLengths.end())
  1221. {
  1222. lineLength = *lineLengthsIt++;
  1223. }
  1224. lineStart = token;
  1225. token += lineLength-1;
  1226. }
  1227. else
  1228. {
  1229. // Skip the rest of this line.
  1230. unsigned int tokenLength = strcspn(token, "\n");
  1231. if (tokenLength > 0)
  1232. {
  1233. // Get first token of next line.
  1234. token += tokenLength;
  1235. charIndex += tokenLength;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. outLocation->x = xPos;
  1241. outLocation->y = yPos;
  1242. return charIndex;
  1243. }
  1244. unsigned int Font::getTokenWidth(const char* token, unsigned int length, unsigned int size, float scale)
  1245. {
  1246. // Calculate width of word or line.
  1247. unsigned int tokenWidth = 0;
  1248. for (unsigned int i = 0; i < length; ++i)
  1249. {
  1250. char c = token[i];
  1251. switch (c)
  1252. {
  1253. case ' ':
  1254. tokenWidth += (float)size*0.5f;
  1255. break;
  1256. case '\t':
  1257. tokenWidth += ((float)size*0.5f)*4;
  1258. break;
  1259. default:
  1260. int glyphIndex = c - 32;
  1261. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  1262. {
  1263. Glyph& g = _glyphs[glyphIndex];
  1264. tokenWidth += g.width * scale + ((float)size*0.125f);
  1265. }
  1266. break;
  1267. }
  1268. }
  1269. return tokenWidth;
  1270. }
  1271. unsigned int Font::getReversedTokenLength(const char* token, const char* bufStart)
  1272. {
  1273. const char* cursor = token;
  1274. char c = cursor[0];
  1275. unsigned int length = 0;
  1276. while (cursor != bufStart && c != ' ' && c != '\r' && c != '\n' && c != '\t')
  1277. {
  1278. length++;
  1279. cursor--;
  1280. c = cursor[0];
  1281. }
  1282. if (cursor == bufStart)
  1283. {
  1284. length++;
  1285. }
  1286. return length;
  1287. }
  1288. int Font::handleDelimiters(const char** token, const unsigned int size, const int iteration, const int areaX, int* xPos, int* yPos, unsigned int* lineLength,
  1289. std::vector<int>::const_iterator* xPositionsIt, std::vector<int>::const_iterator xPositionsEnd, unsigned int* charIndex,
  1290. const Vector2* stopAtPosition, const int currentIndex, const int destIndex)
  1291. {
  1292. char delimiter = *token[0];
  1293. bool nextLine = true;
  1294. while (delimiter == ' ' ||
  1295. delimiter == '\t' ||
  1296. delimiter == '\r' ||
  1297. delimiter == '\n' ||
  1298. delimiter == 0)
  1299. {
  1300. if ((stopAtPosition &&
  1301. stopAtPosition->x >= *xPos && stopAtPosition->x < floor(*xPos + ((float)size*0.5f)) &&
  1302. stopAtPosition->y >= *yPos && stopAtPosition->y < *yPos + size) ||
  1303. (currentIndex >= 0 && destIndex >= 0 && currentIndex + (int)*lineLength == destIndex))
  1304. {
  1305. // Success + stopAtPosition was reached.
  1306. return 2;
  1307. }
  1308. switch (delimiter)
  1309. {
  1310. case ' ':
  1311. *xPos += (float)size*0.5f;
  1312. (*lineLength)++;
  1313. if (charIndex)
  1314. {
  1315. (*charIndex)++;
  1316. }
  1317. break;
  1318. case '\r':
  1319. case '\n':
  1320. *yPos += size;
  1321. // Only use next xPos for first newline character (in case of multiple consecutive newlines).
  1322. if (nextLine)
  1323. {
  1324. if (*xPositionsIt != xPositionsEnd)
  1325. {
  1326. *xPos = **xPositionsIt;
  1327. (*xPositionsIt)++;
  1328. }
  1329. else
  1330. {
  1331. *xPos = areaX;
  1332. }
  1333. nextLine = false;
  1334. *lineLength = 0;
  1335. if (charIndex)
  1336. {
  1337. (*charIndex)++;
  1338. }
  1339. }
  1340. break;
  1341. case '\t':
  1342. *xPos += ((float)size*0.5f)*4;
  1343. (*lineLength)++;
  1344. if (charIndex)
  1345. {
  1346. (*charIndex)++;
  1347. }
  1348. break;
  1349. case 0:
  1350. // EOF reached.
  1351. return 0;
  1352. }
  1353. *token += iteration;
  1354. delimiter = *token[0];
  1355. }
  1356. // Success.
  1357. return 1;
  1358. }
  1359. void Font::addLineInfo(const Rectangle& area, int lineWidth, int lineLength, Justify hAlign,
  1360. std::vector<int>* xPositions, std::vector<unsigned int>* lineLengths, bool rightToLeft)
  1361. {
  1362. int hWhitespace = area.width - lineWidth;
  1363. if (hAlign == ALIGN_HCENTER)
  1364. {
  1365. (*xPositions).push_back(area.x + hWhitespace / 2);
  1366. }
  1367. else if (hAlign == ALIGN_RIGHT)
  1368. {
  1369. (*xPositions).push_back(area.x + hWhitespace);
  1370. }
  1371. if (rightToLeft)
  1372. {
  1373. (*lineLengths).push_back(lineLength);
  1374. }
  1375. }
  1376. SpriteBatch* Font::getSpriteBatch()
  1377. {
  1378. return _batch;
  1379. }
  1380. Font::Justify Font::getJustifyFromString(const char* justify)
  1381. {
  1382. if (!justify)
  1383. {
  1384. return Font::ALIGN_TOP_LEFT;
  1385. }
  1386. if (strcmp(justify, "ALIGN_LEFT") == 0)
  1387. {
  1388. return Font::ALIGN_LEFT;
  1389. }
  1390. else if (strcmp(justify, "ALIGN_HCENTER") == 0)
  1391. {
  1392. return Font::ALIGN_HCENTER;
  1393. }
  1394. else if (strcmp(justify, "ALIGN_RIGHT") == 0)
  1395. {
  1396. return Font::ALIGN_RIGHT;
  1397. }
  1398. else if (strcmp(justify, "ALIGN_TOP") == 0)
  1399. {
  1400. return Font::ALIGN_TOP;
  1401. }
  1402. else if (strcmp(justify, "ALIGN_VCENTER") == 0)
  1403. {
  1404. return Font::ALIGN_VCENTER;
  1405. }
  1406. else if (strcmp(justify, "ALIGN_BOTTOM") == 0)
  1407. {
  1408. return Font::ALIGN_BOTTOM;
  1409. }
  1410. else if (strcmp(justify, "ALIGN_TOP_LEFT") == 0)
  1411. {
  1412. return Font::ALIGN_TOP_LEFT;
  1413. }
  1414. else if (strcmp(justify, "ALIGN_VCENTER_LEFT") == 0)
  1415. {
  1416. return Font::ALIGN_VCENTER_LEFT;
  1417. }
  1418. else if (strcmp(justify, "ALIGN_BOTTOM_LEFT") == 0)
  1419. {
  1420. return Font::ALIGN_BOTTOM_LEFT;
  1421. }
  1422. else if (strcmp(justify, "ALIGN_TOP_HCENTER") == 0)
  1423. {
  1424. return Font::ALIGN_TOP_HCENTER;
  1425. }
  1426. else if (strcmp(justify, "ALIGN_VCENTER_HCENTER") == 0)
  1427. {
  1428. return Font::ALIGN_VCENTER_HCENTER;
  1429. }
  1430. else if (strcmp(justify, "ALIGN_BOTTOM_HCENTER") == 0)
  1431. {
  1432. return Font::ALIGN_BOTTOM_HCENTER;
  1433. }
  1434. else if (strcmp(justify, "ALIGN_TOP_RIGHT") == 0)
  1435. {
  1436. return Font::ALIGN_TOP_RIGHT;
  1437. }
  1438. else if (strcmp(justify, "ALIGN_VCENTER_RIGHT") == 0)
  1439. {
  1440. return Font::ALIGN_VCENTER_RIGHT;
  1441. }
  1442. else if (strcmp(justify, "ALIGN_BOTTOM_RIGHT") == 0)
  1443. {
  1444. return Font::ALIGN_BOTTOM_RIGHT;
  1445. }
  1446. // Default.
  1447. return Font::ALIGN_TOP_LEFT;
  1448. }
  1449. }