Font.cpp 48 KB

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