Font.cpp 53 KB

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