Font.cpp 55 KB

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