*/}}

fontstash.h 48 KB

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