You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fontstash.h 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. //
  2. // Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef FONS_H
  19. #define FONS_H
  20. #define FONS_INVALID -1
  21. enum FONSflags {
  22. FONS_ZERO_TOPLEFT = 1,
  23. FONS_ZERO_BOTTOMLEFT = 2,
  24. };
  25. enum FONSalign {
  26. // Horizontal align
  27. FONS_ALIGN_LEFT = 1<<0, // Default
  28. FONS_ALIGN_CENTER = 1<<1,
  29. FONS_ALIGN_RIGHT = 1<<2,
  30. // Vertical align
  31. FONS_ALIGN_TOP = 1<<3,
  32. FONS_ALIGN_MIDDLE = 1<<4,
  33. FONS_ALIGN_BOTTOM = 1<<5,
  34. FONS_ALIGN_BASELINE = 1<<6, // Default
  35. };
  36. enum FONSerrorCode {
  37. // Font atlas is full.
  38. FONS_ATLAS_FULL = 1,
  39. // Scratch memory used to render glyphs is full, requested size reported in 'val', you may need to bump up FONS_SCRATCH_BUF_SIZE.
  40. FONS_SCRATCH_FULL = 2,
  41. // Calls to fonsPushState has created too large stack, if you need deep state stack bump up FONS_MAX_STATES.
  42. FONS_STATES_OVERFLOW = 3,
  43. // Trying to pop too many states fonsPopState().
  44. FONS_STATES_UNDERFLOW = 4,
  45. };
  46. struct FONSparams {
  47. int width, height;
  48. unsigned char flags;
  49. void* userPtr;
  50. int (*renderCreate)(void* uptr, int width, int height);
  51. int (*renderResize)(void* uptr, int width, int height);
  52. void (*renderUpdate)(void* uptr, int* rect, const unsigned char* data);
  53. void (*renderDraw)(void* uptr, const float* verts, const float* tcoords, const unsigned int* colors, int nverts);
  54. void (*renderDelete)(void* uptr);
  55. };
  56. typedef struct FONSparams FONSparams;
  57. struct FONSquad
  58. {
  59. float x0,y0,s0,t0;
  60. float x1,y1,s1,t1;
  61. };
  62. typedef struct FONSquad FONSquad;
  63. struct FONStextIter {
  64. float x, y, nextx, nexty, scale, spacing;
  65. unsigned int codepoint;
  66. short isize, iblur;
  67. struct FONSfont* font;
  68. int prevGlyphIndex;
  69. const char* str;
  70. const char* next;
  71. const char* end;
  72. unsigned int utf8state;
  73. };
  74. typedef struct FONStextIter FONStextIter;
  75. typedef struct FONScontext FONScontext;
  76. // Constructor and destructor.
  77. FONScontext* fonsCreateInternal(FONSparams* params);
  78. void fonsDeleteInternal(FONScontext* s);
  79. void fonsSetErrorCallback(FONScontext* s, void (*callback)(void* uptr, int error, int val), void* uptr);
  80. // Returns current atlas size.
  81. void fonsGetAtlasSize(FONScontext* s, int* width, int* height);
  82. // Expands the atlas size.
  83. int fonsExpandAtlas(FONScontext* s, int width, int height);
  84. // Resets the whole stash.
  85. int fonsResetAtlas(FONScontext* stash, int width, int height);
  86. // Add fonts
  87. int fonsAddFont(FONScontext* s, const char* name, const char* path);
  88. int fonsAddFontMem(FONScontext* s, const char* name, unsigned char* data, int ndata, int freeData);
  89. int fonsGetFontByName(FONScontext* s, const char* name);
  90. // State handling
  91. void fonsPushState(FONScontext* s);
  92. void fonsPopState(FONScontext* s);
  93. void fonsClearState(FONScontext* s);
  94. // State setting
  95. void fonsSetSize(FONScontext* s, float size);
  96. void fonsSetColor(FONScontext* s, unsigned int color);
  97. void fonsSetSpacing(FONScontext* s, float spacing);
  98. void fonsSetBlur(FONScontext* s, float blur);
  99. void fonsSetAlign(FONScontext* s, int align);
  100. void fonsSetFont(FONScontext* s, int font);
  101. // Draw text
  102. float fonsDrawText(FONScontext* s, float x, float y, const char* string, const char* end);
  103. // Measure text
  104. float fonsTextBounds(FONScontext* s, float x, float y, const char* string, const char* end, float* bounds);
  105. void fonsLineBounds(FONScontext* s, float y, float* miny, float* maxy);
  106. void fonsVertMetrics(FONScontext* s, float* ascender, float* descender, float* lineh);
  107. // Text iterator
  108. int fonsTextIterInit(FONScontext* stash, FONStextIter* iter, float x, float y, const char* str, const char* end);
  109. int fonsTextIterNext(FONScontext* stash, FONStextIter* iter, struct FONSquad* quad);
  110. // Pull texture changes
  111. const unsigned char* fonsGetTextureData(FONScontext* stash, int* width, int* height);
  112. int fonsValidateTexture(FONScontext* s, int* dirty);
  113. // Draws the stash texture for debugging
  114. void fonsDrawDebug(FONScontext* s, float x, float y);
  115. #endif // FONTSTASH_H
  116. #ifdef FONTSTASH_IMPLEMENTATION
  117. #define FONS_NOTUSED(v) (void)sizeof(v)
  118. #ifdef FONS_USE_FREETYPE
  119. #include <ft2build.h>
  120. #include FT_FREETYPE_H
  121. #include FT_ADVANCES_H
  122. #include <math.h>
  123. struct FONSttFontImpl {
  124. FT_Face font;
  125. };
  126. typedef struct FONSttFontImpl FONSttFontImpl;
  127. static FT_Library ftLibrary;
  128. int fons__tt_init(FONScontext *context)
  129. {
  130. FT_Error ftError;
  131. FONS_NOTUSED(context);
  132. ftError = FT_Init_FreeType(&ftLibrary);
  133. return ftError == 0;
  134. }
  135. int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize)
  136. {
  137. FT_Error ftError;
  138. FONS_NOTUSED(context);
  139. //font->font.userdata = stash;
  140. ftError = FT_New_Memory_Face(ftLibrary, (const FT_Byte*)data, dataSize, 0, &font->font);
  141. return ftError == 0;
  142. }
  143. void fons__tt_getFontVMetrics(FONSttFontImpl *font, int *ascent, int *descent, int *lineGap)
  144. {
  145. *ascent = font->font->ascender;
  146. *descent = font->font->descender;
  147. *lineGap = font->font->height - (*ascent - *descent);
  148. }
  149. float fons__tt_getPixelHeightScale(FONSttFontImpl *font, float size)
  150. {
  151. return size / (font->font->ascender - font->font->descender);
  152. }
  153. int fons__tt_getGlyphIndex(FONSttFontImpl *font, int codepoint)
  154. {
  155. return FT_Get_Char_Index(font->font, codepoint);
  156. }
  157. int fons__tt_buildGlyphBitmap(FONSttFontImpl *font, int glyph, float size, float scale,
  158. int *advance, int *lsb, int *x0, int *y0, int *x1, int *y1)
  159. {
  160. FT_Error ftError;
  161. FT_GlyphSlot ftGlyph;
  162. FT_Fixed advFixed;
  163. FONS_NOTUSED(scale);
  164. ftError = FT_Set_Pixel_Sizes(font->font, 0, (FT_UInt)(size * (float)font->font->units_per_EM / (float)(font->font->ascender - font->font->descender)));
  165. if (ftError) return 0;
  166. ftError = FT_Load_Glyph(font->font, glyph, FT_LOAD_RENDER);
  167. if (ftError) return 0;
  168. ftError = FT_Get_Advance(font->font, glyph, FT_LOAD_NO_SCALE, &advFixed);
  169. if (ftError) return 0;
  170. ftGlyph = font->font->glyph;
  171. *advance = (int)advFixed;
  172. *lsb = (int)ftGlyph->metrics.horiBearingX;
  173. *x0 = ftGlyph->bitmap_left;
  174. *x1 = *x0 + ftGlyph->bitmap.width;
  175. *y0 = -ftGlyph->bitmap_top;
  176. *y1 = *y0 + ftGlyph->bitmap.rows;
  177. return 1;
  178. }
  179. void fons__tt_renderGlyphBitmap(FONSttFontImpl *font, unsigned char *output, int outWidth, int outHeight, int outStride,
  180. float scaleX, float scaleY, int glyph)
  181. {
  182. FT_GlyphSlot ftGlyph = font->font->glyph;
  183. int ftGlyphOffset = 0;
  184. int x, y;
  185. FONS_NOTUSED(outWidth);
  186. FONS_NOTUSED(outHeight);
  187. FONS_NOTUSED(scaleX);
  188. FONS_NOTUSED(scaleY);
  189. FONS_NOTUSED(glyph); // glyph has already been loaded by fons__tt_buildGlyphBitmap
  190. for ( y = 0; y < ftGlyph->bitmap.rows; y++ ) {
  191. for ( x = 0; x < ftGlyph->bitmap.width; x++ ) {
  192. output[(y * outStride) + x] = ftGlyph->bitmap.buffer[ftGlyphOffset++];
  193. }
  194. }
  195. }
  196. int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)
  197. {
  198. FT_Vector ftKerning;
  199. FT_Get_Kerning(font->font, glyph1, glyph2, FT_KERNING_DEFAULT, &ftKerning);
  200. return (int)((ftKerning.x + 32) >> 6); // Round up and convert to integer
  201. }
  202. #else
  203. #define STB_TRUETYPE_IMPLEMENTATION
  204. static void* fons__tmpalloc(size_t size, void* up);
  205. static void fons__tmpfree(void* ptr, void* up);
  206. #define STBTT_malloc(x,u) fons__tmpalloc(x,u)
  207. #define STBTT_free(x,u) fons__tmpfree(x,u)
  208. #include "stb_truetype.h"
  209. struct FONSttFontImpl {
  210. stbtt_fontinfo font;
  211. };
  212. typedef struct FONSttFontImpl FONSttFontImpl;
  213. int fons__tt_init(FONScontext *context)
  214. {
  215. FONS_NOTUSED(context);
  216. return 1;
  217. }
  218. int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize)
  219. {
  220. int stbError;
  221. FONS_NOTUSED(dataSize);
  222. font->font.userdata = context;
  223. stbError = stbtt_InitFont(&font->font, data, 0);
  224. return stbError;
  225. }
  226. void fons__tt_getFontVMetrics(FONSttFontImpl *font, int *ascent, int *descent, int *lineGap)
  227. {
  228. stbtt_GetFontVMetrics(&font->font, ascent, descent, lineGap);
  229. }
  230. float fons__tt_getPixelHeightScale(FONSttFontImpl *font, float size)
  231. {
  232. return stbtt_ScaleForPixelHeight(&font->font, size);
  233. }
  234. int fons__tt_getGlyphIndex(FONSttFontImpl *font, int codepoint)
  235. {
  236. return stbtt_FindGlyphIndex(&font->font, codepoint);
  237. }
  238. int fons__tt_buildGlyphBitmap(FONSttFontImpl *font, int glyph, float size, float scale,
  239. int *advance, int *lsb, int *x0, int *y0, int *x1, int *y1)
  240. {
  241. FONS_NOTUSED(size);
  242. stbtt_GetGlyphHMetrics(&font->font, glyph, advance, lsb);
  243. stbtt_GetGlyphBitmapBox(&font->font, glyph, scale, scale, x0, y0, x1, y1);
  244. return 1;
  245. }
  246. void fons__tt_renderGlyphBitmap(FONSttFontImpl *font, unsigned char *output, int outWidth, int outHeight, int outStride,
  247. float scaleX, float scaleY, int glyph)
  248. {
  249. stbtt_MakeGlyphBitmap(&font->font, output, outWidth, outHeight, outStride, scaleX, scaleY, glyph);
  250. }
  251. int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)
  252. {
  253. return stbtt_GetGlyphKernAdvance(&font->font, glyph1, glyph2);
  254. }
  255. #endif
  256. #ifndef FONS_SCRATCH_BUF_SIZE
  257. # define FONS_SCRATCH_BUF_SIZE 64000
  258. #endif
  259. #ifndef FONS_HASH_LUT_SIZE
  260. # define FONS_HASH_LUT_SIZE 256
  261. #endif
  262. #ifndef FONS_INIT_FONTS
  263. # define FONS_INIT_FONTS 4
  264. #endif
  265. #ifndef FONS_INIT_GLYPHS
  266. # define FONS_INIT_GLYPHS 256
  267. #endif
  268. #ifndef FONS_INIT_ATLAS_NODES
  269. # define FONS_INIT_ATLAS_NODES 256
  270. #endif
  271. #ifndef FONS_VERTEX_COUNT
  272. # define FONS_VERTEX_COUNT 1024
  273. #endif
  274. #ifndef FONS_MAX_STATES
  275. # define FONS_MAX_STATES 20
  276. #endif
  277. #ifndef FONS_MAX_FALLBACKS
  278. # define FONS_MAX_FALLBACKS 20
  279. #endif
  280. static unsigned int fons__hashint(unsigned int a)
  281. {
  282. a += ~(a<<15);
  283. a ^= (a>>10);
  284. a += (a<<3);
  285. a ^= (a>>6);
  286. a += ~(a<<11);
  287. a ^= (a>>16);
  288. return a;
  289. }
  290. static int fons__mini(int a, int b)
  291. {
  292. return a < b ? a : b;
  293. }
  294. static int fons__maxi(int a, int b)
  295. {
  296. return a > b ? a : b;
  297. }
  298. struct FONSglyph
  299. {
  300. unsigned int codepoint;
  301. int index;
  302. int next;
  303. short size, blur;
  304. short x0,y0,x1,y1;
  305. short xadv,xoff,yoff;
  306. };
  307. typedef struct FONSglyph FONSglyph;
  308. struct FONSfont
  309. {
  310. FONSttFontImpl font;
  311. char name[64];
  312. unsigned char* data;
  313. int dataSize;
  314. unsigned char freeData;
  315. float ascender;
  316. float descender;
  317. float lineh;
  318. FONSglyph* glyphs;
  319. int cglyphs;
  320. int nglyphs;
  321. int lut[FONS_HASH_LUT_SIZE];
  322. int fallbacks[FONS_MAX_FALLBACKS];
  323. int nfallbacks;
  324. };
  325. typedef struct FONSfont FONSfont;
  326. struct FONSstate
  327. {
  328. int font;
  329. int align;
  330. float size;
  331. unsigned int color;
  332. float blur;
  333. float spacing;
  334. };
  335. typedef struct FONSstate FONSstate;
  336. struct FONSatlasNode {
  337. short x, y, width;
  338. };
  339. typedef struct FONSatlasNode FONSatlasNode;
  340. struct FONSatlas
  341. {
  342. int width, height;
  343. FONSatlasNode* nodes;
  344. int nnodes;
  345. int cnodes;
  346. };
  347. typedef struct FONSatlas FONSatlas;
  348. struct FONScontext
  349. {
  350. FONSparams params;
  351. float itw,ith;
  352. unsigned char* texData;
  353. int dirtyRect[4];
  354. FONSfont** fonts;
  355. FONSatlas* atlas;
  356. int cfonts;
  357. int nfonts;
  358. float verts[FONS_VERTEX_COUNT*2];
  359. float tcoords[FONS_VERTEX_COUNT*2];
  360. unsigned int colors[FONS_VERTEX_COUNT];
  361. int nverts;
  362. unsigned char* scratch;
  363. int nscratch;
  364. FONSstate states[FONS_MAX_STATES];
  365. int nstates;
  366. void (*handleError)(void* uptr, int error, int val);
  367. void* errorUptr;
  368. };
  369. #ifdef STB_TRUETYPE_IMPLEMENTATION
  370. static void* fons__tmpalloc(size_t size, void* up)
  371. {
  372. unsigned char* ptr;
  373. FONScontext* stash = (FONScontext*)up;
  374. // 16-byte align the returned pointer
  375. size = (size + 0xf) & ~0xf;
  376. if (stash->nscratch+(int)size > FONS_SCRATCH_BUF_SIZE) {
  377. if (stash->handleError)
  378. stash->handleError(stash->errorUptr, FONS_SCRATCH_FULL, stash->nscratch+(int)size);
  379. return NULL;
  380. }
  381. ptr = stash->scratch + stash->nscratch;
  382. stash->nscratch += (int)size;
  383. return ptr;
  384. }
  385. static void fons__tmpfree(void* ptr, void* up)
  386. {
  387. (void)ptr;
  388. (void)up;
  389. // empty
  390. }
  391. #endif // STB_TRUETYPE_IMPLEMENTATION
  392. // Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  393. // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  394. #define FONS_UTF8_ACCEPT 0
  395. #define FONS_UTF8_REJECT 12
  396. static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsigned int byte)
  397. {
  398. static const unsigned char utf8d[] = {
  399. // The first part of the table maps bytes to character classes that
  400. // to reduce the size of the transition table and create bitmasks.
  401. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  402. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  403. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  404. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  405. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  406. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  407. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  408. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  409. // The second part is a transition table that maps a combination
  410. // of a state of the automaton and a character class to a state.
  411. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  412. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  413. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  414. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  415. 12,36,12,12,12,12,12,12,12,12,12,12,
  416. };
  417. unsigned int type = utf8d[byte];
  418. *codep = (*state != FONS_UTF8_ACCEPT) ?
  419. (byte & 0x3fu) | (*codep << 6) :
  420. (0xff >> type) & (byte);
  421. *state = utf8d[256 + *state + type];
  422. return *state;
  423. }
  424. // Atlas based on Skyline Bin Packer by Jukka Jylänki
  425. static void fons__deleteAtlas(FONSatlas* atlas)
  426. {
  427. if (atlas == NULL) return;
  428. if (atlas->nodes != NULL) free(atlas->nodes);
  429. free(atlas);
  430. }
  431. static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
  432. {
  433. FONSatlas* atlas = NULL;
  434. // Allocate memory for the font stash.
  435. atlas = (FONSatlas*)malloc(sizeof(FONSatlas));
  436. if (atlas == NULL) goto error;
  437. memset(atlas, 0, sizeof(FONSatlas));
  438. atlas->width = w;
  439. atlas->height = h;
  440. // Allocate space for skyline nodes
  441. atlas->nodes = (FONSatlasNode*)malloc(sizeof(FONSatlasNode) * nnodes);
  442. if (atlas->nodes == NULL) goto error;
  443. memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
  444. atlas->nnodes = 0;
  445. atlas->cnodes = nnodes;
  446. // Init root node.
  447. atlas->nodes[0].x = 0;
  448. atlas->nodes[0].y = 0;
  449. atlas->nodes[0].width = (short)w;
  450. atlas->nnodes++;
  451. return atlas;
  452. error:
  453. if (atlas) fons__deleteAtlas(atlas);
  454. return NULL;
  455. }
  456. static int fons__atlasInsertNode(FONSatlas* atlas, int idx, int x, int y, int w)
  457. {
  458. int i;
  459. // Insert node
  460. if (atlas->nnodes+1 > atlas->cnodes) {
  461. atlas->cnodes = atlas->cnodes == 0 ? 8 : atlas->cnodes * 2;
  462. atlas->nodes = (FONSatlasNode*)realloc(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
  463. if (atlas->nodes == NULL)
  464. return 0;
  465. }
  466. for (i = atlas->nnodes; i > idx; i--)
  467. atlas->nodes[i] = atlas->nodes[i-1];
  468. atlas->nodes[idx].x = (short)x;
  469. atlas->nodes[idx].y = (short)y;
  470. atlas->nodes[idx].width = (short)w;
  471. atlas->nnodes++;
  472. return 1;
  473. }
  474. static void fons__atlasRemoveNode(FONSatlas* atlas, int idx)
  475. {
  476. int i;
  477. if (atlas->nnodes == 0) return;
  478. for (i = idx; i < atlas->nnodes-1; i++)
  479. atlas->nodes[i] = atlas->nodes[i+1];
  480. atlas->nnodes--;
  481. }
  482. static void fons__atlasExpand(FONSatlas* atlas, int w, int h)
  483. {
  484. // Insert node for empty space
  485. if (w > atlas->width)
  486. fons__atlasInsertNode(atlas, atlas->nnodes, atlas->width, 0, w - atlas->width);
  487. atlas->width = w;
  488. atlas->height = h;
  489. }
  490. static void fons__atlasReset(FONSatlas* atlas, int w, int h)
  491. {
  492. atlas->width = w;
  493. atlas->height = h;
  494. atlas->nnodes = 0;
  495. // Init root node.
  496. atlas->nodes[0].x = 0;
  497. atlas->nodes[0].y = 0;
  498. atlas->nodes[0].width = (short)w;
  499. atlas->nnodes++;
  500. }
  501. static int fons__atlasAddSkylineLevel(FONSatlas* atlas, int idx, int x, int y, int w, int h)
  502. {
  503. int i;
  504. // Insert new node
  505. if (fons__atlasInsertNode(atlas, idx, x, y+h, w) == 0)
  506. return 0;
  507. // Delete skyline segments that fall under the shadow of the new segment.
  508. for (i = idx+1; i < atlas->nnodes; i++) {
  509. if (atlas->nodes[i].x < atlas->nodes[i-1].x + atlas->nodes[i-1].width) {
  510. int shrink = atlas->nodes[i-1].x + atlas->nodes[i-1].width - atlas->nodes[i].x;
  511. atlas->nodes[i].x += (short)shrink;
  512. atlas->nodes[i].width -= (short)shrink;
  513. if (atlas->nodes[i].width <= 0) {
  514. fons__atlasRemoveNode(atlas, i);
  515. i--;
  516. } else {
  517. break;
  518. }
  519. } else {
  520. break;
  521. }
  522. }
  523. // Merge same height skyline segments that are next to each other.
  524. for (i = 0; i < atlas->nnodes-1; i++) {
  525. if (atlas->nodes[i].y == atlas->nodes[i+1].y) {
  526. atlas->nodes[i].width += atlas->nodes[i+1].width;
  527. fons__atlasRemoveNode(atlas, i+1);
  528. i--;
  529. }
  530. }
  531. return 1;
  532. }
  533. static int fons__atlasRectFits(FONSatlas* atlas, int i, int w, int h)
  534. {
  535. // Checks if there is enough space at the location of skyline span 'i',
  536. // and return the max height of all skyline spans under that at that location,
  537. // (think tetris block being dropped at that position). Or -1 if no space found.
  538. int x = atlas->nodes[i].x;
  539. int y = atlas->nodes[i].y;
  540. int spaceLeft;
  541. if (x + w > atlas->width)
  542. return -1;
  543. spaceLeft = w;
  544. while (spaceLeft > 0) {
  545. if (i == atlas->nnodes) return -1;
  546. y = fons__maxi(y, atlas->nodes[i].y);
  547. if (y + h > atlas->height) return -1;
  548. spaceLeft -= atlas->nodes[i].width;
  549. ++i;
  550. }
  551. return y;
  552. }
  553. static int fons__atlasAddRect(FONSatlas* atlas, int rw, int rh, int* rx, int* ry)
  554. {
  555. int besth = atlas->height, bestw = atlas->width, besti = -1;
  556. int bestx = -1, besty = -1, i;
  557. // Bottom left fit heuristic.
  558. for (i = 0; i < atlas->nnodes; i++) {
  559. int y = fons__atlasRectFits(atlas, i, rw, rh);
  560. if (y != -1) {
  561. if (y + rh < besth || (y + rh == besth && atlas->nodes[i].width < bestw)) {
  562. besti = i;
  563. bestw = atlas->nodes[i].width;
  564. besth = y + rh;
  565. bestx = atlas->nodes[i].x;
  566. besty = y;
  567. }
  568. }
  569. }
  570. if (besti == -1)
  571. return 0;
  572. // Perform the actual packing.
  573. if (fons__atlasAddSkylineLevel(atlas, besti, bestx, besty, rw, rh) == 0)
  574. return 0;
  575. *rx = bestx;
  576. *ry = besty;
  577. return 1;
  578. }
  579. static void fons__addWhiteRect(FONScontext* stash, int w, int h)
  580. {
  581. int x, y, gx, gy;
  582. unsigned char* dst;
  583. if (fons__atlasAddRect(stash->atlas, w, h, &gx, &gy) == 0)
  584. return;
  585. // Rasterize
  586. dst = &stash->texData[gx + gy * stash->params.width];
  587. for (y = 0; y < h; y++) {
  588. for (x = 0; x < w; x++)
  589. dst[x] = 0xff;
  590. dst += stash->params.width;
  591. }
  592. stash->dirtyRect[0] = fons__mini(stash->dirtyRect[0], gx);
  593. stash->dirtyRect[1] = fons__mini(stash->dirtyRect[1], gy);
  594. stash->dirtyRect[2] = fons__maxi(stash->dirtyRect[2], gx+w);
  595. stash->dirtyRect[3] = fons__maxi(stash->dirtyRect[3], gy+h);
  596. }
  597. FONScontext* fonsCreateInternal(FONSparams* params)
  598. {
  599. FONScontext* stash = NULL;
  600. // Allocate memory for the font stash.
  601. stash = (FONScontext*)malloc(sizeof(FONScontext));
  602. if (stash == NULL) goto error;
  603. memset(stash, 0, sizeof(FONScontext));
  604. stash->params = *params;
  605. // Allocate scratch buffer.
  606. stash->scratch = (unsigned char*)malloc(FONS_SCRATCH_BUF_SIZE);
  607. if (stash->scratch == NULL) goto error;
  608. // Initialize implementation library
  609. if (!fons__tt_init(stash)) goto error;
  610. if (stash->params.renderCreate != NULL) {
  611. if (stash->params.renderCreate(stash->params.userPtr, stash->params.width, stash->params.height) == 0)
  612. goto error;
  613. }
  614. stash->atlas = fons__allocAtlas(stash->params.width, stash->params.height, FONS_INIT_ATLAS_NODES);
  615. if (stash->atlas == NULL) goto error;
  616. // Allocate space for fonts.
  617. stash->fonts = (FONSfont**)malloc(sizeof(FONSfont*) * FONS_INIT_FONTS);
  618. if (stash->fonts == NULL) goto error;
  619. memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
  620. stash->cfonts = FONS_INIT_FONTS;
  621. stash->nfonts = 0;
  622. // Create texture for the cache.
  623. stash->itw = 1.0f/stash->params.width;
  624. stash->ith = 1.0f/stash->params.height;
  625. stash->texData = (unsigned char*)malloc(stash->params.width * stash->params.height);
  626. if (stash->texData == NULL) goto error;
  627. memset(stash->texData, 0, stash->params.width * stash->params.height);
  628. stash->dirtyRect[0] = stash->params.width;
  629. stash->dirtyRect[1] = stash->params.height;
  630. stash->dirtyRect[2] = 0;
  631. stash->dirtyRect[3] = 0;
  632. // Add white rect at 0,0 for debug drawing.
  633. fons__addWhiteRect(stash, 2,2);
  634. fonsPushState(stash);
  635. fonsClearState(stash);
  636. return stash;
  637. error:
  638. fonsDeleteInternal(stash);
  639. return NULL;
  640. }
  641. static FONSstate* fons__getState(FONScontext* stash)
  642. {
  643. return &stash->states[stash->nstates-1];
  644. }
  645. int fonsAddFallbackFont(FONScontext* stash, int base, int fallback)
  646. {
  647. FONSfont* baseFont = stash->fonts[base];
  648. if (baseFont->nfallbacks < FONS_MAX_FALLBACKS) {
  649. baseFont->fallbacks[baseFont->nfallbacks++] = fallback;
  650. return 1;
  651. }
  652. return 0;
  653. }
  654. void fonsSetSize(FONScontext* stash, float size)
  655. {
  656. fons__getState(stash)->size = size;
  657. }
  658. void fonsSetColor(FONScontext* stash, unsigned int color)
  659. {
  660. fons__getState(stash)->color = color;
  661. }
  662. void fonsSetSpacing(FONScontext* stash, float spacing)
  663. {
  664. fons__getState(stash)->spacing = spacing;
  665. }
  666. void fonsSetBlur(FONScontext* stash, float blur)
  667. {
  668. fons__getState(stash)->blur = blur;
  669. }
  670. void fonsSetAlign(FONScontext* stash, int align)
  671. {
  672. fons__getState(stash)->align = align;
  673. }
  674. void fonsSetFont(FONScontext* stash, int font)
  675. {
  676. fons__getState(stash)->font = font;
  677. }
  678. void fonsPushState(FONScontext* stash)
  679. {
  680. if (stash->nstates >= FONS_MAX_STATES) {
  681. if (stash->handleError)
  682. stash->handleError(stash->errorUptr, FONS_STATES_OVERFLOW, 0);
  683. return;
  684. }
  685. if (stash->nstates > 0)
  686. memcpy(&stash->states[stash->nstates], &stash->states[stash->nstates-1], sizeof(FONSstate));
  687. stash->nstates++;
  688. }
  689. void fonsPopState(FONScontext* stash)
  690. {
  691. if (stash->nstates <= 1) {
  692. if (stash->handleError)
  693. stash->handleError(stash->errorUptr, FONS_STATES_UNDERFLOW, 0);
  694. return;
  695. }
  696. stash->nstates--;
  697. }
  698. void fonsClearState(FONScontext* stash)
  699. {
  700. FONSstate* state = fons__getState(stash);
  701. state->size = 12.0f;
  702. state->color = 0xffffffff;
  703. state->font = 0;
  704. state->blur = 0;
  705. state->spacing = 0;
  706. state->align = FONS_ALIGN_LEFT | FONS_ALIGN_BASELINE;
  707. }
  708. static void fons__freeFont(FONSfont* font)
  709. {
  710. if (font == NULL) return;
  711. if (font->glyphs) free(font->glyphs);
  712. if (font->freeData && font->data) free(font->data);
  713. free(font);
  714. }
  715. static int fons__allocFont(FONScontext* stash)
  716. {
  717. FONSfont* font = NULL;
  718. if (stash->nfonts+1 > stash->cfonts) {
  719. stash->cfonts = stash->cfonts == 0 ? 8 : stash->cfonts * 2;
  720. stash->fonts = (FONSfont**)realloc(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
  721. if (stash->fonts == NULL)
  722. return -1;
  723. }
  724. font = (FONSfont*)malloc(sizeof(FONSfont));
  725. if (font == NULL) goto error;
  726. memset(font, 0, sizeof(FONSfont));
  727. font->glyphs = (FONSglyph*)malloc(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
  728. if (font->glyphs == NULL) goto error;
  729. font->cglyphs = FONS_INIT_GLYPHS;
  730. font->nglyphs = 0;
  731. stash->fonts[stash->nfonts++] = font;
  732. return stash->nfonts-1;
  733. error:
  734. fons__freeFont(font);
  735. return FONS_INVALID;
  736. }
  737. int fonsAddFont(FONScontext* stash, const char* name, const char* path)
  738. {
  739. FILE* fp = 0;
  740. int dataSize = 0, readed;
  741. unsigned char* data = NULL;
  742. // Read in the font data.
  743. fp = fopen(path, "rb");
  744. if (fp == NULL) goto error;
  745. fseek(fp,0,SEEK_END);
  746. dataSize = (int)ftell(fp);
  747. fseek(fp,0,SEEK_SET);
  748. data = (unsigned char*)malloc(dataSize);
  749. if (data == NULL) goto error;
  750. readed = fread(data, 1, dataSize, fp);
  751. fclose(fp);
  752. fp = 0;
  753. if (readed != dataSize) goto error;
  754. return fonsAddFontMem(stash, name, data, dataSize, 1);
  755. error:
  756. if (data) free(data);
  757. if (fp) fclose(fp);
  758. return FONS_INVALID;
  759. }
  760. int fonsAddFontMem(FONScontext* stash, const char* name, unsigned char* data, int dataSize, int freeData)
  761. {
  762. int i, ascent, descent, fh, lineGap;
  763. FONSfont* font;
  764. int idx = fons__allocFont(stash);
  765. if (idx == FONS_INVALID)
  766. return FONS_INVALID;
  767. font = stash->fonts[idx];
  768. strncpy(font->name, name, sizeof(font->name));
  769. font->name[sizeof(font->name)-1] = '\0';
  770. // Init hash lookup.
  771. for (i = 0; i < FONS_HASH_LUT_SIZE; ++i)
  772. font->lut[i] = -1;
  773. // Read in the font data.
  774. font->dataSize = dataSize;
  775. font->data = data;
  776. font->freeData = (unsigned char)freeData;
  777. // Init font
  778. stash->nscratch = 0;
  779. if (!fons__tt_loadFont(stash, &font->font, data, dataSize)) goto error;
  780. // Store normalized line height. The real line height is got
  781. // by multiplying the lineh by font size.
  782. fons__tt_getFontVMetrics( &font->font, &ascent, &descent, &lineGap);
  783. fh = ascent - descent;
  784. font->ascender = (float)ascent / (float)fh;
  785. font->descender = (float)descent / (float)fh;
  786. font->lineh = (float)(fh + lineGap) / (float)fh;
  787. return idx;
  788. error:
  789. fons__freeFont(font);
  790. stash->nfonts--;
  791. return FONS_INVALID;
  792. }
  793. int fonsGetFontByName(FONScontext* s, const char* name)
  794. {
  795. int i;
  796. for (i = 0; i < s->nfonts; i++) {
  797. if (strcmp(s->fonts[i]->name, name) == 0)
  798. return i;
  799. }
  800. return FONS_INVALID;
  801. }
  802. static FONSglyph* fons__allocGlyph(FONSfont* font)
  803. {
  804. if (font->nglyphs+1 > font->cglyphs) {
  805. font->cglyphs = font->cglyphs == 0 ? 8 : font->cglyphs * 2;
  806. font->glyphs = (FONSglyph*)realloc(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
  807. if (font->glyphs == NULL) return NULL;
  808. }
  809. font->nglyphs++;
  810. return &font->glyphs[font->nglyphs-1];
  811. }
  812. // Based on Exponential blur, Jani Huhtanen, 2006
  813. #define APREC 16
  814. #define ZPREC 7
  815. static void fons__blurCols(unsigned char* dst, int w, int h, int dstStride, int alpha)
  816. {
  817. int x, y;
  818. for (y = 0; y < h; y++) {
  819. int z = 0; // force zero border
  820. for (x = 1; x < w; x++) {
  821. z += (alpha * (((int)(dst[x]) << ZPREC) - z)) >> APREC;
  822. dst[x] = (unsigned char)(z >> ZPREC);
  823. }
  824. dst[w-1] = 0; // force zero border
  825. z = 0;
  826. for (x = w-2; x >= 0; x--) {
  827. z += (alpha * (((int)(dst[x]) << ZPREC) - z)) >> APREC;
  828. dst[x] = (unsigned char)(z >> ZPREC);
  829. }
  830. dst[0] = 0; // force zero border
  831. dst += dstStride;
  832. }
  833. }
  834. static void fons__blurRows(unsigned char* dst, int w, int h, int dstStride, int alpha)
  835. {
  836. int x, y;
  837. for (x = 0; x < w; x++) {
  838. int z = 0; // force zero border
  839. for (y = dstStride; y < h*dstStride; y += dstStride) {
  840. z += (alpha * (((int)(dst[y]) << ZPREC) - z)) >> APREC;
  841. dst[y] = (unsigned char)(z >> ZPREC);
  842. }
  843. dst[(h-1)*dstStride] = 0; // force zero border
  844. z = 0;
  845. for (y = (h-2)*dstStride; y >= 0; y -= dstStride) {
  846. z += (alpha * (((int)(dst[y]) << ZPREC) - z)) >> APREC;
  847. dst[y] = (unsigned char)(z >> ZPREC);
  848. }
  849. dst[0] = 0; // force zero border
  850. dst++;
  851. }
  852. }
  853. static void fons__blur(FONScontext* stash, unsigned char* dst, int w, int h, int dstStride, int blur)
  854. {
  855. int alpha;
  856. float sigma;
  857. (void)stash;
  858. if (blur < 1)
  859. return;
  860. // Calculate the alpha such that 90% of the kernel is within the radius. (Kernel extends to infinity)
  861. sigma = (float)blur * 0.57735f; // 1 / sqrt(3)
  862. alpha = (int)((1<<APREC) * (1.0f - expf(-2.3f / (sigma+1.0f))));
  863. fons__blurRows(dst, w, h, dstStride, alpha);
  864. fons__blurCols(dst, w, h, dstStride, alpha);
  865. fons__blurRows(dst, w, h, dstStride, alpha);
  866. fons__blurCols(dst, w, h, dstStride, alpha);
  867. // fons__blurrows(dst, w, h, dstStride, alpha);
  868. // fons__blurcols(dst, w, h, dstStride, alpha);
  869. }
  870. static FONSglyph* fons__getGlyph(FONScontext* stash, FONSfont* font, unsigned int codepoint,
  871. short isize, short iblur)
  872. {
  873. int i, g, advance, lsb, x0, y0, x1, y1, gw, gh, gx, gy, x, y;
  874. float scale;
  875. FONSglyph* glyph = NULL;
  876. unsigned int h;
  877. float size = isize/10.0f;
  878. int pad, added;
  879. unsigned char* bdst;
  880. unsigned char* dst;
  881. FONSfont* renderFont = font;
  882. if (isize < 2) return NULL;
  883. if (iblur > 20) iblur = 20;
  884. pad = iblur+2;
  885. // Reset allocator.
  886. stash->nscratch = 0;
  887. // Find code point and size.
  888. h = fons__hashint(codepoint) & (FONS_HASH_LUT_SIZE-1);
  889. i = font->lut[h];
  890. while (i != -1) {
  891. if (font->glyphs[i].codepoint == codepoint && font->glyphs[i].size == isize && font->glyphs[i].blur == iblur)
  892. return &font->glyphs[i];
  893. i = font->glyphs[i].next;
  894. }
  895. // Could not find glyph, create it.
  896. g = fons__tt_getGlyphIndex(&font->font, codepoint);
  897. // Try to find the glyph in fallback fonts.
  898. if (g == 0) {
  899. for (i = 0; i < font->nfallbacks; ++i) {
  900. FONSfont* fallbackFont = stash->fonts[font->fallbacks[i]];
  901. int fallbackIndex = fons__tt_getGlyphIndex(&fallbackFont->font, codepoint);
  902. if (fallbackIndex != 0) {
  903. g = fallbackIndex;
  904. renderFont = fallbackFont;
  905. break;
  906. }
  907. }
  908. // It is possible that we did not find a fallback glyph.
  909. // In that case the glyph index 'g' is 0, and we'll proceed below and cache empty glyph.
  910. }
  911. scale = fons__tt_getPixelHeightScale(&renderFont->font, size);
  912. fons__tt_buildGlyphBitmap(&renderFont->font, g, size, scale, &advance, &lsb, &x0, &y0, &x1, &y1);
  913. gw = x1-x0 + pad*2;
  914. gh = y1-y0 + pad*2;
  915. // Find free spot for the rect in the atlas
  916. added = fons__atlasAddRect(stash->atlas, gw, gh, &gx, &gy);
  917. if (added == 0 && stash->handleError != NULL) {
  918. // Atlas is full, let the user to resize the atlas (or not), and try again.
  919. stash->handleError(stash->errorUptr, FONS_ATLAS_FULL, 0);
  920. added = fons__atlasAddRect(stash->atlas, gw, gh, &gx, &gy);
  921. }
  922. if (added == 0) return NULL;
  923. // Init glyph.
  924. glyph = fons__allocGlyph(font);
  925. glyph->codepoint = codepoint;
  926. glyph->size = isize;
  927. glyph->blur = iblur;
  928. glyph->index = g;
  929. glyph->x0 = (short)gx;
  930. glyph->y0 = (short)gy;
  931. glyph->x1 = (short)(glyph->x0+gw);
  932. glyph->y1 = (short)(glyph->y0+gh);
  933. glyph->xadv = (short)(scale * advance * 10.0f);
  934. glyph->xoff = (short)(x0 - pad);
  935. glyph->yoff = (short)(y0 - pad);
  936. glyph->next = 0;
  937. // Insert char to hash lookup.
  938. glyph->next = font->lut[h];
  939. font->lut[h] = font->nglyphs-1;
  940. // Rasterize
  941. dst = &stash->texData[(glyph->x0+pad) + (glyph->y0+pad) * stash->params.width];
  942. fons__tt_renderGlyphBitmap(&renderFont->font, dst, gw-pad*2,gh-pad*2, stash->params.width, scale,scale, g);
  943. // Make sure there is one pixel empty border.
  944. dst = &stash->texData[glyph->x0 + glyph->y0 * stash->params.width];
  945. for (y = 0; y < gh; y++) {
  946. dst[y*stash->params.width] = 0;
  947. dst[gw-1 + y*stash->params.width] = 0;
  948. }
  949. for (x = 0; x < gw; x++) {
  950. dst[x] = 0;
  951. dst[x + (gh-1)*stash->params.width] = 0;
  952. }
  953. // Debug code to color the glyph background
  954. /* unsigned char* fdst = &stash->texData[glyph->x0 + glyph->y0 * stash->params.width];
  955. for (y = 0; y < gh; y++) {
  956. for (x = 0; x < gw; x++) {
  957. int a = (int)fdst[x+y*stash->params.width] + 20;
  958. if (a > 255) a = 255;
  959. fdst[x+y*stash->params.width] = a;
  960. }
  961. }*/
  962. // Blur
  963. if (iblur > 0) {
  964. stash->nscratch = 0;
  965. bdst = &stash->texData[glyph->x0 + glyph->y0 * stash->params.width];
  966. fons__blur(stash, bdst, gw,gh, stash->params.width, iblur);
  967. }
  968. stash->dirtyRect[0] = fons__mini(stash->dirtyRect[0], glyph->x0);
  969. stash->dirtyRect[1] = fons__mini(stash->dirtyRect[1], glyph->y0);
  970. stash->dirtyRect[2] = fons__maxi(stash->dirtyRect[2], glyph->x1);
  971. stash->dirtyRect[3] = fons__maxi(stash->dirtyRect[3], glyph->y1);
  972. return glyph;
  973. }
  974. static void fons__getQuad(FONScontext* stash, FONSfont* font,
  975. int prevGlyphIndex, FONSglyph* glyph,
  976. float scale, float spacing, float* x, float* y, FONSquad* q)
  977. {
  978. float rx,ry,xoff,yoff,x0,y0,x1,y1;
  979. if (prevGlyphIndex != -1) {
  980. float adv = fons__tt_getGlyphKernAdvance(&font->font, prevGlyphIndex, glyph->index) * scale;
  981. *x += (int)(adv + spacing + 0.5f);
  982. }
  983. // Each glyph has 2px border to allow good interpolation,
  984. // one pixel to prevent leaking, and one to allow good interpolation for rendering.
  985. // Inset the texture region by one pixel for correct interpolation.
  986. xoff = (short)(glyph->xoff+1);
  987. yoff = (short)(glyph->yoff+1);
  988. x0 = (float)(glyph->x0+1);
  989. y0 = (float)(glyph->y0+1);
  990. x1 = (float)(glyph->x1-1);
  991. y1 = (float)(glyph->y1-1);
  992. if (stash->params.flags & FONS_ZERO_TOPLEFT) {
  993. rx = (float)(int)(*x + xoff);
  994. ry = (float)(int)(*y + yoff);
  995. q->x0 = rx;
  996. q->y0 = ry;
  997. q->x1 = rx + x1 - x0;
  998. q->y1 = ry + y1 - y0;
  999. q->s0 = x0 * stash->itw;
  1000. q->t0 = y0 * stash->ith;
  1001. q->s1 = x1 * stash->itw;
  1002. q->t1 = y1 * stash->ith;
  1003. } else {
  1004. rx = (float)(int)(*x + xoff);
  1005. ry = (float)(int)(*y - yoff);
  1006. q->x0 = rx;
  1007. q->y0 = ry;
  1008. q->x1 = rx + x1 - x0;
  1009. q->y1 = ry - y1 + y0;
  1010. q->s0 = x0 * stash->itw;
  1011. q->t0 = y0 * stash->ith;
  1012. q->s1 = x1 * stash->itw;
  1013. q->t1 = y1 * stash->ith;
  1014. }
  1015. *x += (int)(glyph->xadv / 10.0f + 0.5f);
  1016. }
  1017. static void fons__flush(FONScontext* stash)
  1018. {
  1019. // Flush texture
  1020. if (stash->dirtyRect[0] < stash->dirtyRect[2] && stash->dirtyRect[1] < stash->dirtyRect[3]) {
  1021. if (stash->params.renderUpdate != NULL)
  1022. stash->params.renderUpdate(stash->params.userPtr, stash->dirtyRect, stash->texData);
  1023. // Reset dirty rect
  1024. stash->dirtyRect[0] = stash->params.width;
  1025. stash->dirtyRect[1] = stash->params.height;
  1026. stash->dirtyRect[2] = 0;
  1027. stash->dirtyRect[3] = 0;
  1028. }
  1029. // Flush triangles
  1030. if (stash->nverts > 0) {
  1031. if (stash->params.renderDraw != NULL)
  1032. stash->params.renderDraw(stash->params.userPtr, stash->verts, stash->tcoords, stash->colors, stash->nverts);
  1033. stash->nverts = 0;
  1034. }
  1035. }
  1036. static __inline void fons__vertex(FONScontext* stash, float x, float y, float s, float t, unsigned int c)
  1037. {
  1038. stash->verts[stash->nverts*2+0] = x;
  1039. stash->verts[stash->nverts*2+1] = y;
  1040. stash->tcoords[stash->nverts*2+0] = s;
  1041. stash->tcoords[stash->nverts*2+1] = t;
  1042. stash->colors[stash->nverts] = c;
  1043. stash->nverts++;
  1044. }
  1045. static float fons__getVertAlign(FONScontext* stash, FONSfont* font, int align, short isize)
  1046. {
  1047. if (stash->params.flags & FONS_ZERO_TOPLEFT) {
  1048. if (align & FONS_ALIGN_TOP) {
  1049. return font->ascender * (float)isize/10.0f;
  1050. } else if (align & FONS_ALIGN_MIDDLE) {
  1051. return (font->ascender + font->descender) / 2.0f * (float)isize/10.0f;
  1052. } else if (align & FONS_ALIGN_BASELINE) {
  1053. return 0.0f;
  1054. } else if (align & FONS_ALIGN_BOTTOM) {
  1055. return font->descender * (float)isize/10.0f;
  1056. }
  1057. } else {
  1058. if (align & FONS_ALIGN_TOP) {
  1059. return -font->ascender * (float)isize/10.0f;
  1060. } else if (align & FONS_ALIGN_MIDDLE) {
  1061. return -(font->ascender + font->descender) / 2.0f * (float)isize/10.0f;
  1062. } else if (align & FONS_ALIGN_BASELINE) {
  1063. return 0.0f;
  1064. } else if (align & FONS_ALIGN_BOTTOM) {
  1065. return -font->descender * (float)isize/10.0f;
  1066. }
  1067. }
  1068. return 0.0;
  1069. }
  1070. float fonsDrawText(FONScontext* stash,
  1071. float x, float y,
  1072. const char* str, const char* end)
  1073. {
  1074. FONSstate* state = fons__getState(stash);
  1075. unsigned int codepoint;
  1076. unsigned int utf8state = 0;
  1077. FONSglyph* glyph = NULL;
  1078. FONSquad q;
  1079. int prevGlyphIndex = -1;
  1080. short isize = (short)(state->size*10.0f);
  1081. short iblur = (short)state->blur;
  1082. float scale;
  1083. FONSfont* font;
  1084. float width;
  1085. if (stash == NULL) return x;
  1086. if (state->font < 0 || state->font >= stash->nfonts) return x;
  1087. font = stash->fonts[state->font];
  1088. if (font->data == NULL) return x;
  1089. scale = fons__tt_getPixelHeightScale(&font->font, (float)isize/10.0f);
  1090. if (end == NULL)
  1091. end = str + strlen(str);
  1092. // Align horizontally
  1093. if (state->align & FONS_ALIGN_LEFT) {
  1094. // empty
  1095. } else if (state->align & FONS_ALIGN_RIGHT) {
  1096. width = fonsTextBounds(stash, x,y, str, end, NULL);
  1097. x -= width;
  1098. } else if (state->align & FONS_ALIGN_CENTER) {
  1099. width = fonsTextBounds(stash, x,y, str, end, NULL);
  1100. x -= width * 0.5f;
  1101. }
  1102. // Align vertically.
  1103. y += fons__getVertAlign(stash, font, state->align, isize);
  1104. for (; str != end; ++str) {
  1105. if (fons__decutf8(&utf8state, &codepoint, *(const unsigned char*)str))
  1106. continue;
  1107. glyph = fons__getGlyph(stash, font, codepoint, isize, iblur);
  1108. if (glyph != NULL) {
  1109. fons__getQuad(stash, font, prevGlyphIndex, glyph, scale, state->spacing, &x, &y, &q);
  1110. if (stash->nverts+6 > FONS_VERTEX_COUNT)
  1111. fons__flush(stash);
  1112. fons__vertex(stash, q.x0, q.y0, q.s0, q.t0, state->color);
  1113. fons__vertex(stash, q.x1, q.y1, q.s1, q.t1, state->color);
  1114. fons__vertex(stash, q.x1, q.y0, q.s1, q.t0, state->color);
  1115. fons__vertex(stash, q.x0, q.y0, q.s0, q.t0, state->color);
  1116. fons__vertex(stash, q.x0, q.y1, q.s0, q.t1, state->color);
  1117. fons__vertex(stash, q.x1, q.y1, q.s1, q.t1, state->color);
  1118. }
  1119. prevGlyphIndex = glyph != NULL ? glyph->index : -1;
  1120. }
  1121. fons__flush(stash);
  1122. return x;
  1123. }
  1124. int fonsTextIterInit(FONScontext* stash, FONStextIter* iter,
  1125. float x, float y, const char* str, const char* end)
  1126. {
  1127. FONSstate* state = fons__getState(stash);
  1128. float width;
  1129. memset(iter, 0, sizeof(*iter));
  1130. if (stash == NULL) return 0;
  1131. if (state->font < 0 || state->font >= stash->nfonts) return 0;
  1132. iter->font = stash->fonts[state->font];
  1133. if (iter->font->data == NULL) return 0;
  1134. iter->isize = (short)(state->size*10.0f);
  1135. iter->iblur = (short)state->blur;
  1136. iter->scale = fons__tt_getPixelHeightScale(&iter->font->font, (float)iter->isize/10.0f);
  1137. // Align horizontally
  1138. if (state->align & FONS_ALIGN_LEFT) {
  1139. // empty
  1140. } else if (state->align & FONS_ALIGN_RIGHT) {
  1141. width = fonsTextBounds(stash, x,y, str, end, NULL);
  1142. x -= width;
  1143. } else if (state->align & FONS_ALIGN_CENTER) {
  1144. width = fonsTextBounds(stash, x,y, str, end, NULL);
  1145. x -= width * 0.5f;
  1146. }
  1147. // Align vertically.
  1148. y += fons__getVertAlign(stash, iter->font, state->align, iter->isize);
  1149. if (end == NULL)
  1150. end = str + strlen(str);
  1151. iter->x = iter->nextx = x;
  1152. iter->y = iter->nexty = y;
  1153. iter->spacing = state->spacing;
  1154. iter->str = str;
  1155. iter->next = str;
  1156. iter->end = end;
  1157. iter->codepoint = 0;
  1158. iter->prevGlyphIndex = -1;
  1159. return 1;
  1160. }
  1161. int fonsTextIterNext(FONScontext* stash, FONStextIter* iter, FONSquad* quad)
  1162. {
  1163. FONSglyph* glyph = NULL;
  1164. const char* str = iter->next;
  1165. iter->str = iter->next;
  1166. if (str == iter->end)
  1167. return 0;
  1168. for (; str != iter->end; str++) {
  1169. if (fons__decutf8(&iter->utf8state, &iter->codepoint, *(const unsigned char*)str))
  1170. continue;
  1171. str++;
  1172. // Get glyph and quad
  1173. iter->x = iter->nextx;
  1174. iter->y = iter->nexty;
  1175. glyph = fons__getGlyph(stash, iter->font, iter->codepoint, iter->isize, iter->iblur);
  1176. if (glyph != NULL)
  1177. fons__getQuad(stash, iter->font, iter->prevGlyphIndex, glyph, iter->scale, iter->spacing, &iter->nextx, &iter->nexty, quad);
  1178. iter->prevGlyphIndex = glyph != NULL ? glyph->index : -1;
  1179. break;
  1180. }
  1181. iter->next = str;
  1182. return 1;
  1183. }
  1184. void fonsDrawDebug(FONScontext* stash, float x, float y)
  1185. {
  1186. int i;
  1187. int w = stash->params.width;
  1188. int h = stash->params.height;
  1189. float u = w == 0 ? 0 : (1.0f / w);
  1190. float v = h == 0 ? 0 : (1.0f / h);
  1191. if (stash->nverts+6+6 > FONS_VERTEX_COUNT)
  1192. fons__flush(stash);
  1193. // Draw background
  1194. fons__vertex(stash, x+0, y+0, u, v, 0x0fffffff);
  1195. fons__vertex(stash, x+w, y+h, u, v, 0x0fffffff);
  1196. fons__vertex(stash, x+w, y+0, u, v, 0x0fffffff);
  1197. fons__vertex(stash, x+0, y+0, u, v, 0x0fffffff);
  1198. fons__vertex(stash, x+0, y+h, u, v, 0x0fffffff);
  1199. fons__vertex(stash, x+w, y+h, u, v, 0x0fffffff);
  1200. // Draw texture
  1201. fons__vertex(stash, x+0, y+0, 0, 0, 0xffffffff);
  1202. fons__vertex(stash, x+w, y+h, 1, 1, 0xffffffff);
  1203. fons__vertex(stash, x+w, y+0, 1, 0, 0xffffffff);
  1204. fons__vertex(stash, x+0, y+0, 0, 0, 0xffffffff);
  1205. fons__vertex(stash, x+0, y+h, 0, 1, 0xffffffff);
  1206. fons__vertex(stash, x+w, y+h, 1, 1, 0xffffffff);
  1207. // Drawbug draw atlas
  1208. for (i = 0; i < stash->atlas->nnodes; i++) {
  1209. FONSatlasNode* n = &stash->atlas->nodes[i];
  1210. if (stash->nverts+6 > FONS_VERTEX_COUNT)
  1211. fons__flush(stash);
  1212. fons__vertex(stash, x+n->x+0, y+n->y+0, u, v, 0xc00000ff);
  1213. fons__vertex(stash, x+n->x+n->width, y+n->y+1, u, v, 0xc00000ff);
  1214. fons__vertex(stash, x+n->x+n->width, y+n->y+0, u, v, 0xc00000ff);
  1215. fons__vertex(stash, x+n->x+0, y+n->y+0, u, v, 0xc00000ff);
  1216. fons__vertex(stash, x+n->x+0, y+n->y+1, u, v, 0xc00000ff);
  1217. fons__vertex(stash, x+n->x+n->width, y+n->y+1, u, v, 0xc00000ff);
  1218. }
  1219. fons__flush(stash);
  1220. }
  1221. float fonsTextBounds(FONScontext* stash,
  1222. float x, float y,
  1223. const char* str, const char* end,
  1224. float* bounds)
  1225. {
  1226. FONSstate* state = fons__getState(stash);
  1227. unsigned int codepoint;
  1228. unsigned int utf8state = 0;
  1229. FONSquad q;
  1230. FONSglyph* glyph = NULL;
  1231. int prevGlyphIndex = -1;
  1232. short isize = (short)(state->size*10.0f);
  1233. short iblur = (short)state->blur;
  1234. float scale;
  1235. FONSfont* font;
  1236. float startx, advance;
  1237. float minx, miny, maxx, maxy;
  1238. if (stash == NULL) return 0;
  1239. if (state->font < 0 || state->font >= stash->nfonts) return 0;
  1240. font = stash->fonts[state->font];
  1241. if (font->data == NULL) return 0;
  1242. scale = fons__tt_getPixelHeightScale(&font->font, (float)isize/10.0f);
  1243. // Align vertically.
  1244. y += fons__getVertAlign(stash, font, state->align, isize);
  1245. minx = maxx = x;
  1246. miny = maxy = y;
  1247. startx = x;
  1248. if (end == NULL)
  1249. end = str + strlen(str);
  1250. for (; str != end; ++str) {
  1251. if (fons__decutf8(&utf8state, &codepoint, *(const unsigned char*)str))
  1252. continue;
  1253. glyph = fons__getGlyph(stash, font, codepoint, isize, iblur);
  1254. if (glyph != NULL) {
  1255. fons__getQuad(stash, font, prevGlyphIndex, glyph, scale, state->spacing, &x, &y, &q);
  1256. if (q.x0 < minx) minx = q.x0;
  1257. if (q.x1 > maxx) maxx = q.x1;
  1258. if (stash->params.flags & FONS_ZERO_TOPLEFT) {
  1259. if (q.y0 < miny) miny = q.y0;
  1260. if (q.y1 > maxy) maxy = q.y1;
  1261. } else {
  1262. if (q.y1 < miny) miny = q.y1;
  1263. if (q.y0 > maxy) maxy = q.y0;
  1264. }
  1265. }
  1266. prevGlyphIndex = glyph != NULL ? glyph->index : -1;
  1267. }
  1268. advance = x - startx;
  1269. // Align horizontally
  1270. if (state->align & FONS_ALIGN_LEFT) {
  1271. // empty
  1272. } else if (state->align & FONS_ALIGN_RIGHT) {
  1273. minx -= advance;
  1274. maxx -= advance;
  1275. } else if (state->align & FONS_ALIGN_CENTER) {
  1276. minx -= advance * 0.5f;
  1277. maxx -= advance * 0.5f;
  1278. }
  1279. if (bounds) {
  1280. bounds[0] = minx;
  1281. bounds[1] = miny;
  1282. bounds[2] = maxx;
  1283. bounds[3] = maxy;
  1284. }
  1285. return advance;
  1286. }
  1287. void fonsVertMetrics(FONScontext* stash,
  1288. float* ascender, float* descender, float* lineh)
  1289. {
  1290. FONSfont* font;
  1291. FONSstate* state = fons__getState(stash);
  1292. short isize;
  1293. if (stash == NULL) return;
  1294. if (state->font < 0 || state->font >= stash->nfonts) return;
  1295. font = stash->fonts[state->font];
  1296. isize = (short)(state->size*10.0f);
  1297. if (font->data == NULL) return;
  1298. if (ascender)
  1299. *ascender = font->ascender*isize/10.0f;
  1300. if (descender)
  1301. *descender = font->descender*isize/10.0f;
  1302. if (lineh)
  1303. *lineh = font->lineh*isize/10.0f;
  1304. }
  1305. void fonsLineBounds(FONScontext* stash, float y, float* miny, float* maxy)
  1306. {
  1307. FONSfont* font;
  1308. FONSstate* state = fons__getState(stash);
  1309. short isize;
  1310. if (stash == NULL) return;
  1311. if (state->font < 0 || state->font >= stash->nfonts) return;
  1312. font = stash->fonts[state->font];
  1313. isize = (short)(state->size*10.0f);
  1314. if (font->data == NULL) return;
  1315. y += fons__getVertAlign(stash, font, state->align, isize);
  1316. if (stash->params.flags & FONS_ZERO_TOPLEFT) {
  1317. *miny = y - font->ascender * (float)isize/10.0f;
  1318. *maxy = *miny + font->lineh*isize/10.0f;
  1319. } else {
  1320. *maxy = y + font->descender * (float)isize/10.0f;
  1321. *miny = *maxy - font->lineh*isize/10.0f;
  1322. }
  1323. }
  1324. const unsigned char* fonsGetTextureData(FONScontext* stash, int* width, int* height)
  1325. {
  1326. if (width != NULL)
  1327. *width = stash->params.width;
  1328. if (height != NULL)
  1329. *height = stash->params.height;
  1330. return stash->texData;
  1331. }
  1332. int fonsValidateTexture(FONScontext* stash, int* dirty)
  1333. {
  1334. if (stash->dirtyRect[0] < stash->dirtyRect[2] && stash->dirtyRect[1] < stash->dirtyRect[3]) {
  1335. dirty[0] = stash->dirtyRect[0];
  1336. dirty[1] = stash->dirtyRect[1];
  1337. dirty[2] = stash->dirtyRect[2];
  1338. dirty[3] = stash->dirtyRect[3];
  1339. // Reset dirty rect
  1340. stash->dirtyRect[0] = stash->params.width;
  1341. stash->dirtyRect[1] = stash->params.height;
  1342. stash->dirtyRect[2] = 0;
  1343. stash->dirtyRect[3] = 0;
  1344. return 1;
  1345. }
  1346. return 0;
  1347. }
  1348. void fonsDeleteInternal(FONScontext* stash)
  1349. {
  1350. int i;
  1351. if (stash == NULL) return;
  1352. if (stash->params.renderDelete)
  1353. stash->params.renderDelete(stash->params.userPtr);
  1354. for (i = 0; i < stash->nfonts; ++i)
  1355. fons__freeFont(stash->fonts[i]);
  1356. if (stash->atlas) fons__deleteAtlas(stash->atlas);
  1357. if (stash->fonts) free(stash->fonts);
  1358. if (stash->texData) free(stash->texData);
  1359. if (stash->scratch) free(stash->scratch);
  1360. free(stash);
  1361. }
  1362. void fonsSetErrorCallback(FONScontext* stash, void (*callback)(void* uptr, int error, int val), void* uptr)
  1363. {
  1364. if (stash == NULL) return;
  1365. stash->handleError = callback;
  1366. stash->errorUptr = uptr;
  1367. }
  1368. void fonsGetAtlasSize(FONScontext* stash, int* width, int* height)
  1369. {
  1370. if (stash == NULL) return;
  1371. *width = stash->params.width;
  1372. *height = stash->params.height;
  1373. }
  1374. int fonsExpandAtlas(FONScontext* stash, int width, int height)
  1375. {
  1376. int i, maxy = 0;
  1377. unsigned char* data = NULL;
  1378. if (stash == NULL) return 0;
  1379. width = fons__maxi(width, stash->params.width);
  1380. height = fons__maxi(height, stash->params.height);
  1381. if (width == stash->params.width && height == stash->params.height)
  1382. return 1;
  1383. // Flush pending glyphs.
  1384. fons__flush(stash);
  1385. // Create new texture
  1386. if (stash->params.renderResize != NULL) {
  1387. if (stash->params.renderResize(stash->params.userPtr, width, height) == 0)
  1388. return 0;
  1389. }
  1390. // Copy old texture data over.
  1391. data = (unsigned char*)malloc(width * height);
  1392. if (data == NULL)
  1393. return 0;
  1394. for (i = 0; i < stash->params.height; i++) {
  1395. unsigned char* dst = &data[i*width];
  1396. unsigned char* src = &stash->texData[i*stash->params.width];
  1397. memcpy(dst, src, stash->params.width);
  1398. if (width > stash->params.width)
  1399. memset(dst+stash->params.width, 0, width - stash->params.width);
  1400. }
  1401. if (height > stash->params.height)
  1402. memset(&data[stash->params.height * width], 0, (height - stash->params.height) * width);
  1403. free(stash->texData);
  1404. stash->texData = data;
  1405. // Increase atlas size
  1406. fons__atlasExpand(stash->atlas, width, height);
  1407. // Add existing data as dirty.
  1408. for (i = 0; i < stash->atlas->nnodes; i++)
  1409. maxy = fons__maxi(maxy, stash->atlas->nodes[i].y);
  1410. stash->dirtyRect[0] = 0;
  1411. stash->dirtyRect[1] = 0;
  1412. stash->dirtyRect[2] = stash->params.width;
  1413. stash->dirtyRect[3] = maxy;
  1414. stash->params.width = width;
  1415. stash->params.height = height;
  1416. stash->itw = 1.0f/stash->params.width;
  1417. stash->ith = 1.0f/stash->params.height;
  1418. return 1;
  1419. }
  1420. int fonsResetAtlas(FONScontext* stash, int width, int height)
  1421. {
  1422. int i, j;
  1423. if (stash == NULL) return 0;
  1424. // Flush pending glyphs.
  1425. fons__flush(stash);
  1426. // Create new texture
  1427. if (stash->params.renderResize != NULL) {
  1428. if (stash->params.renderResize(stash->params.userPtr, width, height) == 0)
  1429. return 0;
  1430. }
  1431. // Reset atlas
  1432. fons__atlasReset(stash->atlas, width, height);
  1433. // Clear texture data.
  1434. stash->texData = (unsigned char*)realloc(stash->texData, width * height);
  1435. if (stash->texData == NULL) return 0;
  1436. memset(stash->texData, 0, width * height);
  1437. // Reset dirty rect
  1438. stash->dirtyRect[0] = width;
  1439. stash->dirtyRect[1] = height;
  1440. stash->dirtyRect[2] = 0;
  1441. stash->dirtyRect[3] = 0;
  1442. // Reset cached glyphs
  1443. for (i = 0; i < stash->nfonts; i++) {
  1444. FONSfont* font = stash->fonts[i];
  1445. font->nglyphs = 0;
  1446. for (j = 0; j < FONS_HASH_LUT_SIZE; j++)
  1447. font->lut[j] = -1;
  1448. }
  1449. stash->params.width = width;
  1450. stash->params.height = height;
  1451. stash->itw = 1.0f/stash->params.width;
  1452. stash->ith = 1.0f/stash->params.height;
  1453. // Add white rect at 0,0 for debug drawing.
  1454. fons__addWhiteRect(stash, 2,2);
  1455. return 1;
  1456. }
  1457. #endif