Font.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. const float areaHeight = area.height - size;
  264. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  265. if (vAlign == 0)
  266. {
  267. vAlign = ALIGN_TOP;
  268. }
  269. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  270. if (hAlign == 0)
  271. {
  272. hAlign = ALIGN_LEFT;
  273. }
  274. token = text;
  275. // For alignments other than top-left, need to calculate the y position to begin drawing from
  276. // and the starting x position of each line. For right-to-left text, need to determine
  277. // the number of characters on each line.
  278. std::vector<int> xPositions;
  279. std::vector<unsigned int> lineLengths;
  280. if (vAlign != ALIGN_TOP || hAlign != ALIGN_LEFT || rightToLeft)
  281. {
  282. int lineWidth = 0;
  283. int delimWidth = 0;
  284. if (wrap)
  285. {
  286. // Go a word at a time.
  287. bool reachedEOF = false;
  288. unsigned int lineLength = 0;
  289. while (token[0] != 0)
  290. {
  291. unsigned int tokenWidth = 0;
  292. // Handle delimiters until next token.
  293. char delimiter = token[0];
  294. while (delimiter == ' ' ||
  295. delimiter == '\t' ||
  296. delimiter == '\r' ||
  297. delimiter == '\n' ||
  298. delimiter == 0)
  299. {
  300. switch (delimiter)
  301. {
  302. case ' ':
  303. delimWidth += (float)size*0.5f;
  304. lineLength++;
  305. break;
  306. case '\r':
  307. case '\n':
  308. yPos += size;
  309. if (lineWidth > 0)
  310. {
  311. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  312. }
  313. lineWidth = 0;
  314. lineLength = 0;
  315. delimWidth = 0;
  316. break;
  317. case '\t':
  318. delimWidth += ((float)size*0.5f)*4;
  319. lineLength++;
  320. break;
  321. case 0:
  322. reachedEOF = true;
  323. break;
  324. }
  325. if (reachedEOF)
  326. {
  327. break;
  328. }
  329. token++;
  330. delimiter = token[0];
  331. }
  332. if (reachedEOF || token == NULL)
  333. {
  334. break;
  335. }
  336. unsigned int tokenLength = strcspn(token, " \r\n\t");
  337. tokenWidth += getTokenWidth(token, tokenLength, size, scale);
  338. // Wrap if necessary.
  339. if (lineWidth + tokenWidth + delimWidth > area.width)
  340. {
  341. yPos += size;
  342. // Push position of current line.
  343. if (lineLength)
  344. {
  345. addLineInfo(area, lineWidth, lineLength-1, hAlign, &xPositions, &lineLengths, rightToLeft);
  346. }
  347. else
  348. {
  349. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  350. }
  351. // Move token to the next line.
  352. lineWidth = 0;
  353. lineLength = 0;
  354. delimWidth = 0;
  355. }
  356. else
  357. {
  358. lineWidth += delimWidth;
  359. delimWidth = 0;
  360. }
  361. lineWidth += tokenWidth;
  362. lineLength += tokenLength;
  363. token += tokenLength;
  364. }
  365. // Final calculation of vertical position.
  366. int textHeight = yPos - area.y;
  367. int vWhiteSpace = areaHeight - textHeight;
  368. if (vAlign == ALIGN_VCENTER)
  369. {
  370. yPos = area.y + vWhiteSpace / 2;
  371. }
  372. else if (vAlign == ALIGN_BOTTOM)
  373. {
  374. yPos = area.y + vWhiteSpace;
  375. }
  376. // Calculation of final horizontal position.
  377. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  378. }
  379. else
  380. {
  381. // Go a line at a time.
  382. while (token[0] != 0)
  383. {
  384. char delimiter = token[0];
  385. while (delimiter == '\n')
  386. {
  387. yPos += size;
  388. ++token;
  389. delimiter = token[0];
  390. }
  391. unsigned int tokenLength = strcspn(token, "\n");
  392. if (tokenLength == 0)
  393. {
  394. tokenLength = strlen(token);
  395. }
  396. int lineWidth = getTokenWidth(token, tokenLength, size, scale);
  397. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  398. token += tokenLength;
  399. }
  400. int textHeight = yPos - area.y;
  401. int vWhiteSpace = areaHeight - textHeight;
  402. if (vAlign == ALIGN_VCENTER)
  403. {
  404. yPos = area.y + vWhiteSpace / 2;
  405. }
  406. else if (vAlign == ALIGN_BOTTOM)
  407. {
  408. yPos = area.y + vWhiteSpace;
  409. }
  410. }
  411. if (vAlign == ALIGN_TOP)
  412. {
  413. yPos = area.y;
  414. }
  415. }
  416. // Now we have the info we need in order to render.
  417. int xPos = area.x;
  418. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  419. if (xPositionsIt != xPositions.end())
  420. {
  421. xPos = *xPositionsIt++;
  422. }
  423. token = text;
  424. int iteration = 1;
  425. unsigned int lineLength;
  426. unsigned int currentLineLength = 0;
  427. const char* lineStart;
  428. std::vector<unsigned int>::const_iterator lineLengthsIt;
  429. if (rightToLeft)
  430. {
  431. lineStart = token;
  432. lineLengthsIt = lineLengths.begin();
  433. lineLength = *lineLengthsIt++;
  434. token += lineLength - 1;
  435. iteration = -1;
  436. }
  437. while (token[0] != 0)
  438. {
  439. // Handle delimiters until next token.
  440. if (!handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  441. {
  442. break;
  443. }
  444. bool truncated = false;
  445. unsigned int tokenLength;
  446. unsigned int tokenWidth;
  447. unsigned int startIndex;
  448. if (rightToLeft)
  449. {
  450. tokenLength = getReversedTokenLength(token, text);
  451. currentLineLength += tokenLength;
  452. token -= (tokenLength - 1);
  453. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  454. iteration = -1;
  455. startIndex = tokenLength - 1;
  456. }
  457. else
  458. {
  459. tokenLength = strcspn(token, " \r\n\t");
  460. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  461. iteration = 1;
  462. startIndex = 0;
  463. }
  464. // Wrap if necessary.
  465. if (wrap &&
  466. xPos + tokenWidth > area.x + area.width ||
  467. (rightToLeft && currentLineLength > lineLength))
  468. {
  469. yPos += size;
  470. currentLineLength = tokenLength;
  471. if (xPositionsIt != xPositions.end())
  472. {
  473. xPos = *xPositionsIt++;
  474. }
  475. else
  476. {
  477. xPos = area.x;
  478. }
  479. }
  480. bool draw = true;
  481. if (yPos < area.y)
  482. {
  483. // Skip drawing until line break or wrap.
  484. draw = false;
  485. }
  486. else if (yPos > area.y + areaHeight)
  487. {
  488. // Truncate below area's vertical limit.
  489. break;
  490. }
  491. for (int i = startIndex; i < (int)tokenLength && i >= 0; i += iteration)
  492. {
  493. char c = token[i];
  494. int glyphIndex = c - 32; // HACK for ASCII
  495. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  496. {
  497. Glyph& g = _glyphs[glyphIndex];
  498. if (xPos + (int)(g.width*scale) > area.x + area.width)
  499. {
  500. // Truncate this line and go on to the next one.
  501. truncated = true;
  502. break;
  503. }
  504. else if (xPos >= area.x)
  505. {
  506. // Draw this character.
  507. if (draw)
  508. {
  509. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  510. }
  511. }
  512. xPos += g.width*scale + ((float)size*0.125f);
  513. }
  514. }
  515. if (!truncated)
  516. {
  517. if (rightToLeft)
  518. {
  519. if (token == lineStart)
  520. {
  521. token += lineLength;
  522. // Now handle delimiters going forwards.
  523. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  524. {
  525. break;
  526. }
  527. if (lineLengthsIt != lineLengths.end())
  528. {
  529. lineLength = *lineLengthsIt++;
  530. }
  531. lineStart = token;
  532. token += lineLength-1;
  533. }
  534. else
  535. {
  536. token--;
  537. }
  538. }
  539. else
  540. {
  541. token += tokenLength;
  542. }
  543. }
  544. else
  545. {
  546. if (rightToLeft)
  547. {
  548. token = lineStart + lineLength;
  549. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  550. {
  551. break;
  552. }
  553. if (lineLengthsIt != lineLengths.end())
  554. {
  555. lineLength = *lineLengthsIt++;
  556. }
  557. lineStart = token;
  558. token += lineLength-1;
  559. }
  560. else
  561. {
  562. // Skip the rest of this line.
  563. unsigned int tokenLength = strcspn(token, "\n");
  564. if (tokenLength > 0)
  565. {
  566. // Get first token of next line.
  567. token += tokenLength;
  568. }
  569. }
  570. }
  571. }
  572. }
  573. void Font::end()
  574. {
  575. _batch->end();
  576. }
  577. void Font::measureText(const char* text, unsigned int size, unsigned int* width, unsigned int* height)
  578. {
  579. float scale = (float)size / _size;
  580. const int length = strlen(text);
  581. const char* token = text;
  582. *width = 0;
  583. *height = 0;
  584. // Measure a line at a time.
  585. while (token[0] != 0)
  586. {
  587. while (token[0] == '\n')
  588. {
  589. *height += size;
  590. ++token;
  591. }
  592. unsigned int tokenLength = strcspn(token, "\n");
  593. unsigned int tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  594. if (tokenWidth > *width)
  595. {
  596. *width = tokenWidth;
  597. }
  598. token += tokenLength;
  599. }
  600. }
  601. void Font::measureText(const char* text, const Rectangle& viewport, unsigned int size, Rectangle* out, Justify justify, bool wrap, bool ignoreClip)
  602. {
  603. float scale = (float)size / _size;
  604. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  605. if (vAlign == 0)
  606. {
  607. vAlign = ALIGN_TOP;
  608. }
  609. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  610. if (hAlign == 0)
  611. {
  612. hAlign = ALIGN_LEFT;
  613. }
  614. const char* token = text;
  615. std::vector<bool> emptyLines;
  616. std::vector<Vector2> lines;
  617. unsigned int lineWidth = 0;
  618. int yPos = viewport.y;
  619. const float viewportHeight = viewport.height - size;
  620. if (wrap)
  621. {
  622. unsigned int delimWidth = 0;
  623. bool reachedEOF = false;
  624. while (token[0] != 0)
  625. {
  626. // Handle delimiters until next token.
  627. char delimiter = token[0];
  628. while (delimiter == ' ' ||
  629. delimiter == '\t' ||
  630. delimiter == '\r' ||
  631. delimiter == '\n' ||
  632. delimiter == 0)
  633. {
  634. switch (delimiter)
  635. {
  636. case ' ':
  637. delimWidth += (float)size*0.5f;
  638. break;
  639. case '\r':
  640. case '\n':
  641. // Add line-height to vertical cursor.
  642. yPos += size;
  643. if (lineWidth > 0)
  644. {
  645. // Determine horizontal position and width.
  646. int hWhitespace = viewport.width - lineWidth;
  647. int xPos = viewport.x;
  648. if (hAlign == ALIGN_HCENTER)
  649. {
  650. xPos += hWhitespace / 2;
  651. }
  652. else if (hAlign == ALIGN_RIGHT)
  653. {
  654. xPos += hWhitespace;
  655. }
  656. // Record this line's size.
  657. emptyLines.push_back(false);
  658. lines.push_back(Vector2(xPos, lineWidth));
  659. }
  660. else
  661. {
  662. // Record the existence of an empty line.
  663. emptyLines.push_back(true);
  664. lines.push_back(Vector2(FLT_MAX, 0));
  665. }
  666. lineWidth = 0;
  667. delimWidth = 0;
  668. break;
  669. case '\t':
  670. delimWidth += ((float)size*0.5f)*4;
  671. break;
  672. case 0:
  673. reachedEOF = true;
  674. break;
  675. }
  676. if (reachedEOF)
  677. {
  678. break;
  679. }
  680. token++;
  681. delimiter = token[0];
  682. }
  683. if (reachedEOF)
  684. {
  685. break;
  686. }
  687. // Measure the next token.
  688. unsigned int tokenLength = strcspn(token, " \r\n\t");
  689. unsigned int tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  690. // Wrap if necessary.
  691. if (lineWidth + tokenWidth + delimWidth > viewport.width)
  692. {
  693. // Add line-height to vertical cursor.
  694. yPos += size;
  695. // Determine horizontal position and width.
  696. int hWhitespace = viewport.width - lineWidth;
  697. int xPos = viewport.x;
  698. if (hAlign == ALIGN_HCENTER)
  699. {
  700. xPos += hWhitespace / 2;
  701. }
  702. else if (hAlign == ALIGN_RIGHT)
  703. {
  704. xPos += hWhitespace;
  705. }
  706. // Record this line's size.
  707. emptyLines.push_back(false);
  708. lines.push_back(Vector2(xPos, lineWidth));
  709. lineWidth = 0;
  710. }
  711. else
  712. {
  713. lineWidth += delimWidth;
  714. }
  715. delimWidth = 0;
  716. lineWidth += tokenWidth;
  717. token += tokenLength;
  718. }
  719. }
  720. else
  721. {
  722. // Measure a whole line at a time.
  723. int emptyLinesCount = 0;
  724. while (token[0] != 0)
  725. {
  726. // Handle any number of consecutive newlines.
  727. bool nextLine = true;
  728. while (token[0] == '\n')
  729. {
  730. if (nextLine)
  731. {
  732. // Add line-height to vertical cursor.
  733. yPos += size * (emptyLinesCount+1);
  734. nextLine = false;
  735. emptyLinesCount = 0;
  736. emptyLines.push_back(false);
  737. }
  738. else
  739. {
  740. // Record the existence of an empty line.
  741. ++emptyLinesCount;
  742. emptyLines.push_back(true);
  743. lines.push_back(Vector2(FLT_MAX, 0));
  744. }
  745. token++;
  746. }
  747. // Measure the next line.
  748. unsigned int tokenLength = strcspn(token, "\n");
  749. lineWidth = getTokenWidth(token, tokenLength, size, scale);
  750. // Determine horizontal position and width.
  751. int xPos = viewport.x;
  752. int hWhitespace = viewport.width - lineWidth;
  753. if (hAlign == ALIGN_HCENTER)
  754. {
  755. xPos += hWhitespace / 2;
  756. }
  757. else if (hAlign == ALIGN_RIGHT)
  758. {
  759. xPos += hWhitespace;
  760. }
  761. // Record this line's size.
  762. lines.push_back(Vector2(xPos, lineWidth));
  763. token += tokenLength;
  764. }
  765. yPos += size;
  766. }
  767. if (wrap)
  768. {
  769. // Record the size of the last line.
  770. int hWhitespace = viewport.width - lineWidth;
  771. int xPos = viewport.x;
  772. if (hAlign == ALIGN_HCENTER)
  773. {
  774. xPos += hWhitespace / 2;
  775. }
  776. else if (hAlign == ALIGN_RIGHT)
  777. {
  778. xPos += hWhitespace;
  779. }
  780. lines.push_back(Vector2(xPos, lineWidth));
  781. }
  782. int x = INT_MAX;
  783. int y = viewport.y;
  784. unsigned int width = 0;
  785. int height = yPos - viewport.y;
  786. // Calculate top of text without clipping.
  787. int vWhitespace = viewportHeight - height;
  788. if (vAlign == ALIGN_VCENTER)
  789. {
  790. y += vWhitespace / 2;
  791. }
  792. else if (vAlign == ALIGN_BOTTOM)
  793. {
  794. y += vWhitespace;
  795. }
  796. int clippedTop = 0;
  797. int clippedBottom = 0;
  798. if (!ignoreClip)
  799. {
  800. // Trim rect to fit text that would actually be drawn within the given viewport.
  801. if (y >= viewport.y)
  802. {
  803. // Text goes off the bottom of the viewport.
  804. clippedBottom = (height - viewportHeight) / size + 1;
  805. if (clippedBottom > 0)
  806. {
  807. // Also need to crop empty lines above non-empty lines that have been clipped.
  808. unsigned int emptyIndex = emptyLines.size() - clippedBottom;
  809. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  810. {
  811. height -= size;
  812. emptyIndex++;
  813. }
  814. height -= size * clippedBottom;
  815. }
  816. else
  817. {
  818. clippedBottom = 0;
  819. }
  820. }
  821. else
  822. {
  823. // Text goes above the top of the viewport.
  824. clippedTop = (viewport.y - y) / size + 1;
  825. if (clippedTop < 0)
  826. {
  827. clippedTop = 0;
  828. }
  829. // Also need to crop empty lines below non-empty lines that have been clipped.
  830. unsigned int emptyIndex = clippedTop;
  831. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  832. {
  833. y += size;
  834. height -= size;
  835. emptyIndex++;
  836. }
  837. if (vAlign == ALIGN_VCENTER)
  838. {
  839. // In this case lines may be clipped off the bottom as well.
  840. clippedBottom = (height - viewportHeight + vWhitespace/2 + 0.01) / size + 1;
  841. if (clippedBottom > 0)
  842. {
  843. emptyIndex = emptyLines.size() - clippedBottom;
  844. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  845. {
  846. height -= size;
  847. emptyIndex++;
  848. }
  849. height -= size * clippedBottom;
  850. }
  851. else
  852. {
  853. clippedBottom = 0;
  854. }
  855. }
  856. y = y + size * clippedTop;
  857. height = height - size * clippedTop;
  858. }
  859. }
  860. // Determine left-most x coordinate and largest width out of lines that have not been clipped.
  861. for (int i = clippedTop; i < (int)lines.size() - clippedBottom; ++i)
  862. {
  863. if (lines[i].x < x)
  864. {
  865. x = lines[i].x;
  866. }
  867. if (lines[i].y > width)
  868. {
  869. width = lines[i].y;
  870. }
  871. }
  872. if (!ignoreClip)
  873. {
  874. // Guarantee that the output rect will fit within the viewport.
  875. out->x = (x >= viewport.x)? x : viewport.x;
  876. out->y = (y >= viewport.y)? y : viewport.y;
  877. out->width = (width <= viewport.width)? width : viewport.width;
  878. out->height = (height <= viewportHeight)? height : viewportHeight;
  879. }
  880. else
  881. {
  882. out->x = x;
  883. out->y = y;
  884. out->width = width;
  885. out->height = height;
  886. }
  887. }
  888. unsigned int Font::getIndexAtLocation(const char* text, const Rectangle& area, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  889. Justify justify, bool wrap, bool rightToLeft)
  890. {
  891. return getIndexOrLocation(text, area, size, inLocation, outLocation, -1, justify, wrap, rightToLeft);
  892. }
  893. void Font::getLocationAtIndex(const char* text, const Rectangle& clip, unsigned int size, Vector2* outLocation, const unsigned int destIndex,
  894. Justify justify, bool wrap, bool rightToLeft)
  895. {
  896. getIndexOrLocation(text, clip, size, *outLocation, outLocation, (const int)destIndex, justify, wrap, rightToLeft);
  897. }
  898. unsigned int Font::getIndexOrLocation(const char* text, const Rectangle& area, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  899. const int destIndex, Justify justify, bool wrap, bool rightToLeft)
  900. {
  901. unsigned int charIndex = 0;
  902. // Essentially need to measure text until we reach inLocation.
  903. float scale = (float)size / _size;
  904. const char* token = text;
  905. const int length = strlen(text);
  906. int yPos = area.y;
  907. const float areaHeight = area.height - size;
  908. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  909. if (vAlign == 0)
  910. {
  911. vAlign = ALIGN_TOP;
  912. }
  913. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  914. if (hAlign == 0)
  915. {
  916. hAlign = ALIGN_LEFT;
  917. }
  918. token = text;
  919. // For alignments other than top-left, need to calculate the y position to begin drawing from
  920. // and the starting x position of each line. For right-to-left text, need to determine
  921. // the number of characters on each line.
  922. std::vector<int> xPositions;
  923. std::vector<unsigned int> lineLengths;
  924. if (vAlign != ALIGN_TOP || hAlign != ALIGN_LEFT || rightToLeft)
  925. {
  926. int lineWidth = 0;
  927. int delimWidth = 0;
  928. if (wrap)
  929. {
  930. // Go a word at a time.
  931. bool reachedEOF = false;
  932. unsigned int lineLength = 0;
  933. while (token[0] != 0)
  934. {
  935. unsigned int tokenWidth = 0;
  936. // Handle delimiters until next token.
  937. char delimiter = token[0];
  938. while (delimiter == ' ' ||
  939. delimiter == '\t' ||
  940. delimiter == '\r' ||
  941. delimiter == '\n' ||
  942. delimiter == 0)
  943. {
  944. switch (delimiter)
  945. {
  946. case ' ':
  947. delimWidth += (float)size*0.5f;
  948. lineLength++;
  949. break;
  950. case '\r':
  951. case '\n':
  952. yPos += size;
  953. if (lineWidth > 0)
  954. {
  955. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  956. }
  957. lineWidth = 0;
  958. lineLength = 0;
  959. delimWidth = 0;
  960. break;
  961. case '\t':
  962. delimWidth += ((float)size*0.5f)*4;
  963. lineLength++;
  964. break;
  965. case 0:
  966. reachedEOF = true;
  967. break;
  968. }
  969. if (reachedEOF)
  970. {
  971. break;
  972. }
  973. token++;
  974. delimiter = token[0];
  975. }
  976. if (reachedEOF || token == NULL)
  977. {
  978. break;
  979. }
  980. unsigned int tokenLength = strcspn(token, " \r\n\t");
  981. tokenWidth += getTokenWidth(token, tokenLength, size, scale);
  982. // Wrap if necessary.
  983. if (lineWidth + tokenWidth + delimWidth > area.width)
  984. {
  985. yPos += size;
  986. // Push position of current line.
  987. if (lineLength)
  988. {
  989. addLineInfo(area, lineWidth, lineLength-1, hAlign, &xPositions, &lineLengths, rightToLeft);
  990. }
  991. else
  992. {
  993. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  994. }
  995. // Move token to the next line.
  996. lineWidth = 0;
  997. lineLength = 0;
  998. delimWidth = 0;
  999. }
  1000. else
  1001. {
  1002. lineWidth += delimWidth;
  1003. delimWidth = 0;
  1004. }
  1005. lineWidth += tokenWidth;
  1006. lineLength += tokenLength;
  1007. token += tokenLength;
  1008. }
  1009. // Final calculation of vertical position.
  1010. int textHeight = yPos - area.y;
  1011. int vWhiteSpace = areaHeight - textHeight;
  1012. if (vAlign == ALIGN_VCENTER)
  1013. {
  1014. yPos = area.y + vWhiteSpace / 2;
  1015. }
  1016. else if (vAlign == ALIGN_BOTTOM)
  1017. {
  1018. yPos = area.y + vWhiteSpace;
  1019. }
  1020. // Calculation of final horizontal position.
  1021. addLineInfo(area, lineWidth, lineLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  1022. }
  1023. else
  1024. {
  1025. // Go a line at a time.
  1026. while (token[0] != 0)
  1027. {
  1028. char delimiter = token[0];
  1029. while (delimiter == '\n')
  1030. {
  1031. yPos += size;
  1032. ++token;
  1033. delimiter = token[0];
  1034. }
  1035. unsigned int tokenLength = strcspn(token, "\n");
  1036. if (tokenLength == 0)
  1037. {
  1038. tokenLength = strlen(token);
  1039. }
  1040. int lineWidth = getTokenWidth(token, tokenLength, size, scale);
  1041. addLineInfo(area, lineWidth, tokenLength, hAlign, &xPositions, &lineLengths, rightToLeft);
  1042. token += tokenLength;
  1043. }
  1044. int textHeight = yPos - area.y;
  1045. int vWhiteSpace = areaHeight - textHeight;
  1046. if (vAlign == ALIGN_VCENTER)
  1047. {
  1048. yPos = area.y + vWhiteSpace / 2;
  1049. }
  1050. else if (vAlign == ALIGN_BOTTOM)
  1051. {
  1052. yPos = area.y + vWhiteSpace;
  1053. }
  1054. }
  1055. if (vAlign == ALIGN_TOP)
  1056. {
  1057. yPos = area.y;
  1058. }
  1059. }
  1060. // Now we have the info we need in order to render.
  1061. int xPos = area.x;
  1062. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  1063. if (xPositionsIt != xPositions.end())
  1064. {
  1065. xPos = *xPositionsIt++;
  1066. }
  1067. token = text;
  1068. int iteration = 1;
  1069. unsigned int lineLength;
  1070. unsigned int currentLineLength = 0;
  1071. const char* lineStart;
  1072. std::vector<unsigned int>::const_iterator lineLengthsIt;
  1073. if (rightToLeft)
  1074. {
  1075. lineStart = token;
  1076. lineLengthsIt = lineLengths.begin();
  1077. lineLength = *lineLengthsIt++;
  1078. token += lineLength - 1;
  1079. iteration = -1;
  1080. }
  1081. while (token[0] != 0)
  1082. {
  1083. // Handle delimiters until next token.
  1084. unsigned int delimLength = 0;
  1085. int result;
  1086. if (destIndex == -1)
  1087. {
  1088. result = handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &delimLength, &xPositionsIt, xPositions.end(), &charIndex, &inLocation);
  1089. }
  1090. else
  1091. {
  1092. result = handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &delimLength, &xPositionsIt, xPositions.end(), &charIndex, NULL, charIndex, destIndex);
  1093. }
  1094. currentLineLength += delimLength;
  1095. if (result == 0)
  1096. {
  1097. break;
  1098. }
  1099. else if (result == 2)
  1100. {
  1101. outLocation->x = xPos;
  1102. outLocation->y = yPos;
  1103. return charIndex;
  1104. }
  1105. if (destIndex == (int)charIndex ||
  1106. (destIndex == -1 &&
  1107. inLocation.x >= xPos && inLocation.x < floor(xPos + ((float)size*0.125f)) &&
  1108. inLocation.y >= yPos && inLocation.y < yPos + size))
  1109. {
  1110. outLocation->x = xPos;
  1111. outLocation->y = yPos;
  1112. return charIndex;
  1113. }
  1114. bool truncated = false;
  1115. unsigned int tokenLength;
  1116. unsigned int tokenWidth;
  1117. unsigned int startIndex;
  1118. if (rightToLeft)
  1119. {
  1120. tokenLength = getReversedTokenLength(token, text);
  1121. currentLineLength += tokenLength;
  1122. charIndex += tokenLength;
  1123. token -= (tokenLength - 1);
  1124. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  1125. iteration = -1;
  1126. startIndex = tokenLength - 1;
  1127. }
  1128. else
  1129. {
  1130. tokenLength = strcspn(token, " \r\n\t");
  1131. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  1132. iteration = 1;
  1133. startIndex = 0;
  1134. }
  1135. // Wrap if necessary.
  1136. if (wrap &&
  1137. xPos + tokenWidth > area.x + area.width ||
  1138. (rightToLeft && currentLineLength > lineLength))
  1139. {
  1140. yPos += size;
  1141. currentLineLength = tokenLength;
  1142. if (xPositionsIt != xPositions.end())
  1143. {
  1144. xPos = *xPositionsIt++;
  1145. }
  1146. else
  1147. {
  1148. xPos = area.x;
  1149. }
  1150. }
  1151. if (yPos > area.y + areaHeight)
  1152. {
  1153. // Truncate below area's vertical limit.
  1154. break;
  1155. }
  1156. for (int i = startIndex; i < (int)tokenLength && i >= 0; i += iteration)
  1157. {
  1158. char c = token[i];
  1159. int glyphIndex = c - 32; // HACK for ASCII
  1160. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  1161. {
  1162. Glyph& g = _glyphs[glyphIndex];
  1163. if (xPos + (int)(g.width*scale) > area.x + area.width)
  1164. {
  1165. // Truncate this line and go on to the next one.
  1166. truncated = true;
  1167. break;
  1168. }
  1169. // Check against inLocation.
  1170. if (destIndex == (int)charIndex ||
  1171. (destIndex == -1 &&
  1172. inLocation.x >= xPos && inLocation.x < floor(xPos + g.width*scale + ((float)size*0.125f)) &&
  1173. inLocation.y >= yPos && inLocation.y < yPos + size))
  1174. {
  1175. outLocation->x = xPos;
  1176. outLocation->y = yPos;
  1177. return charIndex;
  1178. }
  1179. xPos += g.width*scale + ((float)size*0.125f);
  1180. charIndex++;
  1181. }
  1182. }
  1183. if (!truncated)
  1184. {
  1185. if (rightToLeft)
  1186. {
  1187. if (token == lineStart)
  1188. {
  1189. token += lineLength;
  1190. // Now handle delimiters going forwards.
  1191. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  1192. {
  1193. break;
  1194. }
  1195. charIndex += currentLineLength;
  1196. if (lineLengthsIt != lineLengths.end())
  1197. {
  1198. lineLength = *lineLengthsIt++;
  1199. }
  1200. lineStart = token;
  1201. token += lineLength-1;
  1202. charIndex += tokenLength;
  1203. }
  1204. else
  1205. {
  1206. token--;
  1207. }
  1208. }
  1209. else
  1210. {
  1211. token += tokenLength;
  1212. }
  1213. }
  1214. else
  1215. {
  1216. if (rightToLeft)
  1217. {
  1218. token = lineStart + lineLength;
  1219. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  1220. {
  1221. break;
  1222. }
  1223. if (lineLengthsIt != lineLengths.end())
  1224. {
  1225. lineLength = *lineLengthsIt++;
  1226. }
  1227. lineStart = token;
  1228. token += lineLength-1;
  1229. }
  1230. else
  1231. {
  1232. // Skip the rest of this line.
  1233. unsigned int tokenLength = strcspn(token, "\n");
  1234. if (tokenLength > 0)
  1235. {
  1236. // Get first token of next line.
  1237. token += tokenLength;
  1238. charIndex += tokenLength;
  1239. }
  1240. }
  1241. }
  1242. }
  1243. outLocation->x = xPos;
  1244. outLocation->y = yPos;
  1245. return charIndex;
  1246. }
  1247. unsigned int Font::getTokenWidth(const char* token, unsigned int length, unsigned int size, float scale)
  1248. {
  1249. // Calculate width of word or line.
  1250. unsigned int tokenWidth = 0;
  1251. for (unsigned int i = 0; i < length; ++i)
  1252. {
  1253. char c = token[i];
  1254. switch (c)
  1255. {
  1256. case ' ':
  1257. tokenWidth += (float)size*0.5f;
  1258. break;
  1259. case '\t':
  1260. tokenWidth += ((float)size*0.5f)*4;
  1261. break;
  1262. default:
  1263. int glyphIndex = c - 32;
  1264. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  1265. {
  1266. Glyph& g = _glyphs[glyphIndex];
  1267. tokenWidth += g.width * scale + ((float)size*0.125f);
  1268. }
  1269. break;
  1270. }
  1271. }
  1272. return tokenWidth;
  1273. }
  1274. unsigned int Font::getReversedTokenLength(const char* token, const char* bufStart)
  1275. {
  1276. const char* cursor = token;
  1277. char c = cursor[0];
  1278. unsigned int length = 0;
  1279. while (cursor != bufStart && c != ' ' && c != '\r' && c != '\n' && c != '\t')
  1280. {
  1281. length++;
  1282. cursor--;
  1283. c = cursor[0];
  1284. }
  1285. if (cursor == bufStart)
  1286. {
  1287. length++;
  1288. }
  1289. return length;
  1290. }
  1291. int Font::handleDelimiters(const char** token, const unsigned int size, const int iteration, const int areaX, int* xPos, int* yPos, unsigned int* lineLength,
  1292. std::vector<int>::const_iterator* xPositionsIt, std::vector<int>::const_iterator xPositionsEnd, unsigned int* charIndex,
  1293. const Vector2* stopAtPosition, const int currentIndex, const int destIndex)
  1294. {
  1295. char delimiter = *token[0];
  1296. bool nextLine = true;
  1297. while (delimiter == ' ' ||
  1298. delimiter == '\t' ||
  1299. delimiter == '\r' ||
  1300. delimiter == '\n' ||
  1301. delimiter == 0)
  1302. {
  1303. if ((stopAtPosition &&
  1304. stopAtPosition->x >= *xPos && stopAtPosition->x < floor(*xPos + ((float)size*0.5f)) &&
  1305. stopAtPosition->y >= *yPos && stopAtPosition->y < *yPos + size) ||
  1306. (currentIndex >= 0 && destIndex >= 0 && currentIndex + (int)*lineLength == destIndex))
  1307. {
  1308. // Success + stopAtPosition was reached.
  1309. return 2;
  1310. }
  1311. switch (delimiter)
  1312. {
  1313. case ' ':
  1314. *xPos += (float)size*0.5f;
  1315. (*lineLength)++;
  1316. if (charIndex)
  1317. {
  1318. (*charIndex)++;
  1319. }
  1320. break;
  1321. case '\r':
  1322. case '\n':
  1323. *yPos += size;
  1324. // Only use next xPos for first newline character (in case of multiple consecutive newlines).
  1325. if (nextLine)
  1326. {
  1327. if (*xPositionsIt != xPositionsEnd)
  1328. {
  1329. *xPos = **xPositionsIt;
  1330. (*xPositionsIt)++;
  1331. }
  1332. else
  1333. {
  1334. *xPos = areaX;
  1335. }
  1336. nextLine = false;
  1337. *lineLength = 0;
  1338. if (charIndex)
  1339. {
  1340. (*charIndex)++;
  1341. }
  1342. }
  1343. break;
  1344. case '\t':
  1345. *xPos += ((float)size*0.5f)*4;
  1346. (*lineLength)++;
  1347. if (charIndex)
  1348. {
  1349. (*charIndex)++;
  1350. }
  1351. break;
  1352. case 0:
  1353. // EOF reached.
  1354. return 0;
  1355. }
  1356. *token += iteration;
  1357. delimiter = *token[0];
  1358. }
  1359. // Success.
  1360. return 1;
  1361. }
  1362. void Font::addLineInfo(const Rectangle& area, int lineWidth, int lineLength, Justify hAlign,
  1363. std::vector<int>* xPositions, std::vector<unsigned int>* lineLengths, bool rightToLeft)
  1364. {
  1365. int hWhitespace = area.width - lineWidth;
  1366. if (hAlign == ALIGN_HCENTER)
  1367. {
  1368. (*xPositions).push_back(area.x + hWhitespace / 2);
  1369. }
  1370. else if (hAlign == ALIGN_RIGHT)
  1371. {
  1372. (*xPositions).push_back(area.x + hWhitespace);
  1373. }
  1374. if (rightToLeft)
  1375. {
  1376. (*lineLengths).push_back(lineLength);
  1377. }
  1378. }
  1379. SpriteBatch* Font::getSpriteBatch()
  1380. {
  1381. return _batch;
  1382. }
  1383. Font::Justify Font::getJustifyFromString(const char* justify)
  1384. {
  1385. if (!justify)
  1386. {
  1387. return Font::ALIGN_TOP_LEFT;
  1388. }
  1389. if (strcmp(justify, "ALIGN_LEFT") == 0)
  1390. {
  1391. return Font::ALIGN_LEFT;
  1392. }
  1393. else if (strcmp(justify, "ALIGN_HCENTER") == 0)
  1394. {
  1395. return Font::ALIGN_HCENTER;
  1396. }
  1397. else if (strcmp(justify, "ALIGN_RIGHT") == 0)
  1398. {
  1399. return Font::ALIGN_RIGHT;
  1400. }
  1401. else if (strcmp(justify, "ALIGN_TOP") == 0)
  1402. {
  1403. return Font::ALIGN_TOP;
  1404. }
  1405. else if (strcmp(justify, "ALIGN_VCENTER") == 0)
  1406. {
  1407. return Font::ALIGN_VCENTER;
  1408. }
  1409. else if (strcmp(justify, "ALIGN_BOTTOM") == 0)
  1410. {
  1411. return Font::ALIGN_BOTTOM;
  1412. }
  1413. else if (strcmp(justify, "ALIGN_TOP_LEFT") == 0)
  1414. {
  1415. return Font::ALIGN_TOP_LEFT;
  1416. }
  1417. else if (strcmp(justify, "ALIGN_VCENTER_LEFT") == 0)
  1418. {
  1419. return Font::ALIGN_VCENTER_LEFT;
  1420. }
  1421. else if (strcmp(justify, "ALIGN_BOTTOM_LEFT") == 0)
  1422. {
  1423. return Font::ALIGN_BOTTOM_LEFT;
  1424. }
  1425. else if (strcmp(justify, "ALIGN_TOP_HCENTER") == 0)
  1426. {
  1427. return Font::ALIGN_TOP_HCENTER;
  1428. }
  1429. else if (strcmp(justify, "ALIGN_VCENTER_HCENTER") == 0)
  1430. {
  1431. return Font::ALIGN_VCENTER_HCENTER;
  1432. }
  1433. else if (strcmp(justify, "ALIGN_BOTTOM_HCENTER") == 0)
  1434. {
  1435. return Font::ALIGN_BOTTOM_HCENTER;
  1436. }
  1437. else if (strcmp(justify, "ALIGN_TOP_RIGHT") == 0)
  1438. {
  1439. return Font::ALIGN_TOP_RIGHT;
  1440. }
  1441. else if (strcmp(justify, "ALIGN_VCENTER_RIGHT") == 0)
  1442. {
  1443. return Font::ALIGN_VCENTER_RIGHT;
  1444. }
  1445. else if (strcmp(justify, "ALIGN_BOTTOM_RIGHT") == 0)
  1446. {
  1447. return Font::ALIGN_BOTTOM_RIGHT;
  1448. }
  1449. // Default.
  1450. return Font::ALIGN_TOP_LEFT;
  1451. }
  1452. }