forked from aligrudi/neatmkfn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotf.c
1440 lines (1372 loc) · 39.6 KB
/
otf.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
/* OpenType and TrueType fonts */
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mkfn.h"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
#define NGLYPHS (1 << 16)
#define NLOOKUPS (1 << 12)
#define GNLEN (64)
#define NGRPS 2048
#define U32(buf, off) (htonl(*(u32 *) ((buf) + (off))))
#define U16(buf, off) (htons(*(u16 *) ((buf) + (off))))
#define U8(buf, off) (*(u8 *) ((buf) + (off)))
#define S16(buf, off) ((s16) htons(*(u16 *) ((buf) + (off))))
#define S32(buf, off) ((s32) htonl(*(u32 *) ((buf) + (off))))
#define GCTXLEN 16 /* number of context backtrack coverage arrays */
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
typedef int s32;
typedef short s16;
static char glyph_name[NGLYPHS][GNLEN];
static int glyph_code[NGLYPHS];
static int glyph_bbox[NGLYPHS][4];
static int glyph_wid[NGLYPHS];
static int glyph_n;
static int upm; /* units per em */
static int sec; /* current font section (lookup index * 10) */
struct otf {
void *otf; /* TTC header or offset table */
void *off; /* offset table */
char name[128]; /* font name */
};
static char *macset[];
static char *stdset[];
static int owid(int w)
{
return (w < 0 ? w * 1000 - upm / 2 : w * 1000 + upm / 2) / upm;
}
static int uwid(int w)
{
int d = 72000 / mkfn_res;
return (w < 0 ? owid(w) - d / 20 : owid(w) + d / 20) * 10 / d;
}
/* whether the script is right-to-left */
static int otf_r2l(char *feat)
{
char *scrp = strchr(feat, ':') + 1;
return !strcmp("arab", scrp) || !strcmp("hebr", scrp);
}
/* report unsupported otf tables */
static void otf_unsupported(char *sub, int type, int fmt)
{
if (mkfn_warn) {
fprintf(stderr, "neatmkfn: unsupported %s lookup %d", sub, type);
if (fmt > 0)
fprintf(stderr, " format %d", fmt);
fprintf(stderr, "\n");
}
}
/* find the otf table with the given name */
static void *otf_table(struct otf *otf, char *name)
{
int nrecs = U16(otf->off, 4);
int i;
for (i = 0; i < nrecs; i++) {
void *rec = otf->off + 12 + i * 16; /* an otf table record */
if (!strncmp(rec, name, 4))
return otf->otf + U32(rec, 8);
}
return NULL;
}
/* obtain postscript font name from name table */
static void otf_name(struct otf *otf, void *tab)
{
char name[256];
void *str = tab + U16(tab, 4); /* storage area */
int n = U16(tab, 2); /* number of name records */
int i;
for (i = 0; i < n; i++) {
void *rec = tab + 6 + 12 * i;
int pid = U16(rec, 0); /* platform id */
int eid = U16(rec, 2); /* encoding id */
int lid = U16(rec, 4); /* language id */
int nid = U16(rec, 6); /* name id */
int len = U16(rec, 8); /* string length */
int off = U16(rec, 10); /* string offset */
if (pid == 1 && eid == 0 && lid == 0 && nid == 6) {
memcpy(name, str + off, len);
name[len] = '\0';
snprintf(otf->name, sizeof(otf->name), "%s", name);
}
}
}
/* parse otf cmap format 4 subtable */
static void otf_cmap4(struct otf *otf, void *cmap4)
{
int nsegs;
void *ends, *begs, *deltas, *offsets;
int beg, end, delta, offset;
int i, j;
nsegs = U16(cmap4, 6) / 2;
ends = cmap4 + 14;
begs = ends + 2 * nsegs + 2;
deltas = begs + 2 * nsegs;
offsets = deltas + 2 * nsegs;
for (i = 0; i < nsegs; i++) {
beg = U16(begs, 2 * i);
end = U16(ends, 2 * i);
delta = U16(deltas, 2 * i);
offset = U16(offsets, 2 * i);
if (offset) {
for (j = beg; j <= end; j++)
glyph_code[(U16(offsets + 2 * i,
offset + (j - beg) * 2) + delta) & 0xffff] = j;
} else {
for (j = beg; j <= end; j++)
glyph_code[(j + delta) & 0xffff] = j;
}
}
}
/* parse otf cmap header */
static void otf_cmap(struct otf *otf, void *cmap)
{
int nrecs = U16(cmap, 2);
int i;
for (i = 0; i < nrecs; i++) {
void *rec = cmap + 4 + i * 8; /* a cmap record */
int plat = U16(rec, 0);
int enc = U16(rec, 2);
void *tab = cmap + U32(rec, 4); /* a cmap subtable */
int fmt = U16(tab, 0);
if (plat == 3 && enc == 1 && fmt == 4)
otf_cmap4(otf, tab);
}
}
static void otf_post(struct otf *otf, void *post)
{
void *post2; /* version 2.0 header */
void *index; /* glyph name indices */
void *names; /* glyph names */
int cname = 0;
int i;
if (U32(post, 0) != 0x20000)
return;
post2 = post + 32;
glyph_n = U16(post2, 0);
index = post2 + 2;
names = index + 2 * glyph_n;
for (i = 0; i < glyph_n; i++) {
int idx = U16(index, 2 * i);
if (idx < 258) {
strcpy(glyph_name[i], macset[idx]);
} else {
memcpy(glyph_name[i], names + cname + 1,
U8(names, cname));
glyph_name[i][U8(names, cname)] = '\0';
cname += U8(names, cname) + 1;
}
}
}
static void otf_glyf(struct otf *otf, void *glyf)
{
void *maxp = otf_table(otf, "maxp");
void *head = otf_table(otf, "head");
void *loca = otf_table(otf, "loca");
void *gdat;
void *gdat_next;
int n = U16(maxp, 4);
int fmt = U16(head, 50);
int i, j;
if (!glyph_n)
glyph_n = n;
for (i = 0; i < n; i++) {
if (fmt) {
gdat = glyf + U32(loca, 4 * i);
gdat_next = glyf + U32(loca, 4 * (i + 1));
} else {
gdat = glyf + U16(loca, 2 * i) * 2;
gdat_next = glyf + U16(loca, 2 * (i + 1)) * 2;
}
if (gdat < gdat_next)
for (j = 0; j < 4; j++)
glyph_bbox[i][j] = S16(gdat, 2 + 2 * j);
}
}
static void otf_hmtx(struct otf *otf, void *hmtx)
{
void *hhea = otf_table(otf, "hhea");
int n;
int i;
n = U16(hhea, 34);
for (i = 0; i < n; i++)
glyph_wid[i] = U16(hmtx, i * 4);
for (i = n; i < glyph_n; i++)
glyph_wid[i] = glyph_wid[n - 1];
}
static void otf_kern(struct otf *otf, void *kern)
{
int off = 4;
int i, j;
int n = U16(kern, 2); /* number of kern subtables */
for (i = 0; i < n; i++) {
void *tab = kern + off; /* a kern subtable */
int cov = U16(tab, 4);
off += U16(tab, 2);
if ((cov >> 8) == 0 && (cov & 1)) { /* format 0 */
int npairs = U16(tab, 6);
for (j = 0; j < npairs; j++) {
int c1 = U16(tab, 14 + 6 * j);
int c2 = U16(tab, 14 + 6 * j + 2);
int val = S16(tab, 14 + 6 * j + 4);
mkfn_kern(glyph_name[c1], glyph_name[c2],
uwid(val));
}
}
}
}
static int *coverage(void *cov, int *ncov)
{
int fmt = U16(cov, 0);
int n = U16(cov, 2);
int beg, end;
int i, j;
int *out = malloc(glyph_n * sizeof(*out));
int cnt = 0;
if (fmt == 1) {
for (i = 0; i < n; i++)
out[cnt++] = U16(cov, 4 + 2 * i);
}
if (fmt == 2) {
for (i = 0; i < n; i++) {
beg = U16(cov, 4 + 6 * i);
end = U16(cov, 4 + 6 * i + 2);
for (j = beg; j <= end; j++)
out[cnt++] = j;
}
}
if (ncov)
*ncov = cnt;
return out;
}
static int classdef(void *tab, int *gl, int *cls)
{
int fmt = U16(tab, 0);
int ngl = 0;
int i, j;
if (fmt == 1) {
int beg = U16(tab, 2);
ngl = U16(tab, 4);
for (i = 0; i < ngl; i++) {
gl[i] = beg + i;
cls[i] = U16(tab, 6 + 2 * i);
}
}
if (fmt == 2) {
int n = U16(tab, 2);
for (i = 0; i < n; i++) {
int beg = U16(tab, 4 + 6 * i);
int end = U16(tab, 4 + 6 * i + 2);
for (j = beg; j <= end; j++) {
gl[ngl] = j;
cls[ngl] = U16(tab, 4 + 6 * i + 4);
ngl++;
}
}
}
return ngl;
}
static int intcmp(void *v1, void *v2)
{
return *(int *) v1 - *(int *) v2;
}
static int ggrp_make(int *src, int n);
static int ggrp_class(int *src, int *cls, int nsrc, int id)
{
int *g = malloc(nsrc * sizeof(g[0]));
int n = 0;
int i;
int grp;
for (i = 0; i < nsrc; i++)
if (cls[i] == id)
g[n++] = src[i];
qsort(g, n, sizeof(g[0]), (void *) intcmp);
grp = ggrp_make(g, n);
free(g);
return grp;
}
static int ggrp_coverage(int *g, int n)
{
qsort(g, n, sizeof(g[0]), (void *) intcmp);
return ggrp_make(g, n);
}
static int valuerecord_len(int fmt)
{
int off = 0;
int i;
for (i = 0; i < 8; i++)
if (fmt & (1 << i))
off += 2;
return off;
}
static void valuerecord_print(int fmt, void *rec)
{
int vals[8] = {0};
int off = 0;
int i;
for (i = 0; i < 8; i++) {
if (fmt & (1 << i)) {
vals[i] = uwid(S16(rec, off));
off += 2;
}
}
if (fmt)
printf(":%+d%+d%+d%+d", vals[0], vals[1], vals[2], vals[3]);
}
static int valuerecord_small(int fmt, void *rec)
{
int off = 0;
int i;
for (i = 0; i < 8; i++) {
if (fmt & (1 << i)) {
if (abs(uwid(S16(rec, off))) >= MAX(1, mkfn_kmin))
return 0;
off += 2;
}
}
return 1;
}
/* single adjustment positioning */
static void otf_gpostype1(struct otf *otf, void *sub, char *feat)
{
int fmt = U16(sub, 0);
int vfmt = U16(sub, 4);
int *cov;
int ncov, nvals;
int vlen = valuerecord_len(vfmt);
int i;
cov = coverage(sub + U16(sub, 2), &ncov);
if (fmt == 1) {
for (i = 0; i < ncov; i++) {
if (valuerecord_small(vfmt, sub + 6))
continue;
printf("gpos %s 1 %s", feat, glyph_name[cov[i]]);
valuerecord_print(vfmt, sub + 6);
printf("\n");
}
}
if (fmt == 2) {
nvals = U16(sub, 6);
for (i = 0; i < nvals; i++) {
if (valuerecord_small(vfmt, sub + 6))
continue;
printf("gpos %s 1 %s", feat, glyph_name[cov[i]]);
valuerecord_print(vfmt, sub + 8 + i * vlen);
printf("\n");
}
}
free(cov);
}
/* pair adjustment positioning */
static void otf_gpostype2(struct otf *otf, void *sub, char *feat)
{
int fmt = U16(sub, 0);
int vfmt1 = U16(sub, 4); /* valuerecord 1 */
int vfmt2 = U16(sub, 6); /* valuerecord 2 */
int fmtoff1, fmtoff2;
int vrlen; /* the length of vfmt1 and vfmt2 */
int i, j;
vrlen = valuerecord_len(vfmt1) + valuerecord_len(vfmt2);
if (fmt == 1) {
int nc1 = U16(sub, 8);
int *cov = coverage(sub + U16(sub, 2), NULL);
for (i = 0; i < nc1; i++) {
void *c2 = sub + U16(sub, 10 + 2 * i);
int nc2 = U16(c2, 0);
for (j = 0; j < nc2; j++) {
int second = U16(c2 + 2 + (2 + vrlen) * j, 0);
fmtoff1 = 2 + (2 + vrlen) * j + 2;
fmtoff2 = fmtoff1 + valuerecord_len(vfmt1);
if (valuerecord_small(vfmt1, c2 + fmtoff1) &&
valuerecord_small(vfmt2, c2 + fmtoff2))
continue;
printf("gpos %s 2", feat);
printf(" %s", glyph_name[cov[i]]);
valuerecord_print(vfmt1, c2 + fmtoff1);
printf(" %s", glyph_name[second]);
valuerecord_print(vfmt2, c2 + fmtoff2);
printf("\n");
}
}
free(cov);
}
if (fmt == 2) {
static int gl1[NGLYPHS], gl2[NGLYPHS];
static int cls1[NGLYPHS], cls2[NGLYPHS];
static int grp1[NGLYPHS], grp2[NGLYPHS];
int ngl1 = classdef(sub + U16(sub, 8), gl1, cls1);
int ngl2 = classdef(sub + U16(sub, 10), gl2, cls2);
int ncls1 = U16(sub, 12);
int ncls2 = U16(sub, 14);
for (i = 0; i < ncls1; i++)
grp1[i] = ggrp_class(gl1, cls1, ngl1, i);
for (i = 0; i < ncls2; i++)
grp2[i] = ggrp_class(gl2, cls2, ngl2, i);
for (i = 0; i < ncls1; i++) {
for (j = 0; j < ncls2; j++) {
fmtoff1 = 16 + (i * ncls2 + j) * vrlen;
fmtoff2 = fmtoff1 + valuerecord_len(vfmt1);
if (valuerecord_small(vfmt1, sub + fmtoff1) &&
valuerecord_small(vfmt2, sub + fmtoff2))
continue;
printf("gpos %s %d", feat, 2);
printf(" @%d", grp1[i]);
valuerecord_print(vfmt1, sub + fmtoff1);
printf(" @%d", grp2[j]);
valuerecord_print(vfmt2, sub + fmtoff2);
printf("\n");
}
}
}
}
/* cursive attachment positioning */
static void otf_gpostype3(struct otf *otf, void *sub, char *feat)
{
int fmt = U16(sub, 0);
int *cov, *icov, *ocov;
int i, n;
int icnt = 0;
int ocnt = 0;
int igrp, ogrp;
if (fmt != 1)
return;
cov = coverage(sub + U16(sub, 2), NULL);
n = U16(sub, 4);
icov = malloc(n * sizeof(icov[0]));
ocov = malloc(n * sizeof(ocov[0]));
for (i = 0; i < n; i++)
if (U16(sub, 6 + 4 * i))
ocov[ocnt++] = cov[i];
for (i = 0; i < n; i++)
if (U16(sub, 6 + 4 * i + 2))
icov[icnt++] = cov[i];
igrp = ggrp_coverage(icov, icnt);
ogrp = ggrp_coverage(ocov, ocnt);
free(icov);
free(ocov);
for (i = 0; i < n; i++) {
int prev = U16(sub, 6 + 4 * i);
int next = U16(sub, 6 + 4 * i + 2);
if (prev) {
int dx = -uwid(S16(sub, prev + 2));
int dy = -uwid(S16(sub, prev + 4));
if (otf_r2l(feat))
dx += uwid(glyph_wid[cov[i]]);
printf("gpos %s 2 @%d %s:%+d%+d%+d%+d\n",
feat, igrp, glyph_name[cov[i]],
0, 0, dx, dy);
}
if (next) {
int dx = uwid(S16(sub, next + 2)) - uwid(glyph_wid[cov[i]]);
int dy = uwid(S16(sub, next + 4));
if (otf_r2l(feat)) {
dx += uwid(glyph_wid[cov[i]]);
}
printf("gpos %s 2 %s @%d:%+d%+d%+d%+d\n",
feat, glyph_name[cov[i]], ogrp,
0, 0, dx, dy);
}
}
free(cov);
}
/* mark-to-base attachment positioning */
static void otf_gpostype4(struct otf *otf, void *sub, char *feat)
{
int fmt = U16(sub, 0);
int *mcov; /* mark coverage */
int *bcov; /* base coverage */
int cgrp[1024]; /* glyph groups assigned to classes */
int bgrp; /* the group assigned to base glyphs */
int mcnt; /* mark coverage size */
int bcnt; /* base coverage size */
int ccnt; /* class count */
void *marks; /* mark array table */
void *bases; /* base array table */
int i, j;
if (fmt != 1)
return;
mcov = coverage(sub + U16(sub, 2), &mcnt);
bcov = coverage(sub + U16(sub, 4), &bcnt);
ccnt = U16(sub, 6);
marks = sub + U16(sub, 8);
bases = sub + U16(sub, 10);
/* define a group for base glyphs */
bgrp = ggrp_coverage(bcov, bcnt);
/* define a group for each mark class */
for (i = 0; i < ccnt; i++) {
int *grp = malloc(mcnt * sizeof(grp[0]));
int cnt = 0;
for (j = 0; j < mcnt; j++)
if (U16(marks, 2 + 4 * j) == i)
grp[cnt++] = mcov[j];
cgrp[i] = ggrp_coverage(grp, cnt);
free(grp);
}
/* GPOS rules for each mark after base glyphs */
printf("gsec %d\n", sec);
for (i = 0; i < mcnt; i++) {
void *mark = marks + U16(marks, 2 + 4 * i + 2); /* mark anchor */
int dx = -uwid(S16(mark, 2));
int dy = -uwid(S16(mark, 4));
if (otf_r2l(feat)) {
dx += uwid(glyph_wid[mcov[i]]);
dy = -dy;
}
printf("gpos %s 2 @%d %s:%+d%+d%+d%+d\n",
feat, bgrp, glyph_name[mcov[i]], dx, dy, 0, 0);
}
/* GPOS rules for each base glyph before a mark */
printf("gsec %d\n", sec + 1);
for (i = 0; i < bcnt; i++) {
for (j = 0; j < ccnt; j++) {
void *base = bases + U16(bases, 2 + ccnt * 2 * i + 2 * j);
int dx = uwid(S16(base, 2)) - uwid(glyph_wid[bcov[i]]);
int dy = uwid(S16(base, 4));
if (otf_r2l(feat)) {
dx += uwid(glyph_wid[bcov[i]]);
dy = -dy;
}
printf("gpos %s 2 %s @%d:%+d%+d%+d%+d\n",
feat, glyph_name[bcov[i]], cgrp[j], dx, dy, 0, 0);
}
}
free(mcov);
free(bcov);
}
/* mark-to-ligature attachment positioning */
static void otf_gpostype5(struct otf *otf, void *sub, char *feat)
{
int fmt = U16(sub, 0);
int *mcov; /* mark coverage */
int *lcov; /* ligature coverage */
int cgrp[1024]; /* glyph groups assigned to classes */
int lgrp; /* the group assigned to base glyphs */
int mcnt; /* mark coverage size */
int lcnt; /* ligature coverage size */
int ccnt; /* class count */
void *marks; /* mark array table */
void *ligas; /* ligature array table */
int i, j, k;
/* only marks at the end of ligatures are supported */
if (fmt != 1)
return;
mcov = coverage(sub + U16(sub, 2), &mcnt);
lcov = coverage(sub + U16(sub, 4), &lcnt);
ccnt = U16(sub, 6);
marks = sub + U16(sub, 8);
ligas = sub + U16(sub, 10);
/* define a group for ligatures */
lgrp = ggrp_coverage(lcov, lcnt);
/* define a group for each mark class */
for (i = 0; i < ccnt; i++) {
int *grp = malloc(mcnt * sizeof(grp[0]));
int cnt = 0;
for (j = 0; j < mcnt; j++)
if (U16(marks, 2 + 4 * j) == i)
grp[cnt++] = mcov[j];
cgrp[i] = ggrp_coverage(grp, cnt);
free(grp);
}
/* GPOS rules for each mark after a ligature */
printf("gsec %d\n", sec);
for (i = 0; i < mcnt; i++) {
void *mark = marks + U16(marks, 2 + 4 * i + 2); /* mark anchor */
int dx = -uwid(S16(mark, 2));
int dy = -uwid(S16(mark, 4));
if (otf_r2l(feat)) {
dx += uwid(glyph_wid[mcov[i]]);
dy = -dy;
}
printf("gpos %s 2 @%d %s:%+d%+d%+d%+d\n",
feat, lgrp, glyph_name[mcov[i]], dx, dy, 0, 0);
}
printf("gsec %d\n", sec + 1);
/* GPOS rules for each ligature before a mark */
for (i = 0; i < lcnt; i++) {
void *ligattach = ligas + U16(ligas, 2 + 2 * i);
int comcnt = U16(ligattach, 0); /* component count */
/* considering only the last component */
k = comcnt - 1;
if (comcnt == 0)
continue;
if (!U16(ligattach, 2 + 2 * ccnt * k))
continue;
for (j = 0; j < ccnt; j++) {
char *base = ligattach + U16(ligattach, 2 + 2 * ccnt * k + 2 * j);
int dx = uwid(S16(base, 2)) - uwid(glyph_wid[lcov[i]]);
int dy = uwid(S16(base, 4));
if (otf_r2l(feat)) {
dx += uwid(glyph_wid[lcov[i]]);
dy = -dy;
}
printf("gpos %s 2 %s @%d:%+d%+d%+d%+d\n",
feat, glyph_name[lcov[i]], cgrp[j], dx, dy, 0, 0);
}
}
free(mcov);
free(lcov);
}
/* gsub context */
struct gctx {
int bgrp[GCTXLEN]; /* backtrack coverage arrays */
int igrp[GCTXLEN]; /* input coverage arrays */
int lgrp[GCTXLEN]; /* lookahead coverage arrays*/
int bn, in, ln; /* size of b[], i[], l[] */
int seqidx; /* sequence index */
};
static int gctx_len(struct gctx *ctx, int patlen)
{
return ctx ? ctx->bn + ctx->in + ctx->ln - patlen : 0;
}
static void gctx_backtrack(struct gctx *ctx)
{
int i;
if (!ctx)
return;
for (i = 0; i < ctx->bn; i++)
printf(" =@%d", ctx->bgrp[i]);
for (i = 0; i < ctx->seqidx; i++)
printf(" =@%d", ctx->igrp[i]);
}
static void gctx_lookahead(struct gctx *ctx, int patlen)
{
int i;
if (!ctx)
return;
for (i = ctx->seqidx + patlen; i < ctx->in; i++)
printf(" =@%d", ctx->igrp[i]);
for (i = 0; i < ctx->ln; i++)
printf(" =@%d", ctx->lgrp[i]);
}
/* single substitution */
static void otf_gsubtype1(struct otf *otf, void *sub, char *feat, struct gctx *ctx)
{
int *cov;
int fmt = U16(sub, 0);
int ncov;
int i;
cov = coverage(sub + U16(sub, 2), &ncov);
if (fmt == 1) {
for (i = 0; i < ncov; i++) {
int dst = cov[i] + S16(sub, 4);
if (dst >= glyph_n || dst < 0)
continue;
printf("gsub %s %d", feat, 2 + gctx_len(ctx, 1));
gctx_backtrack(ctx);
printf(" -%s +%s", glyph_name[cov[i]], glyph_name[dst]);
gctx_lookahead(ctx, 1);
printf("\n");
}
}
if (fmt == 2) {
int n = U16(sub, 4);
for (i = 0; i < n; i++) {
printf("gsub %s %d", feat, 2 + gctx_len(ctx, 1));
gctx_backtrack(ctx);
printf(" -%s +%s", glyph_name[cov[i]],
glyph_name[U16(sub, 6 + 2 * i)]);
gctx_lookahead(ctx, 1);
printf("\n");
}
}
free(cov);
}
/* alternate substitution */
static void otf_gsubtype3(struct otf *otf, void *sub, char *feat, struct gctx *ctx)
{
int *cov;
int fmt = U16(sub, 0);
int n, i, j;
if (fmt != 1)
return;
cov = coverage(sub + U16(sub, 2), NULL);
n = U16(sub, 4);
for (i = 0; i < n; i++) {
void *alt = sub + U16(sub, 6 + 2 * i);
int nalt = U16(alt, 0);
for (j = 0; j < nalt; j++) {
printf("gsub %s %d", feat, 2 + gctx_len(ctx, 1));
gctx_backtrack(ctx);
printf(" -%s +%s", glyph_name[cov[i]],
glyph_name[U16(alt, 2 + 2 * j)]);
gctx_lookahead(ctx, 1);
printf("\n");
}
}
free(cov);
}
/* ligature substitution */
static void otf_gsubtype4(struct otf *otf, void *sub, char *feat, struct gctx *ctx)
{
int fmt = U16(sub, 0);
int *cov;
int n, i, j, k;
if (fmt != 1)
return;
cov = coverage(sub + U16(sub, 2), NULL);
n = U16(sub, 4);
for (i = 0; i < n; i++) {
void *set = sub + U16(sub, 6 + 2 * i);
int nset = U16(set, 0);
for (j = 0; j < nset; j++) {
void *lig = set + U16(set, 2 + 2 * j);
int nlig = U16(lig, 2);
printf("gsub %s %d", feat, nlig + 1 + gctx_len(ctx, nlig));
gctx_backtrack(ctx);
printf(" -%s", glyph_name[cov[i]]);
for (k = 0; k < nlig - 1; k++)
printf(" -%s", glyph_name[U16(lig, 4 + 2 * k)]);
printf(" +%s", glyph_name[U16(lig, 0)]);
gctx_lookahead(ctx, nlig);
printf("\n");
}
}
free(cov);
}
/* chaining contextual substitution */
static void otf_gsubtype6(struct otf *otf, void *sub, char *feat, void *gsub)
{
struct gctx ctx = {{0}};
void *lookups = gsub + U16(gsub, 8);
int fmt = U16(sub, 0);
int *cov;
int i, j, nsub, ncov;
int off = 2;
if (fmt != 3) {
otf_unsupported("GSUB", 6, fmt);
return;
}
ctx.bn = U16(sub, off);
for (i = 0; i < ctx.bn; i++) {
cov = coverage(sub + U16(sub, off + 2 + 2 * i), &ncov);
ctx.bgrp[i] = ggrp_coverage(cov, ncov);
free(cov);
}
off += 2 + 2 * ctx.bn;
ctx.in = U16(sub, off);
for (i = 0; i < ctx.in; i++) {
cov = coverage(sub + U16(sub, off + 2 + 2 * i), &ncov);
ctx.igrp[i] = ggrp_coverage(cov, ncov);
free(cov);
}
off += 2 + 2 * ctx.in;
ctx.ln = U16(sub, off);
for (i = 0; i < ctx.ln; i ++) {
cov = coverage(sub + U16(sub, off + 2 + 2 * i), &ncov);
ctx.lgrp[i] = ggrp_coverage(cov, ncov);
free(cov);
}
off += 2 + 2 * ctx.ln;
nsub = U16(sub, off); /* nsub > 1 is not supported */
for (i = 0; i < nsub && i < 1; i++) {
int lidx = U16(sub, off + 2 + 4 * i + 2);
void *lookup = lookups + U16(lookups, 2 + 2 * lidx);
int ltype = U16(lookup, 0);
int ntabs = U16(lookup, 4);
ctx.seqidx = U16(sub, off + 2 + 4 * i);
for (j = 0; j < ntabs; j++) {
void *tab = lookup + U16(lookup, 6 + 2 * j);
int type = ltype;
if (type == 7) { /* extension substitution */
type = U16(tab, 2);
tab = tab + U32(tab, 4);
}
if (type == 1)
otf_gsubtype1(otf, tab, feat, &ctx);
if (type == 3)
otf_gsubtype3(otf, tab, feat, &ctx);
if (type == 4)
otf_gsubtype4(otf, tab, feat, &ctx);
}
}
}
/* an otf gsub/gpos lookup */
struct otflookup {
char scrp[8]; /* script name */
char lang[8]; /* language name */
char feat[8]; /* feature name */
int lookup; /* index into the lookup table */
};
/* parse the given gsub/gpos feature table */
static int otf_featrec(struct otf *otf, void *gtab, void *featrec,
char *stag, char *ltag,
struct otflookup *lookups, int lookups_n)
{
void *feats = gtab + U16(gtab, 6);
void *feat = feats + U16(featrec, 4);
char ftag[8] = "";
int n = U16(feat, 2);
int i, j;
memcpy(ftag, featrec, 4);
for (i = 0; i < n; i++) {
int lookup = U16(feat, 4 + 2 * i); /* lookup index */
/* do not store features common to all languages in a script */
for (j = 0; j < lookups_n; j++)
if (lookups[j].lookup == lookup && !lookups[j].lang[0])
if (!strcmp(lookups[j].scrp, stag) &&
!strcmp(lookups[j].feat, ftag))
break;
if (j == lookups_n) {
strcpy(lookups[j].feat, ftag);
strcpy(lookups[j].scrp, stag);
strcpy(lookups[j].lang, ltag);
lookups[j].lookup = U16(feat, 4 + 2 * i);
lookups_n++;
}
}
return lookups_n;
}
/* parse the given language table and its feature tables */
static int otf_lang(struct otf *otf, void *gtab, void *lang, char *stag, char *ltag,
struct otflookup *lookups, int lookups_n)
{
void *feats = gtab + U16(gtab, 6);
int featidx = U16(lang, 2);
int nfeat = U16(lang, 4);
int i;
if (featidx != 0xffff)
lookups_n = otf_featrec(otf, gtab, feats + 2 + 6 * featidx,
stag, ltag, lookups, lookups_n);
for (i = 0; i < nfeat; i++)
lookups_n = otf_featrec(otf, gtab, feats + 2 + 6 * U16(lang, 6 + 2 * i),
stag, ltag, lookups, lookups_n);
return lookups_n;
}
/* return lookup table tag (i.e. liga:latn:ENG); returns a static buffer */
static char *lookuptag(struct otflookup *lu)
{
static char tag[16];
sprintf(tag, "%s:%s", lu->feat, lu->scrp[0] ? lu->scrp : "DFLT");
if (lu->lang[0])
sprintf(strchr(tag, '\0'), ":%s", lu->lang);
return tag;
}
static int lookupcmp(void *v1, void *v2)
{
struct otflookup *l1 = v1;
struct otflookup *l2 = v2;
if (strcmp(l1->scrp, l2->scrp))
return strcmp(l1->scrp, l2->scrp);
if (mkfn_featrank(l1->scrp, l1->feat) != mkfn_featrank(l1->scrp, l2->feat))
return mkfn_featrank(l1->scrp, l1->feat) - mkfn_featrank(l1->scrp, l2->feat);
return l1->lookup - l2->lookup;
}
/* extract lookup tables for all features of the given gsub/gpos table */
static int otf_gtab(struct otf *otf, void *gpos, struct otflookup *lookups)
{
void *scripts = gpos + U16(gpos, 4);
int nscripts, nlangs;
void *script;
char stag[8], ltag[8]; /* script and language tags */
int i, j;
int n = 0;
nscripts = U16(scripts, 0);
for (i = 0; i < nscripts; i++) {
void *grec = scripts + 2 + 6 * i;
memcpy(stag, grec, 4);
stag[4] = '\0';
if (!mkfn_script(stag, nscripts))
continue;
script = scripts + U16(grec, 4);
nlangs = U16(script, 2);
if (U16(script, 0) && mkfn_lang(NULL, nlangs + (U16(script, 0) != 0)))
n = otf_lang(otf, gpos, script + U16(script, 0),
stag, "", lookups, n);
for (j = 0; j < nlangs; j++) {
void *lrec = script + 4 + 6 * j;
memcpy(ltag, lrec, 4);
ltag[4] = '\0';
if (mkfn_lang(ltag, nlangs + (U16(script, 0) != 0)))
n = otf_lang(otf, gpos, script + U16(lrec, 4),
stag, ltag, lookups, n);
}
}
qsort(lookups, n, sizeof(lookups[0]), (void *) lookupcmp);
return n;
}
static void otf_gpos(struct otf *otf, void *gpos)
{
struct otflookup lookups[NLOOKUPS];
void *lookuplist = gpos + U16(gpos, 8);
int nlookups = otf_gtab(otf, gpos, lookups);
int i, j;
if (mkfn_dry)
return;
for (i = 0; i < nlookups; i++) {
void *lookup = lookuplist + U16(lookuplist, 2 + 2 * lookups[i].lookup);
int ltype = U16(lookup, 0);
int ntabs = U16(lookup, 4);
char *tag = lookuptag(&lookups[i]);
sec = (i + 1) * 10;
printf("gsec %d %s\n", sec, tag);
for (j = 0; j < ntabs; j++) {
void *tab = lookup + U16(lookup, 6 + 2 * j);
int type = ltype;
if (type == 9) { /* extension positioning */
type = U16(tab, 2);
tab = tab + U32(tab, 4);
}
switch (type) {
case 1:
otf_gpostype1(otf, tab, tag);
break;
case 2:
otf_gpostype2(otf, tab, tag);
break;
case 3:
otf_gpostype3(otf, tab, tag);
break;
case 4:
otf_gpostype4(otf, tab, tag);
break;
case 5:
otf_gpostype5(otf, tab, tag);
break;
default:
otf_unsupported("GPOS", type, 0);
}
}
}
}
static void otf_gsub(struct otf *otf, void *gsub)
{
struct otflookup lookups[NLOOKUPS];
void *lookuplist = gsub + U16(gsub, 8);
int nlookups = otf_gtab(otf, gsub, lookups);
int i, j;
if (mkfn_dry)
return;
for (i = 0; i < nlookups; i++) {
void *lookup = lookuplist + U16(lookuplist, 2 + 2 * lookups[i].lookup);
int ltype = U16(lookup, 0);
int ntabs = U16(lookup, 4);
char *tag = lookuptag(&lookups[i]);
sec = (i + 1) * 10;
printf("gsec %d %s\n", sec, tag);