Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/otl/build.c

raw
1 #include "private.h"
2
3 #define LARGE_SUBTABLE_LIMIT 4096
4
5 static uint32_t featureNameToTag(const sds name) {
6 uint32_t tag = 0;
7 if (sdslen(name) > 0) {
8 tag |= ((uint8_t)name[0]) << 24;
9 } else {
10 tag |= ((uint8_t)' ') << 24;
11 }
12 if (sdslen(name) > 1) {
13 tag |= ((uint8_t)name[1]) << 16;
14 } else {
15 tag |= ((uint8_t)' ') << 16;
16 }
17 if (sdslen(name) > 2) {
18 tag |= ((uint8_t)name[2]) << 8;
19 } else {
20 tag |= ((uint8_t)' ') << 8;
21 }
22 if (sdslen(name) > 3) {
23 tag |= ((uint8_t)name[3]) << 0;
24 } else {
25 tag |= ((uint8_t)' ') << 0;
26 }
27 return tag;
28 }
29
30 typedef caryll_Buffer *(*_otl_Builder)(const otl_Subtable *_subtable,
31 otl_BuildHeuristics heuristics);
32
33 static tableid_t _declare_lookup_writer(otl_LookupType type, _otl_Builder fn,
34 const otl_Lookup *lookup, caryll_Buffer ***subtables,
35 size_t *lastOffset, bool *preferExtensionForThisLUT,
36 otl_BuildHeuristics heuristics) {
37 if (lookup->type == type) {
38 NEW(*subtables, lookup->subtables.length);
39 size_t totalBufSizeShort = 0;
40 size_t totalBufSizeExt = 0;
41 for (tableid_t j = 0; j < lookup->subtables.length; j++) {
42 caryll_Buffer *buf = fn(lookup->subtables.items[j], heuristics);
43 (*subtables)[j] = buf;
44 totalBufSizeShort += buf->size;
45 totalBufSizeExt += 8;
46 }
47 if (totalBufSizeShort > LARGE_SUBTABLE_LIMIT) {
48 *lastOffset += totalBufSizeExt;
49 *preferExtensionForThisLUT = true;
50 } else {
51 *lastOffset += totalBufSizeShort;
52 *preferExtensionForThisLUT = false;
53 }
54 return lookup->subtables.length;
55 }
56 return 0;
57 }
58
59 #define LOOKUP_WRITER(type, fn) \
60 if (!written) \
61 written = _declare_lookup_writer(type, fn, lookup, subtables, lastOffset, \
62 preferExtensionForThisLUT, heuristics);
63
64 static tableid_t _build_lookup(const otl_Lookup *lookup, caryll_Buffer ***subtables,
65 size_t *lastOffset, bool *preferExtensionForThisLUT,
66 otl_BuildHeuristics heuristics) {
67 if (lookup->type == otl_type_gpos_chaining || lookup->type == otl_type_gsub_chaining) {
68 return otfcc_classifiedBuildChaining(lookup, subtables, lastOffset);
69 }
70
71 tableid_t written = 0;
72 LOOKUP_WRITER(otl_type_gsub_single, otfcc_build_gsub_single_subtable);
73 LOOKUP_WRITER(otl_type_gsub_multiple, otfcc_build_gsub_multi_subtable);
74 LOOKUP_WRITER(otl_type_gsub_alternate, otfcc_build_gsub_multi_subtable);
75 LOOKUP_WRITER(otl_type_gsub_ligature, otfcc_build_gsub_ligature_subtable);
76 LOOKUP_WRITER(otl_type_gsub_reverse, otfcc_build_gsub_reverse);
77 LOOKUP_WRITER(otl_type_gpos_single, otfcc_build_gpos_single);
78 LOOKUP_WRITER(otl_type_gpos_pair, otfcc_build_gpos_pair);
79 LOOKUP_WRITER(otl_type_gpos_cursive, otfcc_build_gpos_cursive);
80 LOOKUP_WRITER(otl_type_gpos_markToBase, otfcc_build_gpos_markToSingle);
81 LOOKUP_WRITER(otl_type_gpos_markToMark, otfcc_build_gpos_markToSingle);
82 LOOKUP_WRITER(otl_type_gpos_markToLigature, otfcc_build_gpos_markToLigature);
83 return written;
84 }
85
86 static otl_BuildHeuristics getLookupHeuristics(const table_OTL *table, const otl_Lookup *lut) {
87 otl_BuildHeuristics heu = OTL_BH_NORMAL;
88 // GSUB VERT heuristics
89 // GDI have some restrictions on the internal format of the lookup inisde a VERT feature
90 if (lut->type == otl_type_gsub_single) {
91 for (tableid_t j = 0; j < table->features.length; j++) {
92 const otl_Feature *fea = table->features.items[j];
93 if (featureNameToTag(fea->name) != OTFCC_CHR('v','e','r','t')) continue;
94 for (tableid_t k = 0; k < fea->lookups.length; k++) {
95 if (fea->lookups.items[k] == lut) heu |= OTL_BH_GSUB_VERT;
96 }
97 }
98 }
99 return heu;
100 }
101
102 // When writing lookups, otfcc will try to maintain everything correctly.
103 // That is, we will use extended layout lookups automatically when the
104 // offsets are too large.
105 static bk_Block *writeOTLLookups(const table_OTL *table, const otfcc_Options *options,
106 const char *tag) {
107 caryll_Buffer ***subtables;
108 NEW(subtables, table->lookups.length);
109 bool *preferExtForThisLut;
110 tableid_t *subtableQuantity;
111 NEW(subtableQuantity, table->lookups.length);
112 NEW(preferExtForThisLut, table->lookups.length);
113
114 size_t lastOffset = 0;
115 for (tableid_t j = 0; j < table->lookups.length; j++) {
116 otl_Lookup *lookup = table->lookups.items[j];
117 otl_BuildHeuristics heu = getLookupHeuristics(table, lookup);
118 logProgress("Building lookup %s (%u/%u)\n", lookup->name, j,
119 (uint32_t)table->lookups.length);
120 subtableQuantity[j] =
121 _build_lookup(lookup, &(subtables[j]), &lastOffset, &(preferExtForThisLut[j]), heu);
122 }
123
124 size_t headerSize = 2 + 2 * table->lookups.length;
125 for (tableid_t j = 0; j < table->lookups.length; j++) {
126 if (subtableQuantity[j]) { headerSize += 6 + 2 * subtableQuantity[j]; }
127 }
128 bool useExtended = lastOffset >= 0xFF00 - headerSize;
129
130 bk_Block *root = bk_new_Block(b16, table->lookups.length, // LookupCount
131 bkover);
132 for (tableid_t j = 0; j < table->lookups.length; j++) {
133 if (!subtableQuantity[j]) {
134 logNotice("Lookup %s is empty.\n", table->lookups.items[j]->name);
135 }
136 otl_Lookup *lookup = table->lookups.items[j];
137 const bool canBeContextual = otfcc_chainingLookupIsContextualLookup(lookup);
138 const bool useExtendedForIt = useExtended || preferExtForThisLut[j];
139 if (useExtendedForIt) {
140 logNotice("[OTFCC-fea] Using extended OpenType table layout for %s/%s.\n", tag,
141 lookup->name);
142 }
143 uint16_t lookupType =
144 useExtendedForIt
145 ? (lookup->type > otl_type_gpos_unknown
146 ? otl_type_gpos_extend - otl_type_gpos_unknown
147 : lookup->type > otl_type_gsub_unknown
148 ? otl_type_gsub_extend - otl_type_gsub_unknown
149 : 0)
150 : (lookup->type > otl_type_gpos_unknown
151 ? lookup->type - otl_type_gpos_unknown
152 : lookup->type > otl_type_gsub_unknown ? lookup->type - otl_type_gsub_unknown
153 : 0) -
154 (canBeContextual ? 1 : 0);
155
156 bk_Block *blk = bk_new_Block(b16, lookupType, // LookupType
157 b16, lookup->flags, // LookupFlag
158 b16, subtableQuantity[j], // SubTableCount
159 bkover);
160
161 for (tableid_t k = 0; k < subtableQuantity[j]; k++) {
162 if (useExtendedForIt) {
163 uint16_t extensionLookupType = (lookup->type > otl_type_gpos_unknown
164 ? lookup->type - otl_type_gpos_unknown
165 : lookup->type > otl_type_gsub_unknown
166 ? lookup->type - otl_type_gsub_unknown
167 : 0) -
168 (canBeContextual ? 1 : 0);
169
170 bk_Block *stub =
171 bk_new_Block(b16, 1, // format
172 b16, extensionLookupType, // ExtensionLookupType
173 p32, bk_newBlockFromBuffer(subtables[j][k]), // ExtensionOffset
174 bkover);
175 bk_push(blk, p16, stub, bkover);
176 } else {
177 bk_push(blk, p16, bk_newBlockFromBuffer(subtables[j][k]), bkover);
178 }
179 }
180 bk_push(blk, b16, 0, // MarkFilteringSet
181 bkover);
182 bk_push(root, p16, blk, bkover);
183 FREE(subtables[j]);
184 }
185 FREE(subtables);
186 FREE(subtableQuantity);
187 FREE(preferExtForThisLut);
188 return root;
189 }
190
191 static bk_Block *writeOTLFeatures(const table_OTL *table, const otfcc_Options *options) {
192 bk_Block *root = bk_new_Block(b16, table->features.length, bkover);
193 for (tableid_t j = 0; j < table->features.length; j++) {
194 bk_Block *fea = bk_new_Block(p16, NULL, // FeatureParams
195 b16, table->features.items[j]->lookups.length, // LookupCount
196 bkover);
197 for (tableid_t k = 0; k < table->features.items[j]->lookups.length; k++) {
198 // reverse lookup
199 for (tableid_t l = 0; l < table->lookups.length; l++) {
200 if (table->features.items[j]->lookups.items[k] == table->lookups.items[l]) {
201 bk_push(fea, b16, l, bkover);
202 break;
203 }
204 }
205 }
206 bk_push(root, b32, featureNameToTag(table->features.items[j]->name), // FeatureTag
207 p16, fea, // Feature
208 bkover);
209 }
210 return root;
211 }
212
213 // script hash
214 typedef struct {
215 sds tag;
216 uint16_t lc;
217 otl_LanguageSystem *dl;
218 otl_LanguageSystem **ll;
219 UT_hash_handle hh;
220 } script_stat_hash;
221
222 static tableid_t featureIndex(const otl_Feature *feature, const table_OTL *table) {
223 for (tableid_t j = 0; j < table->features.length; j++)
224 if (table->features.items[j] == feature) { return j; }
225 return 0xFFFF;
226 }
227 static bk_Block *writeLanguage(otl_LanguageSystem *lang, const table_OTL *table) {
228 if (!lang) return NULL;
229 bk_Block *root =
230 bk_new_Block(p16, NULL, // LookupOrder
231 b16, featureIndex(lang->requiredFeature, table), // ReqFeatureIndex
232 b16, lang->features.length, // FeatureCount
233 bkover);
234 for (tableid_t k = 0; k < lang->features.length; k++) {
235 bk_push(root, b16, featureIndex(lang->features.items[k], table), bkover);
236 }
237 return root;
238 }
239
240 static bk_Block *writeScript(script_stat_hash *script, const table_OTL *table) {
241 bk_Block *root = bk_new_Block(p16, writeLanguage(script->dl, table), // DefaultLangSys
242 b16, script->lc, // LangSysCount
243 bkover);
244
245 for (tableid_t j = 0; j < script->lc; j++) {
246 sds tag = sdsnewlen(script->ll[j]->name + 5, 4);
247
248 bk_push(root, b32, featureNameToTag(tag), // LangSysTag
249 p16, writeLanguage(script->ll[j], table), // LangSys
250 bkover);
251 sdsfree(tag);
252 }
253 return root;
254 }
255 static bk_Block *writeOTLScriptAndLanguages(const table_OTL *table, const otfcc_Options *options) {
256 script_stat_hash *h = NULL;
257 for (tableid_t j = 0; j < table->languages.length; j++) {
258 otl_LanguageSystem *language = table->languages.items[j];
259 sds scriptTag = sdsnewlen(language->name, 4);
260 bool isDefault = strncmp(language->name + 5, "DFLT", 4) == 0 ||
261 strncmp(language->name + 5, "dflt", 4) == 0;
262
263 script_stat_hash *s = NULL;
264 HASH_FIND_STR(h, scriptTag, s);
265 if (s) {
266 if (isDefault) {
267 s->dl = language;
268 } else {
269 s->lc += 1;
270 s->ll[s->lc - 1] = language;
271 }
272 sdsfree(scriptTag);
273 } else {
274 NEW(s);
275 s->tag = scriptTag;
276 s->dl = NULL;
277 NEW(s->ll, table->languages.length);
278 if (isDefault) {
279 s->dl = language;
280 s->lc = 0;
281 } else {
282 s->lc = 1;
283 s->ll[s->lc - 1] = language;
284 }
285 HASH_ADD_STR(h, tag, s);
286 }
287 }
288
289 bk_Block *root = bk_new_Block(b16, HASH_COUNT(h), bkover);
290
291 script_stat_hash *s, *tmp;
292 HASH_ITER(hh, h, s, tmp) {
293 bk_push(root, b32, featureNameToTag(s->tag), // ScriptTag
294 p16, writeScript(s, table), // Script
295 bkover);
296 HASH_DEL(h, s);
297 sdsfree(s->tag);
298 FREE(s->ll);
299 FREE(s);
300 }
301 return root;
302 }
303
304 caryll_Buffer *otfcc_buildOtl(const table_OTL *table, const otfcc_Options *options,
305 const char *tag) {
306 if (!table) return NULL;
307 caryll_Buffer *buf;
308 loggedStep("%s", tag) {
309 bk_Block *lookups = writeOTLLookups(table, options, tag);
310 bk_Block *features = writeOTLFeatures(table, options);
311 bk_Block *languages = writeOTLScriptAndLanguages(table, options);
312 bk_Block *root = bk_new_Block(b32, 0x10000, // Version
313 p16, languages, // ScriptList
314 p16, features, // FeatureList
315 p16, lookups, // LookupList
316 bkover);
317 buf = bk_build_Block(root);
318 }
319 return buf;
320 }