forked from memononen/fontstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontstash.h
1687 lines (1422 loc) · 45 KB
/
fontstash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#ifndef FONS_H
#define FONS_H
#define FONS_INVALID -1
enum FONSflags {
FONS_ZERO_TOPLEFT = 1,
FONS_ZERO_BOTTOMLEFT = 2,
};
enum FONSalign {
// Horizontal align
FONS_ALIGN_LEFT = 1<<0, // Default
FONS_ALIGN_CENTER = 1<<1,
FONS_ALIGN_RIGHT = 1<<2,
// Vertical align
FONS_ALIGN_TOP = 1<<3,
FONS_ALIGN_MIDDLE = 1<<4,
FONS_ALIGN_BOTTOM = 1<<5,
FONS_ALIGN_BASELINE = 1<<6, // Default
};
enum FONSerrorCode {
// Font atlas is full.
FONS_ATLAS_FULL = 1,
// Scratch memory used to render glyphs is full, requested size reported in 'val', you may need to bump up FONS_SCRATCH_BUF_SIZE.
FONS_SCRATCH_FULL = 2,
// Calls to fonsPushState has craeted too large stack, if you need deep state stack bump up FONS_MAX_STATES.
FONS_STATES_OVERFLOW = 3,
// Trying to pop too many states fonsPopState().
FONS_STATES_UNDERFLOW = 4,
};
struct FONSparams {
int width, height;
unsigned char flags;
void* userPtr;
int (*renderCreate)(void* uptr, int width, int height);
int (*renderResize)(void* uptr, int width, int height);
void (*renderUpdate)(void* uptr, int* rect, const unsigned char* data);
void (*renderDraw)(void* uptr, const float* verts, const float* tcoords, const unsigned int* colors, int nverts);
void (*renderDelete)(void* uptr);
};
typedef struct FONSparams FONSparams;
struct FONSquad
{
float x0,y0,s0,t0;
float x1,y1,s1,t1;
};
typedef struct FONSquad FONSquad;
struct FONStextIter {
float x, y, nextx, nexty, scale, spacing;
unsigned int codepoint;
short isize, iblur;
struct FONSfont* font;
int prevGlyphIndex;
const char* str;
const char* next;
const char* end;
unsigned int utf8state;
};
typedef struct FONStextIter FONStextIter;
typedef struct FONScontext FONScontext;
// Contructor and destructor.
FONScontext* fonsCreateInternal(FONSparams* params);
void fonsDeleteInternal(FONScontext* s);
void fonsSetErrorCallback(FONScontext* s, void (*callback)(void* uptr, int error, int val), void* uptr);
// Returns current atlas size.
void fonsGetAtlasSize(FONScontext* s, int* width, int* height);
// Expands the atlas size.
int fonsExpandAtlas(FONScontext* s, int width, int height);
// Reseta the whole stash.
int fonsResetAtlas(FONScontext* stash, int width, int height);
// Add fonts
int fonsAddFont(FONScontext* s, const char* name, const char* path);
int fonsAddFontMem(FONScontext* s, const char* name, unsigned char* data, int ndata, int freeData);
int fonsGetFontByName(FONScontext* s, const char* name);
// State handling
void fonsPushState(FONScontext* s);
void fonsPopState(FONScontext* s);
void fonsClearState(FONScontext* s);
// State setting
void fonsSetSize(FONScontext* s, float size);
void fonsSetColor(FONScontext* s, unsigned int color);
void fonsSetSpacing(FONScontext* s, float spacing);
void fonsSetBlur(FONScontext* s, float blur);
void fonsSetAlign(FONScontext* s, int align);
void fonsSetFont(FONScontext* s, int font);
// Draw text
float fonsDrawText(FONScontext* s, float x, float y, const char* string, const char* end);
// Measure text
float fonsTextBounds(FONScontext* s, float x, float y, const char* string, const char* end, float* bounds);
void fonsLineBounds(FONScontext* s, float y, float* miny, float* maxy);
void fonsVertMetrics(FONScontext* s, float* ascender, float* descender, float* lineh);
// Text iterator
int fonsTextIterInit(FONScontext* stash, FONStextIter* iter, float x, float y, const char* str, const char* end);
int fonsTextIterNext(FONScontext* stash, FONStextIter* iter, struct FONSquad* quad);
// Pull texture changes
const unsigned char* fonsGetTextureData(FONScontext* stash, int* width, int* height);
int fonsValidateTexture(FONScontext* s, int* dirty);
// Draws the stash texture for debugging
void fonsDrawDebug(FONScontext* s, float x, float y);
#endif // FONTSTASH_H
#ifdef FONTSTASH_IMPLEMENTATION
#define FONS_NOTUSED(v) (void)sizeof(v)
#ifdef FONS_USE_FREETYPE
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_ADVANCES_H
#include <math.h>
struct FONSttFontImpl {
FT_Face font;
};
typedef struct FONSttFontImpl FONSttFontImpl;
static FT_Library ftLibrary;
int fons__tt_init()
{
FT_Error ftError;
ftError = FT_Init_FreeType(&ftLibrary);
return ftError == 0;
}
int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize)
{
FT_Error ftError;
FONS_NOTUSED(context);
//font->font.userdata = stash;
ftError = FT_New_Memory_Face(ftLibrary, (const FT_Byte*)data, dataSize, 0, &font->font);
return ftError == 0;
}
void fons__tt_getFontVMetrics(FONSttFontImpl *font, int *ascent, int *descent, int *lineGap)
{
*ascent = font->font->ascender;
*descent = font->font->descender;
*lineGap = font->font->height - (*ascent - *descent);
}
float fons__tt_getPixelHeightScale(FONSttFontImpl *font, float size)
{
return size / (font->font->ascender - font->font->descender);
}
int fons__tt_getGlyphIndex(FONSttFontImpl *font, int codepoint)
{
return FT_Get_Char_Index(font->font, codepoint);
}
int fons__tt_buildGlyphBitmap(FONSttFontImpl *font, int glyph, float size, float scale,
int *advance, int *lsb, int *x0, int *y0, int *x1, int *y1)
{
FT_Error ftError;
FT_GlyphSlot ftGlyph;
FONS_NOTUSED(scale);
ftError = FT_Set_Pixel_Sizes(font->font, 0, (FT_UInt)(size * (float)font->font->units_per_EM / (float)(font->font->ascender - font->font->descender)));
if (ftError) return 0;
ftError = FT_Load_Glyph(font->font, glyph, FT_LOAD_RENDER);
if (ftError) return 0;
ftError = FT_Get_Advance(font->font, glyph, FT_LOAD_NO_SCALE, (FT_Fixed*)advance);
if (ftError) return 0;
ftGlyph = font->font->glyph;
*lsb = ftGlyph->metrics.horiBearingX;
*x0 = ftGlyph->bitmap_left;
*x1 = *x0 + ftGlyph->bitmap.width;
*y0 = -ftGlyph->bitmap_top;
*y1 = *y0 + ftGlyph->bitmap.rows;
return 1;
}
void fons__tt_renderGlyphBitmap(FONSttFontImpl *font, unsigned char *output, int outWidth, int outHeight, int outStride,
float scaleX, float scaleY, int glyph)
{
FT_GlyphSlot ftGlyph = font->font->glyph;
int ftGlyphOffset = 0;
int x, y;
FONS_NOTUSED(outWidth);
FONS_NOTUSED(outHeight);
FONS_NOTUSED(scaleX);
FONS_NOTUSED(scaleY);
FONS_NOTUSED(glyph); // glyph has already been loaded by fons__tt_buildGlyphBitmap
for ( y = 0; y < ftGlyph->bitmap.rows; y++ ) {
for ( x = 0; x < ftGlyph->bitmap.width; x++ ) {
output[(y * outStride) + x] = ftGlyph->bitmap.buffer[ftGlyphOffset++];
}
}
}
int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)
{
FT_Vector ftKerning;
FT_Get_Kerning(font->font, glyph1, glyph2, FT_KERNING_DEFAULT, &ftKerning);
return ftKerning.x;
}
#else
#define STB_TRUETYPE_IMPLEMENTATION
static void* fons__tmpalloc(size_t size, void* up);
static void fons__tmpfree(void* ptr, void* up);
#define STBTT_malloc(x,u) fons__tmpalloc(x,u)
#define STBTT_free(x,u) fons__tmpfree(x,u)
#include "stb_truetype.h"
struct FONSttFontImpl {
stbtt_fontinfo font;
};
typedef struct FONSttFontImpl FONSttFontImpl;
int fons__tt_init(FONScontext *context)
{
FONS_NOTUSED(context);
return 1;
}
int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize)
{
int stbError;
FONS_NOTUSED(dataSize);
font->font.userdata = context;
stbError = stbtt_InitFont(&font->font, data, 0);
return stbError;
}
void fons__tt_getFontVMetrics(FONSttFontImpl *font, int *ascent, int *descent, int *lineGap)
{
stbtt_GetFontVMetrics(&font->font, ascent, descent, lineGap);
}
float fons__tt_getPixelHeightScale(FONSttFontImpl *font, float size)
{
return stbtt_ScaleForPixelHeight(&font->font, size);
}
int fons__tt_getGlyphIndex(FONSttFontImpl *font, int codepoint)
{
return stbtt_FindGlyphIndex(&font->font, codepoint);
}
int fons__tt_buildGlyphBitmap(FONSttFontImpl *font, int glyph, float size, float scale,
int *advance, int *lsb, int *x0, int *y0, int *x1, int *y1)
{
FONS_NOTUSED(size);
stbtt_GetGlyphHMetrics(&font->font, glyph, advance, lsb);
stbtt_GetGlyphBitmapBox(&font->font, glyph, scale, scale, x0, y0, x1, y1);
return 1;
}
void fons__tt_renderGlyphBitmap(FONSttFontImpl *font, unsigned char *output, int outWidth, int outHeight, int outStride,
float scaleX, float scaleY, int glyph)
{
stbtt_MakeGlyphBitmap(&font->font, output, outWidth, outHeight, outStride, scaleX, scaleY, glyph);
}
int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)
{
return stbtt_GetGlyphKernAdvance(&font->font, glyph1, glyph2);
}
#endif
#ifndef FONS_SCRATCH_BUF_SIZE
# define FONS_SCRATCH_BUF_SIZE 16000
#endif
#ifndef FONS_HASH_LUT_SIZE
# define FONS_HASH_LUT_SIZE 256
#endif
#ifndef FONS_INIT_FONTS
# define FONS_INIT_FONTS 4
#endif
#ifndef FONS_INIT_GLYPHS
# define FONS_INIT_GLYPHS 256
#endif
#ifndef FONS_INIT_ATLAS_NODES
# define FONS_INIT_ATLAS_NODES 256
#endif
#ifndef FONS_VERTEX_COUNT
# define FONS_VERTEX_COUNT 1024
#endif
#ifndef FONS_MAX_STATES
# define FONS_MAX_STATES 20
#endif
static unsigned int fons__hashint(unsigned int a)
{
a += ~(a<<15);
a ^= (a>>10);
a += (a<<3);
a ^= (a>>6);
a += ~(a<<11);
a ^= (a>>16);
return a;
}
static int fons__mini(int a, int b)
{
return a < b ? a : b;
}
static int fons__maxi(int a, int b)
{
return a > b ? a : b;
}
struct FONSglyph
{
unsigned int codepoint;
int index;
int next;
short size, blur;
short x0,y0,x1,y1;
short xadv,xoff,yoff;
};
typedef struct FONSglyph FONSglyph;
struct FONSfont
{
FONSttFontImpl font;
char name[64];
unsigned char* data;
int dataSize;
unsigned char freeData;
float ascender;
float descender;
float lineh;
FONSglyph* glyphs;
int cglyphs;
int nglyphs;
int lut[FONS_HASH_LUT_SIZE];
};
typedef struct FONSfont FONSfont;
struct FONSstate
{
int font;
int align;
float size;
unsigned int color;
float blur;
float spacing;
};
typedef struct FONSstate FONSstate;
struct FONSatlasNode {
short x, y, width;
};
typedef struct FONSatlasNode FONSatlasNode;
struct FONSatlas
{
int width, height;
FONSatlasNode* nodes;
int nnodes;
int cnodes;
};
typedef struct FONSatlas FONSatlas;
struct FONScontext
{
FONSparams params;
float itw,ith;
unsigned char* texData;
int dirtyRect[4];
FONSfont** fonts;
FONSatlas* atlas;
int cfonts;
int nfonts;
float verts[FONS_VERTEX_COUNT*2];
float tcoords[FONS_VERTEX_COUNT*2];
unsigned int colors[FONS_VERTEX_COUNT];
int nverts;
unsigned char* scratch;
int nscratch;
FONSstate states[FONS_MAX_STATES];
int nstates;
void (*handleError)(void* uptr, int error, int val);
void* errorUptr;
};
static void* fons__tmpalloc(size_t size, void* up)
{
unsigned char* ptr;
FONScontext* stash = (FONScontext*)up;
// 16-byte align the returned pointer
size = (size + 0xf) & ~0xf;
if (stash->nscratch+(int)size > FONS_SCRATCH_BUF_SIZE) {
if (stash->handleError)
stash->handleError(stash->errorUptr, FONS_SCRATCH_FULL, stash->nscratch+(int)size);
return NULL;
}
ptr = stash->scratch + stash->nscratch;
stash->nscratch += (int)size;
return ptr;
}
static void fons__tmpfree(void* ptr, void* up)
{
(void)ptr;
(void)up;
// empty
}
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
#define FONS_UTF8_ACCEPT 0
#define FONS_UTF8_REJECT 12
static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsigned int byte)
{
static const unsigned char utf8d[] = {
// The first part of the table maps bytes to character classes that
// to reduce the size of the transition table and create bitmasks.
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,
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,
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,
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,
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,
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,
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,
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,
// The second part is a transition table that maps a combination
// of a state of the automaton and a character class to a state.
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
12,36,12,12,12,12,12,12,12,12,12,12,
};
unsigned int type = utf8d[byte];
*codep = (*state != FONS_UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
*state = utf8d[256 + *state + type];
return *state;
}
// Atlas based on Skyline Bin Packer by Jukka Jylänki
static void fons__deleteAtlas(FONSatlas* atlas)
{
if (atlas == NULL) return;
if (atlas->nodes != NULL) free(atlas->nodes);
free(atlas);
}
static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
{
FONSatlas* atlas = NULL;
// Allocate memory for the font stash.
atlas = (FONSatlas*)malloc(sizeof(FONSatlas));
if (atlas == NULL) goto error;
memset(atlas, 0, sizeof(FONSatlas));
atlas->width = w;
atlas->height = h;
// Allocate space for skyline nodes
atlas->nodes = (FONSatlasNode*)malloc(sizeof(FONSatlasNode) * nnodes);
if (atlas->nodes == NULL) goto error;
memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
atlas->nnodes = 0;
atlas->cnodes = nnodes;
// Init root node.
atlas->nodes[0].x = 0;
atlas->nodes[0].y = 0;
atlas->nodes[0].width = (short)w;
atlas->nnodes++;
return atlas;
error:
if (atlas) fons__deleteAtlas(atlas);
return NULL;
}
static int fons__atlasInsertNode(FONSatlas* atlas, int idx, int x, int y, int w)
{
int i;
// Insert node
if (atlas->nnodes+1 > atlas->cnodes) {
atlas->cnodes = atlas->cnodes == 0 ? 8 : atlas->cnodes * 2;
atlas->nodes = (FONSatlasNode*)realloc(atlas->nodes, sizeof(FONSatlasNode) * atlas->cnodes);
if (atlas->nodes == NULL)
return 0;
}
for (i = atlas->nnodes; i > idx; i--)
atlas->nodes[i] = atlas->nodes[i-1];
atlas->nodes[idx].x = (short)x;
atlas->nodes[idx].y = (short)y;
atlas->nodes[idx].width = (short)w;
atlas->nnodes++;
return 1;
}
static void fons__atlasRemoveNode(FONSatlas* atlas, int idx)
{
int i;
if (atlas->nnodes == 0) return;
for (i = idx; i < atlas->nnodes-1; i++)
atlas->nodes[i] = atlas->nodes[i+1];
atlas->nnodes--;
}
static void fons__atlasExpand(FONSatlas* atlas, int w, int h)
{
// Insert node for empty space
if (w > atlas->width)
fons__atlasInsertNode(atlas, atlas->nnodes, atlas->width, 0, w - atlas->width);
atlas->width = w;
atlas->height = h;
}
static void fons__atlasReset(FONSatlas* atlas, int w, int h)
{
atlas->width = w;
atlas->height = h;
atlas->nnodes = 0;
// Init root node.
atlas->nodes[0].x = 0;
atlas->nodes[0].y = 0;
atlas->nodes[0].width = (short)w;
atlas->nnodes++;
}
static int fons__atlasAddSkylineLevel(FONSatlas* atlas, int idx, int x, int y, int w, int h)
{
int i;
// Insert new node
if (fons__atlasInsertNode(atlas, idx, x, y+h, w) == 0)
return 0;
// Delete skyline segments that fall under the shaodw of the new segment.
for (i = idx+1; i < atlas->nnodes; i++) {
if (atlas->nodes[i].x < atlas->nodes[i-1].x + atlas->nodes[i-1].width) {
int shrink = atlas->nodes[i-1].x + atlas->nodes[i-1].width - atlas->nodes[i].x;
atlas->nodes[i].x += (short)shrink;
atlas->nodes[i].width -= (short)shrink;
if (atlas->nodes[i].width <= 0) {
fons__atlasRemoveNode(atlas, i);
i--;
} else {
break;
}
} else {
break;
}
}
// Merge same height skyline segments that are next to each other.
for (i = 0; i < atlas->nnodes-1; i++) {
if (atlas->nodes[i].y == atlas->nodes[i+1].y) {
atlas->nodes[i].width += atlas->nodes[i+1].width;
fons__atlasRemoveNode(atlas, i+1);
i--;
}
}
return 1;
}
static int fons__atlasRectFits(FONSatlas* atlas, int i, int w, int h)
{
// Checks if there is enough space at the location of skyline span 'i',
// and return the max height of all skyline spans under that at that location,
// (think tetris block being dropped at that position). Or -1 if no space found.
int x = atlas->nodes[i].x;
int y = atlas->nodes[i].y;
int spaceLeft;
if (x + w > atlas->width)
return -1;
spaceLeft = w;
while (spaceLeft > 0) {
if (i == atlas->nnodes) return -1;
y = fons__maxi(y, atlas->nodes[i].y);
if (y + h > atlas->height) return -1;
spaceLeft -= atlas->nodes[i].width;
++i;
}
return y;
}
static int fons__atlasAddRect(FONSatlas* atlas, int rw, int rh, int* rx, int* ry)
{
int besth = atlas->height, bestw = atlas->width, besti = -1;
int bestx = -1, besty = -1, i;
// Bottom left fit heuristic.
for (i = 0; i < atlas->nnodes; i++) {
int y = fons__atlasRectFits(atlas, i, rw, rh);
if (y != -1) {
if (y + rh < besth || (y + rh == besth && atlas->nodes[i].width < bestw)) {
besti = i;
bestw = atlas->nodes[i].width;
besth = y + rh;
bestx = atlas->nodes[i].x;
besty = y;
}
}
}
if (besti == -1)
return 0;
// Perform the actual packing.
if (fons__atlasAddSkylineLevel(atlas, besti, bestx, besty, rw, rh) == 0)
return 0;
*rx = bestx;
*ry = besty;
return 1;
}
static void fons__addWhiteRect(FONScontext* stash, int w, int h)
{
int x, y, gx, gy;
unsigned char* dst;
if (fons__atlasAddRect(stash->atlas, w, h, &gx, &gy) == 0)
return;
// Rasterize
dst = &stash->texData[gx + gy * stash->params.width];
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++)
dst[x] = 0xff;
dst += stash->params.width;
}
stash->dirtyRect[0] = fons__mini(stash->dirtyRect[0], gx);
stash->dirtyRect[1] = fons__mini(stash->dirtyRect[1], gy);
stash->dirtyRect[2] = fons__maxi(stash->dirtyRect[2], gx+w);
stash->dirtyRect[3] = fons__maxi(stash->dirtyRect[3], gy+h);
}
FONScontext* fonsCreateInternal(FONSparams* params)
{
FONScontext* stash = NULL;
// Allocate memory for the font stash.
stash = (FONScontext*)malloc(sizeof(FONScontext));
if (stash == NULL) goto error;
memset(stash, 0, sizeof(FONScontext));
stash->params = *params;
// Allocate scratch buffer.
stash->scratch = (unsigned char*)malloc(FONS_SCRATCH_BUF_SIZE);
if (stash->scratch == NULL) goto error;
// Initialize implementation library
if (!fons__tt_init(stash)) goto error;
if (stash->params.renderCreate != NULL) {
if (stash->params.renderCreate(stash->params.userPtr, stash->params.width, stash->params.height) == 0)
goto error;
}
stash->atlas = fons__allocAtlas(stash->params.width, stash->params.height, FONS_INIT_ATLAS_NODES);
if (stash->atlas == NULL) goto error;
// Allocate space for fonts.
stash->fonts = (FONSfont**)malloc(sizeof(FONSfont*) * FONS_INIT_FONTS);
if (stash->fonts == NULL) goto error;
memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
stash->cfonts = FONS_INIT_FONTS;
stash->nfonts = 0;
// Create texture for the cache.
stash->itw = 1.0f/stash->params.width;
stash->ith = 1.0f/stash->params.height;
stash->texData = (unsigned char*)malloc(stash->params.width * stash->params.height);
if (stash->texData == NULL) goto error;
memset(stash->texData, 0, stash->params.width * stash->params.height);
stash->dirtyRect[0] = stash->params.width;
stash->dirtyRect[1] = stash->params.height;
stash->dirtyRect[2] = 0;
stash->dirtyRect[3] = 0;
// Add white rect at 0,0 for debug drawing.
fons__addWhiteRect(stash, 2,2);
fonsPushState(stash);
fonsClearState(stash);
return stash;
error:
fonsDeleteInternal(stash);
return NULL;
}
static FONSstate* fons__getState(FONScontext* stash)
{
return &stash->states[stash->nstates-1];
}
void fonsSetSize(FONScontext* stash, float size)
{
fons__getState(stash)->size = size;
}
void fonsSetColor(FONScontext* stash, unsigned int color)
{
fons__getState(stash)->color = color;
}
void fonsSetSpacing(FONScontext* stash, float spacing)
{
fons__getState(stash)->spacing = spacing;
}
void fonsSetBlur(FONScontext* stash, float blur)
{
fons__getState(stash)->blur = blur;
}
void fonsSetAlign(FONScontext* stash, int align)
{
fons__getState(stash)->align = align;
}
void fonsSetFont(FONScontext* stash, int font)
{
fons__getState(stash)->font = font;
}
void fonsPushState(FONScontext* stash)
{
if (stash->nstates >= FONS_MAX_STATES) {
if (stash->handleError)
stash->handleError(stash->errorUptr, FONS_STATES_OVERFLOW, 0);
return;
}
if (stash->nstates > 0)
memcpy(&stash->states[stash->nstates], &stash->states[stash->nstates-1], sizeof(FONSstate));
stash->nstates++;
}
void fonsPopState(FONScontext* stash)
{
if (stash->nstates <= 1) {
if (stash->handleError)
stash->handleError(stash->errorUptr, FONS_STATES_UNDERFLOW, 0);
return;
}
stash->nstates--;
}
void fonsClearState(FONScontext* stash)
{
FONSstate* state = fons__getState(stash);
state->size = 12.0f;
state->color = 0xffffffff;
state->font = 0;
state->blur = 0;
state->spacing = 0;
state->align = FONS_ALIGN_LEFT | FONS_ALIGN_BASELINE;
}
static void fons__freeFont(FONSfont* font)
{
if (font == NULL) return;
if (font->glyphs) free(font->glyphs);
if (font->freeData && font->data) free(font->data);
free(font);
}
static int fons__allocFont(FONScontext* stash)
{
FONSfont* font = NULL;
if (stash->nfonts+1 > stash->cfonts) {
stash->cfonts = stash->cfonts == 0 ? 8 : stash->cfonts * 2;
stash->fonts = (FONSfont**)realloc(stash->fonts, sizeof(FONSfont*) * stash->cfonts);
if (stash->fonts == NULL)
return -1;
}
font = (FONSfont*)malloc(sizeof(FONSfont));
if (font == NULL) goto error;
memset(font, 0, sizeof(FONSfont));
font->glyphs = (FONSglyph*)malloc(sizeof(FONSglyph) * FONS_INIT_GLYPHS);
if (font->glyphs == NULL) goto error;
font->cglyphs = FONS_INIT_GLYPHS;
font->nglyphs = 0;
stash->fonts[stash->nfonts++] = font;
return stash->nfonts-1;
error:
fons__freeFont(font);
return FONS_INVALID;
}
int fonsAddFont(FONScontext* stash, const char* name, const char* path)
{
FILE* fp = 0;
int dataSize = 0;
unsigned char* data = NULL;
// Read in the font data.
fp = fopen(path, "rb");
if (fp == NULL) goto error;
fseek(fp,0,SEEK_END);
dataSize = (int)ftell(fp);
fseek(fp,0,SEEK_SET);
data = (unsigned char*)malloc(dataSize);
if (data == NULL) goto error;
fread(data, 1, dataSize, fp);
fclose(fp);
fp = 0;
return fonsAddFontMem(stash, name, data, dataSize, 1);
error:
if (data) free(data);
if (fp) fclose(fp);
return FONS_INVALID;
}
int fonsAddFontMem(FONScontext* stash, const char* name, unsigned char* data, int dataSize, int freeData)
{
int i, ascent, descent, fh, lineGap;
FONSfont* font;
int idx = fons__allocFont(stash);
if (idx == FONS_INVALID)
return FONS_INVALID;
font = stash->fonts[idx];
strncpy(font->name, name, sizeof(font->name));
font->name[sizeof(font->name)-1] = '\0';
// Init hash lookup.
for (i = 0; i < FONS_HASH_LUT_SIZE; ++i)
font->lut[i] = -1;
// Read in the font data.
font->dataSize = dataSize;
font->data = data;
font->freeData = (unsigned char)freeData;
// Init font
stash->nscratch = 0;
if (!fons__tt_loadFont(stash, &font->font, data, dataSize)) goto error;
// Store normalized line height. The real line height is got
// by multiplying the lineh by font size.
fons__tt_getFontVMetrics( &font->font, &ascent, &descent, &lineGap);
fh = ascent - descent;
font->ascender = (float)ascent / (float)fh;
font->descender = (float)descent / (float)fh;
font->lineh = (float)(fh + lineGap) / (float)fh;
return idx;
error:
fons__freeFont(font);
stash->nfonts--;
return FONS_INVALID;
}
int fonsGetFontByName(FONScontext* s, const char* name)
{
int i;
for (i = 0; i < s->nfonts; i++) {
if (strcmp(s->fonts[i]->name, name) == 0)
return i;
}
return FONS_INVALID;
}
static FONSglyph* fons__allocGlyph(FONSfont* font)
{
if (font->nglyphs+1 > font->cglyphs) {
font->cglyphs = font->cglyphs == 0 ? 8 : font->cglyphs * 2;
font->glyphs = (FONSglyph*)realloc(font->glyphs, sizeof(FONSglyph) * font->cglyphs);
if (font->glyphs == NULL) return NULL;
}
font->nglyphs++;
return &font->glyphs[font->nglyphs-1];
}
// Based on Exponential blur, Jani Huhtanen, 2006
#define APREC 16
#define ZPREC 7
static void fons__blurCols(unsigned char* dst, int w, int h, int dstStride, int alpha)
{
int x, y;
for (y = 0; y < h; y++) {
int z = 0; // force zero border
for (x = 1; x < w; x++) {
z += (alpha * (((int)(dst[x]) << ZPREC) - z)) >> APREC;
dst[x] = (unsigned char)(z >> ZPREC);
}
dst[w-1] = 0; // force zero border
z = 0;
for (x = w-2; x >= 0; x--) {
z += (alpha * (((int)(dst[x]) << ZPREC) - z)) >> APREC;
dst[x] = (unsigned char)(z >> ZPREC);
}
dst[0] = 0; // force zero border
dst += dstStride;
}
}
static void fons__blurRows(unsigned char* dst, int w, int h, int dstStride, int alpha)
{
int x, y;
for (x = 0; x < w; x++) {
int z = 0; // force zero border
for (y = dstStride; y < h*dstStride; y += dstStride) {
z += (alpha * (((int)(dst[y]) << ZPREC) - z)) >> APREC;
dst[y] = (unsigned char)(z >> ZPREC);
}
dst[(h-1)*dstStride] = 0; // force zero border
z = 0;
for (y = (h-2)*dstStride; y >= 0; y -= dstStride) {
z += (alpha * (((int)(dst[y]) << ZPREC) - z)) >> APREC;
dst[y] = (unsigned char)(z >> ZPREC);
}
dst[0] = 0; // force zero border
dst++;
}
}
static void fons__blur(FONScontext* stash, unsigned char* dst, int w, int h, int dstStride, int blur)
{
int alpha;
float sigma;
(void)stash;
if (blur < 1)
return;
// Calculate the alpha such that 90% of the kernel is within the radius. (Kernel extends to infinity)
sigma = (float)blur * 0.57735f; // 1 / sqrt(3)
alpha = (int)((1<<APREC) * (1.0f - expf(-2.3f / (sigma+1.0f))));
fons__blurRows(dst, w, h, dstStride, alpha);