summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/mfluadir/otfcc/lib/table/otl/subtables/chaining/read.c
blob: 0059efeaec97e2647eea031518ecf769386b9442 (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
#include "../chaining.h"
#include "common.h"

typedef struct {
	otl_ClassDef *bc;
	otl_ClassDef *ic;
	otl_ClassDef *fc;
} classdefs;

static otl_Coverage *singleCoverage(font_file_pointer data, uint32_t tableLength, uint16_t gid,
                             uint32_t _offset, uint16_t kind, const glyphid_t maxGlyphs,
                             void *userdata) {
	otl_Coverage *cov;
	NEW(cov);
	cov->numGlyphs = 1;
	NEW(cov->glyphs);
	cov->glyphs[0] = Handle.fromIndex((glyphid_t)gid);
	return cov;
}
static otl_Coverage *classCoverage(font_file_pointer data, uint32_t tableLength, uint16_t cls,
                            uint32_t _offset, uint16_t kind, const glyphid_t maxGlyphs,
                            void *_classdefs) {
	classdefs *defs = (classdefs *)_classdefs;
	otl_ClassDef *cd = (kind == 1 ? defs->bc : kind == 2 ? defs->ic : defs->fc);
	otl_Coverage *cov;
	NEW(cov);
	cov->numGlyphs = 0;
	cov->glyphs = NULL;
	glyphid_t count = 0;
	if (cls == 0) {
		for (glyphid_t k = 0; k < maxGlyphs; k++) {
			bool found = false;
			for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
				if (cd->classes[j] > 0 && cd->glyphs[j].index == k) {
					found = true;
					break;
				}
			}
			if (!found) count += 1;
		}
	} else {
		for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
			if (cd->classes[j] == cls) count++;
		}
	}
	if (!count) return cov;

	cov->numGlyphs = count;
	NEW(cov->glyphs, count);
	glyphid_t jj = 0;
	if (cls == 0) {
		for (glyphid_t k = 0; k < maxGlyphs; k++) {
			bool found = false;
			for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
				if (cd->classes[j] > 0 && cd->glyphs[j].index == k) {
					found = true;
					break;
				}
			}
			if (!found) cov->glyphs[jj++] = Handle.fromIndex(k);
		}
	} else {
		for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
			if (cd->classes[j] == cls) { cov->glyphs[jj++] = Handle.dup(cd->glyphs[j]); }
		}
	}
	return cov;
}
static otl_Coverage *format3Coverage(font_file_pointer data, uint32_t tableLength, uint16_t shift,
                              uint32_t _offset, uint16_t kind, const glyphid_t maxGlyphs,
                              void *userdata) {
	return Coverage.read(data, tableLength, _offset + shift - 2);
}

typedef otl_Coverage *(*CoverageReaderHandler)(font_file_pointer, uint32_t, uint16_t, uint32_t,
                                               uint16_t, glyphid_t, void *);

static otl_ChainingRule *GeneralReadContextualRule(font_file_pointer data, uint32_t tableLength,
                                            uint32_t offset, uint16_t startGID, bool minusOne,
                                            CoverageReaderHandler fn, const glyphid_t maxGlyphs,
                                            void *userdata) {
	otl_ChainingRule *rule;
	NEW(rule);
	rule->match = NULL;
	rule->apply = NULL;

	uint16_t minusOneQ = (minusOne ? 1 : 0);

	checkLength(offset + 4);
	uint16_t nInput = read_16u(data + offset);
	uint16_t nApply = read_16u(data + offset + 2);
	checkLength(offset + 4 + 2 * nInput + 4 * nApply);

	rule->matchCount = nInput;
	rule->inputBegins = 0;
	rule->inputEnds = nInput;

	NEW(rule->match, rule->matchCount);
	uint16_t jj = 0;
	if (minusOne) {
		rule->match[jj++] = fn(data, tableLength, startGID, offset, 2, maxGlyphs, userdata);
	}
	for (uint16_t j = 0; j < nInput - minusOneQ; j++) {
		uint32_t gid = read_16u(data + offset + 4 + j * 2);
		rule->match[jj++] = fn(data, tableLength, gid, offset, 2, maxGlyphs, userdata);
	}
	rule->applyCount = nApply;
	NEW(rule->apply, rule->applyCount);
	for (tableid_t j = 0; j < nApply; j++) {
		rule->apply[j].index =
		    rule->inputBegins +
		    read_16u(data + offset + 4 + 2 * (rule->matchCount - minusOneQ) + j * 4);
		rule->apply[j].lookup = Handle.fromIndex(
		    read_16u(data + offset + 4 + 2 * (rule->matchCount - minusOneQ) + j * 4 + 2));
	}
	reverseBacktracks(rule);
	return rule;

FAIL:
	DELETE(deleteRule, rule);
	return NULL;
}
static subtable_chaining *readContextualFormat1(subtable_chaining *subtable,
                                                const font_file_pointer data, uint32_t tableLength,
                                                uint32_t offset, const glyphid_t maxGlyphs) {
	// Contextual Substitution Subtable, Simple.
	checkLength(offset + 6);

	uint16_t covOffset = offset + read_16u(data + offset + 2);
	otl_Coverage *firstCoverage = Coverage.read(data, tableLength, covOffset);

	tableid_t chainSubRuleSetCount = read_16u(data + offset + 4);
	if (chainSubRuleSetCount != firstCoverage->numGlyphs) goto FAIL;
	checkLength(offset + 6 + 2 * chainSubRuleSetCount);

	tableid_t totalRules = 0;
	for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
		uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
		checkLength(srsOffset + 2);
		totalRules += read_16u(data + srsOffset);
		checkLength(srsOffset + 2 + 2 * read_16u(data + srsOffset));
	}
	subtable->rulesCount = totalRules;
	NEW(subtable->rules, totalRules);

	tableid_t jj = 0;
	for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
		uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
		tableid_t srsCount = read_16u(data + srsOffset);
		for (tableid_t k = 0; k < srsCount; k++) {
			uint32_t srOffset = srsOffset + read_16u(data + srsOffset + 2 + k * 2);
			subtable->rules[jj] = GeneralReadContextualRule(data, tableLength, srOffset,
			                                                firstCoverage->glyphs[j].index, true,
			                                                singleCoverage, maxGlyphs, NULL);
			jj += 1;
		}
	}

	Coverage.free(firstCoverage);
	return subtable;
FAIL:
	iSubtable_chaining.free(subtable);
	return NULL;
}
static subtable_chaining *readContextualFormat2(subtable_chaining *subtable,
                                                const font_file_pointer data, uint32_t tableLength,
                                                uint32_t offset, const glyphid_t maxGlyphs) {
	// Contextual Substitution Subtable, Class based.
	checkLength(offset + 8);

	classdefs *cds;
	NEW(cds);
	cds->bc = NULL;
	cds->ic = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 4));
	cds->fc = NULL;

	tableid_t chainSubClassSetCnt = read_16u(data + offset + 6);
	checkLength(offset + 12 + 2 * chainSubClassSetCnt);

	tableid_t totalRules = 0;
	for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
		uint32_t srcOffset = read_16u(data + offset + 8 + j * 2);
		if (srcOffset) { totalRules += read_16u(data + offset + srcOffset); }
	}
	subtable->rulesCount = totalRules;
	NEW(subtable->rules, totalRules);

	tableid_t jj = 0;
	for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
		uint32_t srcOffset = read_16u(data + offset + 8 + j * 2);
		if (srcOffset) {
			tableid_t srsCount = read_16u(data + offset + srcOffset);
			for (tableid_t k = 0; k < srsCount; k++) {
				uint32_t srOffset =
				    offset + srcOffset + read_16u(data + offset + srcOffset + 2 + k * 2);
				subtable->rules[jj] = GeneralReadContextualRule(
				    data, tableLength, srOffset, j, true, classCoverage, maxGlyphs, cds);
				jj += 1;
			}
		}
	}
	if (cds) {
		if (cds->bc) ClassDef.free(cds->bc);
		if (cds->ic) ClassDef.free(cds->ic);
		if (cds->fc) ClassDef.free(cds->fc);
		FREE(cds);
	}
	return subtable;
FAIL:
	iSubtable_chaining.free(subtable);
	return NULL;
}
otl_Subtable *otl_read_contextual(const font_file_pointer data, uint32_t tableLength,
                                  uint32_t offset, const glyphid_t maxGlyphs,
                                  const otfcc_Options *options) {
	uint16_t format = 0;
	subtable_chaining *subtable = iSubtable_chaining.create();
	subtable->type = otl_chaining_poly;

	checkLength(offset + 2);
	format = read_16u(data + offset);
	if (format == 1) {
		return (otl_Subtable *)readContextualFormat1(subtable, data, tableLength, offset,
		                                             maxGlyphs);
	} else if (format == 2) {
		return (otl_Subtable *)readContextualFormat2(subtable, data, tableLength, offset,
		                                             maxGlyphs);
	} else if (format == 3) {
		// Contextual Substitution Subtable, Coverage based.
		subtable->rulesCount = 1;
		NEW(subtable->rules, 1);
		subtable->rules[0] = GeneralReadContextualRule(data, tableLength, offset + 2, 0, false,
		                                               format3Coverage, maxGlyphs, NULL);
		return (otl_Subtable *)subtable;
	}
FAIL:
	logWarning("Unsupported format %d.\n", format);
	iSubtable_chaining.free(subtable);
	return NULL;
}

static otl_ChainingRule *GeneralReadChainingRule(font_file_pointer data, uint32_t tableLength,
                                          uint32_t offset, uint16_t startGID, bool minusOne,
                                          CoverageReaderHandler fn, const glyphid_t maxGlyphs,
                                          void *userdata) {
	otl_ChainingRule *rule;
	NEW(rule);
	rule->match = NULL;
	rule->apply = NULL;

	uint16_t minusOneQ = (minusOne ? 1 : 0);

	checkLength(offset + 8);
	tableid_t nBack = read_16u(data + offset);
	checkLength(offset + 2 + 2 * nBack + 2);
	tableid_t nInput = read_16u(data + offset + 2 + 2 * nBack);
	checkLength(offset + 4 + 2 * (nBack + nInput - minusOneQ) + 2);
	tableid_t nLookaround = read_16u(data + offset + 4 + 2 * (nBack + nInput - minusOneQ));
	checkLength(offset + 6 + 2 * (nBack + nInput - minusOneQ + nLookaround) + 2);
	tableid_t nApply = read_16u(data + offset + 6 + 2 * (nBack + nInput - minusOneQ + nLookaround));
	checkLength(offset + 8 + 2 * (nBack + nInput - minusOneQ + nLookaround) + nApply * 4);

	rule->matchCount = nBack + nInput + nLookaround;
	rule->inputBegins = nBack;
	rule->inputEnds = nBack + nInput;

	NEW(rule->match, rule->matchCount);
	tableid_t jj = 0;
	for (tableid_t j = 0; j < nBack; j++) {
		uint32_t gid = read_16u(data + offset + 2 + j * 2);
		rule->match[jj++] = fn(data, tableLength, gid, offset, 1, maxGlyphs, userdata);
	}
	if (minusOne) {
		rule->match[jj++] = fn(data, tableLength, startGID, offset, 2, maxGlyphs, userdata);
	}
	for (tableid_t j = 0; j < nInput - minusOneQ; j++) {
		uint32_t gid = read_16u(data + offset + 4 + 2 * rule->inputBegins + j * 2);
		rule->match[jj++] = fn(data, tableLength, gid, offset, 2, maxGlyphs, userdata);
	}
	for (tableid_t j = 0; j < nLookaround; j++) {
		uint32_t gid = read_16u(data + offset + 6 + 2 * (rule->inputEnds - minusOneQ) + j * 2);
		rule->match[jj++] = fn(data, tableLength, gid, offset, 3, maxGlyphs, userdata);
	}
	rule->applyCount = nApply;
	NEW(rule->apply, rule->applyCount);
	for (tableid_t j = 0; j < nApply; j++) {
		rule->apply[j].index =
		    rule->inputBegins +
		    read_16u(data + offset + 8 + 2 * (rule->matchCount - minusOneQ) + j * 4);
		rule->apply[j].lookup = Handle.fromIndex(
		    read_16u(data + offset + 8 + 2 * (rule->matchCount - minusOneQ) + j * 4 + 2));
	}
	reverseBacktracks(rule);
	return rule;

FAIL:
	DELETE(deleteRule, rule);
	return NULL;
}
static subtable_chaining *readChainingFormat1(subtable_chaining *subtable,
                                              const font_file_pointer data, uint32_t tableLength,
                                              uint32_t offset, const glyphid_t maxGlyphs) {
	// Contextual Substitution Subtable, Simple.
	checkLength(offset + 6);

	uint16_t covOffset = offset + read_16u(data + offset + 2);
	otl_Coverage *firstCoverage = Coverage.read(data, tableLength, covOffset);

	tableid_t chainSubRuleSetCount = read_16u(data + offset + 4);
	if (chainSubRuleSetCount != firstCoverage->numGlyphs) goto FAIL;
	checkLength(offset + 6 + 2 * chainSubRuleSetCount);

	tableid_t totalRules = 0;
	for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
		uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
		checkLength(srsOffset + 2);
		totalRules += read_16u(data + srsOffset);
		checkLength(srsOffset + 2 + 2 * read_16u(data + srsOffset));
	}
	subtable->rulesCount = totalRules;
	NEW(subtable->rules, totalRules);

	tableid_t jj = 0;
	for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
		uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
		tableid_t srsCount = read_16u(data + srsOffset);
		for (tableid_t k = 0; k < srsCount; k++) {
			uint32_t srOffset = srsOffset + read_16u(data + srsOffset + 2 + k * 2);
			subtable->rules[jj] =
			    GeneralReadChainingRule(data, tableLength, srOffset, firstCoverage->glyphs[j].index,
			                            true, singleCoverage, maxGlyphs, NULL);
			jj += 1;
		}
	}

	Coverage.free(firstCoverage);
	return subtable;
FAIL:
	iSubtable_chaining.free(subtable);
	return NULL;
}
static subtable_chaining *readChainingFormat2(subtable_chaining *subtable,
                                              const font_file_pointer data, uint32_t tableLength,
                                              uint32_t offset, const glyphid_t maxGlyphs) {
	// Chaining Contextual Substitution Subtable, Class based.
	checkLength(offset + 12);

	classdefs *cds;
	NEW(cds);
	cds->bc = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 4));
	cds->ic = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 6));
	cds->fc = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 8));

	tableid_t chainSubClassSetCnt = read_16u(data + offset + 10);
	checkLength(offset + 12 + 2 * chainSubClassSetCnt);

	tableid_t totalRules = 0;
	for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
		uint32_t srcOffset = read_16u(data + offset + 12 + j * 2);
		if (srcOffset) { totalRules += read_16u(data + offset + srcOffset); }
	}
	subtable->rulesCount = totalRules;
	NEW(subtable->rules, totalRules);

	tableid_t jj = 0;
	for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
		uint32_t srcOffset = read_16u(data + offset + 12 + j * 2);
		if (srcOffset) {
			tableid_t srsCount = read_16u(data + offset + srcOffset);
			for (tableid_t k = 0; k < srsCount; k++) {
				uint32_t dsrOffset = read_16u(data + offset + srcOffset + 2 + k * 2);
				uint32_t srOffset = offset + srcOffset + dsrOffset;
				subtable->rules[jj] = GeneralReadChainingRule(data, tableLength, srOffset, j, true,
				                                              classCoverage, maxGlyphs, cds);
				jj += 1;
			}
		}
	}

	if (cds) {
		if (cds->bc) ClassDef.free(cds->bc);
		if (cds->ic) ClassDef.free(cds->ic);
		if (cds->fc) ClassDef.free(cds->fc);
		FREE(cds);
	}
	return subtable;
FAIL:
	iSubtable_chaining.free(subtable);
	return NULL;
}
otl_Subtable *otl_read_chaining(const font_file_pointer data, uint32_t tableLength, uint32_t offset,
                                const glyphid_t maxGlyphs, const otfcc_Options *options) {
	uint16_t format = 0;
	subtable_chaining *subtable = iSubtable_chaining.create();
	subtable->type = otl_chaining_poly;

	checkLength(offset + 2);
	format = read_16u(data + offset);
	if (format == 1) {
		return (otl_Subtable *)readChainingFormat1(subtable, data, tableLength, offset, maxGlyphs);
	} else if (format == 2) {
		return (otl_Subtable *)readChainingFormat2(subtable, data, tableLength, offset, maxGlyphs);
	} else if (format == 3) {
		// Chaining Contextual Substitution Subtable, Coverage based.
		// This table has exactly one rule within it, and i love it.
		subtable->rulesCount = 1;
		NEW(subtable->rules, 1);
		subtable->rules[0] = GeneralReadChainingRule(data, tableLength, offset + 2, 0, false,
		                                             format3Coverage, maxGlyphs, NULL);
		return (otl_Subtable *)subtable;
	}
FAIL:
	logWarning("Unsupported format %d.\n", format);
	iSubtable_chaining.free(subtable);
	return NULL;
}