Debian Codegraph @

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

raw
1 #include "../glyf.h"
2
3 #include "support/util.h"
4 #include "support/ttinstr/ttinstr.h"
5
6
7 // point
8 static void createPoint(glyf_Point *p) {
9 p->x = iVQ.createStill(0);
10 p->y = iVQ.createStill(0);
11 p->onCurve = true;
12 }
13 static void copyPoint(glyf_Point *dst, const glyf_Point *src) {
14 iVQ.copy(&dst->x, &src->x);
15 iVQ.copy(&dst->y, &src->y);
16 dst->onCurve = src->onCurve;
17 }
18 static void disposePoint(glyf_Point *p) {
19 iVQ.dispose(&p->x);
20 iVQ.dispose(&p->y);
21 }
22
23 caryll_standardValType(glyf_Point, glyf_iPoint, createPoint, copyPoint, disposePoint);
24
25 // contour
26 caryll_standardVectorImpl(glyf_Contour, glyf_Point, glyf_iPoint, glyf_iContour);
27 caryll_standardVectorImpl(glyf_ContourList, glyf_Contour, glyf_iContour, glyf_iContourList);
28
29 // ref
30 static INLINE void initGlyfReference(glyf_ComponentReference *ref) {
31 ref->glyph = Handle.empty();
32 ref->x = iVQ.createStill(0);
33 ref->y = iVQ.createStill(0);
34 ref->a = 1;
35 ref->b = 0;
36 ref->c = 0;
37 ref->d = 1;
38 ref->isAnchored = REF_XY;
39 ref->inner = ref->outer = 0;
40 ref->roundToGrid = false;
41 ref->useMyMetrics = false;
42 }
43 static void copyGlyfReference(glyf_ComponentReference *dst, const glyf_ComponentReference *src) {
44 iVQ.copy(&dst->x, &src->x);
45 iVQ.copy(&dst->y, &src->y);
46 Handle.copy(&dst->glyph, &src->glyph);
47 dst->a = src->a;
48 dst->b = src->b;
49 dst->c = src->c;
50 dst->d = src->d;
51 dst->isAnchored = src->isAnchored;
52 dst->inner = src->inner;
53 dst->outer = src->outer;
54 dst->roundToGrid = src->roundToGrid;
55 dst->useMyMetrics = src->useMyMetrics;
56 }
57 static INLINE void disposeGlyfReference(glyf_ComponentReference *ref) {
58 iVQ.dispose(&ref->x);
59 iVQ.dispose(&ref->y);
60 Handle.dispose(&ref->glyph);
61 }
62 caryll_standardValType(glyf_ComponentReference, glyf_iComponentReference, initGlyfReference,
63 copyGlyfReference, disposeGlyfReference);
64 caryll_standardVectorImpl(glyf_ReferenceList, glyf_ComponentReference, glyf_iComponentReference,
65 glyf_iReferenceList);
66
67 // stem
68 caryll_standardType(glyf_PostscriptStemDef, glyf_iPostscriptStemDef);
69 caryll_standardVectorImpl(glyf_StemDefList, glyf_PostscriptStemDef, glyf_iPostscriptStemDef,
70 glyf_iStemDefList);
71
72 // mask
73 caryll_standardType(glyf_PostscriptHintMask, glyf_iPostscriptHintMask);
74 caryll_standardVectorImpl(glyf_MaskList, glyf_PostscriptHintMask, glyf_iPostscriptHintMask,
75 glyf_iMaskList);
76
77 glyf_Glyph *otfcc_newGlyf_glyph() {
78 glyf_Glyph *g;
79 NEW(g);
80 g->name = NULL;
81 iVQ.init(&g->horizontalOrigin);
82 iVQ.init(&g->advanceWidth);
83 iVQ.init(&g->verticalOrigin);
84 iVQ.init(&g->advanceHeight);
85
86 glyf_iContourList.init(&g->contours);
87 glyf_iReferenceList.init(&g->references);
88 glyf_iStemDefList.init(&g->stemH);
89 glyf_iStemDefList.init(&g->stemV);
90 glyf_iMaskList.init(&g->hintMasks);
91 glyf_iMaskList.init(&g->contourMasks);
92
93 g->instructionsLength = 0;
94 g->instructions = NULL;
95 g->fdSelect = Handle.empty();
96 g->yPel = 0;
97
98 g->stat.xMin = 0;
99 g->stat.xMax = 0;
100 g->stat.yMin = 0;
101 g->stat.yMax = 0;
102 g->stat.nestDepth = 0;
103 g->stat.nPoints = 0;
104 g->stat.nContours = 0;
105 g->stat.nCompositePoints = 0;
106 g->stat.nCompositeContours = 0;
107 return g;
108 }
109 static void otfcc_deleteGlyf_glyph(glyf_Glyph *g) {
110 if (!g) return;
111 iVQ.dispose(&g->horizontalOrigin);
112 iVQ.dispose(&g->advanceWidth);
113 iVQ.dispose(&g->verticalOrigin);
114 iVQ.dispose(&g->advanceHeight);
115 sdsfree(g->name);
116 glyf_iContourList.dispose(&g->contours);
117 glyf_iReferenceList.dispose(&g->references);
118 glyf_iStemDefList.dispose(&g->stemH);
119 glyf_iStemDefList.dispose(&g->stemV);
120 glyf_iMaskList.dispose(&g->hintMasks);
121 glyf_iMaskList.dispose(&g->contourMasks);
122 if (g->instructions) { FREE(g->instructions); }
123 Handle.dispose(&g->fdSelect);
124 g->name = NULL;
125 FREE(g);
126 }
127
128 static INLINE void initGlyfPtr(glyf_GlyphPtr *g) {
129 *g = NULL;
130 }
131 static void copyGlyfPtr(glyf_GlyphPtr *dst, const glyf_GlyphPtr *src) {
132 *dst = *src;
133 }
134 static INLINE void disposeGlyfPtr(glyf_GlyphPtr *g) {
135 otfcc_deleteGlyf_glyph(*g);
136 }
137 caryll_ElementInterfaceOf(glyf_GlyphPtr) glyf_iGlyphPtr = {
138 .init = initGlyfPtr,
139 .copy = copyGlyfPtr,
140 .dispose = disposeGlyfPtr,
141 };
142 caryll_standardVectorImpl(table_glyf, glyf_GlyphPtr, glyf_iGlyphPtr, table_iGlyf);
143
144 // to json
145 static void glyf_glyph_dump_contours(glyf_Glyph *g, json_value *target, const GlyfIOContext *ctx) {
146 if (!g->contours.length) return;
147 json_value *contours = json_array_new(g->contours.length);
148 for (shapeid_t k = 0; k < g->contours.length; k++) {
149 glyf_Contour *c = &(g->contours.items[k]);
150 json_value *contour = json_array_new(c->length);
151 for (shapeid_t m = 0; m < c->length; m++) {
152 json_value *point = json_object_new(4);
153 json_object_push(point, "x", json_new_VQ(c->items[m].x, ctx->fvar));
154 json_object_push(point, "y", json_new_VQ(c->items[m].y, ctx->fvar));
155 json_object_push(point, "on", json_boolean_new(c->items[m].onCurve & MASK_ON_CURVE));
156 json_array_push(contour, point);
157 }
158 json_array_push(contours, preserialize(contour));
159 }
160 json_object_push(target, "contours", contours);
161 }
162
163 static void glyf_glyph_dump_references(glyf_Glyph *g, json_value *target,
164 const GlyfIOContext *ctx) {
165 if (!g->references.length) return;
166 json_value *references = json_array_new(g->references.length);
167 for (shapeid_t k = 0; k < g->references.length; k++) {
168 glyf_ComponentReference *r = &(g->references.items[k]);
169 json_value *ref = json_object_new(9);
170 json_object_push(ref, "glyph",
171 json_string_new_length((uint32_t)sdslen(r->glyph.name), r->glyph.name));
172 json_object_push(ref, "x", json_new_VQ(r->x, ctx->fvar));
173 json_object_push(ref, "y", json_new_VQ(r->y, ctx->fvar));
174 json_object_push(ref, "a", json_new_position(r->a));
175 json_object_push(ref, "b", json_new_position(r->b));
176 json_object_push(ref, "c", json_new_position(r->c));
177 json_object_push(ref, "d", json_new_position(r->d));
178 if (r->isAnchored != REF_XY) {
179 json_object_push(ref, "isAnchored", json_boolean_new(true));
180 json_object_push(ref, "inner", json_integer_new(r->inner));
181 json_object_push(ref, "outer", json_integer_new(r->outer));
182 }
183 if (r->roundToGrid) { json_object_push(ref, "roundToGrid", json_boolean_new(true)); }
184 if (r->useMyMetrics) { json_object_push(ref, "useMyMetrics", json_boolean_new(true)); }
185 json_array_push(references, preserialize(ref));
186 }
187 json_object_push(target, "references", references);
188 }
189 static json_value *glyf_glyph_dump_stemdefs(glyf_StemDefList *stems) {
190 json_value *a = json_array_new(stems->length);
191 for (shapeid_t j = 0; j < stems->length; j++) {
192 json_value *stem = json_object_new(3);
193 json_object_push(stem, "position", json_new_position(stems->items[j].position));
194 json_object_push(stem, "width", json_new_position(stems->items[j].width));
195 json_array_push(a, stem);
196 }
197 return a;
198 }
199 static json_value *glyf_glyph_dump_maskdefs(glyf_MaskList *masks, glyf_StemDefList *hh,
200 glyf_StemDefList *vv) {
201 json_value *a = json_array_new(masks->length);
202 for (shapeid_t j = 0; j < masks->length; j++) {
203 json_value *mask = json_object_new(3);
204 json_object_push(mask, "contoursBefore", json_integer_new(masks->items[j].contoursBefore));
205 json_object_push(mask, "pointsBefore", json_integer_new(masks->items[j].pointsBefore));
206 json_value *h = json_array_new(hh->length);
207 for (shapeid_t k = 0; k < hh->length; k++) {
208 json_array_push(h, json_boolean_new(masks->items[j].maskH[k]));
209 }
210 json_object_push(mask, "maskH", h);
211 json_value *v = json_array_new(vv->length);
212 for (shapeid_t k = 0; k < vv->length; k++) {
213 json_array_push(v, json_boolean_new(masks->items[j].maskV[k]));
214 }
215 json_object_push(mask, "maskV", v);
216 json_array_push(a, mask);
217 }
218 return a;
219 }
220
221 static json_value *glyf_dump_glyph(glyf_Glyph *g, const otfcc_Options *options,
222 const GlyfIOContext *ctx) {
223 json_value *glyph = json_object_new(12);
224 json_object_push(glyph, "advanceWidth", json_new_VQ(g->advanceWidth, ctx->fvar));
225 if (iVQ.isStill(g->horizontalOrigin) &&
226 fabs(iVQ.getStill(g->horizontalOrigin)) > 1.0 / 1000.0) {
227 json_object_push(glyph, "horizontalOrigin", json_new_VQ(g->horizontalOrigin, ctx->fvar));
228 }
229 if (ctx->hasVerticalMetrics) {
230 json_object_push(glyph, "advanceHeight", json_new_VQ(g->advanceHeight, ctx->fvar));
231 json_object_push(glyph, "verticalOrigin", json_new_VQ(g->verticalOrigin, ctx->fvar));
232 }
233 glyf_glyph_dump_contours(g, glyph, ctx);
234 glyf_glyph_dump_references(g, glyph, ctx);
235 if (ctx->exportFDSelect) {
236 json_object_push(glyph, "CFF_fdSelect", json_string_new(g->fdSelect.name));
237 json_object_push(glyph, "CFF_CID", json_integer_new(g->cid));
238 }
239
240 // hinting data
241 if (!options->ignore_hints) {
242 if (g->instructions && g->instructionsLength) {
243 json_object_push(glyph, "instructions",
244 dump_ttinstr(g->instructions, g->instructionsLength, options));
245 }
246 if (g->stemH.length) {
247 json_object_push(glyph, "stemH", preserialize(glyf_glyph_dump_stemdefs(&g->stemH)));
248 }
249 if (g->stemV.length) {
250 json_object_push(glyph, "stemV", preserialize(glyf_glyph_dump_stemdefs(&g->stemV)));
251 }
252 if (g->hintMasks.length) {
253 json_object_push(
254 glyph, "hintMasks",
255 preserialize(glyf_glyph_dump_maskdefs(&g->hintMasks, &g->stemH, &g->stemV)));
256 }
257 if (g->contourMasks.length) {
258 json_object_push(
259 glyph, "contourMasks",
260 preserialize(glyf_glyph_dump_maskdefs(&g->contourMasks, &g->stemH, &g->stemV)));
261 }
262 if (g->yPel) { json_object_push(glyph, "LTSH_yPel", json_integer_new(g->yPel)); }
263 }
264 return glyph;
265 }
266 static void otfcc_dump_glyphorder(const table_glyf *table, json_value *root) {
267 if (!table) return;
268 json_value *order = json_array_new(table->length);
269 for (glyphid_t j = 0; j < table->length; j++) {
270 json_array_push(order, json_string_new_length((uint32_t)sdslen(table->items[j]->name),
271 table->items[j]->name));
272 }
273 json_object_push(root, "glyph_order", preserialize(order));
274 }
275 void otfcc_dumpGlyf(const table_glyf *table, json_value *root, const otfcc_Options *options,
276 const GlyfIOContext *ctx) {
277 if (!table) return;
278 loggedStep("glyf") {
279 json_value *glyf = json_object_new(table->length);
280 for (glyphid_t j = 0; j < table->length; j++) {
281 glyf_Glyph *g = table->items[j];
282 json_object_push(glyf, g->name, glyf_dump_glyph(g, options, ctx));
283 }
284 json_object_push(root, "glyf", glyf);
285 if (!options->ignore_glyph_order) otfcc_dump_glyphorder(table, root);
286 }
287 }
288
289 // from json
290 static glyf_Point glyf_parse_point(json_value *pointdump) {
291 glyf_Point point;
292 glyf_iPoint.init(&point);
293 if (!pointdump || pointdump->type != json_object) return point;
294 for (uint32_t _k = 0; _k < pointdump->u.object.length; _k++) {
295 char *ck = pointdump->u.object.values[_k].name;
296 json_value *cv = pointdump->u.object.values[_k].value;
297 if (strcmp(ck, "x") == 0) {
298 iVQ.replace(&point.x, json_vqOf(cv, NULL));
299 } else if (strcmp(ck, "y") == 0) {
300 iVQ.replace(&point.y, json_vqOf(cv, NULL));
301 } else if (strcmp(ck, "on") == 0) {
302 point.onCurve = json_boolof(cv);
303 }
304 }
305 return point;
306 }
307
308 static void glyf_parse_contours(json_value *col, glyf_Glyph *g) {
309 if (!col) { return; }
310 shapeid_t nContours = col->u.array.length;
311 for (shapeid_t j = 0; j < nContours; j++) {
312 json_value *contourdump = col->u.array.values[j];
313 glyf_Contour contour;
314 glyf_iContour.initCapN(&contour, contourdump && contourdump->type == json_array
315 ? contourdump->u.array.length
316 : 1);
317 if (contourdump && contourdump->type == json_array) {
318 for (shapeid_t k = 0; k < contourdump->u.array.length; k++) {
319 glyf_iContour.push(&contour, glyf_parse_point(contourdump->u.array.values[k]));
320 }
321 }
322 glyf_iContourList.push(&g->contours, contour);
323 }
324 }
325
326 static glyf_ComponentReference glyf_parse_reference(json_value *refdump) {
327 json_value *_gname = json_obj_get_type(refdump, "glyph", json_string);
328 glyf_ComponentReference ref = glyf_iComponentReference.empty();
329 if (_gname) {
330 ref.glyph = Handle.fromName(sdsnewlen(_gname->u.string.ptr, _gname->u.string.length));
331 iVQ.replace(&ref.x, json_vqOf(json_obj_get(refdump, "x"), NULL));
332 iVQ.replace(&ref.y, json_vqOf(json_obj_get(refdump, "y"), NULL));
333 ref.a = json_obj_getnum_fallback(refdump, "a", 1.0);
334 ref.b = json_obj_getnum_fallback(refdump, "b", 0.0);
335 ref.c = json_obj_getnum_fallback(refdump, "c", 0.0);
336 ref.d = json_obj_getnum_fallback(refdump, "d", 1.0);
337 ref.roundToGrid = json_obj_getbool(refdump, "roundToGrid");
338 ref.useMyMetrics = json_obj_getbool(refdump, "useMyMetrics");
339 if (json_obj_getbool(refdump, "isAnchored")) {
340 ref.isAnchored = REF_ANCHOR_XY;
341 ref.inner = json_obj_getint(refdump, "inner");
342 ref.outer = json_obj_getint(refdump, "outer");
343 }
344 } else {
345 // Invalid glyph references
346 ref.glyph.name = NULL;
347 iVQ.replace(&ref.x, iVQ.createStill(0));
348 iVQ.replace(&ref.y, iVQ.createStill(0));
349 ref.a = 1.0;
350 ref.b = 0.0;
351 ref.c = 0.0;
352 ref.d = 1.0;
353 ref.roundToGrid = false;
354 ref.useMyMetrics = false;
355 }
356 return ref;
357 }
358 static void glyf_parse_references(json_value *col, glyf_Glyph *g) {
359 if (!col) { return; }
360 for (shapeid_t j = 0; j < col->u.array.length; j++) {
361 glyf_iReferenceList.push(&g->references, glyf_parse_reference(col->u.array.values[j]));
362 }
363 }
364
365 static void makeInstrsForGlyph(void *_g, uint8_t *instrs, uint32_t len) {
366 glyf_Glyph *g = (glyf_Glyph *)_g;
367 g->instructionsLength = len;
368 g->instructions = instrs;
369 }
370 static void wrongInstrsForGlyph(void *_g, const char *reason, int pos) {
371 glyf_Glyph *g = (glyf_Glyph *)_g;
372 fprintf(stderr, "[OTFCC] TrueType instructions parse error : %s, at %d in /%s\n", reason, pos,
373 g->name);
374 }
375
376 static void parse_stems(json_value *sd, glyf_StemDefList *stems) {
377 if (!sd) return;
378 for (shapeid_t j = 0; j < sd->u.array.length; j++) {
379 json_value *s = sd->u.array.values[j];
380 if (s->type != json_object) continue;
381 glyf_PostscriptStemDef sdef;
382 sdef.map = 0;
383 sdef.position = json_obj_getnum(s, "position");
384 sdef.width = json_obj_getnum(s, "width");
385 glyf_iStemDefList.push(stems, sdef);
386 }
387 }
388 static void parse_maskbits(bool *arr, json_value *bits) {
389 if (!bits) {
390 for (shapeid_t j = 0; j < 0x100; j++) {
391 arr[j] = false;
392 }
393 } else {
394 for (shapeid_t j = 0; j < 0x100 && j < bits->u.array.length; j++) {
395 json_value *b = bits->u.array.values[j];
396 switch (b->type) {
397 case json_boolean:
398 arr[j] = b->u.boolean;
399 break;
400 case json_integer:
401 arr[j] = b->u.integer;
402 break;
403 case json_double:
404 arr[j] = b->u.dbl;
405 break;
406 default:
407 arr[j] = false;
408 }
409 }
410 }
411 }
412 static void parse_masks(json_value *md, glyf_MaskList *masks) {
413 if (!md) return;
414 for (shapeid_t j = 0; j < md->u.array.length; j++) {
415 json_value *m = md->u.array.values[j];
416 if (m->type != json_object) continue;
417
418 glyf_PostscriptHintMask mask;
419 mask.pointsBefore = json_obj_getint(m, "pointsBefore");
420 mask.contoursBefore = json_obj_getint(m, "contoursBefore");
421 parse_maskbits(&(mask.maskH[0]), json_obj_get_type(m, "maskH", json_array));
422 parse_maskbits(&(mask.maskV[0]), json_obj_get_type(m, "maskV", json_array));
423 glyf_iMaskList.push(masks, mask);
424 }
425 }
426
427 static glyf_Glyph *otfcc_glyf_parse_glyph(json_value *glyphdump, otfcc_GlyphOrderEntry *order_entry,
428 const otfcc_Options *options) {
429 glyf_Glyph *g = otfcc_newGlyf_glyph();
430 g->name = sdsdup(order_entry->name);
431 iVQ.replace(&g->advanceWidth, json_vqOf(json_obj_get(glyphdump, "advanceWidth"), NULL));
432 iVQ.replace(&g->horizontalOrigin, json_vqOf(json_obj_get(glyphdump, "horizontalOrigin"), NULL));
433 iVQ.replace(&g->advanceHeight, json_vqOf(json_obj_get(glyphdump, "advanceHeight"), NULL));
434 iVQ.replace(&g->verticalOrigin, json_vqOf(json_obj_get(glyphdump, "verticalOrigin"), NULL));
435 glyf_parse_contours(json_obj_get_type(glyphdump, "contours", json_array), g);
436 glyf_parse_references(json_obj_get_type(glyphdump, "references", json_array), g);
437 if (!options->ignore_hints) {
438 parse_ttinstr(json_obj_get(glyphdump, "instructions"), g, makeInstrsForGlyph,
439 wrongInstrsForGlyph);
440 parse_stems(json_obj_get_type(glyphdump, "stemH", json_array), &g->stemH);
441 parse_stems(json_obj_get_type(glyphdump, "stemV", json_array), &g->stemV);
442 parse_masks(json_obj_get_type(glyphdump, "hintMasks", json_array), &g->hintMasks);
443 parse_masks(json_obj_get_type(glyphdump, "contourMasks", json_array), &g->contourMasks);
444 g->yPel = json_obj_getint(glyphdump, "LTSH_yPel");
445 }
446 // Glyph data of other tables
447 g->fdSelect = Handle.fromName(json_obj_getsds(glyphdump, "CFF_fdSelect"));
448 if (!g->yPel) { g->yPel = json_obj_getint(glyphdump, "yPel"); }
449 return g;
450 }
451
452 table_glyf *otfcc_parseGlyf(const json_value *root, otfcc_GlyphOrder *glyph_order,
453 const otfcc_Options *options) {
454 if (root->type != json_object || !glyph_order) return NULL;
455 table_glyf *glyf = NULL;
456 json_value *table;
457 if ((table = json_obj_get_type(root, "glyf", json_object))) {
458 loggedStep("glyf") {
459 glyphid_t numGlyphs = table->u.object.length;
460 glyf = table_iGlyf.createN(numGlyphs);
461 for (glyphid_t j = 0; j < numGlyphs; j++) {
462 sds gname = sdsnewlen(table->u.object.values[j].name,
463 table->u.object.values[j].name_length);
464 json_value *glyphdump = table->u.object.values[j].value;
465 otfcc_GlyphOrderEntry *order_entry = NULL;
466 HASH_FIND(hhName, glyph_order->byName, gname, sdslen(gname), order_entry);
467 if (glyphdump->type == json_object && order_entry &&
468 !glyf->items[order_entry->gid]) {
469 glyf->items[order_entry->gid] =
470 otfcc_glyf_parse_glyph(glyphdump, order_entry, options);
471 }
472 json_value_free(glyphdump);
473 json_value *v = json_null_new();
474 v->parent = table;
475 table->u.object.values[j].value = v;
476 sdsfree(gname);
477 }
478 }
479 return glyf;
480 }
481 return NULL;
482 }