fontstash.h 47 KB

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