Font.cpp 54 KB

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