Debian Codegraph @

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

raw
1 #include "SVG.h"
2
3 #include "support/util.h"
4 #include "bk/bkgraph.h"
5
6
7 static INLINE void initSVGAssigment(svg_Assignment *a) {
8 memset(a, 0, sizeof(*a));
9 }
10 static INLINE void copySVGAssigment(svg_Assignment *dst, const svg_Assignment *src) {
11 dst->start = src->start;
12 dst->end = src->end;
13 dst->document = bufnew();
14 bufwrite_buf(dst->document, src->document);
15 }
16 static INLINE void disposeSVGAssignment(svg_Assignment *a) {
17 buffree(a->document);
18 }
19 caryll_standardValType(svg_Assignment, svg_iAssignment, initSVGAssigment, copySVGAssigment,
20 disposeSVGAssignment);
21 caryll_standardVectorImpl(table_SVG, svg_Assignment, svg_iAssignment, table_iSVG);
22
23 table_SVG *otfcc_readSVG(const otfcc_Packet packet, const otfcc_Options *options) {
24 table_SVG *svg = NULL;
25 FOR_TABLE(OTFCC_CHR('S','V','G',' '), table) {
26 if (table.length < 10) goto FAIL;
27 uint32_t offsetToSVGDocIndex = read_32u(table.data + 2);
28 if (table.length < offsetToSVGDocIndex + 2) goto FAIL;
29 uint16_t numEntries = read_16u(table.data + offsetToSVGDocIndex);
30 if (table.length < offsetToSVGDocIndex + 2 + 12 * numEntries) goto FAIL;
31
32 svg = table_iSVG.create();
33 for (glyphid_t j = 0; j < numEntries; j++) {
34 font_file_pointer record = table.data + offsetToSVGDocIndex + 2 + 12 * j;
35 svg_Assignment asg = svg_iAssignment.empty();
36 asg.start = read_16u(record);
37 asg.end = read_16u(record + 2);
38 uint32_t docstart = read_32u(record + 4);
39 uint32_t doclen = read_32u(record + 8);
40 if (offsetToSVGDocIndex + docstart + doclen <= table.length) {
41 asg.document = bufnew();
42 bufwrite_bytes(asg.document, doclen, table.data + offsetToSVGDocIndex + docstart);
43 } else {
44 asg.document = bufnew();
45 }
46 table_iSVG.push(svg, asg);
47 }
48 return svg;
49 FAIL:
50 table_iSVG.dispose(svg);
51 svg = NULL;
52 }
53 return svg;
54 }
55
56 static bool canUsePlainFormat(const caryll_Buffer *buf) {
57 return (buf->size > 4 && buf->data[0] == '<' && buf->data[1] == 's' && buf->data[2] == 'v' &&
58 buf->data[3] == 'g') // <svg
59 || (buf->size > 5 && buf->data[0] == '<' && buf->data[1] == '?' && buf->data[2] == 'x' &&
60 buf->data[3] == 'm' && buf->data[4] == 'l');
61 }
62 void otfcc_dumpSVG(const table_SVG *svg, json_value *root, const otfcc_Options *options) {
63 if (!svg) return;
64 loggedStep("SVG ") {
65 json_value *_svg = json_array_new(svg->length);
66 foreach (svg_Assignment *a, *svg) {
67 json_value *_a = json_object_new(4);
68 json_object_push(_a, "start", json_integer_new(a->start));
69 json_object_push(_a, "end", json_integer_new(a->end));
70 if (canUsePlainFormat(a->document)) {
71 json_object_push(_a, "format", json_string_new("plain"));
72 json_object_push(
73 _a, "document",
74 json_string_new_length((uint32_t)a->document->size, (char *)a->document->data));
75 } else {
76 size_t len = 0;
77 uint8_t *buf = base64_encode(a->document->data, a->document->size, &len);
78 json_object_push(_a, "format", json_string_new("base64"));
79 json_object_push(_a, "document",
80 json_string_new_length((uint32_t)len, (char *)buf));
81 FREE(buf);
82 }
83 json_array_push(_svg, _a);
84 }
85 json_object_push(root, "SVG_", _svg);
86 }
87 }
88
89 table_SVG *otfcc_parseSVG(const json_value *root, const otfcc_Options *options) {
90 json_value *_svg = NULL;
91 if (!(_svg = json_obj_get_type(root, "SVG_", json_array))) return NULL;
92 table_SVG *svg = table_iSVG.create();
93 loggedStep("SVG ") {
94 for (glyphid_t j = 0; j < _svg->u.array.length; j++) {
95 json_value *_a = _svg->u.array.values[j];
96 if (!_a || _a->type != json_object) continue;
97 const char *format = json_obj_getstr_share(_a, "format");
98 sds doc = json_obj_getsds(_a, "document");
99 if (!format || !doc) continue;
100 svg_Assignment asg = svg_iAssignment.empty();
101 asg.start = json_obj_getint(_a, "start");
102 asg.end = json_obj_getint(_a, "end");
103
104 if (strcmp(format, "plain") == 0) {
105 asg.document = bufnew();
106 bufwrite_bytes(asg.document, sdslen(doc), (uint8_t *)doc);
107 sdsfree(doc);
108 } else {
109 asg.document = bufnew();
110 size_t len = 0;
111 uint8_t *buf = base64_encode((uint8_t *)doc, sdslen(doc), &len);
112 bufwrite_bytes(asg.document, len, buf);
113 FREE(buf);
114 sdsfree(doc);
115 }
116 table_iSVG.push(svg, asg);
117 }
118 }
119 return svg;
120 }
121
122 static int byStartGID(const svg_Assignment *a, const svg_Assignment *b) {
123 return a->start - b->start;
124 }
125
126 caryll_Buffer *otfcc_buildSVG(const table_SVG *_svg, const otfcc_Options *options) {
127 if (!_svg || !_svg->length) return NULL;
128
129 // sort assignments
130 table_SVG svg;
131 table_iSVG.copy(&svg, _svg);
132 table_iSVG.sort(&svg, byStartGID);
133
134 bk_Block *major = bk_new_Block(b16, svg.length, // numEntries
135 bkover);
136 foreach (svg_Assignment *a, svg) {
137 bk_push(major, // SVG Document Index Entry
138 b16, a->start, // startGlyphID
139 b16, a->end, // endGlyphID
140 p32, bk_newBlockFromBufferCopy(a->document), // svgDocOffset
141 b32, a->document->size, // svgDocLength
142 bkover);
143 }
144 bk_Block *root = bk_new_Block(b16, 0, // Version
145 p32, major, // offsetToSVGDocIndex
146 b32, 0, // Reserved
147 bkover);
148 table_iSVG.dispose(&svg);
149 return bk_build_Block(root);
150 }