Debian Codegraph @

texk/ttfdump/libttf/gsub.c

raw
1 /* gsub.c -- Glyph Substitution Table
2 */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "ttf.h"
10 #include "ttfutil.h"
11
12 static Sub11Ptr makeGSUB11 (FILE *fp, ULONG offset)
13 {
14 USHORT cOffset;
15 Sub11Ptr sub = XCALLOC1 (Sub11);
16
17 cOffset = ttfGetUSHORT (fp);
18 sub->deltaGlyphID = ttfGetUSHORT (fp);
19 sub->coverage = otfMakeCoverage (fp, offset + cOffset);
20
21 return sub;
22 }
23
24 static void printGSUB11 (FILE *fp, Sub11Ptr sub)
25 {
26 fprintf (fp, " - Single Substitution Delta\n\t ");
27 otfPrintCoverage (fp, sub->coverage);
28 fprintf (fp, "\t deltaGlyphID: %d\n", sub->deltaGlyphID);
29 }
30
31 static void freeGSUB11 (Sub11Ptr sub)
32 {
33 otfFreeCoverage (sub->coverage);
34 }
35
36 static Sub12Ptr makeGSUB12 (FILE *fp, ULONG offset)
37 {
38 USHORT cOffset;
39 Sub12Ptr sub = XCALLOC1 (Sub12);
40
41 cOffset = ttfGetUSHORT (fp);
42 sub->glyphCount = ttfGetUSHORT (fp);
43 sub->substitute = ttfMakeUSHORT (sub->glyphCount, fp);
44 sub->coverage = otfMakeCoverage (fp, offset + cOffset);
45
46 return sub;
47 }
48
49 static void printGSUB12 (FILE *fp, Sub12Ptr sub)
50 {
51 int i;
52
53 fprintf (fp, " - Single Substitution List\n\t ");
54 otfPrintCoverage (fp, sub->coverage);
55 fprintf (fp, "\t glyphCount: %d\n\t\t substitute: %d",
56 sub->glyphCount, sub->substitute[0]);
57 for (i = 1; i < sub->glyphCount; i++)
58 fprintf (fp, i % 8 ? ", %d" : ",\n\t\t\t %d", sub->substitute[i]);
59 fprintf (fp, "\n");
60 }
61
62 static void freeGSUB12 (Sub12Ptr sub)
63 {
64 otfFreeCoverage (sub->coverage);
65 free (sub->substitute);
66 }
67
68 static void gsubLoadSequence (SequencePtr sequence, FILE *fp, ULONG offset)
69 {
70 xfseek (fp, offset, SEEK_SET, "gsubLoadSequence");
71
72 sequence->glyphCount = ttfGetUSHORT (fp);
73 sequence->substitute = ttfMakeUSHORT (sequence->glyphCount, fp);
74 }
75
76 static Sub21Ptr makeGSUB21 (FILE *fp, ULONG offset)
77 {
78 int i;
79 USHORT cOffset;
80 USHORT *sOffset;
81 Sub21Ptr sub = XCALLOC1 (Sub21);
82
83 cOffset = ttfGetUSHORT (fp);
84 sub->sequenceCount = ttfGetUSHORT (fp);
85 sOffset = ttfMakeUSHORT (sub->sequenceCount, fp);
86 sub->coverage = otfMakeCoverage (fp, offset + cOffset);
87 sub->sequence = XCALLOC (sub->sequenceCount, Sequence);
88 for (i = 0; i < sub->sequenceCount; i++)
89 gsubLoadSequence (&sub->sequence[i], fp, offset + sOffset[i]);
90 free (sOffset);
91
92 return sub;
93 }
94
95 static void printGSUB21 (FILE *fp, Sub21Ptr sub)
96 {
97 int i;
98
99 fprintf (fp, " - Multiple Substitution\n\t ");
100 otfPrintCoverage (fp, sub->coverage);
101 fprintf (fp, "\t sequenceCount: %d\n", sub->sequenceCount);
102 for (i = 0; i < sub->sequenceCount; i++) {
103 int j;
104
105 fprintf (fp, "\t %2d. glyphCount: %d - ", i, sub->sequence[i].glyphCount);
106 for (j = 0; j < sub->sequence[i].glyphCount; j++)
107 fprintf (fp, j == 0 ? "- %d" : ", %d", sub->sequence[i].substitute[j]);
108 fprintf (fp, "\n");
109 }
110 }
111
112 static void freeGSUB21 (Sub21Ptr sub)
113 {
114 int i;
115
116 otfFreeCoverage (sub->coverage);
117 for (i = 0; i < sub->sequenceCount; i++)
118 free (sub->sequence[i].substitute);
119 free (sub->sequence);
120 }
121
122 static void gsubLoadAlternateSet (AlternateSetPtr alternateSet, FILE *fp, ULONG offset)
123 {
124 xfseek (fp, offset, SEEK_SET, "gsubLoadAlternateSet");
125
126 alternateSet->glyphCount = ttfGetUSHORT (fp);
127 alternateSet->alternate = ttfMakeUSHORT (alternateSet->glyphCount, fp);
128 }
129
130 static Sub31Ptr makeGSUB31 (FILE *fp, ULONG offset)
131 {
132 int i;
133 USHORT cOffset;
134 USHORT *aOffset;
135 Sub31Ptr sub = XCALLOC1 (Sub31);
136
137 cOffset = ttfGetUSHORT (fp);
138 sub->alternateSetCount = ttfGetUSHORT (fp);
139 aOffset = ttfMakeUSHORT (sub->alternateSetCount, fp);
140 sub->coverage = otfMakeCoverage (fp, offset + cOffset);
141 sub->alternateSet = XCALLOC (sub->alternateSetCount, AlternateSet);
142 for (i = 0; i < sub->alternateSetCount; i++)
143 gsubLoadAlternateSet (&sub->alternateSet[i], fp, offset + aOffset[i]);
144 free (aOffset);
145
146 return sub;
147 }
148
149 static void printGSUB31 (FILE *fp, Sub31Ptr sub)
150 {
151 int i;
152
153 fprintf (fp, " - Alternate Substitution\n\t ");
154 otfPrintCoverage (fp, sub->coverage);
155 fprintf (fp, "\t alternateSetCount: %d\n", sub->alternateSetCount);
156 for (i = 0; i < sub->alternateSetCount; i++) {
157 int j;
158
159 fprintf (fp, "\t %2d. glyphCount: %d ", i, sub->alternateSet[i].glyphCount);
160 for (j = 0; j < sub->alternateSet[i].glyphCount; j++)
161 fprintf (fp, j == 0 ? "- %d" : ", %d", sub->alternateSet[i].alternate[j]);
162 fprintf (fp, "\n");
163 }
164 }
165
166 static void freeGSUB31 (Sub31Ptr sub)
167 {
168 int i;
169
170 otfFreeCoverage (sub->coverage);
171 for (i = 0; i < sub->alternateSetCount; i++)
172 free (sub->alternateSet[i].alternate);
173 free (sub->alternateSet);
174 }
175
176 static void gsubLoadLigature (LigaturePtr ligature, FILE *fp, ULONG offset)
177 {
178 xfseek (fp, offset, SEEK_SET, "gsubLoadLigature");
179
180 ligature->ligGlyph = ttfGetUSHORT (fp);
181 ligature->compCount = ttfGetUSHORT (fp);
182 ligature->component = ttfMakeUSHORT (ligature->compCount - 1, fp);
183 }
184
185 static void gsubLoadLigatureSet (LigatureSetPtr ligatureSet, FILE *fp, ULONG offset)
186 {
187 int i;
188 USHORT *lOffset;
189
190 xfseek (fp, offset, SEEK_SET, "gsubLoadLigatureSet");
191
192 ligatureSet->ligatureCount = ttfGetUSHORT (fp);
193 lOffset = ttfMakeUSHORT (ligatureSet->ligatureCount, fp);
194 ligatureSet->ligature = XCALLOC (ligatureSet->ligatureCount, Ligature);
195 for (i = 0; i < ligatureSet->ligatureCount; i++)
196 gsubLoadLigature (&ligatureSet->ligature[i], fp, offset + lOffset[i]);
197 free (lOffset);
198 }
199
200 static Sub41Ptr makeGSUB41 (FILE *fp, ULONG offset)
201 {
202 int i;
203 USHORT cOffset;
204 USHORT *lOffset;
205 Sub41Ptr sub = XCALLOC1 (Sub41);
206
207 cOffset = ttfGetUSHORT (fp);
208 sub->ligSetCount = ttfGetUSHORT (fp);
209 lOffset = ttfMakeUSHORT (sub->ligSetCount, fp);
210 sub->coverage = otfMakeCoverage (fp, offset + cOffset);
211 sub->ligatureSet = XCALLOC (sub->ligSetCount, LigatureSet);
212 for (i = 0; i < sub->ligSetCount; i++)
213 gsubLoadLigatureSet (&sub->ligatureSet[i], fp, offset + lOffset[i]);
214 free (lOffset);
215
216 return sub;
217 }
218
219 static void printGSUB41 (FILE *fp, Sub41Ptr sub)
220 {
221 int i;
222
223 fprintf (fp, " - Ligature Substitution\n\t ");
224 otfPrintCoverage (fp, sub->coverage);
225 fprintf (fp, "\t ligSetCount: %d\n", sub->ligSetCount);
226 for (i = 0; i < sub->ligSetCount; i++) {
227 int j;
228
229 fprintf (fp, "\t %2d. ligatureCount: %d\n", i, sub->ligatureSet[i].ligatureCount);
230 for (j = 0; j < sub->ligatureSet[i].ligatureCount; j++) {
231 int k;
232
233 fprintf (fp, "\t %2d. ligGlyph: %d, compCount: %d ", j,
234 sub->ligatureSet[i].ligature[j].ligGlyph,
235 sub->ligatureSet[i].ligature[j].compCount);
236 for (k = 0; k < sub->ligatureSet[i].ligature[j].compCount - 1; k++)
237 fprintf (fp, k == 0 ? "- %d" : ", %d", sub->ligatureSet[i].ligature[j].component[k]);
238 fprintf (fp, "\n");
239 }
240 }
241 }
242
243 static void freeGSUB41 (Sub41Ptr sub)
244 {
245 int i;
246
247 otfFreeCoverage (sub->coverage);
248 for (i = 0; i < sub->ligSetCount; i++) {
249 int j;
250
251 for (j = 0; j < sub->ligatureSet[i].ligatureCount; j++)
252 free (sub->ligatureSet[i].ligature[j].component);
253 free (sub->ligatureSet[i].ligature);
254 }
255 free (sub->ligatureSet);
256 }
257
258 #define makeGSUB51 makeOTFCtx1
259 #define printGSUB51 printOTFCtx1
260 #define freeGSUB51 freeOTFCtx1
261
262 #define makeGSUB52 makeOTFCtx2
263 #define printGSUB52 printOTFCtx2
264 #define freeGSUB52 freeOTFCtx2
265
266 #define makeGSUB53 makeOTFCtx3
267 #define printGSUB53 printOTFCtx3
268 #define freeGSUB53 freeOTFCtx3
269
270 #define makeGSUB61 makeOTFChn1
271 #define printGSUB61 printOTFChn1
272 #define freeGSUB61 freeOTFChn1
273
274 #define makeGSUB62 makeOTFChn2
275 #define printGSUB62 printOTFChn2
276 #define freeGSUB62 freeOTFChn2
277
278 #define makeGSUB63 makeOTFChn3
279 #define printGSUB63 printOTFChn3
280 #define freeGSUB63 freeOTFChn3
281
282 static Sub81Ptr makeGSUB81 (FILE *fp, ULONG offset)
283 {
284 int i;
285 USHORT cOffset;
286 USHORT *bOffset, *lOffset;
287 Sub81Ptr sub = XCALLOC1 (Sub81);
288
289 cOffset = ttfGetUSHORT (fp);
290 sub->backtrackGlyphCount = ttfGetUSHORT (fp);
291 bOffset = ttfMakeUSHORT (sub->backtrackGlyphCount, fp);
292 sub->lookaheadGlyphCount = ttfGetUSHORT (fp);
293 lOffset = ttfMakeUSHORT (sub->lookaheadGlyphCount, fp);
294 sub->glyphCount = ttfGetUSHORT (fp);
295 sub->substitute = ttfMakeUSHORT (sub->glyphCount, fp);
296 sub->coverage = otfMakeCoverage (fp, offset + cOffset);
297 sub->backtrack = XCALLOC (sub->backtrackGlyphCount, CoveragePtr);
298 for (i = 0; i < sub->backtrackGlyphCount; i++)
299 sub->backtrack[i] = otfMakeCoverage (fp, offset + bOffset[i]);
300 free (bOffset);
301 sub->lookahead = XCALLOC (sub->lookaheadGlyphCount, CoveragePtr);
302 for (i = 0; i < sub->lookaheadGlyphCount; i++)
303 sub->lookahead[i] = otfMakeCoverage (fp, offset + lOffset[i]);
304 free (lOffset);
305
306 return sub;
307 }
308
309 static void printGSUB81 (FILE *fp, Sub81Ptr sub)
310 {
311 int i;
312
313 fprintf (fp, " - Reverse Chaining Context Single Substitution\n\t ");
314 otfPrintCoverage (fp, sub->coverage);
315 fprintf (fp, "\t backtrackGlyphCount: %d\n", sub->backtrackGlyphCount);
316 for (i = 0; i < sub->backtrackGlyphCount; i++) {
317 fprintf (fp, "\t %2d. backtrack", i);
318 otfPrintCoverage (fp, sub->backtrack[i]);
319 }
320 fprintf (fp, "\t lookaheadGlyphCount: %d\n", sub->lookaheadGlyphCount);
321 for (i = 0; i < sub->lookaheadGlyphCount; i++) {
322 fprintf (fp, "\t %2d. lookahead", i);
323 otfPrintCoverage (fp, sub->lookahead[i]);
324 }
325 fprintf (fp, "\t glyphCount: %d\n\t substitute - %d",
326 sub->glyphCount, sub->substitute[0]);
327 for (i = 1; i < sub->glyphCount; i++)
328 fprintf (fp, i % 8 ? ",\n\t\t %d" : ", %d", sub->substitute[i]);
329 }
330
331 static void freeGSUB81 (Sub81Ptr sub)
332 {
333 int i;
334
335 otfFreeCoverage (sub->coverage);
336 for (i = 0; i < sub->backtrackGlyphCount; i++)
337 otfFreeCoverage (sub->backtrack[i]);
338 free (sub->backtrack);
339 for (i = 0; i < sub->lookaheadGlyphCount; i++)
340 otfFreeCoverage (sub->lookahead[i]);
341 free (sub->lookahead);
342 free (sub->substitute);
343 }
344
345 static LookupPtr makeGSUBLookup (FILE *fp, USHORT lookupType, ULONG offset)
346 {
347 LookupPtr lookup;
348 USHORT lookupFormat;
349
350 again:
351 if (lookupType == 0 || lookupType > SubLookup_Max)
352 ttfError ("Unrecognized GSUB lookupType\n");
353
354 xfseek (fp, offset, SEEK_SET, "makeGSUBLookup");
355
356 lookupFormat = ttfGetUSHORT (fp);
357 if (lookupFormat & 0xfff0)
358 lookupFormat = 0x000f;
359
360 switch (lookupType << 4 | lookupFormat)
361 {
362 case 0x011:
363 lookup.sub.sub11 = makeGSUB11 (fp, offset);
364 break;
365 case 0x012:
366 lookup.sub.sub12 = makeGSUB12 (fp, offset);
367 break;
368 case 0x021:
369 lookup.sub.sub21 = makeGSUB21 (fp, offset);
370 break;
371 case 0x031:
372 lookup.sub.sub31 = makeGSUB31 (fp, offset);
373 break;
374 case 0x041:
375 lookup.sub.sub41 = makeGSUB41 (fp, offset);
376 break;
377 case 0x051:
378 lookup.sub.sub51 = makeGSUB51 (fp, offset);
379 break;
380 case 0x052:
381 lookup.sub.sub52 = makeGSUB52 (fp, offset);
382 break;
383 case 0x053:
384 lookup.sub.sub53 = makeGSUB53 (fp, offset);
385 break;
386 case 0x061:
387 lookup.sub.sub61 = makeGSUB61 (fp, offset);
388 break;
389 case 0x062:
390 lookup.sub.sub62 = makeGSUB62 (fp, offset);
391 break;
392 case 0x063:
393 lookup.sub.sub63 = makeGSUB63 (fp, offset);
394 break;
395 case 0x081:
396 lookup.sub.sub81 = makeGSUB81 (fp, offset);
397 break;
398 case 0x071:
399 lookupType = ttfGetUSHORT (fp);
400 if (lookupType == 7)
401 ttfError ("Invalid GSUB extensionLookupType\n");
402 offset += ttfGetULONG (fp);
403 goto again;
404 default:
405 ttfError ("Unrecognized GSUB lookupFormat\n");
406 }
407
408 lookup.otf->lookupType = lookupType;
409 lookup.otf->lookupFormat = lookupFormat;
410
411 return lookup;
412 }
413
414 static void printGSUBLookup (FILE *fp, LookupPtr lookup)
415 {
416 switch (lookup.otf->lookupType << 4 | lookup.otf->lookupFormat)
417 {
418 case 0x011:
419 printGSUB11 (fp, lookup.sub.sub11);
420 break;
421 case 0x012:
422 printGSUB12 (fp, lookup.sub.sub12);
423 break;
424 case 0x021:
425 printGSUB21 (fp, lookup.sub.sub21);
426 break;
427 case 0x031:
428 printGSUB31 (fp, lookup.sub.sub31);
429 break;
430 case 0x041:
431 printGSUB41 (fp, lookup.sub.sub41);
432 break;
433 case 0x051:
434 printGSUB51 (fp, lookup.sub.sub51);
435 break;
436 case 0x052:
437 printGSUB52 (fp, lookup.sub.sub52);
438 break;
439 case 0x053:
440 printGSUB53 (fp, lookup.sub.sub53);
441 break;
442 case 0x061:
443 printGSUB61 (fp, lookup.sub.sub61);
444 break;
445 case 0x062:
446 printGSUB62 (fp, lookup.sub.sub62);
447 break;
448 case 0x063:
449 printGSUB63 (fp, lookup.sub.sub63);
450 break;
451 case 0x081:
452 printGSUB81 (fp, lookup.sub.sub81);
453 break;
454 default:
455 ttfError ("Internal error: printGSUBLookup\n");
456 }
457 }
458
459 static void freeGSUBLookup (LookupPtr lookup)
460 {
461 switch (lookup.otf->lookupType << 4 | lookup.otf->lookupFormat)
462 {
463 case 0x011:
464 freeGSUB11 (lookup.sub.sub11);
465 break;
466 case 0x012:
467 freeGSUB12 (lookup.sub.sub12);
468 break;
469 case 0x021:
470 freeGSUB21 (lookup.sub.sub21);
471 break;
472 case 0x031:
473 freeGSUB31 (lookup.sub.sub31);
474 break;
475 case 0x041:
476 freeGSUB41 (lookup.sub.sub41);
477 break;
478 case 0x051:
479 freeGSUB51 (lookup.sub.sub51);
480 break;
481 case 0x052:
482 freeGSUB52 (lookup.sub.sub52);
483 break;
484 case 0x053:
485 freeGSUB53 (lookup.sub.sub53);
486 break;
487 case 0x061:
488 freeGSUB61 (lookup.sub.sub61);
489 break;
490 case 0x062:
491 freeGSUB62 (lookup.sub.sub62);
492 break;
493 case 0x063:
494 freeGSUB63 (lookup.sub.sub63);
495 break;
496 case 0x081:
497 freeGSUB81 (lookup.sub.sub81);
498 break;
499 default:
500 ttfError ("Internal error: freeGSUBLookup\n");
501 }
502 }
503
504 static void ttfLoadGSUB (FILE *fp, GSUBPtr gsub, ULONG offset)
505 {
506 USHORT sOffset, fOffset, lOffset;
507
508 xfseek (fp, offset, SEEK_SET, "ttfLoadGSUB");
509
510 gsub->version = ttfGetFixed (fp);
511 sOffset = ttfGetUSHORT (fp);
512 fOffset = ttfGetUSHORT (fp);
513 lOffset = ttfGetUSHORT (fp);
514
515 gsub->scriptList = otfMakeScriptList (fp, offset + sOffset);
516 gsub->featureList = otfMakeFeatureList (fp, offset + fOffset);
517 gsub->lookupList = otfMakeLookupList (fp, offset + lOffset, &makeGSUBLookup);
518 }
519
520 void ttfInitGSUB (TTFontPtr font)
521 {
522 ULONG tag = FT_MAKE_TAG ('G', 'S', 'U', 'B');
523 TableDirPtr ptd;
524
525 if ((ptd = ttfLookUpTableDir (tag, font)) != NULL)
526 {
527 font->gsub = XCALLOC1 (GSUB);
528 ttfLoadGSUB (font->fp, font->gsub, ptd->offset);
529 }
530 }
531
532 void ttfPrintGSUB (FILE *fp, GSUBPtr gsub)
533 {
534 int b[2];
535
536 FixedSplit (gsub->version, b);
537
538 fprintf (fp, "'GSUB' Table - Glyph Substitution Data\n");
539 fprintf (fp, "--------------------------------------\n");
540 fprintf (fp, "\t 'GSUB' Version:\t %d.%d\n",b[1],b[0]);
541
542 otfPrintScriptList (fp, gsub->scriptList);
543 otfPrintFeatureList (fp, gsub->featureList);
544 otfPrintLookupList (fp, gsub->lookupList, &printGSUBLookup);
545 }
546
547 void ttfFreeGSUB (GSUBPtr gsub)
548 {
549 if (gsub != NULL)
550 {
551 otfFreeScriptList (gsub->scriptList);
552 otfFreeFeatureList (gsub->featureList);
553 otfFreeLookupList (gsub->lookupList, &freeGSUBLookup);
554 free (gsub);
555 }
556 }