summaryrefslogtreecommitdiff
path: root/Build/source/texk/ttfdump/libttf/gsub.c
blob: b0ca3ee5b54ce08f2ebe4023154702072a822378 (plain)
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
/* gsub.c -- Glyph Substitution Table
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include "ttf.h"
#include "ttfutil.h"

static Sub11Ptr makeGSUB11 (FILE *fp, ULONG offset)
{
    USHORT cOffset;
    Sub11Ptr sub = XCALLOC1 (Sub11);

    cOffset = ttfGetUSHORT (fp);
    sub->deltaGlyphID = ttfGetUSHORT (fp);
    sub->coverage = otfMakeCoverage (fp, offset + cOffset);

    return sub;
}

static void printGSUB11 (FILE *fp, Sub11Ptr sub)
{
    fprintf (fp, " - Single Substitution Delta\n\t  ");
    otfPrintCoverage (fp, sub->coverage);
    fprintf (fp, "\t  deltaGlyphID: %d\n", sub->deltaGlyphID);
}

static void freeGSUB11 (Sub11Ptr sub)
{
    otfFreeCoverage (sub->coverage);
}

static Sub12Ptr makeGSUB12 (FILE *fp, ULONG offset)
{
    USHORT cOffset;
    Sub12Ptr sub = XCALLOC1 (Sub12);

    cOffset = ttfGetUSHORT (fp);
    sub->glyphCount = ttfGetUSHORT (fp);
    sub->substitute = ttfMakeUSHORT (sub->glyphCount, fp);
    sub->coverage = otfMakeCoverage (fp, offset + cOffset);

    return sub;
}

static void printGSUB12 (FILE *fp, Sub12Ptr sub)
{
    int i;

    fprintf (fp, " - Single Substitution List\n\t  ");
    otfPrintCoverage (fp, sub->coverage);
    fprintf (fp, "\t  glyphCount: %d\n\t\t  substitute: %d",
                 sub->glyphCount, sub->substitute[0]);
    for (i = 1; i < sub->glyphCount; i++)
        fprintf (fp, i % 8 ? ", %d" : ",\n\t\t\t      %d", sub->substitute[i]);
    fprintf (fp, "\n");
}

static void freeGSUB12 (Sub12Ptr sub)
{
    otfFreeCoverage (sub->coverage);
    free (sub->substitute);
}

static void gsubLoadSequence (SequencePtr sequence, FILE *fp, ULONG offset)
{
    xfseek (fp, offset, SEEK_SET, "gsubLoadSequence");

    sequence->glyphCount = ttfGetUSHORT (fp);
    sequence->substitute = ttfMakeUSHORT (sequence->glyphCount, fp);
}

static Sub21Ptr makeGSUB21 (FILE *fp, ULONG offset)
{
    int i;
    USHORT cOffset;
    USHORT *sOffset;
    Sub21Ptr sub = XCALLOC1 (Sub21);

    cOffset = ttfGetUSHORT (fp);
    sub->sequenceCount = ttfGetUSHORT (fp);
    sOffset = ttfMakeUSHORT (sub->sequenceCount, fp);
    sub->coverage = otfMakeCoverage (fp, offset + cOffset);
    sub->sequence = XCALLOC (sub->sequenceCount, Sequence);
    for (i = 0; i < sub->sequenceCount; i++)
        gsubLoadSequence (&sub->sequence[i], fp, offset + sOffset[i]);
    free (sOffset);

    return sub;
}

static void printGSUB21 (FILE *fp, Sub21Ptr sub)
{
    int i;

    fprintf (fp, " - Multiple Substitution\n\t  ");
    otfPrintCoverage (fp, sub->coverage);
    fprintf (fp, "\t  sequenceCount: %d\n", sub->sequenceCount);
    for (i = 0; i < sub->sequenceCount; i++) {
        int j;

        fprintf (fp, "\t  %2d. glyphCount: %d - ", i, sub->sequence[i].glyphCount);
        for (j = 0; j < sub->sequence[i].glyphCount; j++)
            fprintf (fp, j == 0 ? "- %d" : ", %d", sub->sequence[i].substitute[j]);
        fprintf (fp, "\n");
    }
}

static void freeGSUB21 (Sub21Ptr sub)
{
    int i;

    otfFreeCoverage (sub->coverage);
    for (i = 0; i < sub->sequenceCount; i++)
        free (sub->sequence[i].substitute);
    free (sub->sequence);
}

static void gsubLoadAlternateSet (AlternateSetPtr alternateSet, FILE *fp, ULONG offset)
{
    xfseek (fp, offset, SEEK_SET, "gsubLoadAlternateSet");

    alternateSet->glyphCount = ttfGetUSHORT (fp);
    alternateSet->alternate = ttfMakeUSHORT (alternateSet->glyphCount, fp);
}

static Sub31Ptr makeGSUB31 (FILE *fp, ULONG offset)
{
    int i;
    USHORT cOffset;
    USHORT *aOffset;
    Sub31Ptr sub = XCALLOC1 (Sub31);

    cOffset = ttfGetUSHORT (fp);
    sub->alternateSetCount = ttfGetUSHORT (fp);
    aOffset = ttfMakeUSHORT (sub->alternateSetCount, fp);
    sub->coverage = otfMakeCoverage (fp, offset + cOffset);
    sub->alternateSet = XCALLOC (sub->alternateSetCount, AlternateSet);
    for (i = 0; i < sub->alternateSetCount; i++)
        gsubLoadAlternateSet (&sub->alternateSet[i], fp, offset + aOffset[i]);
    free (aOffset);

    return sub;
}

static void printGSUB31 (FILE *fp, Sub31Ptr sub)
{
    int i;

    fprintf (fp, " - Alternate Substitution\n\t  ");
    otfPrintCoverage (fp, sub->coverage);
    fprintf (fp, "\t  alternateSetCount: %d\n", sub->alternateSetCount);
    for (i = 0; i < sub->alternateSetCount; i++) {
        int j;

        fprintf (fp, "\t  %2d. glyphCount: %d ", i, sub->alternateSet[i].glyphCount);
        for (j = 0; j < sub->alternateSet[i].glyphCount; j++)
            fprintf (fp, j == 0 ? "- %d" : ", %d", sub->alternateSet[i].alternate[j]);
        fprintf (fp, "\n");
    }
}

static void freeGSUB31 (Sub31Ptr sub)
{
    int i;

    otfFreeCoverage (sub->coverage);
    for (i = 0; i < sub->alternateSetCount; i++)
        free (sub->alternateSet[i].alternate);
    free (sub->alternateSet);
}

static void gsubLoadLigature (LigaturePtr ligature, FILE *fp, ULONG offset)
{
    xfseek (fp, offset, SEEK_SET, "gsubLoadLigature");

    ligature->ligGlyph = ttfGetUSHORT (fp);
    ligature->compCount = ttfGetUSHORT (fp);
    ligature->component = ttfMakeUSHORT (ligature->compCount - 1, fp);
}

static void gsubLoadLigatureSet (LigatureSetPtr ligatureSet, FILE *fp, ULONG offset)
{
    int i;
    USHORT *lOffset;

    xfseek (fp, offset, SEEK_SET, "gsubLoadLigatureSet");

    ligatureSet->ligatureCount = ttfGetUSHORT (fp);
    lOffset = ttfMakeUSHORT (ligatureSet->ligatureCount, fp);
    ligatureSet->ligature = XCALLOC (ligatureSet->ligatureCount, Ligature);
    for (i = 0; i < ligatureSet->ligatureCount; i++)
        gsubLoadLigature (&ligatureSet->ligature[i], fp, offset + lOffset[i]);
    free (lOffset);
}

static Sub41Ptr makeGSUB41 (FILE *fp, ULONG offset)
{
    int i;
    USHORT cOffset;
    USHORT *lOffset;
    Sub41Ptr sub = XCALLOC1 (Sub41);

    cOffset = ttfGetUSHORT (fp);
    sub->ligSetCount = ttfGetUSHORT (fp);
    lOffset = ttfMakeUSHORT (sub->ligSetCount, fp);
    sub->coverage = otfMakeCoverage (fp, offset + cOffset);
    sub->ligatureSet = XCALLOC (sub->ligSetCount, LigatureSet);
    for (i = 0; i < sub->ligSetCount; i++)
        gsubLoadLigatureSet (&sub->ligatureSet[i], fp, offset + lOffset[i]);
    free (lOffset);

    return sub;
}

static void printGSUB41 (FILE *fp, Sub41Ptr sub)
{
    int i;

    fprintf (fp, " - Ligature Substitution\n\t  ");
    otfPrintCoverage (fp, sub->coverage);
    fprintf (fp, "\t  ligSetCount: %d\n", sub->ligSetCount);
    for (i = 0; i < sub->ligSetCount; i++) {
        int j;

        fprintf (fp, "\t  %2d. ligatureCount: %d\n", i, sub->ligatureSet[i].ligatureCount);
        for (j = 0; j < sub->ligatureSet[i].ligatureCount; j++) {
            int k;

            fprintf (fp, "\t      %2d. ligGlyph: %d, compCount: %d ", j,
                         sub->ligatureSet[i].ligature[j].ligGlyph,
                         sub->ligatureSet[i].ligature[j].compCount);
            for (k = 0; k < sub->ligatureSet[i].ligature[j].compCount - 1; k++)
                fprintf (fp, k == 0 ? "- %d" : ", %d", sub->ligatureSet[i].ligature[j].component[k]);
            fprintf (fp, "\n");
        }
    }
}

static void freeGSUB41 (Sub41Ptr sub)
{
    int i;

    otfFreeCoverage (sub->coverage);
    for (i = 0; i < sub->ligSetCount; i++) {
        int j;

        for (j = 0; j < sub->ligatureSet[i].ligatureCount; j++)
            free (sub->ligatureSet[i].ligature[j].component);
        free (sub->ligatureSet[i].ligature);
    }
    free (sub->ligatureSet);
}

#define makeGSUB51 makeOTFCtx1
#define printGSUB51 printOTFCtx1
#define freeGSUB51 freeOTFCtx1

#define makeGSUB52 makeOTFCtx2
#define printGSUB52 printOTFCtx2
#define freeGSUB52 freeOTFCtx2

#define makeGSUB53 makeOTFCtx3
#define printGSUB53 printOTFCtx3
#define freeGSUB53 freeOTFCtx3

#define makeGSUB61 makeOTFChn1
#define printGSUB61 printOTFChn1
#define freeGSUB61 freeOTFChn1

#define makeGSUB62 makeOTFChn2
#define printGSUB62 printOTFChn2
#define freeGSUB62 freeOTFChn2

#define makeGSUB63 makeOTFChn3
#define printGSUB63 printOTFChn3
#define freeGSUB63 freeOTFChn3

static Sub81Ptr makeGSUB81 (FILE *fp, ULONG offset)
{
    int i;
    USHORT cOffset;
    USHORT *bOffset, *lOffset;
    Sub81Ptr sub = XCALLOC1 (Sub81);

    cOffset = ttfGetUSHORT (fp);
    sub->backtrackGlyphCount = ttfGetUSHORT (fp);
    bOffset = ttfMakeUSHORT (sub->backtrackGlyphCount, fp);
    sub->lookaheadGlyphCount = ttfGetUSHORT (fp);
    lOffset = ttfMakeUSHORT (sub->lookaheadGlyphCount, fp);
    sub->glyphCount = ttfGetUSHORT (fp);
    sub->substitute = ttfMakeUSHORT (sub->glyphCount, fp);
    sub->coverage = otfMakeCoverage (fp, offset + cOffset);
    sub->backtrack = XCALLOC (sub->backtrackGlyphCount, CoveragePtr);
    for (i = 0; i < sub->backtrackGlyphCount; i++)
        sub->backtrack[i] = otfMakeCoverage (fp, offset + bOffset[i]);
    free (bOffset);
    sub->lookahead = XCALLOC (sub->lookaheadGlyphCount, CoveragePtr);
    for (i = 0; i < sub->lookaheadGlyphCount; i++)
        sub->lookahead[i] = otfMakeCoverage (fp, offset + lOffset[i]);
    free (lOffset);

    return sub;
}

static void printGSUB81 (FILE *fp, Sub81Ptr sub)
{
    int i;

    fprintf (fp, " - Reverse Chaining Context Single Substitution\n\t  ");
    otfPrintCoverage (fp, sub->coverage);
    fprintf (fp, "\t  backtrackGlyphCount: %d\n", sub->backtrackGlyphCount);
    for (i = 0; i < sub->backtrackGlyphCount; i++) {
        fprintf (fp, "\t  %2d. backtrack", i);
        otfPrintCoverage (fp, sub->backtrack[i]);
    }
    fprintf (fp, "\t  lookaheadGlyphCount: %d\n", sub->lookaheadGlyphCount);
    for (i = 0; i < sub->lookaheadGlyphCount; i++) {
        fprintf (fp, "\t  %2d. lookahead", i);
        otfPrintCoverage (fp, sub->lookahead[i]);
    }
    fprintf (fp, "\t  glyphCount: %d\n\t  substitute - %d",
                 sub->glyphCount, sub->substitute[0]);
    for (i = 1; i < sub->glyphCount; i++)
        fprintf (fp, i % 8 ? ",\n\t\t       %d" : ", %d", sub->substitute[i]);
}

static void freeGSUB81 (Sub81Ptr sub)
{
    int i;

    otfFreeCoverage (sub->coverage);
    for (i = 0; i < sub->backtrackGlyphCount; i++)
        otfFreeCoverage (sub->backtrack[i]);
    free (sub->backtrack);
    for (i = 0; i < sub->lookaheadGlyphCount; i++)
        otfFreeCoverage (sub->lookahead[i]);
    free (sub->lookahead);
    free (sub->substitute);
}

static LookupPtr makeGSUBLookup (FILE *fp, USHORT lookupType, ULONG offset)
{
    LookupPtr lookup;
    USHORT lookupFormat;

again:
    if (lookupType == 0 || lookupType > SubLookup_Max)
        ttfError ("Unrecognized GSUB lookupType\n");

    xfseek (fp, offset, SEEK_SET, "makeGSUBLookup");

    lookupFormat = ttfGetUSHORT (fp);
    if (lookupFormat & 0xfff0)
        lookupFormat = 0x000f;

    switch (lookupType << 4 | lookupFormat)
        {
        case 0x011:
            lookup.sub.sub11 = makeGSUB11 (fp, offset);
            break;
        case 0x012:
            lookup.sub.sub12 = makeGSUB12 (fp, offset);
            break;
        case 0x021:
            lookup.sub.sub21 = makeGSUB21 (fp, offset);
            break;
        case 0x031:
            lookup.sub.sub31 = makeGSUB31 (fp, offset);
            break;
        case 0x041:
            lookup.sub.sub41 = makeGSUB41 (fp, offset);
            break;
        case 0x051:
            lookup.sub.sub51 = makeGSUB51 (fp, offset);
            break;
        case 0x052:
            lookup.sub.sub52 = makeGSUB52 (fp, offset);
            break;
        case 0x053:
            lookup.sub.sub53 = makeGSUB53 (fp, offset);
            break;
        case 0x061:
            lookup.sub.sub61 = makeGSUB61 (fp, offset);
            break;
        case 0x062:
            lookup.sub.sub62 = makeGSUB62 (fp, offset);
            break;
        case 0x063:
            lookup.sub.sub63 = makeGSUB63 (fp, offset);
            break;
        case 0x081:
            lookup.sub.sub81 = makeGSUB81 (fp, offset);
            break;
        case 0x071:
            lookupType = ttfGetUSHORT (fp);
            if (lookupType == 7)
                ttfError ("Invalid GSUB extensionLookupType\n");
            offset += ttfGetULONG (fp);
            goto again;
        default:
            ttfError ("Unrecognized GSUB lookupFormat\n");
        }

    lookup.otf->lookupType = lookupType;
    lookup.otf->lookupFormat = lookupFormat;

    return lookup;
}

static void printGSUBLookup (FILE *fp, LookupPtr lookup)
{
    switch (lookup.otf->lookupType << 4 | lookup.otf->lookupFormat)
        {
        case 0x011:
            printGSUB11 (fp, lookup.sub.sub11);
            break;
        case 0x012:
            printGSUB12 (fp, lookup.sub.sub12);
            break;
        case 0x021:
            printGSUB21 (fp, lookup.sub.sub21);
            break;
        case 0x031:
            printGSUB31 (fp, lookup.sub.sub31);
            break;
        case 0x041:
            printGSUB41 (fp, lookup.sub.sub41);
            break;
        case 0x051:
            printGSUB51 (fp, lookup.sub.sub51);
            break;
        case 0x052:
            printGSUB52 (fp, lookup.sub.sub52);
            break;
        case 0x053:
            printGSUB53 (fp, lookup.sub.sub53);
            break;
        case 0x061:
            printGSUB61 (fp, lookup.sub.sub61);
            break;
        case 0x062:
            printGSUB62 (fp, lookup.sub.sub62);
            break;
        case 0x063:
            printGSUB63 (fp, lookup.sub.sub63);
            break;
        case 0x081:
            printGSUB81 (fp, lookup.sub.sub81);
            break;
        default:
            ttfError ("Internal error: printGSUBLookup\n");
        }
}

static void freeGSUBLookup (LookupPtr lookup)
{
    switch (lookup.otf->lookupType << 4 | lookup.otf->lookupFormat)
        {
        case 0x011:
            freeGSUB11 (lookup.sub.sub11);
            break;
        case 0x012:
            freeGSUB12 (lookup.sub.sub12);
            break;
        case 0x021:
            freeGSUB21 (lookup.sub.sub21);
            break;
        case 0x031:
            freeGSUB31 (lookup.sub.sub31);
            break;
        case 0x041:
            freeGSUB41 (lookup.sub.sub41);
            break;
        case 0x051:
            freeGSUB51 (lookup.sub.sub51);
            break;
        case 0x052:
            freeGSUB52 (lookup.sub.sub52);
            break;
        case 0x053:
            freeGSUB53 (lookup.sub.sub53);
            break;
        case 0x061:
            freeGSUB61 (lookup.sub.sub61);
            break;
        case 0x062:
            freeGSUB62 (lookup.sub.sub62);
            break;
        case 0x063:
            freeGSUB63 (lookup.sub.sub63);
            break;
        case 0x081:
            freeGSUB81 (lookup.sub.sub81);
            break;
        default:
            ttfError ("Internal error: freeGSUBLookup\n");
        }
}

static void ttfLoadGSUB (FILE *fp, GSUBPtr gsub, ULONG offset)
{
    USHORT sOffset, fOffset, lOffset;

    xfseek (fp, offset, SEEK_SET, "ttfLoadGSUB");

    gsub->version = ttfGetFixed (fp);
    sOffset = ttfGetUSHORT (fp);
    fOffset = ttfGetUSHORT (fp);
    lOffset = ttfGetUSHORT (fp);

    gsub->scriptList = otfMakeScriptList (fp, offset + sOffset);
    gsub->featureList = otfMakeFeatureList (fp, offset + fOffset);
    gsub->lookupList = otfMakeLookupList (fp, offset + lOffset, &makeGSUBLookup);
}

void ttfInitGSUB (TTFontPtr font)
{
    ULONG tag = FT_MAKE_TAG ('G', 'S', 'U', 'B');
    TableDirPtr ptd;

    if ((ptd = ttfLookUpTableDir (tag, font)) != NULL)
        {
            font->gsub = XCALLOC1 (GSUB);
            ttfLoadGSUB (font->fp, font->gsub, ptd->offset);
        }
}

void ttfPrintGSUB (FILE *fp, GSUBPtr gsub)
{
    int b[2];

    FixedSplit (gsub->version, b);

    fprintf (fp, "'GSUB' Table - Glyph Substitution Data\n");
    fprintf (fp, "--------------------------------------\n");
    fprintf (fp, "\t 'GSUB' Version:\t %d.%d\n",b[1],b[0]);

    otfPrintScriptList (fp, gsub->scriptList);
    otfPrintFeatureList (fp, gsub->featureList);
    otfPrintLookupList (fp, gsub->lookupList, &printGSUBLookup);
}

void ttfFreeGSUB (GSUBPtr gsub)
{
    if (gsub != NULL)
        {
            otfFreeScriptList (gsub->scriptList);
            otfFreeFeatureList (gsub->featureList);
            otfFreeLookupList (gsub->lookupList, &freeGSUBLookup);
            free (gsub);
        }
}