w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

COLR.c
Go to the documentation of this file.
1 #include "COLR.h"
2 
3 #include "support/util.h"
4 #include "bk/bkgraph.h"
5 
6 
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 }
15  Handle.dispose(&layer->glyph);
16 }
18 caryll_standardVectorImpl(colr_LayerList, colr_Layer, colr_iLayer, colr_iLayerList);
19 
21  Handle.init(&mapping->glyph);
22  colr_iLayerList.init(&mapping->layers);
23 }
25  Handle.copy(&dst->glyph, &src->glyph);
26  colr_iLayerList.copy(&dst->layers, &src->layers);
27 }
29  Handle.dispose(&mapping->glyph);
30  colr_iLayerList.dispose(&mapping->layers);
31 }
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;
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++) {
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 }
static INLINE void disposeMapping(colr_Mapping *mapping)
Definition: COLR.c:28
static int byGID(const colr_Mapping *a, const colr_Mapping *b)
Definition: COLR.c:148
static const size_t layerRecLength
Definition: COLR.c:36
static INLINE void copyLayer(colr_Layer *dst, const colr_Layer *src)
Definition: COLR.c:10
static INLINE void initLayer(colr_Layer *layer)
Definition: COLR.c:7
caryll_Buffer * otfcc_buildCOLR(const table_COLR *_colr, const otfcc_Options *options)
Definition: COLR.c:152
static INLINE void disposeLayer(colr_Layer *layer)
Definition: COLR.c:14
table_COLR * otfcc_parseCOLR(const json_value *root, const otfcc_Options *options)
Definition: COLR.c:113
caryll_standardType(colr_Layer, colr_iLayer, initLayer, copyLayer, disposeLayer)
static INLINE void copyMapping(colr_Mapping *dst, const colr_Mapping *src)
Definition: COLR.c:24
table_COLR * otfcc_readCOLR(const otfcc_Packet packet, const otfcc_Options *options)
Definition: COLR.c:38
static const size_t baseGlyphRecLength
Definition: COLR.c:35
static INLINE void initMapping(colr_Mapping *mapping)
Definition: COLR.c:20
void otfcc_dumpCOLR(const table_COLR *colr, json_value *root, const otfcc_Options *options)
Definition: COLR.c:92
caryll_standardVectorImpl(colr_LayerList, colr_Layer, colr_iLayer, colr_iLayerList)
static uint32_t read_32u(const uint8_t *src)
Definition: bin-io.h:132
static uint16_t read_16u(const uint8_t *src)
Definition: bin-io.h:121
bk_Block * bk_new_Block(int type0,...)
Definition: bkblock.c:72
bk_Block * bk_push(bk_Block *b, int type0,...)
Definition: bkblock.c:81
@ b16
Definition: bkblock.h:17
@ p32
Definition: bkblock.h:20
@ bkover
Definition: bkblock.h:15
caryll_Buffer * bk_build_Block(bk_Block *root)
Definition: bkgraph.c:396
#define OTFCC_CHR(a, b, c, d)
Definition: caryll-font.c:7
#define b
Definition: jpegint.h:372
#define a(n)
Definition: gpos-common.c:148
json_value * json_array_new(size_t length)
json_value * json_array_push(json_value *array, json_value *)
json_value * json_string_new(const char *)
json_value * json_object_push(json_value *object, const char *name, json_value *)
json_value * json_integer_new(int64_t)
json_value * json_object_new(size_t length)
static json_value * preserialize(MOVE json_value *x)
Definition: json-funcs.h:187
static json_value * json_obj_get_type(const json_value *obj, const char *key, const json_type type)
Definition: json-funcs.h:34
#define NULL
Definition: ftobjs.h:61
#define NEW
Definition: gdkanji.c:77
unsigned short uint16_t
Definition: stdint.h:79
unsigned int uint32_t
Definition: stdint.h:80
#define INLINE
Definition: port.h:26
#define root
Definition: ctangleboot.c:69
#define logWarning(...)
Definition: aliases.h:14
#define Handle
Definition: aliases.h:45
#define FOR_TABLE(name, table)
Definition: aliases.h:33
#define loggedStep(...)
Definition: aliases.h:6
int k
Definition: otp-parser.c:70
int colors
Definition: ppmtogif.c:51
uint16_t glyphid_t
Definition: primitives.h:14
uint16_t colorid_t
Definition: primitives.h:18
@ json_string
Definition: json.h:89
@ json_array
Definition: json.h:86
@ json_object
Definition: json.h:85
sds sdsnewlen(const void *init, size_t initlen)
json_type type
Definition: json.h:113
char * ptr
Definition: json.h:124
unsigned int length
Definition: json.h:123
json_object_entry * values
Definition: json.h:132
union _json_value::@1795 u
struct _json_value::@1795::@1799 array
struct _json_value::@1795::@1797 string
Definition: COLR.h:7
Definition: table.h:30
int j
Definition: t4ht.c:1589
m
Definition: tex4ht.c:3990
int mapping
Definition: ttf2pfb.c:116
#define FAIL(ec)