Debian Codegraph @

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

raw
1 #include "COLR.h"
2
3 #include "support/util.h"
4 #include "bk/bkgraph.h"
5
6
7 static INLINE void initLayer(colr_Layer *layer) {
8 Handle.init(&layer->glyph);
9 }
10 static INLINE void copyLayer(colr_Layer *dst, const colr_Layer *src) {
11 Handle.copy(&dst->glyph, &src->glyph);
12 dst->paletteIndex = src->paletteIndex;
13 }
14 static INLINE void disposeLayer(colr_Layer *layer) {
15 Handle.dispose(&layer->glyph);
16 }
17 caryll_standardType(colr_Layer, colr_iLayer, initLayer, copyLayer, disposeLayer);
18 caryll_standardVectorImpl(colr_LayerList, colr_Layer, colr_iLayer, colr_iLayerList);
19
20 static INLINE void initMapping(colr_Mapping *mapping) {
21 Handle.init(&mapping->glyph);
22 colr_iLayerList.init(&mapping->layers);
23 }
24 static INLINE void copyMapping(colr_Mapping *dst, const colr_Mapping *src) {
25 Handle.copy(&dst->glyph, &src->glyph);
26 colr_iLayerList.copy(&dst->layers, &src->layers);
27 }
28 static INLINE void disposeMapping(colr_Mapping *mapping) {
29 Handle.dispose(&mapping->glyph);
30 colr_iLayerList.dispose(&mapping->layers);
31 }
32 caryll_standardType(colr_Mapping, colr_iMapping, initMapping, copyMapping, disposeMapping);
33 caryll_standardVectorImpl(table_COLR, colr_Mapping, colr_iMapping, table_iCOLR);
34
35 static const size_t baseGlyphRecLength = 6;
36 static const size_t layerRecLength = 4;
37
38 table_COLR *otfcc_readCOLR(const otfcc_Packet packet, const otfcc_Options *options) {
39 table_COLR *colr = NULL;
40 FOR_TABLE(OTFCC_CHR('C','O','L','R'), table) {
41 if (table.length < 14) goto FAIL;
42 uint16_t numBaseGlyphRecords = read_16u(table.data + 2);
43 uint16_t numLayerRecords = read_16u(table.data + 12);
44 uint32_t offsetBaseGlyphRecord = read_32u(table.data + 4);
45 uint32_t offsetLayerRecord = read_32u(table.data + 8);
46 if (table.length < offsetBaseGlyphRecord + baseGlyphRecLength * numBaseGlyphRecords)
47 goto FAIL;
48 if (table.length < offsetLayerRecord + layerRecLength * numLayerRecords) goto FAIL;
49
50 // parse layer data
51 glyphid_t *gids;
52 colorid_t *colors;
53 NEW(gids, numLayerRecords);
54 NEW(colors, numLayerRecords);
55 for (glyphid_t j = 0; j < numLayerRecords; j++) {
56 gids[j] = read_16u(table.data + offsetLayerRecord + layerRecLength * j);
57 colors[j] = read_16u(table.data + offsetLayerRecord + layerRecLength * j + 2);
58 }
59 // parse decomposition data
60 colr = table_iCOLR.create();
61 for (glyphid_t j = 0; j < numBaseGlyphRecords; j++) {
62 colr_Mapping mapping;
63 colr_iMapping.init(&mapping);
64 uint16_t gid = read_16u(table.data + offsetBaseGlyphRecord + baseGlyphRecLength * j);
65 uint16_t firstLayerIndex =
66 read_16u(table.data + offsetBaseGlyphRecord + baseGlyphRecLength * j + 2);
67 uint16_t numLayers =
68 read_16u(table.data + offsetBaseGlyphRecord + baseGlyphRecLength * j + 4);
69
70 glyph_handle baseGlyph = Handle.fromIndex(gid);
71 Handle.move(&mapping.glyph, &baseGlyph);
72 for (glyphid_t k = 0; k < numLayers; k++) {
73 if (k + firstLayerIndex < numLayerRecords) {
74 colr_iLayerList.push(&mapping.layers,
75 (colr_Layer){
76 .glyph = Handle.fromIndex(gids[k + firstLayerIndex]),
77 .paletteIndex = colors[k + firstLayerIndex],
78 });
79 }
80 }
81 table_iCOLR.push(colr, mapping);
82 }
83 return colr;
84 FAIL:
85 logWarning("Table 'COLR' corrupted.\n");
86 table_iCOLR.free(colr);
87 colr = NULL;
88 }
89 return colr;
90 }
91
92 void otfcc_dumpCOLR(const table_COLR *colr, json_value *root, const otfcc_Options *options) {
93 if (!colr) return;
94 loggedStep("COLR") {
95 json_value *_colr = json_array_new(colr->length);
96 foreach (colr_Mapping *mapping, *colr) {
97 json_value *_map = json_object_new(2);
98 json_object_push(_map, "from", json_string_new(mapping->glyph.name));
99 json_value *_layers = json_array_new(mapping->layers.length);
100 foreach (colr_Layer *layer, mapping->layers) {
101 json_value *_layer = json_object_new(2);
102 json_object_push(_layer, "layer", json_string_new(layer->glyph.name));
103 json_object_push(_layer, "paletteIndex", json_integer_new(layer->paletteIndex));
104 json_array_push(_layers, _layer);
105 }
106 json_object_push(_map, "to", preserialize(_layers));
107 json_array_push(_colr, _map);
108 }
109 json_object_push(root, "COLR", _colr);
110 }
111 }
112
113 table_COLR *otfcc_parseCOLR(const json_value *root, const otfcc_Options *options) {
114 json_value *_colr = NULL;
115 if (!(_colr = json_obj_get_type(root, "COLR", json_array))) return NULL;
116 table_COLR *colr = table_iCOLR.create();
117 loggedStep("COLR") {
118 for (glyphid_t j = 0; j < _colr->u.array.length; j++) {
119 json_value *_mapping = _colr->u.array.values[j];
120 if (!_mapping || _mapping->type != json_object) continue;
121 json_value *_baseglyph = json_obj_get_type(_mapping, "from", json_string);
122 json_value *_layers = json_obj_get_type(_mapping, "to", json_array);
123 if (!_baseglyph || !_layers) continue;
124
125 colr_Mapping m;
126 colr_iMapping.init(&m);
127 m.glyph =
128 Handle.fromName(sdsnewlen(_baseglyph->u.string.ptr, _baseglyph->u.string.length));
129 for (glyphid_t k = 0; k < _layers->u.array.length; k++) {
130 json_value *_layer = _layers->u.array.values[k];
131 if (!_layer || _layer->type != json_object) continue;
132 json_value *_layerglyph = json_obj_get_type(_layer, "layer", json_string);
133 if (!_layerglyph) continue;
134 colr_iLayerList.push(
135 &m.layers,
136 (colr_Layer){
137 .glyph = Handle.fromName(
138 sdsnewlen(_layerglyph->u.string.ptr, _layerglyph->u.string.length)),
139 .paletteIndex = json_obj_getint_fallback(_layer, "paletteIndex", 0xFFFF),
140 });
141 }
142 table_iCOLR.push(colr, m);
143 }
144 }
145 return colr;
146 }
147
148 static int byGID(const colr_Mapping *a, const colr_Mapping *b) {
149 return a->glyph.index - b->glyph.index;
150 }
151
152 caryll_Buffer *otfcc_buildCOLR(const table_COLR *_colr, const otfcc_Options *options) {
153 if (!_colr || !_colr->length) return NULL;
154
155 // sort base defs
156 table_COLR colr;
157 table_iCOLR.copy(&colr, _colr);
158 table_iCOLR.sort(&colr, byGID);
159
160 glyphid_t currentLayerIndex = 0;
161 bk_Block *layerRecords = bk_new_Block(bkover);
162 bk_Block *baseRecords = bk_new_Block(bkover);
163 foreach (colr_Mapping *mapping, colr) {
164 bk_push(baseRecords, b16, mapping->glyph.index, // GID
165 b16, currentLayerIndex, // firstLayerIndex
166 b16, mapping->layers.length, // numLayers
167 bkover);
168 foreach (colr_Layer *layer, mapping->layers) {
169 bk_push(layerRecords, b16, layer->glyph.index, // GID
170 b16, layer->paletteIndex, // paletteIndex
171 bkover);
172 currentLayerIndex += 1;
173 }
174 }
175
176 bk_Block *root = bk_new_Block(b16, 0, // Version
177 b16, colr.length, // numBaseGlyphRecords
178 p32, baseRecords, // offsetBaseGlyphRecord
179 p32, layerRecords, // offsetLayerRecord
180 b16, currentLayerIndex, // numLayerRecords
181 bkover);
182 table_iCOLR.dispose(&colr);
183 return bk_build_Block(root);
184 }