Font.cpp 55 KB

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