Font.cpp 53 KB

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