forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuklear_font.c
3380 lines (3137 loc) · 132 KB
/
nuklear_font.c
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
#include "nuklear.h"
#include "nuklear_internal.h"
#ifdef NK_INCLUDE_FONT_BAKING
/* -------------------------------------------------------------
*
* RECT PACK
*
* --------------------------------------------------------------*/
/* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
/* Sean Barrett 2014 */
#define NK_RP__MAXVAL 0xffff
typedef unsigned short nk_rp_coord;
struct nk_rp_rect {
/* reserved for your use: */
int id;
/* input: */
nk_rp_coord w, h;
/* output: */
nk_rp_coord x, y;
int was_packed;
/* non-zero if valid packing */
}; /* 16 bytes, nominally */
struct nk_rp_node {
nk_rp_coord x,y;
struct nk_rp_node *next;
};
struct nk_rp_context {
int width;
int height;
int align;
int init_mode;
int heuristic;
int num_nodes;
struct nk_rp_node *active_head;
struct nk_rp_node *free_head;
struct nk_rp_node extra[2];
/* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
};
struct nk_rp__findresult {
int x,y;
struct nk_rp_node **prev_link;
};
enum NK_RP_HEURISTIC {
NK_RP_HEURISTIC_Skyline_default=0,
NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
NK_RP_HEURISTIC_Skyline_BF_sortHeight
};
enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
NK_INTERN void
nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
{
if (allow_out_of_mem)
/* if it's ok to run out of memory, then don't bother aligning them; */
/* this gives better packing, but may fail due to OOM (even though */
/* the rectangles easily fit). @TODO a smarter approach would be to only */
/* quantize once we've hit OOM, then we could get rid of this parameter. */
context->align = 1;
else {
/* if it's not ok to run out of memory, then quantize the widths */
/* so that num_nodes is always enough nodes. */
/* */
/* I.e. num_nodes * align >= width */
/* align >= width / num_nodes */
/* align = ceil(width/num_nodes) */
context->align = (context->width + context->num_nodes-1) / context->num_nodes;
}
}
NK_INTERN void
nk_rp_init_target(struct nk_rp_context *context, int width, int height,
struct nk_rp_node *nodes, int num_nodes)
{
int i;
#ifndef STBRP_LARGE_RECTS
NK_ASSERT(width <= 0xffff && height <= 0xffff);
#endif
for (i=0; i < num_nodes-1; ++i)
nodes[i].next = &nodes[i+1];
nodes[i].next = 0;
context->init_mode = NK_RP__INIT_skyline;
context->heuristic = NK_RP_HEURISTIC_Skyline_default;
context->free_head = &nodes[0];
context->active_head = &context->extra[0];
context->width = width;
context->height = height;
context->num_nodes = num_nodes;
nk_rp_setup_allow_out_of_mem(context, 0);
/* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
context->extra[0].x = 0;
context->extra[0].y = 0;
context->extra[0].next = &context->extra[1];
context->extra[1].x = (nk_rp_coord) width;
context->extra[1].y = 65535;
context->extra[1].next = 0;
}
/* find minimum y position if it starts at x1 */
NK_INTERN int
nk_rp__skyline_find_min_y(struct nk_rp_context *c, struct nk_rp_node *first,
int x0, int width, int *pwaste)
{
struct nk_rp_node *node = first;
int x1 = x0 + width;
int min_y, visited_width, waste_area;
NK_ASSERT(first->x <= x0);
NK_UNUSED(c);
NK_ASSERT(node->next->x > x0);
/* we ended up handling this in the caller for efficiency */
NK_ASSERT(node->x <= x0);
min_y = 0;
waste_area = 0;
visited_width = 0;
while (node->x < x1)
{
if (node->y > min_y) {
/* raise min_y higher. */
/* we've accounted for all waste up to min_y, */
/* but we'll now add more waste for everything we've visited */
waste_area += visited_width * (node->y - min_y);
min_y = node->y;
/* the first time through, visited_width might be reduced */
if (node->x < x0)
visited_width += node->next->x - x0;
else
visited_width += node->next->x - node->x;
} else {
/* add waste area */
int under_width = node->next->x - node->x;
if (under_width + visited_width > width)
under_width = width - visited_width;
waste_area += under_width * (min_y - node->y);
visited_width += under_width;
}
node = node->next;
}
*pwaste = waste_area;
return min_y;
}
NK_INTERN struct nk_rp__findresult
nk_rp__skyline_find_best_pos(struct nk_rp_context *c, int width, int height)
{
int best_waste = (1<<30), best_x, best_y = (1 << 30);
struct nk_rp__findresult fr;
struct nk_rp_node **prev, *node, *tail, **best = 0;
/* align to multiple of c->align */
width = (width + c->align - 1);
width -= width % c->align;
NK_ASSERT(width % c->align == 0);
node = c->active_head;
prev = &c->active_head;
while (node->x + width <= c->width) {
int y,waste;
y = nk_rp__skyline_find_min_y(c, node, node->x, width, &waste);
/* actually just want to test BL */
if (c->heuristic == NK_RP_HEURISTIC_Skyline_BL_sortHeight) {
/* bottom left */
if (y < best_y) {
best_y = y;
best = prev;
}
} else {
/* best-fit */
if (y + height <= c->height) {
/* can only use it if it first vertically */
if (y < best_y || (y == best_y && waste < best_waste)) {
best_y = y;
best_waste = waste;
best = prev;
}
}
}
prev = &node->next;
node = node->next;
}
best_x = (best == 0) ? 0 : (*best)->x;
/* if doing best-fit (BF), we also have to try aligning right edge to each node position */
/* */
/* e.g, if fitting */
/* */
/* ____________________ */
/* |____________________| */
/* */
/* into */
/* */
/* | | */
/* | ____________| */
/* |____________| */
/* */
/* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
/* */
/* This makes BF take about 2x the time */
if (c->heuristic == NK_RP_HEURISTIC_Skyline_BF_sortHeight)
{
tail = c->active_head;
node = c->active_head;
prev = &c->active_head;
/* find first node that's admissible */
while (tail->x < width)
tail = tail->next;
while (tail)
{
int xpos = tail->x - width;
int y,waste;
NK_ASSERT(xpos >= 0);
/* find the left position that matches this */
while (node->next->x <= xpos) {
prev = &node->next;
node = node->next;
}
NK_ASSERT(node->next->x > xpos && node->x <= xpos);
y = nk_rp__skyline_find_min_y(c, node, xpos, width, &waste);
if (y + height < c->height) {
if (y <= best_y) {
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
best_x = xpos;
NK_ASSERT(y <= best_y);
best_y = y;
best_waste = waste;
best = prev;
}
}
}
tail = tail->next;
}
}
fr.prev_link = best;
fr.x = best_x;
fr.y = best_y;
return fr;
}
NK_INTERN struct nk_rp__findresult
nk_rp__skyline_pack_rectangle(struct nk_rp_context *context, int width, int height)
{
/* find best position according to heuristic */
struct nk_rp__findresult res = nk_rp__skyline_find_best_pos(context, width, height);
struct nk_rp_node *node, *cur;
/* bail if: */
/* 1. it failed */
/* 2. the best node doesn't fit (we don't always check this) */
/* 3. we're out of memory */
if (res.prev_link == 0 || res.y + height > context->height || context->free_head == 0) {
res.prev_link = 0;
return res;
}
/* on success, create new node */
node = context->free_head;
node->x = (nk_rp_coord) res.x;
node->y = (nk_rp_coord) (res.y + height);
context->free_head = node->next;
/* insert the new node into the right starting point, and */
/* let 'cur' point to the remaining nodes needing to be */
/* stitched back in */
cur = *res.prev_link;
if (cur->x < res.x) {
/* preserve the existing one, so start testing with the next one */
struct nk_rp_node *next = cur->next;
cur->next = node;
cur = next;
} else {
*res.prev_link = node;
}
/* from here, traverse cur and free the nodes, until we get to one */
/* that shouldn't be freed */
while (cur->next && cur->next->x <= res.x + width) {
struct nk_rp_node *next = cur->next;
/* move the current node to the free list */
cur->next = context->free_head;
context->free_head = cur;
cur = next;
}
/* stitch the list back in */
node->next = cur;
if (cur->x < res.x + width)
cur->x = (nk_rp_coord) (res.x + width);
return res;
}
NK_INTERN int
nk_rect_height_compare(const void *a, const void *b)
{
const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
if (p->h > q->h)
return -1;
if (p->h < q->h)
return 1;
return (p->w > q->w) ? -1 : (p->w < q->w);
}
NK_INTERN int
nk_rect_original_order(const void *a, const void *b)
{
const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
}
NK_INTERN void
nk_rp_qsort(struct nk_rp_rect *array, unsigned int len, int(*cmp)(const void*,const void*))
{
/* iterative quick sort */
#define NK_MAX_SORT_STACK 64
unsigned right, left = 0, stack[NK_MAX_SORT_STACK], pos = 0;
unsigned seed = len/2 * 69069+1;
for (;;) {
for (; left+1 < len; len++) {
struct nk_rp_rect pivot, tmp;
if (pos == NK_MAX_SORT_STACK) len = stack[pos = 0];
pivot = array[left+seed%(len-left)];
seed = seed * 69069 + 1;
stack[pos++] = len;
for (right = left-1;;) {
while (cmp(&array[++right], &pivot) < 0);
while (cmp(&pivot, &array[--len]) < 0);
if (right >= len) break;
tmp = array[right];
array[right] = array[len];
array[len] = tmp;
}
}
if (pos == 0) break;
left = len;
len = stack[--pos];
}
#undef NK_MAX_SORT_STACK
}
NK_INTERN void
nk_rp_pack_rects(struct nk_rp_context *context, struct nk_rp_rect *rects, int num_rects)
{
int i;
/* we use the 'was_packed' field internally to allow sorting/unsorting */
for (i=0; i < num_rects; ++i) {
rects[i].was_packed = i;
}
/* sort according to heuristic */
nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_height_compare);
for (i=0; i < num_rects; ++i) {
struct nk_rp__findresult fr = nk_rp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
if (fr.prev_link) {
rects[i].x = (nk_rp_coord) fr.x;
rects[i].y = (nk_rp_coord) fr.y;
} else {
rects[i].x = rects[i].y = NK_RP__MAXVAL;
}
}
/* unsort */
nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_original_order);
/* set was_packed flags */
for (i=0; i < num_rects; ++i)
rects[i].was_packed = !(rects[i].x == NK_RP__MAXVAL && rects[i].y == NK_RP__MAXVAL);
}
/*
* ==============================================================
*
* TRUETYPE
*
* ===============================================================
*/
/* stb_truetype.h - v1.07 - public domain */
#define NK_TT_MAX_OVERSAMPLE 8
#define NK_TT__OVER_MASK (NK_TT_MAX_OVERSAMPLE-1)
struct nk_tt_bakedchar {
unsigned short x0,y0,x1,y1;
/* coordinates of bbox in bitmap */
float xoff,yoff,xadvance;
};
struct nk_tt_aligned_quad{
float x0,y0,s0,t0; /* top-left */
float x1,y1,s1,t1; /* bottom-right */
};
struct nk_tt_packedchar {
unsigned short x0,y0,x1,y1;
/* coordinates of bbox in bitmap */
float xoff,yoff,xadvance;
float xoff2,yoff2;
};
struct nk_tt_pack_range {
float font_size;
int first_unicode_codepoint_in_range;
/* if non-zero, then the chars are continuous, and this is the first codepoint */
int *array_of_unicode_codepoints;
/* if non-zero, then this is an array of unicode codepoints */
int num_chars;
struct nk_tt_packedchar *chardata_for_range; /* output */
unsigned char h_oversample, v_oversample;
/* don't set these, they're used internally */
};
struct nk_tt_pack_context {
void *pack_info;
int width;
int height;
int stride_in_bytes;
int padding;
unsigned int h_oversample, v_oversample;
unsigned char *pixels;
void *nodes;
};
struct nk_tt_fontinfo {
const unsigned char* data; /* pointer to .ttf file */
int fontstart;/* offset of start of font */
int numGlyphs;/* number of glyphs, needed for range checking */
int loca,head,glyf,hhea,hmtx,kern; /* table locations as offset from start of .ttf */
int index_map; /* a cmap mapping for our chosen character encoding */
int indexToLocFormat; /* format needed to map from glyph index to glyph */
};
enum {
NK_TT_vmove=1,
NK_TT_vline,
NK_TT_vcurve
};
struct nk_tt_vertex {
short x,y,cx,cy;
unsigned char type,padding;
};
struct nk_tt__bitmap{
int w,h,stride;
unsigned char *pixels;
};
struct nk_tt__hheap_chunk {
struct nk_tt__hheap_chunk *next;
};
struct nk_tt__hheap {
struct nk_allocator alloc;
struct nk_tt__hheap_chunk *head;
void *first_free;
int num_remaining_in_head_chunk;
};
struct nk_tt__edge {
float x0,y0, x1,y1;
int invert;
};
struct nk_tt__active_edge {
struct nk_tt__active_edge *next;
float fx,fdx,fdy;
float direction;
float sy;
float ey;
};
struct nk_tt__point {float x,y;};
#define NK_TT_MACSTYLE_DONTCARE 0
#define NK_TT_MACSTYLE_BOLD 1
#define NK_TT_MACSTYLE_ITALIC 2
#define NK_TT_MACSTYLE_UNDERSCORE 4
#define NK_TT_MACSTYLE_NONE 8
/* <= not same as 0, this makes us check the bitfield is 0 */
enum { /* platformID */
NK_TT_PLATFORM_ID_UNICODE =0,
NK_TT_PLATFORM_ID_MAC =1,
NK_TT_PLATFORM_ID_ISO =2,
NK_TT_PLATFORM_ID_MICROSOFT =3
};
enum { /* encodingID for NK_TT_PLATFORM_ID_UNICODE */
NK_TT_UNICODE_EID_UNICODE_1_0 =0,
NK_TT_UNICODE_EID_UNICODE_1_1 =1,
NK_TT_UNICODE_EID_ISO_10646 =2,
NK_TT_UNICODE_EID_UNICODE_2_0_BMP=3,
NK_TT_UNICODE_EID_UNICODE_2_0_FULL=4
};
enum { /* encodingID for NK_TT_PLATFORM_ID_MICROSOFT */
NK_TT_MS_EID_SYMBOL =0,
NK_TT_MS_EID_UNICODE_BMP =1,
NK_TT_MS_EID_SHIFTJIS =2,
NK_TT_MS_EID_UNICODE_FULL =10
};
enum { /* encodingID for NK_TT_PLATFORM_ID_MAC; same as Script Manager codes */
NK_TT_MAC_EID_ROMAN =0, NK_TT_MAC_EID_ARABIC =4,
NK_TT_MAC_EID_JAPANESE =1, NK_TT_MAC_EID_HEBREW =5,
NK_TT_MAC_EID_CHINESE_TRAD =2, NK_TT_MAC_EID_GREEK =6,
NK_TT_MAC_EID_KOREAN =3, NK_TT_MAC_EID_RUSSIAN =7
};
enum { /* languageID for NK_TT_PLATFORM_ID_MICROSOFT; same as LCID... */
/* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
NK_TT_MS_LANG_ENGLISH =0x0409, NK_TT_MS_LANG_ITALIAN =0x0410,
NK_TT_MS_LANG_CHINESE =0x0804, NK_TT_MS_LANG_JAPANESE =0x0411,
NK_TT_MS_LANG_DUTCH =0x0413, NK_TT_MS_LANG_KOREAN =0x0412,
NK_TT_MS_LANG_FRENCH =0x040c, NK_TT_MS_LANG_RUSSIAN =0x0419,
NK_TT_MS_LANG_GERMAN =0x0407, NK_TT_MS_LANG_SPANISH =0x0409,
NK_TT_MS_LANG_HEBREW =0x040d, NK_TT_MS_LANG_SWEDISH =0x041D
};
enum { /* languageID for NK_TT_PLATFORM_ID_MAC */
NK_TT_MAC_LANG_ENGLISH =0 , NK_TT_MAC_LANG_JAPANESE =11,
NK_TT_MAC_LANG_ARABIC =12, NK_TT_MAC_LANG_KOREAN =23,
NK_TT_MAC_LANG_DUTCH =4 , NK_TT_MAC_LANG_RUSSIAN =32,
NK_TT_MAC_LANG_FRENCH =1 , NK_TT_MAC_LANG_SPANISH =6 ,
NK_TT_MAC_LANG_GERMAN =2 , NK_TT_MAC_LANG_SWEDISH =5 ,
NK_TT_MAC_LANG_HEBREW =10, NK_TT_MAC_LANG_CHINESE_SIMPLIFIED =33,
NK_TT_MAC_LANG_ITALIAN =3 , NK_TT_MAC_LANG_CHINESE_TRAD =19
};
#define nk_ttBYTE(p) (* (const nk_byte *) (p))
#define nk_ttCHAR(p) (* (const char *) (p))
#if defined(NK_BIGENDIAN) && !defined(NK_ALLOW_UNALIGNED_TRUETYPE)
#define nk_ttUSHORT(p) (* (nk_ushort *) (p))
#define nk_ttSHORT(p) (* (nk_short *) (p))
#define nk_ttULONG(p) (* (nk_uint *) (p))
#define nk_ttLONG(p) (* (nk_int *) (p))
#else
static nk_ushort nk_ttUSHORT(const nk_byte *p) { return (nk_ushort)(p[0]*256 + p[1]); }
static nk_short nk_ttSHORT(const nk_byte *p) { return (nk_short)(p[0]*256 + p[1]); }
static nk_uint nk_ttULONG(const nk_byte *p) { return (nk_uint)((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]); }
#endif
#define nk_tt_tag4(p,c0,c1,c2,c3)\
((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
#define nk_tt_tag(p,str) nk_tt_tag4(p,str[0],str[1],str[2],str[3])
NK_INTERN int nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
int glyph_index, struct nk_tt_vertex **pvertices);
NK_INTERN nk_uint
nk_tt__find_table(const nk_byte *data, nk_uint fontstart, const char *tag)
{
/* @OPTIMIZE: binary search */
nk_int num_tables = nk_ttUSHORT(data+fontstart+4);
nk_uint tabledir = fontstart + 12;
nk_int i;
for (i = 0; i < num_tables; ++i) {
nk_uint loc = tabledir + (nk_uint)(16*i);
if (nk_tt_tag(data+loc+0, tag))
return nk_ttULONG(data+loc+8);
}
return 0;
}
NK_INTERN int
nk_tt_InitFont(struct nk_tt_fontinfo *info, const unsigned char *data2, int fontstart)
{
nk_uint cmap, t;
nk_int i,numTables;
const nk_byte *data = (const nk_byte *) data2;
info->data = data;
info->fontstart = fontstart;
cmap = nk_tt__find_table(data, (nk_uint)fontstart, "cmap"); /* required */
info->loca = (int)nk_tt__find_table(data, (nk_uint)fontstart, "loca"); /* required */
info->head = (int)nk_tt__find_table(data, (nk_uint)fontstart, "head"); /* required */
info->glyf = (int)nk_tt__find_table(data, (nk_uint)fontstart, "glyf"); /* required */
info->hhea = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hhea"); /* required */
info->hmtx = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hmtx"); /* required */
info->kern = (int)nk_tt__find_table(data, (nk_uint)fontstart, "kern"); /* not required */
if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
return 0;
t = nk_tt__find_table(data, (nk_uint)fontstart, "maxp");
if (t) info->numGlyphs = nk_ttUSHORT(data+t+4);
else info->numGlyphs = 0xffff;
/* find a cmap encoding table we understand *now* to avoid searching */
/* later. (todo: could make this installable) */
/* the same regardless of glyph. */
numTables = nk_ttUSHORT(data + cmap + 2);
info->index_map = 0;
for (i=0; i < numTables; ++i)
{
nk_uint encoding_record = cmap + 4 + 8 * (nk_uint)i;
/* find an encoding we understand: */
switch(nk_ttUSHORT(data+encoding_record)) {
case NK_TT_PLATFORM_ID_MICROSOFT:
switch (nk_ttUSHORT(data+encoding_record+2)) {
case NK_TT_MS_EID_UNICODE_BMP:
case NK_TT_MS_EID_UNICODE_FULL:
/* MS/Unicode */
info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
break;
default: break;
} break;
case NK_TT_PLATFORM_ID_UNICODE:
/* Mac/iOS has these */
/* all the encodingIDs are unicode, so we don't bother to check it */
info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
break;
default: break;
}
}
if (info->index_map == 0)
return 0;
info->indexToLocFormat = nk_ttUSHORT(data+info->head + 50);
return 1;
}
NK_INTERN int
nk_tt_FindGlyphIndex(const struct nk_tt_fontinfo *info, int unicode_codepoint)
{
const nk_byte *data = info->data;
nk_uint index_map = (nk_uint)info->index_map;
nk_ushort format = nk_ttUSHORT(data + index_map + 0);
if (format == 0) { /* apple byte encoding */
nk_int bytes = nk_ttUSHORT(data + index_map + 2);
if (unicode_codepoint < bytes-6)
return nk_ttBYTE(data + index_map + 6 + unicode_codepoint);
return 0;
} else if (format == 6) {
nk_uint first = nk_ttUSHORT(data + index_map + 6);
nk_uint count = nk_ttUSHORT(data + index_map + 8);
if ((nk_uint) unicode_codepoint >= first && (nk_uint) unicode_codepoint < first+count)
return nk_ttUSHORT(data + index_map + 10 + (unicode_codepoint - (int)first)*2);
return 0;
} else if (format == 2) {
NK_ASSERT(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
return 0;
} else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
nk_ushort segcount = nk_ttUSHORT(data+index_map+6) >> 1;
nk_ushort searchRange = nk_ttUSHORT(data+index_map+8) >> 1;
nk_ushort entrySelector = nk_ttUSHORT(data+index_map+10);
nk_ushort rangeShift = nk_ttUSHORT(data+index_map+12) >> 1;
/* do a binary search of the segments */
nk_uint endCount = index_map + 14;
nk_uint search = endCount;
if (unicode_codepoint > 0xffff)
return 0;
/* they lie from endCount .. endCount + segCount */
/* but searchRange is the nearest power of two, so... */
if (unicode_codepoint >= nk_ttUSHORT(data + search + rangeShift*2))
search += (nk_uint)(rangeShift*2);
/* now decrement to bias correctly to find smallest */
search -= 2;
while (entrySelector) {
nk_ushort end;
searchRange >>= 1;
end = nk_ttUSHORT(data + search + searchRange*2);
if (unicode_codepoint > end)
search += (nk_uint)(searchRange*2);
--entrySelector;
}
search += 2;
{
nk_ushort offset, start;
nk_ushort item = (nk_ushort) ((search - endCount) >> 1);
NK_ASSERT(unicode_codepoint <= nk_ttUSHORT(data + endCount + 2*item));
start = nk_ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
if (unicode_codepoint < start)
return 0;
offset = nk_ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
if (offset == 0)
return (nk_ushort) (unicode_codepoint + nk_ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
return nk_ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
}
} else if (format == 12 || format == 13) {
nk_uint ngroups = nk_ttULONG(data+index_map+12);
nk_int low,high;
low = 0; high = (nk_int)ngroups;
/* Binary search the right group. */
while (low < high) {
nk_int mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
nk_uint start_char = nk_ttULONG(data+index_map+16+mid*12);
nk_uint end_char = nk_ttULONG(data+index_map+16+mid*12+4);
if ((nk_uint) unicode_codepoint < start_char)
high = mid;
else if ((nk_uint) unicode_codepoint > end_char)
low = mid+1;
else {
nk_uint start_glyph = nk_ttULONG(data+index_map+16+mid*12+8);
if (format == 12)
return (int)start_glyph + (int)unicode_codepoint - (int)start_char;
else /* format == 13 */
return (int)start_glyph;
}
}
return 0; /* not found */
}
/* @TODO */
NK_ASSERT(0);
return 0;
}
NK_INTERN void
nk_tt_setvertex(struct nk_tt_vertex *v, nk_byte type, nk_int x, nk_int y, nk_int cx, nk_int cy)
{
v->type = type;
v->x = (nk_short) x;
v->y = (nk_short) y;
v->cx = (nk_short) cx;
v->cy = (nk_short) cy;
}
NK_INTERN int
nk_tt__GetGlyfOffset(const struct nk_tt_fontinfo *info, int glyph_index)
{
int g1,g2;
if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
if (info->indexToLocFormat == 0) {
g1 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
g2 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
} else {
g1 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4);
g2 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4 + 4);
}
return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
}
NK_INTERN int
nk_tt_GetGlyphBox(const struct nk_tt_fontinfo *info, int glyph_index,
int *x0, int *y0, int *x1, int *y1)
{
int g = nk_tt__GetGlyfOffset(info, glyph_index);
if (g < 0) return 0;
if (x0) *x0 = nk_ttSHORT(info->data + g + 2);
if (y0) *y0 = nk_ttSHORT(info->data + g + 4);
if (x1) *x1 = nk_ttSHORT(info->data + g + 6);
if (y1) *y1 = nk_ttSHORT(info->data + g + 8);
return 1;
}
NK_INTERN int
nk_tt__close_shape(struct nk_tt_vertex *vertices, int num_vertices, int was_off,
int start_off, nk_int sx, nk_int sy, nk_int scx, nk_int scy, nk_int cx, nk_int cy)
{
if (start_off) {
if (was_off)
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, sx,sy,scx,scy);
} else {
if (was_off)
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve,sx,sy,cx,cy);
else
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline,sx,sy,0,0);
}
return num_vertices;
}
NK_INTERN int
nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
int glyph_index, struct nk_tt_vertex **pvertices)
{
nk_short numberOfContours;
const nk_byte *endPtsOfContours;
const nk_byte *data = info->data;
struct nk_tt_vertex *vertices=0;
int num_vertices=0;
int g = nk_tt__GetGlyfOffset(info, glyph_index);
*pvertices = 0;
if (g < 0) return 0;
numberOfContours = nk_ttSHORT(data + g);
if (numberOfContours > 0) {
nk_byte flags=0,flagcount;
nk_int ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
nk_int x,y,cx,cy,sx,sy, scx,scy;
const nk_byte *points;
endPtsOfContours = (data + g + 10);
ins = nk_ttUSHORT(data + g + 10 + numberOfContours * 2);
points = data + g + 10 + numberOfContours * 2 + 2 + ins;
n = 1+nk_ttUSHORT(endPtsOfContours + numberOfContours*2-2);
m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
vertices = (struct nk_tt_vertex *)alloc->alloc(alloc->userdata, 0, (nk_size)m * sizeof(vertices[0]));
if (vertices == 0)
return 0;
next_move = 0;
flagcount=0;
/* in first pass, we load uninterpreted data into the allocated array */
/* above, shifted to the end of the array so we won't overwrite it when */
/* we create our final data starting from the front */
off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
/* first load flags */
for (i=0; i < n; ++i) {
if (flagcount == 0) {
flags = *points++;
if (flags & 8)
flagcount = *points++;
} else --flagcount;
vertices[off+i].type = flags;
}
/* now load x coordinates */
x=0;
for (i=0; i < n; ++i) {
flags = vertices[off+i].type;
if (flags & 2) {
nk_short dx = *points++;
x += (flags & 16) ? dx : -dx; /* ??? */
} else {
if (!(flags & 16)) {
x = x + (nk_short) (points[0]*256 + points[1]);
points += 2;
}
}
vertices[off+i].x = (nk_short) x;
}
/* now load y coordinates */
y=0;
for (i=0; i < n; ++i) {
flags = vertices[off+i].type;
if (flags & 4) {
nk_short dy = *points++;
y += (flags & 32) ? dy : -dy; /* ??? */
} else {
if (!(flags & 32)) {
y = y + (nk_short) (points[0]*256 + points[1]);
points += 2;
}
}
vertices[off+i].y = (nk_short) y;
}
/* now convert them to our format */
num_vertices=0;
sx = sy = cx = cy = scx = scy = 0;
for (i=0; i < n; ++i)
{
flags = vertices[off+i].type;
x = (nk_short) vertices[off+i].x;
y = (nk_short) vertices[off+i].y;
if (next_move == i) {
if (i != 0)
num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
/* now start the new one */
start_off = !(flags & 1);
if (start_off) {
/* if we start off with an off-curve point, then when we need to find a point on the curve */
/* where we can start, and we need to save some state for when we wraparound. */
scx = x;
scy = y;
if (!(vertices[off+i+1].type & 1)) {
/* next point is also a curve point, so interpolate an on-point curve */
sx = (x + (nk_int) vertices[off+i+1].x) >> 1;
sy = (y + (nk_int) vertices[off+i+1].y) >> 1;
} else {
/* otherwise just use the next point as our start point */
sx = (nk_int) vertices[off+i+1].x;
sy = (nk_int) vertices[off+i+1].y;
++i; /* we're using point i+1 as the starting point, so skip it */
}
} else {
sx = x;
sy = y;
}
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vmove,sx,sy,0,0);
was_off = 0;
next_move = 1 + nk_ttUSHORT(endPtsOfContours+j*2);
++j;
} else {
if (!(flags & 1))
{ /* if it's a curve */
if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
cx = x;
cy = y;
was_off = 1;
} else {
if (was_off)
nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, x,y, cx, cy);
else nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline, x,y,0,0);
was_off = 0;
}
}
}
num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
} else if (numberOfContours == -1) {
/* Compound shapes. */
int more = 1;
const nk_byte *comp = data + g + 10;
num_vertices = 0;
vertices = 0;
while (more)
{
nk_ushort flags, gidx;
int comp_num_verts = 0, i;
struct nk_tt_vertex *comp_verts = 0, *tmp = 0;
float mtx[6] = {1,0,0,1,0,0}, m, n;
flags = (nk_ushort)nk_ttSHORT(comp); comp+=2;
gidx = (nk_ushort)nk_ttSHORT(comp); comp+=2;
if (flags & 2) { /* XY values */
if (flags & 1) { /* shorts */
mtx[4] = nk_ttSHORT(comp); comp+=2;
mtx[5] = nk_ttSHORT(comp); comp+=2;
} else {
mtx[4] = nk_ttCHAR(comp); comp+=1;
mtx[5] = nk_ttCHAR(comp); comp+=1;
}
} else {
/* @TODO handle matching point */
NK_ASSERT(0);
}
if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
mtx[0] = mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
mtx[1] = mtx[2] = 0;
} else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
mtx[1] = mtx[2] = 0;
mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
} else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
mtx[1] = nk_ttSHORT(comp)/16384.0f; comp+=2;
mtx[2] = nk_ttSHORT(comp)/16384.0f; comp+=2;
mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
}
/* Find transformation scales. */
m = (float) NK_SQRT(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
n = (float) NK_SQRT(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
/* Get indexed glyph. */
comp_num_verts = nk_tt_GetGlyphShape(info, alloc, gidx, &comp_verts);
if (comp_num_verts > 0)
{
/* Transform vertices. */
for (i = 0; i < comp_num_verts; ++i) {
struct nk_tt_vertex* v = &comp_verts[i];
short x,y;
x=v->x; y=v->y;
v->x = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
v->y = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
x=v->cx; y=v->cy;
v->cx = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
}
/* Append vertices. */
tmp = (struct nk_tt_vertex*)alloc->alloc(alloc->userdata, 0,
(nk_size)(num_vertices+comp_num_verts)*sizeof(struct nk_tt_vertex));
if (!tmp) {
if (vertices) alloc->free(alloc->userdata, vertices);
if (comp_verts) alloc->free(alloc->userdata, comp_verts);
return 0;
}
if (num_vertices > 0) NK_MEMCPY(tmp, vertices, (nk_size)num_vertices*sizeof(struct nk_tt_vertex));
NK_MEMCPY(tmp+num_vertices, comp_verts, (nk_size)comp_num_verts*sizeof(struct nk_tt_vertex));
if (vertices) alloc->free(alloc->userdata,vertices);
vertices = tmp;
alloc->free(alloc->userdata,comp_verts);
num_vertices += comp_num_verts;
}
/* More components ? */
more = flags & (1<<5);
}
} else if (numberOfContours < 0) {
/* @TODO other compound variations? */
NK_ASSERT(0);
} else {
/* numberOfCounters == 0, do nothing */
}
*pvertices = vertices;
return num_vertices;
}
NK_INTERN void
nk_tt_GetGlyphHMetrics(const struct nk_tt_fontinfo *info, int glyph_index,
int *advanceWidth, int *leftSideBearing)
{
nk_ushort numOfLongHorMetrics = nk_ttUSHORT(info->data+info->hhea + 34);
if (glyph_index < numOfLongHorMetrics) {
if (advanceWidth)
*advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index);
if (leftSideBearing)
*leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
} else {