Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/otl/subtables/chaining/read.c

raw
1 #include "../chaining.h"
2 #include "common.h"
3
4 typedef struct {
5 otl_ClassDef *bc;
6 otl_ClassDef *ic;
7 otl_ClassDef *fc;
8 } classdefs;
9
10 static otl_Coverage *singleCoverage(font_file_pointer data, uint32_t tableLength, uint16_t gid,
11 uint32_t _offset, uint16_t kind, const glyphid_t maxGlyphs,
12 void *userdata) {
13 otl_Coverage *cov;
14 NEW(cov);
15 cov->numGlyphs = 1;
16 NEW(cov->glyphs);
17 cov->glyphs[0] = Handle.fromIndex((glyphid_t)gid);
18 return cov;
19 }
20 static otl_Coverage *classCoverage(font_file_pointer data, uint32_t tableLength, uint16_t cls,
21 uint32_t _offset, uint16_t kind, const glyphid_t maxGlyphs,
22 void *_classdefs) {
23 classdefs *defs = (classdefs *)_classdefs;
24 otl_ClassDef *cd = (kind == 1 ? defs->bc : kind == 2 ? defs->ic : defs->fc);
25 otl_Coverage *cov;
26 NEW(cov);
27 cov->numGlyphs = 0;
28 cov->glyphs = NULL;
29 glyphid_t count = 0;
30 if (cls == 0) {
31 for (glyphid_t k = 0; k < maxGlyphs; k++) {
32 bool found = false;
33 for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
34 if (cd->classes[j] > 0 && cd->glyphs[j].index == k) {
35 found = true;
36 break;
37 }
38 }
39 if (!found) count += 1;
40 }
41 } else {
42 for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
43 if (cd->classes[j] == cls) count++;
44 }
45 }
46 if (!count) return cov;
47
48 cov->numGlyphs = count;
49 NEW(cov->glyphs, count);
50 glyphid_t jj = 0;
51 if (cls == 0) {
52 for (glyphid_t k = 0; k < maxGlyphs; k++) {
53 bool found = false;
54 for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
55 if (cd->classes[j] > 0 && cd->glyphs[j].index == k) {
56 found = true;
57 break;
58 }
59 }
60 if (!found) cov->glyphs[jj++] = Handle.fromIndex(k);
61 }
62 } else {
63 for (glyphid_t j = 0; j < cd->numGlyphs; j++) {
64 if (cd->classes[j] == cls) { cov->glyphs[jj++] = Handle.dup(cd->glyphs[j]); }
65 }
66 }
67 return cov;
68 }
69 static otl_Coverage *format3Coverage(font_file_pointer data, uint32_t tableLength, uint16_t shift,
70 uint32_t _offset, uint16_t kind, const glyphid_t maxGlyphs,
71 void *userdata) {
72 return Coverage.read(data, tableLength, _offset + shift - 2);
73 }
74
75 typedef otl_Coverage *(*CoverageReaderHandler)(font_file_pointer, uint32_t, uint16_t, uint32_t,
76 uint16_t, glyphid_t, void *);
77
78 static otl_ChainingRule *GeneralReadContextualRule(font_file_pointer data, uint32_t tableLength,
79 uint32_t offset, uint16_t startGID, bool minusOne,
80 CoverageReaderHandler fn, const glyphid_t maxGlyphs,
81 void *userdata) {
82 otl_ChainingRule *rule;
83 NEW(rule);
84 rule->match = NULL;
85 rule->apply = NULL;
86
87 uint16_t minusOneQ = (minusOne ? 1 : 0);
88
89 checkLength(offset + 4);
90 uint16_t nInput = read_16u(data + offset);
91 uint16_t nApply = read_16u(data + offset + 2);
92 checkLength(offset + 4 + 2 * nInput + 4 * nApply);
93
94 rule->matchCount = nInput;
95 rule->inputBegins = 0;
96 rule->inputEnds = nInput;
97
98 NEW(rule->match, rule->matchCount);
99 uint16_t jj = 0;
100 if (minusOne) {
101 rule->match[jj++] = fn(data, tableLength, startGID, offset, 2, maxGlyphs, userdata);
102 }
103 for (uint16_t j = 0; j < nInput - minusOneQ; j++) {
104 uint32_t gid = read_16u(data + offset + 4 + j * 2);
105 rule->match[jj++] = fn(data, tableLength, gid, offset, 2, maxGlyphs, userdata);
106 }
107 rule->applyCount = nApply;
108 NEW(rule->apply, rule->applyCount);
109 for (tableid_t j = 0; j < nApply; j++) {
110 rule->apply[j].index =
111 rule->inputBegins +
112 read_16u(data + offset + 4 + 2 * (rule->matchCount - minusOneQ) + j * 4);
113 rule->apply[j].lookup = Handle.fromIndex(
114 read_16u(data + offset + 4 + 2 * (rule->matchCount - minusOneQ) + j * 4 + 2));
115 }
116 reverseBacktracks(rule);
117 return rule;
118
119 FAIL:
120 DELETE(deleteRule, rule);
121 return NULL;
122 }
123 static subtable_chaining *readContextualFormat1(subtable_chaining *subtable,
124 const font_file_pointer data, uint32_t tableLength,
125 uint32_t offset, const glyphid_t maxGlyphs) {
126 // Contextual Substitution Subtable, Simple.
127 checkLength(offset + 6);
128
129 uint16_t covOffset = offset + read_16u(data + offset + 2);
130 otl_Coverage *firstCoverage = Coverage.read(data, tableLength, covOffset);
131
132 tableid_t chainSubRuleSetCount = read_16u(data + offset + 4);
133 if (chainSubRuleSetCount != firstCoverage->numGlyphs) goto FAIL;
134 checkLength(offset + 6 + 2 * chainSubRuleSetCount);
135
136 tableid_t totalRules = 0;
137 for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
138 uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
139 checkLength(srsOffset + 2);
140 totalRules += read_16u(data + srsOffset);
141 checkLength(srsOffset + 2 + 2 * read_16u(data + srsOffset));
142 }
143 subtable->rulesCount = totalRules;
144 NEW(subtable->rules, totalRules);
145
146 tableid_t jj = 0;
147 for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
148 uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
149 tableid_t srsCount = read_16u(data + srsOffset);
150 for (tableid_t k = 0; k < srsCount; k++) {
151 uint32_t srOffset = srsOffset + read_16u(data + srsOffset + 2 + k * 2);
152 subtable->rules[jj] = GeneralReadContextualRule(data, tableLength, srOffset,
153 firstCoverage->glyphs[j].index, true,
154 singleCoverage, maxGlyphs, NULL);
155 jj += 1;
156 }
157 }
158
159 Coverage.free(firstCoverage);
160 return subtable;
161 FAIL:
162 iSubtable_chaining.free(subtable);
163 return NULL;
164 }
165 static subtable_chaining *readContextualFormat2(subtable_chaining *subtable,
166 const font_file_pointer data, uint32_t tableLength,
167 uint32_t offset, const glyphid_t maxGlyphs) {
168 // Contextual Substitution Subtable, Class based.
169 checkLength(offset + 8);
170
171 classdefs *cds;
172 NEW(cds);
173 cds->bc = NULL;
174 cds->ic = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 4));
175 cds->fc = NULL;
176
177 tableid_t chainSubClassSetCnt = read_16u(data + offset + 6);
178 checkLength(offset + 12 + 2 * chainSubClassSetCnt);
179
180 tableid_t totalRules = 0;
181 for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
182 uint32_t srcOffset = read_16u(data + offset + 8 + j * 2);
183 if (srcOffset) { totalRules += read_16u(data + offset + srcOffset); }
184 }
185 subtable->rulesCount = totalRules;
186 NEW(subtable->rules, totalRules);
187
188 tableid_t jj = 0;
189 for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
190 uint32_t srcOffset = read_16u(data + offset + 8 + j * 2);
191 if (srcOffset) {
192 tableid_t srsCount = read_16u(data + offset + srcOffset);
193 for (tableid_t k = 0; k < srsCount; k++) {
194 uint32_t srOffset =
195 offset + srcOffset + read_16u(data + offset + srcOffset + 2 + k * 2);
196 subtable->rules[jj] = GeneralReadContextualRule(
197 data, tableLength, srOffset, j, true, classCoverage, maxGlyphs, cds);
198 jj += 1;
199 }
200 }
201 }
202 if (cds) {
203 if (cds->bc) ClassDef.free(cds->bc);
204 if (cds->ic) ClassDef.free(cds->ic);
205 if (cds->fc) ClassDef.free(cds->fc);
206 FREE(cds);
207 }
208 return subtable;
209 FAIL:
210 iSubtable_chaining.free(subtable);
211 return NULL;
212 }
213 otl_Subtable *otl_read_contextual(const font_file_pointer data, uint32_t tableLength,
214 uint32_t offset, const glyphid_t maxGlyphs,
215 const otfcc_Options *options) {
216 uint16_t format = 0;
217 subtable_chaining *subtable = iSubtable_chaining.create();
218 subtable->type = otl_chaining_poly;
219
220 checkLength(offset + 2);
221 format = read_16u(data + offset);
222 if (format == 1) {
223 return (otl_Subtable *)readContextualFormat1(subtable, data, tableLength, offset,
224 maxGlyphs);
225 } else if (format == 2) {
226 return (otl_Subtable *)readContextualFormat2(subtable, data, tableLength, offset,
227 maxGlyphs);
228 } else if (format == 3) {
229 // Contextual Substitution Subtable, Coverage based.
230 subtable->rulesCount = 1;
231 NEW(subtable->rules, 1);
232 subtable->rules[0] = GeneralReadContextualRule(data, tableLength, offset + 2, 0, false,
233 format3Coverage, maxGlyphs, NULL);
234 return (otl_Subtable *)subtable;
235 }
236 FAIL:
237 logWarning("Unsupported format %d.\n", format);
238 iSubtable_chaining.free(subtable);
239 return NULL;
240 }
241
242 static otl_ChainingRule *GeneralReadChainingRule(font_file_pointer data, uint32_t tableLength,
243 uint32_t offset, uint16_t startGID, bool minusOne,
244 CoverageReaderHandler fn, const glyphid_t maxGlyphs,
245 void *userdata) {
246 otl_ChainingRule *rule;
247 NEW(rule);
248 rule->match = NULL;
249 rule->apply = NULL;
250
251 uint16_t minusOneQ = (minusOne ? 1 : 0);
252
253 checkLength(offset + 8);
254 tableid_t nBack = read_16u(data + offset);
255 checkLength(offset + 2 + 2 * nBack + 2);
256 tableid_t nInput = read_16u(data + offset + 2 + 2 * nBack);
257 checkLength(offset + 4 + 2 * (nBack + nInput - minusOneQ) + 2);
258 tableid_t nLookaround = read_16u(data + offset + 4 + 2 * (nBack + nInput - minusOneQ));
259 checkLength(offset + 6 + 2 * (nBack + nInput - minusOneQ + nLookaround) + 2);
260 tableid_t nApply = read_16u(data + offset + 6 + 2 * (nBack + nInput - minusOneQ + nLookaround));
261 checkLength(offset + 8 + 2 * (nBack + nInput - minusOneQ + nLookaround) + nApply * 4);
262
263 rule->matchCount = nBack + nInput + nLookaround;
264 rule->inputBegins = nBack;
265 rule->inputEnds = nBack + nInput;
266
267 NEW(rule->match, rule->matchCount);
268 tableid_t jj = 0;
269 for (tableid_t j = 0; j < nBack; j++) {
270 uint32_t gid = read_16u(data + offset + 2 + j * 2);
271 rule->match[jj++] = fn(data, tableLength, gid, offset, 1, maxGlyphs, userdata);
272 }
273 if (minusOne) {
274 rule->match[jj++] = fn(data, tableLength, startGID, offset, 2, maxGlyphs, userdata);
275 }
276 for (tableid_t j = 0; j < nInput - minusOneQ; j++) {
277 uint32_t gid = read_16u(data + offset + 4 + 2 * rule->inputBegins + j * 2);
278 rule->match[jj++] = fn(data, tableLength, gid, offset, 2, maxGlyphs, userdata);
279 }
280 for (tableid_t j = 0; j < nLookaround; j++) {
281 uint32_t gid = read_16u(data + offset + 6 + 2 * (rule->inputEnds - minusOneQ) + j * 2);
282 rule->match[jj++] = fn(data, tableLength, gid, offset, 3, maxGlyphs, userdata);
283 }
284 rule->applyCount = nApply;
285 NEW(rule->apply, rule->applyCount);
286 for (tableid_t j = 0; j < nApply; j++) {
287 rule->apply[j].index =
288 rule->inputBegins +
289 read_16u(data + offset + 8 + 2 * (rule->matchCount - minusOneQ) + j * 4);
290 rule->apply[j].lookup = Handle.fromIndex(
291 read_16u(data + offset + 8 + 2 * (rule->matchCount - minusOneQ) + j * 4 + 2));
292 }
293 reverseBacktracks(rule);
294 return rule;
295
296 FAIL:
297 DELETE(deleteRule, rule);
298 return NULL;
299 }
300 static subtable_chaining *readChainingFormat1(subtable_chaining *subtable,
301 const font_file_pointer data, uint32_t tableLength,
302 uint32_t offset, const glyphid_t maxGlyphs) {
303 // Contextual Substitution Subtable, Simple.
304 checkLength(offset + 6);
305
306 uint16_t covOffset = offset + read_16u(data + offset + 2);
307 otl_Coverage *firstCoverage = Coverage.read(data, tableLength, covOffset);
308
309 tableid_t chainSubRuleSetCount = read_16u(data + offset + 4);
310 if (chainSubRuleSetCount != firstCoverage->numGlyphs) goto FAIL;
311 checkLength(offset + 6 + 2 * chainSubRuleSetCount);
312
313 tableid_t totalRules = 0;
314 for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
315 uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
316 checkLength(srsOffset + 2);
317 totalRules += read_16u(data + srsOffset);
318 checkLength(srsOffset + 2 + 2 * read_16u(data + srsOffset));
319 }
320 subtable->rulesCount = totalRules;
321 NEW(subtable->rules, totalRules);
322
323 tableid_t jj = 0;
324 for (tableid_t j = 0; j < chainSubRuleSetCount; j++) {
325 uint32_t srsOffset = offset + read_16u(data + offset + 6 + j * 2);
326 tableid_t srsCount = read_16u(data + srsOffset);
327 for (tableid_t k = 0; k < srsCount; k++) {
328 uint32_t srOffset = srsOffset + read_16u(data + srsOffset + 2 + k * 2);
329 subtable->rules[jj] =
330 GeneralReadChainingRule(data, tableLength, srOffset, firstCoverage->glyphs[j].index,
331 true, singleCoverage, maxGlyphs, NULL);
332 jj += 1;
333 }
334 }
335
336 Coverage.free(firstCoverage);
337 return subtable;
338 FAIL:
339 iSubtable_chaining.free(subtable);
340 return NULL;
341 }
342 static subtable_chaining *readChainingFormat2(subtable_chaining *subtable,
343 const font_file_pointer data, uint32_t tableLength,
344 uint32_t offset, const glyphid_t maxGlyphs) {
345 // Chaining Contextual Substitution Subtable, Class based.
346 checkLength(offset + 12);
347
348 classdefs *cds;
349 NEW(cds);
350 cds->bc = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 4));
351 cds->ic = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 6));
352 cds->fc = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 8));
353
354 tableid_t chainSubClassSetCnt = read_16u(data + offset + 10);
355 checkLength(offset + 12 + 2 * chainSubClassSetCnt);
356
357 tableid_t totalRules = 0;
358 for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
359 uint32_t srcOffset = read_16u(data + offset + 12 + j * 2);
360 if (srcOffset) { totalRules += read_16u(data + offset + srcOffset); }
361 }
362 subtable->rulesCount = totalRules;
363 NEW(subtable->rules, totalRules);
364
365 tableid_t jj = 0;
366 for (tableid_t j = 0; j < chainSubClassSetCnt; j++) {
367 uint32_t srcOffset = read_16u(data + offset + 12 + j * 2);
368 if (srcOffset) {
369 tableid_t srsCount = read_16u(data + offset + srcOffset);
370 for (tableid_t k = 0; k < srsCount; k++) {
371 uint32_t dsrOffset = read_16u(data + offset + srcOffset + 2 + k * 2);
372 uint32_t srOffset = offset + srcOffset + dsrOffset;
373 subtable->rules[jj] = GeneralReadChainingRule(data, tableLength, srOffset, j, true,
374 classCoverage, maxGlyphs, cds);
375 jj += 1;
376 }
377 }
378 }
379
380 if (cds) {
381 if (cds->bc) ClassDef.free(cds->bc);
382 if (cds->ic) ClassDef.free(cds->ic);
383 if (cds->fc) ClassDef.free(cds->fc);
384 FREE(cds);
385 }
386 return subtable;
387 FAIL:
388 iSubtable_chaining.free(subtable);
389 return NULL;
390 }
391 otl_Subtable *otl_read_chaining(const font_file_pointer data, uint32_t tableLength, uint32_t offset,
392 const glyphid_t maxGlyphs, const otfcc_Options *options) {
393 uint16_t format = 0;
394 subtable_chaining *subtable = iSubtable_chaining.create();
395 subtable->type = otl_chaining_poly;
396
397 checkLength(offset + 2);
398 format = read_16u(data + offset);
399 if (format == 1) {
400 return (otl_Subtable *)readChainingFormat1(subtable, data, tableLength, offset, maxGlyphs);
401 } else if (format == 2) {
402 return (otl_Subtable *)readChainingFormat2(subtable, data, tableLength, offset, maxGlyphs);
403 } else if (format == 3) {
404 // Chaining Contextual Substitution Subtable, Coverage based.
405 // This table has exactly one rule within it, and i love it.
406 subtable->rulesCount = 1;
407 NEW(subtable->rules, 1);
408 subtable->rules[0] = GeneralReadChainingRule(data, tableLength, offset + 2, 0, false,
409 format3Coverage, maxGlyphs, NULL);
410 return (otl_Subtable *)subtable;
411 }
412 FAIL:
413 logWarning("Unsupported format %d.\n", format);
414 iSubtable_chaining.free(subtable);
415 return NULL;
416 }