Font.cpp 54 KB

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