Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/fvar.c

raw
1 #include "fvar.h"
2
3 #include "support/util.h"
4
5
6 // fvar instance tuple
7 // fvar instance
8 static INLINE void initFvarInstance(fvar_Instance *inst) {
9 memset(inst, 0, sizeof(*inst));
10 iVV.init(&inst->coordinates);
11 }
12 static INLINE void disposeFvarInstance(fvar_Instance *inst) {
13 iVV.dispose(&inst->coordinates);
14 }
15 caryll_standardType(fvar_Instance, fvar_iInstance, initFvarInstance, disposeFvarInstance);
16 caryll_standardVectorImpl(fvar_InstanceList, fvar_Instance, fvar_iInstance, fvar_iInstanceList);
17 // table fvar
18
19 static INLINE void disposeFvarMaster(fvar_Master *m) {
20 sdsfree(m->name);
21 vq_deleteRegion(m->region);
22 }
23
24 static INLINE void initFvar(table_fvar *fvar) {
25 memset(fvar, 0, sizeof(*fvar));
26 vf_iAxes.init(&fvar->axes);
27 fvar_iInstanceList.init(&fvar->instances);
28 }
29 static INLINE void disposeFvar(table_fvar *fvar) {
30 vf_iAxes.dispose(&fvar->axes);
31 fvar_iInstanceList.dispose(&fvar->instances);
32
33 fvar_Master *current, *tmp;
34 HASH_ITER(hh, fvar->masters, current, tmp) {
35 HASH_DEL(fvar->masters, current);
36 disposeFvarMaster(current);
37 FREE(current);
38 }
39 }
40
41 static const vq_Region *fvar_registerRegion(table_fvar *fvar, MOVE vq_Region *region) {
42 fvar_Master *m = NULL;
43 HASH_FIND(hh, fvar->masters, region, VQ_REGION_SIZE(region->dimensions), m);
44 if (m) {
45 vq_deleteRegion(region);
46 return m->region;
47 } else {
48 NEW_CLEAN_1(m);
49 sds sMasterID = sdsfromlonglong(1 + HASH_CNT(hh, fvar->masters));
50 m->name = sdscatsds(sdsnew("m"), sMasterID);
51 sdsfree(sMasterID);
52 m->region = region;
53 HASH_ADD_KEYPTR(hh, fvar->masters, m->region, VQ_REGION_SIZE(region->dimensions), m);
54 return m->region;
55 }
56 }
57
58 static const fvar_Master *fvar_findMasterByRegion(const table_fvar *fvar, const vq_Region *region) {
59 fvar_Master *m = NULL;
60 HASH_FIND(hh, fvar->masters, region, VQ_REGION_SIZE(region->dimensions), m);
61 return m;
62 }
63
64 caryll_standardRefTypeFn(table_fvar, initFvar, disposeFvar);
65 caryll_ElementInterfaceOf(table_fvar) table_iFvar = {caryll_standardRefTypeMethods(table_fvar),
66 .registerRegion = fvar_registerRegion,
67 .findMasterByRegion = fvar_findMasterByRegion};
68
69 // Local typedefs for parsing axis record
70 #pragma pack(1)
71 struct FVARHeader {
72 uint16_t majorVersion;
73 uint16_t minorVersion;
74 uint16_t axesArrayOffset;
75 uint16_t reserved1;
76 uint16_t axisCount;
77 uint16_t axisSize;
78 uint16_t instanceCount;
79 uint16_t instanceSize;
80 };
81
82 struct VariationAxisRecord {
83 uint32_t axisTag;
84 f16dot16 minValue;
85 f16dot16 defaultValue;
86 f16dot16 maxValue;
87 uint16_t flags;
88 uint16_t axisNameID;
89 };
90
91 struct InstanceRecord {
92 uint16_t subfamilyNameID;
93 uint16_t flags;
94 f16dot16 coordinates[];
95 };
96 #pragma pack()
97
98 table_fvar *otfcc_readFvar(const otfcc_Packet packet, const otfcc_Options *options) {
99 table_fvar *fvar = NULL;
100 FOR_TABLE(OTFCC_CHR('f','v','a','r'), table) {
101 font_file_pointer data = table.data;
102 if (table.length < sizeof(struct FVARHeader)) goto FAIL;
103
104 struct FVARHeader *header = (struct FVARHeader *)data;
105 if (be16(header->majorVersion) != 1) goto FAIL;
106 if (be16(header->minorVersion) != 0) goto FAIL;
107 if (be16(header->axesArrayOffset) == 0) goto FAIL;
108 if (be16(header->axisCount) == 0) goto FAIL;
109 if (be16(header->axisSize) != sizeof(struct VariationAxisRecord)) goto FAIL;
110 uint16_t nAxes = be16(header->axisCount);
111 uint16_t instanceSizeWithoutPSNID = 4 + nAxes * sizeof(f16dot16);
112 uint16_t instanceSizeWithPSNID = 2 + instanceSizeWithoutPSNID;
113 if (be16(header->instanceSize) != instanceSizeWithoutPSNID &&
114 be16(header->instanceSize) != instanceSizeWithPSNID)
115 goto FAIL;
116 if (table.length < be16(header->axesArrayOffset) +
117 sizeof(struct VariationAxisRecord) * nAxes +
118 be16(header->instanceSize) * be16(header->instanceCount))
119 goto FAIL;
120
121 fvar = table_iFvar.create();
122
123 // parse axes
124 struct VariationAxisRecord *axisRecord =
125 (struct VariationAxisRecord *)(data + be16(header->axesArrayOffset));
126 for (uint16_t j = 0; j < nAxes; j++) {
127 vf_Axis axis = {.tag = be32(axisRecord->axisTag),
128 .minValue = otfcc_from_fixed(be32(axisRecord->minValue)),
129 .defaultValue = otfcc_from_fixed(be32(axisRecord->defaultValue)),
130 .maxValue = otfcc_from_fixed(be32(axisRecord->maxValue)),
131 .flags = be16(axisRecord->flags),
132 .axisNameID = be16(axisRecord->axisNameID)};
133 vf_iAxes.push(&fvar->axes, axis);
134 axisRecord++;
135 }
136
137 // parse instances
138 uint16_t nInstances = be16(header->instanceCount);
139 bool hasPostscriptNameID = be16(header->instanceSize) == instanceSizeWithPSNID;
140 struct InstanceRecord *instance = (struct InstanceRecord *)axisRecord;
141 for (uint16_t j = 0; j < nInstances; j++) {
142 fvar_Instance inst;
143 fvar_iInstance.init(&inst);
144 inst.subfamilyNameID = be16(instance->subfamilyNameID);
145 inst.flags = be16(instance->flags);
146 for (uint16_t k = 0; k < nAxes; k++) {
147 iVV.push(&inst.coordinates, otfcc_from_fixed(be32(instance->coordinates[k])));
148 }
149 iVV.shrinkToFit(&inst.coordinates);
150 if (hasPostscriptNameID) {
151 inst.postScriptNameID =
152 be16(*(uint16_t *)(((font_file_pointer)instance) + instanceSizeWithoutPSNID));
153 }
154 fvar_iInstanceList.push(&fvar->instances, inst);
155 instance = (struct InstanceRecord *)(((font_file_pointer)instance) +
156 be16(header->instanceSize));
157 }
158 vf_iAxes.shrinkToFit(&fvar->axes);
159 fvar_iInstanceList.shrinkToFit(&fvar->instances);
160
161 return fvar;
162
163 FAIL:
164 logWarning("table 'fvar' corrupted.\n");
165 table_iFvar.free(fvar);
166 fvar = NULL;
167 }
168 return NULL;
169 }
170
171 void otfcc_dumpFvar(const table_fvar *table, json_value *root, const otfcc_Options *options) {
172 if (!table) return;
173 loggedStep("fvar") {
174 json_value *t = json_object_new(2);
175 // dump axes
176 json_value *_axes = json_object_new(table->axes.length);
177 foreach (vf_Axis *axis, table->axes) {
178 json_value *_axis = json_object_new(5);
179 json_object_push(_axis, "minValue", json_double_new(axis->minValue));
180 json_object_push(_axis, "defaultValue", json_double_new(axis->defaultValue));
181 json_object_push(_axis, "maxValue", json_double_new(axis->maxValue));
182 json_object_push(_axis, "flags", json_integer_new(axis->flags));
183 json_object_push(_axis, "axisNameID", json_integer_new(axis->axisNameID));
184 json_object_push_tag(_axes, axis->tag, _axis);
185 }
186 json_object_push(t, "axes", _axes);
187 // dump instances
188 json_value *_instances = json_array_new(table->instances.length);
189 foreach (fvar_Instance *instance, table->instances) {
190 json_value *_instance = json_object_new(4);
191 json_object_push(_instance, "subfamilyNameID",
192 json_integer_new(instance->subfamilyNameID));
193 if (instance->postScriptNameID) {
194 json_object_push(_instance, "postScriptNameID",
195 json_integer_new(instance->postScriptNameID));
196 }
197 json_object_push(_instance, "flags", json_integer_new(instance->flags));
198 json_object_push(_instance, "coordinates", json_new_VVp(&instance->coordinates, table));
199 json_array_push(_instances, _instance);
200 }
201 json_object_push(t, "instances", _instances);
202 // dump masters
203 json_value *_masters = json_object_new(HASH_COUNT(table->masters));
204 fvar_Master *current, *tmp;
205 HASH_ITER(hh, table->masters, current, tmp) {
206 json_object_push(_masters, current->name,
207 preserialize(json_new_VQRegion_Explicit(current->region, table)));
208 }
209 json_object_push(t, "masters", _masters);
210 json_object_push(root, "fvar", t);
211 }
212 }
213
214 // JSON conversion functions
215 // dump
216
217 static json_value *json_new_VQSegment(const vq_Segment *s, const table_fvar *fvar) {
218 switch (s->type) {
219 case VQ_STILL:;
220 return json_new_position(s->val.still);
221 case VQ_DELTA:;
222 json_value *d = json_object_new(3);
223 json_object_push(d, "delta", json_new_position(s->val.delta.quantity));
224 if (!s->val.delta.touched) {
225 json_object_push(d, "implicit", json_boolean_new(!s->val.delta.touched));
226 }
227 json_object_push(d, "on", json_new_VQRegion(s->val.delta.region, fvar));
228 return d;
229 default:;
230 return json_integer_new(0);
231 }
232 }
233 json_value *json_new_VQ(const VQ z, const table_fvar *fvar) {
234
235 if (!z.shift.length) {
236 return preserialize(json_new_position(iVQ.getStill(z)));
237 } else {
238 json_value *a = json_array_new(z.shift.length + 1);
239 json_array_push(a, json_new_position(z.kernel));
240 for (size_t j = 0; j < z.shift.length; j++) {
241 json_array_push(a, json_new_VQSegment(&z.shift.items[j], fvar));
242 }
243 return preserialize(a);
244 }
245 }
246
247 json_value *json_new_VV(const VV x, const table_fvar *fvar) {
248 const vf_Axes *axes = &fvar->axes;
249 if (axes && axes->length == x.length) {
250 json_value *_coord = json_object_new(axes->length);
251 for (size_t m = 0; m < x.length; m++) {
252 vf_Axis *axis = &axes->items[m];
253 char tag[4] = {(axis->tag & 0xff000000) >> 24, (axis->tag & 0xff0000) >> 16,
254 (axis->tag & 0xff00) >> 8, (axis->tag & 0xff)};
255 json_object_push_length(_coord, 4, tag, json_new_position(x.items[m]));
256 }
257 return preserialize(_coord);
258 } else {
259 json_value *_coord = json_array_new(x.length);
260 for (size_t m = 0; m < x.length; m++) {
261 json_array_push(_coord, json_new_position(x.items[m]));
262 }
263 return preserialize(_coord);
264 }
265 }
266 json_value *json_new_VVp(const VV *x, const table_fvar *fvar) {
267 const vf_Axes *axes = &fvar->axes;
268
269 if (axes && axes->length == x->length) {
270 json_value *_coord = json_object_new(axes->length);
271 for (size_t m = 0; m < x->length; m++) {
272 vf_Axis *axis = &axes->items[m];
273 char tag[4] = {(axis->tag & 0xff000000) >> 24, (axis->tag & 0xff0000) >> 16,
274 (axis->tag & 0xff00) >> 8, (axis->tag & 0xff)};
275 json_object_push_length(_coord, 4, tag, json_new_position(x->items[m]));
276 }
277 return preserialize(_coord);
278
279 } else {
280 json_value *_coord = json_array_new(x->length);
281 for (size_t m = 0; m < x->length; m++) {
282 json_array_push(_coord, json_new_position(x->items[m]));
283 }
284 return preserialize(_coord);
285 }
286 }
287 // parse
288 VQ json_vqOf(const json_value *cv, const table_fvar *fvar) {
289 return iVQ.createStill(json_numof(cv));
290 }
291
292 static json_value *json_new_VQAxisSpan(const vq_AxisSpan *s) {
293 if (vq_AxisSpanIsOne(s)) {
294 return json_string_new("*");
295 } else {
296 json_value *a = json_object_new(3);
297 json_object_push(a, "start", json_new_position(s->start));
298 json_object_push(a, "peak", json_new_position(s->peak));
299 json_object_push(a, "end", json_new_position(s->end));
300 return a;
301 }
302 }
303 json_value *json_new_VQRegion_Explicit(const vq_Region *rs, const table_fvar *fvar) {
304 const vf_Axes *axes = &fvar->axes;
305 if (axes && axes->length == rs->dimensions) {
306 json_value *r = json_object_new(rs->dimensions);
307 for (size_t j = 0; j < rs->dimensions; j++) {
308 json_object_push_tag(r, axes->items[j].tag, json_new_VQAxisSpan(&rs->spans[j]));
309 }
310 return r;
311 } else {
312 json_value *r = json_array_new(rs->dimensions);
313 for (size_t j = 0; j < rs->dimensions; j++) {
314 json_array_push(r, json_new_VQAxisSpan(&rs->spans[j]));
315 }
316 return r;
317 }
318 }
319 json_value *json_new_VQRegion(const vq_Region *rs, const table_fvar *fvar) {
320 const fvar_Master *m = table_iFvar.findMasterByRegion(fvar, rs);
321 if (m && m->name) {
322 return json_string_new_length((unsigned int)sdslen(m->name), m->name);
323 } else {
324 return json_new_VQRegion_Explicit(rs, fvar);
325 }
326 }