Font.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  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 (strcmp(justify, "ALIGN_LEFT") == 0)
  1383. {
  1384. return Font::ALIGN_LEFT;
  1385. }
  1386. else if (strcmp(justify, "ALIGN_HCENTER") == 0)
  1387. {
  1388. return Font::ALIGN_HCENTER;
  1389. }
  1390. else if (strcmp(justify, "ALIGN_RIGHT") == 0)
  1391. {
  1392. return Font::ALIGN_RIGHT;
  1393. }
  1394. else if (strcmp(justify, "ALIGN_TOP") == 0)
  1395. {
  1396. return Font::ALIGN_TOP;
  1397. }
  1398. else if (strcmp(justify, "ALIGN_VCENTER") == 0)
  1399. {
  1400. return Font::ALIGN_VCENTER;
  1401. }
  1402. else if (strcmp(justify, "ALIGN_BOTTOM") == 0)
  1403. {
  1404. return Font::ALIGN_BOTTOM;
  1405. }
  1406. else if (strcmp(justify, "ALIGN_TOP_LEFT") == 0)
  1407. {
  1408. return Font::ALIGN_TOP_LEFT;
  1409. }
  1410. else if (strcmp(justify, "ALIGN_VCENTER_LEFT") == 0)
  1411. {
  1412. return Font::ALIGN_VCENTER_LEFT;
  1413. }
  1414. else if (strcmp(justify, "ALIGN_BOTTOM_LEFT") == 0)
  1415. {
  1416. return Font::ALIGN_BOTTOM_LEFT;
  1417. }
  1418. else if (strcmp(justify, "ALIGN_TOP_HCENTER") == 0)
  1419. {
  1420. return Font::ALIGN_TOP_HCENTER;
  1421. }
  1422. else if (strcmp(justify, "ALIGN_VCENTER_HCENTER") == 0)
  1423. {
  1424. return Font::ALIGN_VCENTER_HCENTER;
  1425. }
  1426. else if (strcmp(justify, "ALIGN_BOTTOM_HCENTER") == 0)
  1427. {
  1428. return Font::ALIGN_BOTTOM_HCENTER;
  1429. }
  1430. else if (strcmp(justify, "ALIGN_TOP_RIGHT") == 0)
  1431. {
  1432. return Font::ALIGN_TOP_RIGHT;
  1433. }
  1434. else if (strcmp(justify, "ALIGN_VCENTER_RIGHT") == 0)
  1435. {
  1436. return Font::ALIGN_VCENTER_RIGHT;
  1437. }
  1438. else if (strcmp(justify, "ALIGN_BOTTOM_RIGHT") == 0)
  1439. {
  1440. return Font::ALIGN_BOTTOM_RIGHT;
  1441. }
  1442. // Default.
  1443. return Font::ALIGN_TOP_LEFT;
  1444. }
  1445. }