Font.cpp 52 KB

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