Font.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. #include "Base.h"
  2. #include "Font.h"
  3. #include "Game.h"
  4. #include "FileSystem.h"
  5. #include "Bundle.h"
  6. // Default font vertex shader
  7. #define FONT_VSH \
  8. "uniform mat4 u_projectionMatrix;\n" \
  9. "attribute vec3 a_position;\n" \
  10. "attribute vec2 a_texCoord;\n" \
  11. "attribute vec4 a_color;\n" \
  12. "varying vec2 v_texCoord;\n" \
  13. "varying vec4 v_color;\n" \
  14. "void main()\n" \
  15. "{\n" \
  16. "gl_Position = u_projectionMatrix * vec4(a_position, 1);\n" \
  17. "v_texCoord = a_texCoord;\n" \
  18. "v_color = a_color;\n" \
  19. "}\n"
  20. // Default font fragment shader
  21. #define FONT_FSH \
  22. "#ifdef OPENGL_ES\n" \
  23. "precision highp float;\n" \
  24. "#endif\n" \
  25. "varying vec2 v_texCoord;\n" \
  26. "varying vec4 v_color;\n" \
  27. "uniform sampler2D u_texture;\n" \
  28. "void main()\n" \
  29. "{\n" \
  30. "gl_FragColor = v_color;\n" \
  31. "gl_FragColor.a = texture2D(u_texture, v_texCoord).a;\n" \
  32. "}"
  33. namespace gameplay
  34. {
  35. static std::vector<Font*> __fontCache;
  36. static Effect* __fontEffect = NULL;
  37. Font::Font() :
  38. _style(PLAIN), _size(0), _glyphs(NULL), _glyphCount(0), _texture(NULL), _batch(NULL)
  39. {
  40. }
  41. Font::Font(const Font& copy)
  42. {
  43. // hidden
  44. }
  45. Font::~Font()
  46. {
  47. // Remove this Font from the font cache.
  48. std::vector<Font*>::iterator itr = std::find(__fontCache.begin(), __fontCache.end(), this);
  49. if (itr != __fontCache.end())
  50. {
  51. __fontCache.erase(itr);
  52. }
  53. SAFE_DELETE(_batch);
  54. SAFE_DELETE_ARRAY(_glyphs);
  55. SAFE_RELEASE(_texture);
  56. }
  57. Font* Font::create(const char* path, const char* id)
  58. {
  59. GP_ASSERT(path);
  60. // Search the font cache for a font with the given path and ID.
  61. for (unsigned int i = 0, count = __fontCache.size(); i < count; ++i)
  62. {
  63. Font* f = __fontCache[i];
  64. GP_ASSERT(f);
  65. if (f->_path == path && (id == NULL || f->_id == id))
  66. {
  67. // Found a match.
  68. f->addRef();
  69. return f;
  70. }
  71. }
  72. // Load the bundle.
  73. Bundle* bundle = Bundle::create(path);
  74. if (bundle == NULL)
  75. {
  76. GP_ERROR("Failed to load font bundle '%s'.", path);
  77. return NULL;
  78. }
  79. Font* font = NULL;
  80. if (id == NULL)
  81. {
  82. // Get the ID of the first object in the bundle (assume it's a Font).
  83. const char* id;
  84. if ((id = bundle->getObjectID(0)) == NULL)
  85. {
  86. GP_ERROR("Failed to load font without explicit id; the first object in the font bundle has a null id.");
  87. return NULL;
  88. }
  89. // Load the font using the ID of the first object in the bundle.
  90. font = bundle->loadFont(bundle->getObjectID(0));
  91. }
  92. else
  93. {
  94. // Load the font with the given ID.
  95. font = bundle->loadFont(id);
  96. }
  97. if (font)
  98. {
  99. // Add this font to the cache.
  100. __fontCache.push_back(font);
  101. }
  102. SAFE_RELEASE(bundle);
  103. return font;
  104. }
  105. Font* Font::create(const char* family, Style style, unsigned int size, Glyph* glyphs, int glyphCount, Texture* texture)
  106. {
  107. GP_ASSERT(family);
  108. GP_ASSERT(glyphs);
  109. GP_ASSERT(texture);
  110. // Create the effect for the font's sprite batch.
  111. if (__fontEffect == NULL)
  112. {
  113. __fontEffect = Effect::createFromSource(FONT_VSH, FONT_FSH);
  114. if (__fontEffect == NULL)
  115. {
  116. GP_ERROR("Failed to create effect for font.");
  117. SAFE_RELEASE(texture);
  118. return NULL;
  119. }
  120. }
  121. else
  122. {
  123. __fontEffect->addRef();
  124. }
  125. // Create batch for the font.
  126. SpriteBatch* batch = SpriteBatch::create(texture, __fontEffect, 128);
  127. // Release __fontEffect since the SpriteBatch keeps a reference to it
  128. SAFE_RELEASE(__fontEffect);
  129. if (batch == NULL)
  130. {
  131. GP_ERROR("Failed to create batch for font.");
  132. return NULL;
  133. }
  134. // Increase the ref count of the texture to retain it.
  135. texture->addRef();
  136. Font* font = new Font();
  137. font->_family = family;
  138. font->_style = style;
  139. font->_size = size;
  140. font->_texture = texture;
  141. font->_batch = batch;
  142. // Copy the glyphs array.
  143. font->_glyphs = new Glyph[glyphCount];
  144. memcpy(font->_glyphs, glyphs, sizeof(Glyph) * glyphCount);
  145. font->_glyphCount = glyphCount;
  146. return font;
  147. }
  148. unsigned int Font::getSize()
  149. {
  150. return _size;
  151. }
  152. void Font::begin()
  153. {
  154. GP_ASSERT(_batch);
  155. _batch->begin();
  156. }
  157. Font::Text* Font::createText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size, Justify justify,
  158. bool wrap, bool rightToLeft, const Rectangle* clip)
  159. {
  160. GP_ASSERT(text);
  161. GP_ASSERT(_glyphs);
  162. GP_ASSERT(_batch);
  163. if (size == 0)
  164. size = _size;
  165. GP_ASSERT(_size);
  166. float scale = (float)size / _size;
  167. const int length = strlen(text);
  168. int yPos = area.y;
  169. const float areaHeight = area.height - size;
  170. std::vector<int> xPositions;
  171. std::vector<unsigned int> lineLengths;
  172. getMeasurementInfo(text, area, size, justify, wrap, rightToLeft, &xPositions, &yPos, &lineLengths);
  173. Text* batch = new Text(text);
  174. GP_ASSERT(batch->_vertices);
  175. GP_ASSERT(batch->_indices);
  176. int xPos = area.x;
  177. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  178. if (xPositionsIt != xPositions.end())
  179. {
  180. xPos = *xPositionsIt++;
  181. }
  182. const char* token = text;
  183. int iteration = 1;
  184. unsigned int lineLength;
  185. unsigned int currentLineLength = 0;
  186. const char* lineStart;
  187. std::vector<unsigned int>::const_iterator lineLengthsIt;
  188. if (rightToLeft)
  189. {
  190. lineStart = token;
  191. lineLengthsIt = lineLengths.begin();
  192. lineLength = *lineLengthsIt++;
  193. token += lineLength - 1;
  194. iteration = -1;
  195. }
  196. while (token[0] != 0)
  197. {
  198. // Handle delimiters until next token.
  199. if (!handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  200. {
  201. break;
  202. }
  203. bool truncated = false;
  204. unsigned int tokenLength;
  205. unsigned int tokenWidth;
  206. unsigned int startIndex;
  207. if (rightToLeft)
  208. {
  209. tokenLength = getReversedTokenLength(token, text);
  210. currentLineLength += tokenLength;
  211. token -= (tokenLength - 1);
  212. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  213. iteration = -1;
  214. startIndex = tokenLength - 1;
  215. }
  216. else
  217. {
  218. tokenLength = strcspn(token, " \r\n\t");
  219. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  220. iteration = 1;
  221. startIndex = 0;
  222. }
  223. // Wrap if necessary.
  224. if (wrap && (xPos + (int)tokenWidth > area.x + area.width || (rightToLeft && currentLineLength > lineLength)))
  225. {
  226. yPos += (int)size;
  227. currentLineLength = tokenLength;
  228. if (xPositionsIt != xPositions.end())
  229. {
  230. xPos = *xPositionsIt++;
  231. }
  232. else
  233. {
  234. xPos = area.x;
  235. }
  236. }
  237. bool draw = true;
  238. if (yPos < area.y)
  239. {
  240. // Skip drawing until line break or wrap.
  241. draw = false;
  242. }
  243. else if (yPos > area.y + areaHeight)
  244. {
  245. // Truncate below area's vertical limit.
  246. break;
  247. }
  248. for (int i = startIndex; i < (int)tokenLength && i >= 0; i += iteration)
  249. {
  250. char c = token[i];
  251. int glyphIndex = c - 32; // HACK for ASCII
  252. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  253. {
  254. Glyph& g = _glyphs[glyphIndex];
  255. if (xPos + (int)(g.width*scale) > area.x + area.width)
  256. {
  257. // Truncate this line and go on to the next one.
  258. truncated = true;
  259. break;
  260. }
  261. else if (xPos >= area.x)
  262. {
  263. // Draw this character.
  264. if (draw)
  265. {
  266. if (clip)
  267. {
  268. _batch->addSprite(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, *clip, &batch->_vertices[batch->_vertexCount]);
  269. }
  270. else
  271. {
  272. _batch->addSprite(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, &batch->_vertices[batch->_vertexCount]);
  273. }
  274. if (batch->_vertexCount == 0)
  275. {
  276. // Simply copy values directly into the start of the index array
  277. batch->_indices[batch->_vertexCount] = batch->_vertexCount;
  278. batch->_indices[batch->_vertexCount + 1] = batch->_vertexCount + 1;
  279. batch->_indices[batch->_vertexCount + 2] = batch->_vertexCount + 2;
  280. batch->_indices[batch->_vertexCount + 3] = batch->_vertexCount + 3;
  281. batch->_vertexCount += 4;
  282. batch->_indexCount += 4;
  283. }
  284. else
  285. {
  286. // Create a degenerate triangle to connect separate triangle strips
  287. // by duplicating the previous and next vertices.
  288. batch->_indices[batch->_indexCount] = batch->_indices[batch->_indexCount - 1];
  289. batch->_indices[batch->_indexCount + 1] = batch->_vertexCount;
  290. // Loop through all indices and insert them, their their value offset by
  291. // 'vertexCount' so that they are relative to the first newly insertted vertex
  292. for (unsigned int i = 0; i < 4; ++i)
  293. {
  294. batch->_indices[batch->_indexCount + 2 + i] = i + batch->_vertexCount;
  295. }
  296. batch->_indexCount += 6;
  297. batch->_vertexCount += 4;
  298. }
  299. }
  300. }
  301. xPos += (int)(g.width)*scale + (size >> 3);
  302. }
  303. }
  304. if (!truncated)
  305. {
  306. if (rightToLeft)
  307. {
  308. if (token == lineStart)
  309. {
  310. token += lineLength;
  311. // Now handle delimiters going forwards.
  312. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  313. {
  314. break;
  315. }
  316. if (lineLengthsIt != lineLengths.end())
  317. {
  318. lineLength = *lineLengthsIt++;
  319. }
  320. lineStart = token;
  321. token += lineLength-1;
  322. }
  323. else
  324. {
  325. token--;
  326. }
  327. }
  328. else
  329. {
  330. token += tokenLength;
  331. }
  332. }
  333. else
  334. {
  335. if (rightToLeft)
  336. {
  337. token = lineStart + lineLength;
  338. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  339. {
  340. break;
  341. }
  342. if (lineLengthsIt != lineLengths.end())
  343. {
  344. lineLength = *lineLengthsIt++;
  345. }
  346. lineStart = token;
  347. token += lineLength-1;
  348. }
  349. else
  350. {
  351. // Skip the rest of this line.
  352. unsigned int tokenLength = strcspn(token, "\n");
  353. if (tokenLength > 0)
  354. {
  355. // Get first token of next line.
  356. token += tokenLength;
  357. }
  358. }
  359. }
  360. }
  361. return batch;
  362. }
  363. void Font::drawText(Text* text)
  364. {
  365. GP_ASSERT(_batch);
  366. GP_ASSERT(text->_vertices);
  367. GP_ASSERT(text->_indices);
  368. _batch->draw(text->_vertices, text->_vertexCount, text->_indices, text->_indexCount);
  369. }
  370. void Font::drawText(const char* text, int x, int y, const Vector4& color, unsigned int size, bool rightToLeft)
  371. {
  372. if (size == 0)
  373. size = _size;
  374. GP_ASSERT(_size);
  375. GP_ASSERT(text);
  376. float scale = (float)size / _size;
  377. const char* cursor = NULL;
  378. if (rightToLeft)
  379. {
  380. cursor = text;
  381. }
  382. int xPos = x, yPos = y;
  383. bool done = false;
  384. while (!done)
  385. {
  386. int length;
  387. int startIndex;
  388. int iteration;
  389. if (rightToLeft)
  390. {
  391. char delimiter = cursor[0];
  392. while (!done &&
  393. (delimiter == ' ' ||
  394. delimiter == '\t' ||
  395. delimiter == '\r' ||
  396. delimiter == '\n' ||
  397. delimiter == 0))
  398. {
  399. switch (delimiter)
  400. {
  401. case ' ':
  402. xPos += size >> 1;
  403. break;
  404. case '\r':
  405. case '\n':
  406. yPos += size;
  407. xPos = x;
  408. break;
  409. case '\t':
  410. xPos += (size >> 1)*4;
  411. break;
  412. case 0:
  413. done = true;
  414. break;
  415. }
  416. if (!done)
  417. {
  418. ++cursor;
  419. delimiter = cursor[0];
  420. }
  421. }
  422. length = strcspn(cursor, "\r\n");
  423. startIndex = length - 1;
  424. iteration = -1;
  425. }
  426. else
  427. {
  428. length = strlen(text);
  429. startIndex = 0;
  430. iteration = 1;
  431. }
  432. GP_ASSERT(_glyphs);
  433. GP_ASSERT(_batch);
  434. for (int i = startIndex; i < length && i >= 0; i += iteration)
  435. {
  436. char c = 0;
  437. if (rightToLeft)
  438. {
  439. c = cursor[i];
  440. }
  441. else
  442. {
  443. c = text[i];
  444. }
  445. // Draw this character.
  446. switch (c)
  447. {
  448. case ' ':
  449. xPos += size >> 1;
  450. break;
  451. case '\r':
  452. case '\n':
  453. yPos += size;
  454. xPos = x;
  455. break;
  456. case '\t':
  457. xPos += (size >> 1)*4;
  458. break;
  459. default:
  460. int index = c - 32; // HACK for ASCII
  461. if (index >= 0 && index < (int)_glyphCount)
  462. {
  463. Glyph& g = _glyphs[index];
  464. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  465. xPos += floor(g.width * scale + (float)(size >> 3));
  466. break;
  467. }
  468. break;
  469. }
  470. }
  471. if (rightToLeft)
  472. {
  473. cursor += length;
  474. }
  475. else
  476. {
  477. done = true;
  478. }
  479. }
  480. }
  481. void Font::drawText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size, Justify justify, bool wrap, bool rightToLeft, const Rectangle* clip)
  482. {
  483. GP_ASSERT(text);
  484. if (size == 0)
  485. size = _size;
  486. GP_ASSERT(_size);
  487. float scale = (float)size / _size;
  488. const int length = strlen(text);
  489. int yPos = area.y;
  490. const float areaHeight = area.height - size;
  491. std::vector<int> xPositions;
  492. std::vector<unsigned int> lineLengths;
  493. getMeasurementInfo(text, area, size, justify, wrap, rightToLeft, &xPositions, &yPos, &lineLengths);
  494. // Now we have the info we need in order to render.
  495. int xPos = area.x;
  496. std::vector<int>::const_iterator xPositionsIt = xPositions.begin();
  497. if (xPositionsIt != xPositions.end())
  498. {
  499. xPos = *xPositionsIt++;
  500. }
  501. const char* token = text;
  502. int iteration = 1;
  503. unsigned int lineLength;
  504. unsigned int currentLineLength = 0;
  505. const char* lineStart;
  506. std::vector<unsigned int>::const_iterator lineLengthsIt;
  507. if (rightToLeft)
  508. {
  509. lineStart = token;
  510. lineLengthsIt = lineLengths.begin();
  511. lineLength = *lineLengthsIt++;
  512. token += lineLength - 1;
  513. iteration = -1;
  514. }
  515. while (token[0] != 0)
  516. {
  517. // Handle delimiters until next token.
  518. if (!handleDelimiters(&token, size, iteration, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  519. {
  520. break;
  521. }
  522. bool truncated = false;
  523. unsigned int tokenLength;
  524. unsigned int tokenWidth;
  525. unsigned int startIndex;
  526. if (rightToLeft)
  527. {
  528. tokenLength = getReversedTokenLength(token, text);
  529. currentLineLength += tokenLength;
  530. token -= (tokenLength - 1);
  531. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  532. iteration = -1;
  533. startIndex = tokenLength - 1;
  534. }
  535. else
  536. {
  537. tokenLength = strcspn(token, " \r\n\t");
  538. tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  539. iteration = 1;
  540. startIndex = 0;
  541. }
  542. // Wrap if necessary.
  543. if (wrap && (xPos + (int)tokenWidth > area.x + area.width || (rightToLeft && currentLineLength > lineLength)))
  544. {
  545. yPos += (int)size;
  546. currentLineLength = tokenLength;
  547. if (xPositionsIt != xPositions.end())
  548. {
  549. xPos = *xPositionsIt++;
  550. }
  551. else
  552. {
  553. xPos = area.x;
  554. }
  555. }
  556. bool draw = true;
  557. if (yPos < area.y - size)
  558. {
  559. // Skip drawing until line break or wrap.
  560. draw = false;
  561. }
  562. else if (yPos > area.y + areaHeight)
  563. {
  564. // Truncate below area's vertical limit.
  565. break;
  566. }
  567. GP_ASSERT(_glyphs);
  568. GP_ASSERT(_batch);
  569. for (int i = startIndex; i < (int)tokenLength && i >= 0; i += iteration)
  570. {
  571. char c = token[i];
  572. int glyphIndex = c - 32; // HACK for ASCII
  573. if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
  574. {
  575. Glyph& g = _glyphs[glyphIndex];
  576. if (xPos + (int)(g.width*scale) > area.x + area.width)
  577. {
  578. // Truncate this line and go on to the next one.
  579. truncated = true;
  580. break;
  581. }
  582. else if (xPos >= area.x)
  583. {
  584. // Draw this character.
  585. if (draw)
  586. {
  587. if (clip)
  588. {
  589. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, *clip);
  590. }
  591. else
  592. {
  593. _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
  594. }
  595. }
  596. }
  597. xPos += (int)(g.width)*scale + (size >> 3);
  598. }
  599. }
  600. if (!truncated)
  601. {
  602. if (rightToLeft)
  603. {
  604. if (token == lineStart)
  605. {
  606. token += lineLength;
  607. // Now handle delimiters going forwards.
  608. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  609. {
  610. break;
  611. }
  612. if (lineLengthsIt != lineLengths.end())
  613. {
  614. lineLength = *lineLengthsIt++;
  615. }
  616. lineStart = token;
  617. token += lineLength-1;
  618. }
  619. else
  620. {
  621. token--;
  622. }
  623. }
  624. else
  625. {
  626. token += tokenLength;
  627. }
  628. }
  629. else
  630. {
  631. if (rightToLeft)
  632. {
  633. token = lineStart + lineLength;
  634. if (!handleDelimiters(&token, size, 1, area.x, &xPos, &yPos, &currentLineLength, &xPositionsIt, xPositions.end()))
  635. {
  636. break;
  637. }
  638. if (lineLengthsIt != lineLengths.end())
  639. {
  640. lineLength = *lineLengthsIt++;
  641. }
  642. lineStart = token;
  643. token += lineLength-1;
  644. }
  645. else
  646. {
  647. // Skip the rest of this line.
  648. unsigned int tokenLength = strcspn(token, "\n");
  649. if (tokenLength > 0)
  650. {
  651. // Get first token of next line.
  652. token += tokenLength;
  653. }
  654. }
  655. }
  656. }
  657. }
  658. void Font::end()
  659. {
  660. GP_ASSERT(_batch);
  661. _batch->end();
  662. }
  663. void Font::measureText(const char* text, unsigned int size, unsigned int* width, unsigned int* height)
  664. {
  665. GP_ASSERT(_size);
  666. GP_ASSERT(text);
  667. GP_ASSERT(width);
  668. GP_ASSERT(height);
  669. const int length = strlen(text);
  670. if (length == 0)
  671. {
  672. *width = 0;
  673. *height = 0;
  674. return;
  675. }
  676. float scale = (float)size / _size;
  677. const char* token = text;
  678. *width = 0;
  679. *height = size;
  680. // Measure a line at a time.
  681. while (token[0] != 0)
  682. {
  683. while (token[0] == '\n')
  684. {
  685. *height += size;
  686. ++token;
  687. }
  688. unsigned int tokenLength = strcspn(token, "\n");
  689. unsigned int tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  690. if (tokenWidth > *width)
  691. {
  692. *width = tokenWidth;
  693. }
  694. token += tokenLength;
  695. }
  696. }
  697. void Font::measureText(const char* text, const Rectangle& clip, unsigned int size, Rectangle* out, Justify justify, bool wrap, bool ignoreClip)
  698. {
  699. GP_ASSERT(_size);
  700. GP_ASSERT(text);
  701. GP_ASSERT(out);
  702. if (strlen(text) == 0)
  703. {
  704. out->set(0, 0, 0, 0);
  705. return;
  706. }
  707. float scale = (float)size / _size;
  708. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  709. if (vAlign == 0)
  710. {
  711. vAlign = ALIGN_TOP;
  712. }
  713. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  714. if (hAlign == 0)
  715. {
  716. hAlign = ALIGN_LEFT;
  717. }
  718. const char* token = text;
  719. std::vector<bool> emptyLines;
  720. std::vector<Vector2> lines;
  721. unsigned int lineWidth = 0;
  722. int yPos = clip.y + size;
  723. const float viewportHeight = clip.height;
  724. if (wrap)
  725. {
  726. unsigned int delimWidth = 0;
  727. bool reachedEOF = false;
  728. while (token[0] != 0)
  729. {
  730. // Handle delimiters until next token.
  731. char delimiter = token[0];
  732. while (delimiter == ' ' ||
  733. delimiter == '\t' ||
  734. delimiter == '\r' ||
  735. delimiter == '\n' ||
  736. delimiter == 0)
  737. {
  738. switch (delimiter)
  739. {
  740. case ' ':
  741. delimWidth += size >> 1;
  742. break;
  743. case '\r':
  744. case '\n':
  745. // Add line-height to vertical cursor.
  746. yPos += size;
  747. if (lineWidth > 0)
  748. {
  749. // Determine horizontal position and width.
  750. int hWhitespace = clip.width - lineWidth;
  751. int xPos = clip.x;
  752. if (hAlign == ALIGN_HCENTER)
  753. {
  754. xPos += hWhitespace / 2;
  755. }
  756. else if (hAlign == ALIGN_RIGHT)
  757. {
  758. xPos += hWhitespace;
  759. }
  760. // Record this line's size.
  761. emptyLines.push_back(false);
  762. lines.push_back(Vector2(xPos, lineWidth));
  763. }
  764. else
  765. {
  766. // Record the existence of an empty line.
  767. emptyLines.push_back(true);
  768. lines.push_back(Vector2(FLT_MAX, 0));
  769. }
  770. lineWidth = 0;
  771. delimWidth = 0;
  772. break;
  773. case '\t':
  774. delimWidth += (size >> 1)*4;
  775. break;
  776. case 0:
  777. reachedEOF = true;
  778. break;
  779. }
  780. if (reachedEOF)
  781. {
  782. break;
  783. }
  784. token++;
  785. delimiter = token[0];
  786. }
  787. if (reachedEOF)
  788. {
  789. break;
  790. }
  791. // Measure the next token.
  792. unsigned int tokenLength = strcspn(token, " \r\n\t");
  793. unsigned int tokenWidth = getTokenWidth(token, tokenLength, size, scale);
  794. // Wrap if necessary.
  795. if (lineWidth + tokenWidth + delimWidth > clip.width)
  796. {
  797. // Add line-height to vertical cursor.
  798. yPos += size;
  799. // Determine horizontal position and width.
  800. int hWhitespace = clip.width - lineWidth;
  801. int xPos = clip.x;
  802. if (hAlign == ALIGN_HCENTER)
  803. {
  804. xPos += hWhitespace / 2;
  805. }
  806. else if (hAlign == ALIGN_RIGHT)
  807. {
  808. xPos += hWhitespace;
  809. }
  810. // Record this line's size.
  811. emptyLines.push_back(false);
  812. lines.push_back(Vector2(xPos, lineWidth));
  813. lineWidth = 0;
  814. }
  815. else
  816. {
  817. lineWidth += delimWidth;
  818. }
  819. delimWidth = 0;
  820. lineWidth += tokenWidth;
  821. token += tokenLength;
  822. }
  823. }
  824. else
  825. {
  826. // Measure a whole line at a time.
  827. int emptyLinesCount = 0;
  828. while (token[0] != 0)
  829. {
  830. // Handle any number of consecutive newlines.
  831. bool nextLine = true;
  832. while (token[0] == '\n')
  833. {
  834. if (nextLine)
  835. {
  836. // Add line-height to vertical cursor.
  837. yPos += size * (emptyLinesCount+1);
  838. nextLine = false;
  839. emptyLinesCount = 0;
  840. emptyLines.push_back(false);
  841. }
  842. else
  843. {
  844. // Record the existence of an empty line.
  845. ++emptyLinesCount;
  846. emptyLines.push_back(true);
  847. lines.push_back(Vector2(FLT_MAX, 0));
  848. }
  849. token++;
  850. }
  851. // Measure the next line.
  852. unsigned int tokenLength = strcspn(token, "\n");
  853. lineWidth = getTokenWidth(token, tokenLength, size, scale);
  854. // Determine horizontal position and width.
  855. int xPos = clip.x;
  856. int hWhitespace = clip.width - lineWidth;
  857. if (hAlign == ALIGN_HCENTER)
  858. {
  859. xPos += hWhitespace / 2;
  860. }
  861. else if (hAlign == ALIGN_RIGHT)
  862. {
  863. xPos += hWhitespace;
  864. }
  865. // Record this line's size.
  866. lines.push_back(Vector2(xPos, lineWidth));
  867. token += tokenLength;
  868. }
  869. yPos += size;
  870. }
  871. if (wrap)
  872. {
  873. // Record the size of the last line.
  874. int hWhitespace = clip.width - lineWidth;
  875. int xPos = clip.x;
  876. if (hAlign == ALIGN_HCENTER)
  877. {
  878. xPos += hWhitespace / 2;
  879. }
  880. else if (hAlign == ALIGN_RIGHT)
  881. {
  882. xPos += hWhitespace;
  883. }
  884. lines.push_back(Vector2(xPos, lineWidth));
  885. }
  886. int x = INT_MAX;
  887. int y = clip.y;
  888. unsigned int width = 0;
  889. int height = yPos - clip.y;
  890. // Calculate top of text without clipping.
  891. int vWhitespace = viewportHeight - height;
  892. if (vAlign == ALIGN_VCENTER)
  893. {
  894. y += vWhitespace / 2;
  895. }
  896. else if (vAlign == ALIGN_BOTTOM)
  897. {
  898. y += vWhitespace;
  899. }
  900. int clippedTop = 0;
  901. int clippedBottom = 0;
  902. if (!ignoreClip)
  903. {
  904. // Trim rect to fit text that would actually be drawn within the given clip.
  905. if (y >= clip.y)
  906. {
  907. // Text goes off the bottom of the clip.
  908. clippedBottom = (height - viewportHeight) / size + 1;
  909. if (clippedBottom > 0)
  910. {
  911. // Also need to crop empty lines above non-empty lines that have been clipped.
  912. unsigned int emptyIndex = emptyLines.size() - clippedBottom;
  913. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  914. {
  915. height -= size;
  916. emptyIndex++;
  917. }
  918. height -= size * clippedBottom;
  919. }
  920. else
  921. {
  922. clippedBottom = 0;
  923. }
  924. }
  925. else
  926. {
  927. // Text goes above the top of the clip.
  928. clippedTop = (clip.y - y) / size + 1;
  929. if (clippedTop < 0)
  930. {
  931. clippedTop = 0;
  932. }
  933. // Also need to crop empty lines below non-empty lines that have been clipped.
  934. unsigned int emptyIndex = clippedTop;
  935. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  936. {
  937. y += size;
  938. height -= size;
  939. emptyIndex++;
  940. }
  941. if (vAlign == ALIGN_VCENTER)
  942. {
  943. // In this case lines may be clipped off the bottom as well.
  944. clippedBottom = (height - viewportHeight + vWhitespace/2 + 0.01) / size + 1;
  945. if (clippedBottom > 0)
  946. {
  947. emptyIndex = emptyLines.size() - clippedBottom;
  948. while (emptyIndex < emptyLines.size() && emptyLines[emptyIndex] == true)
  949. {
  950. height -= size;
  951. emptyIndex++;
  952. }
  953. height -= size * clippedBottom;
  954. }
  955. else
  956. {
  957. clippedBottom = 0;
  958. }
  959. }
  960. y = y + size * clippedTop;
  961. height = height - size * clippedTop;
  962. }
  963. }
  964. // Determine left-most x coordinate and largest width out of lines that have not been clipped.
  965. for (int i = clippedTop; i < (int)lines.size() - clippedBottom; ++i)
  966. {
  967. if (lines[i].x < x)
  968. {
  969. x = lines[i].x;
  970. }
  971. if (lines[i].y > width)
  972. {
  973. width = lines[i].y;
  974. }
  975. }
  976. if (!ignoreClip)
  977. {
  978. // Guarantee that the output rect will fit within the clip.
  979. out->x = (x >= clip.x)? x : clip.x;
  980. out->y = (y >= clip.y)? y : clip.y;
  981. out->width = (width <= clip.width)? width : clip.width;
  982. out->height = (height <= viewportHeight)? height : viewportHeight;
  983. }
  984. else
  985. {
  986. out->x = x;
  987. out->y = y;
  988. out->width = width;
  989. out->height = height;
  990. }
  991. }
  992. void Font::getMeasurementInfo(const char* text, const Rectangle& area, unsigned int size, Justify justify, bool wrap, bool rightToLeft,
  993. std::vector<int>* xPositions, int* yPosition, std::vector<unsigned int>* lineLengths)
  994. {
  995. GP_ASSERT(_size);
  996. GP_ASSERT(text);
  997. GP_ASSERT(yPosition);
  998. float scale = (float)size / _size;
  999. Justify vAlign = static_cast<Justify>(justify & 0xF0);
  1000. if (vAlign == 0)
  1001. {
  1002. vAlign = ALIGN_TOP;
  1003. }
  1004. Justify hAlign = static_cast<Justify>(justify & 0x0F);
  1005. if (hAlign == 0)
  1006. {
  1007. hAlign = ALIGN_LEFT;
  1008. }
  1009. const char* token = text;
  1010. const float areaHeight = area.height - size;
  1011. // For alignments other than top-left, need to calculate the y position to begin drawing from
  1012. // and the starting x position of each line. For right-to-left text, need to determine
  1013. // the number of characters on each line.
  1014. if (vAlign != ALIGN_TOP || hAlign != ALIGN_LEFT || rightToLeft)
  1015. {
  1016. int lineWidth = 0;
  1017. int delimWidth = 0;
  1018. if (wrap)
  1019. {
  1020. // Go a word at a time.
  1021. bool reachedEOF = false;
  1022. unsigned int lineLength = 0;
  1023. while (token[0] != 0)
  1024. {
  1025. unsigned int tokenWidth = 0;
  1026. // Handle delimiters until next token.
  1027. char delimiter = token[0];
  1028. while (delimiter == ' ' ||
  1029. delimiter == '\t' ||
  1030. delimiter == '\r' ||
  1031. delimiter == '\n' ||
  1032. delimiter == 0)
  1033. {
  1034. switch (delimiter)
  1035. {
  1036. case ' ':
  1037. delimWidth += size >> 1;
  1038. lineLength++;
  1039. break;
  1040. case '\r':
  1041. case '\n':
  1042. *yPosition += size;
  1043. if (lineWidth > 0)
  1044. {
  1045. addLineInfo(area, lineWidth, lineLength, hAlign, xPositions, lineLengths, rightToLeft);
  1046. }
  1047. lineWidth = 0;
  1048. lineLength = 0;
  1049. delimWidth = 0;
  1050. break;
  1051. case '\t':
  1052. delimWidth += (size >> 1)*4;
  1053. lineLength++;
  1054. break;
  1055. case 0:
  1056. reachedEOF = true;
  1057. break;
  1058. }
  1059. if (reachedEOF)
  1060. {
  1061. break;
  1062. }
  1063. token++;
  1064. delimiter = token[0];
  1065. }
  1066. if (reachedEOF || token == NULL)
  1067. {
  1068. break;
  1069. }
  1070. unsigned int tokenLength = strcspn(token, " \r\n\t");
  1071. tokenWidth += getTokenWidth(token, tokenLength, size, scale);
  1072. // Wrap if necessary.
  1073. if (lineWidth + tokenWidth + delimWidth > area.width)
  1074. {
  1075. *yPosition += size;
  1076. // Push position of current line.
  1077. if (lineLength)
  1078. {
  1079. addLineInfo(area, lineWidth, lineLength-1, hAlign, xPositions, lineLengths, rightToLeft);
  1080. }
  1081. else
  1082. {
  1083. addLineInfo(area, lineWidth, tokenLength, hAlign, xPositions, lineLengths, rightToLeft);
  1084. }
  1085. // Move token to the next line.
  1086. lineWidth = 0;
  1087. lineLength = 0;
  1088. delimWidth = 0;
  1089. }
  1090. else
  1091. {
  1092. lineWidth += delimWidth;
  1093. delimWidth = 0;
  1094. }
  1095. lineWidth += tokenWidth;
  1096. lineLength += tokenLength;
  1097. token += tokenLength;
  1098. }
  1099. // Final calculation of vertical position.
  1100. int textHeight = *yPosition - area.y;
  1101. int vWhiteSpace = areaHeight - textHeight;
  1102. if (vAlign == ALIGN_VCENTER)
  1103. {
  1104. *yPosition = area.y + vWhiteSpace / 2;
  1105. }
  1106. else if (vAlign == ALIGN_BOTTOM)
  1107. {
  1108. *yPosition = area.y + vWhiteSpace;
  1109. }
  1110. // Calculation of final horizontal position.
  1111. addLineInfo(area, lineWidth, lineLength, hAlign, xPositions, lineLengths, rightToLeft);
  1112. }
  1113. else
  1114. {
  1115. // Go a line at a time.
  1116. while (token[0] != 0)
  1117. {
  1118. char delimiter = token[0];
  1119. while (delimiter == '\n')
  1120. {
  1121. *yPosition += size;
  1122. ++token;
  1123. delimiter = token[0];
  1124. }
  1125. unsigned int tokenLength = strcspn(token, "\n");
  1126. if (tokenLength == 0)
  1127. {
  1128. tokenLength = strlen(token);
  1129. }
  1130. int lineWidth = getTokenWidth(token, tokenLength, size, scale);
  1131. addLineInfo(area, lineWidth, tokenLength, hAlign, xPositions, lineLengths, rightToLeft);
  1132. token += tokenLength;
  1133. }
  1134. int textHeight = *yPosition - area.y;
  1135. int vWhiteSpace = areaHeight - textHeight;
  1136. if (vAlign == ALIGN_VCENTER)
  1137. {
  1138. *yPosition = area.y + vWhiteSpace / 2;
  1139. }
  1140. else if (vAlign == ALIGN_BOTTOM)
  1141. {
  1142. *yPosition = area.y + vWhiteSpace;
  1143. }
  1144. }
  1145. if (vAlign == ALIGN_TOP)
  1146. {
  1147. *yPosition = area.y;
  1148. }
  1149. }
  1150. }
  1151. int Font::getIndexAtLocation(const char* text, const Rectangle& area, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  1152. Justify justify, bool wrap, bool rightToLeft)
  1153. {
  1154. return getIndexOrLocation(text, area, size, inLocation, outLocation, -1, justify, wrap, rightToLeft);
  1155. }
  1156. void Font::getLocationAtIndex(const char* text, const Rectangle& clip, unsigned int size, Vector2* outLocation, const unsigned int destIndex,
  1157. Justify justify, bool wrap, bool rightToLeft)
  1158. {
  1159. getIndexOrLocation(text, clip, size, *outLocation, outLocation, (const int)destIndex, justify, wrap, rightToLeft);
  1160. }
  1161. int Font::getIndexOrLocation(const char* text, const Rectangle& area, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  1162. const int destIndex, Justify justify, bool wrap, bool rightToLeft)
  1163. {
  1164. GP_ASSERT(_size);
  1165. GP_ASSERT(text);
  1166. GP_ASSERT(outLocation);
  1167. unsigned int charIndex = 0;
  1168. // Essentially need to measure text until we reach inLocation.
  1169. float scale = (float)size / _size;
  1170. const int length = strlen(text);
  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 = 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 = 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 int 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. }