| 1 |
#include "CPAL.h" |
| 2 |
|
| 3 |
#include "support/util.h" |
| 4 |
#include "bk/bkgraph.h" |
| 5 |
|
| 6 |
|
| 7 |
// Colors are trivial. |
| 8 |
caryll_standardType(cpal_Color, cpal_iColor); |
| 9 |
caryll_standardVectorImpl(cpal_ColorSet, cpal_Color, cpal_iColor, cpal_iColorSet); |
| 10 |
|
| 11 |
// Palettes are non-trivial. |
| 12 |
static INLINE void initPalette(cpal_Palette *p) { |
| 13 |
cpal_iColorSet.init(&p->colorset); |
| 14 |
p->type = 0; |
| 15 |
p->label = 0xFFFF; |
| 16 |
} |
| 17 |
static INLINE void disposePalette(cpal_Palette *p) { |
| 18 |
cpal_iColorSet.dispose(&p->colorset); |
| 19 |
} |
| 20 |
caryll_standardType(cpal_Palette, cpal_iPalette, initPalette, disposePalette); |
| 21 |
caryll_standardVectorImpl(cpal_PaletteSet, cpal_Palette, cpal_iPalette, cpal_iPaletteSet); |
| 22 |
|
| 23 |
// CPAL table |
| 24 |
static INLINE void initCPAL(table_CPAL *cpal) { |
| 25 |
cpal->version = 1; |
| 26 |
cpal_iPaletteSet.init(&cpal->palettes); |
| 27 |
} |
| 28 |
static INLINE void disposeCPAL(table_CPAL *cpal) { |
| 29 |
cpal_iPaletteSet.dispose(&cpal->palettes); |
| 30 |
} |
| 31 |
caryll_standardRefType(table_CPAL, table_iCPAL, initCPAL, disposeCPAL); |
| 32 |
|
| 33 |
const cpal_Color white = {.red = 0xFF, .green = 0xFF, .blue = 0xFF, .alpha = 0xFF, .label = 0xFFFF}; |
| 34 |
|
| 35 |
table_CPAL *otfcc_readCPAL(const otfcc_Packet packet, const otfcc_Options *options) { |
| 36 |
table_CPAL *t = NULL; |
| 37 |
FOR_TABLE(OTFCC_CHR('C','P','A','L'), table) { |
| 38 |
font_file_pointer data = table.data; |
| 39 |
uint32_t length = table.length; |
| 40 |
if (length < 2) goto FAIL; |
| 41 |
t = table_iCPAL.create(); |
| 42 |
uint16_t version = read_16u(data); |
| 43 |
uint32_t tableHeaderLength = (version == 0 ? 14 : 26); |
| 44 |
if (length < tableHeaderLength) goto FAIL; |
| 45 |
|
| 46 |
t->version = version; |
| 47 |
uint16_t numPalettesEntries = read_16u(data + 2); |
| 48 |
uint16_t numPalettes = read_16u(data + 4); |
| 49 |
uint16_t numColorRecords = read_16u(data + 6); |
| 50 |
uint32_t offsetFirstColorRecord = read_32u(data + 8); |
| 51 |
if (length < offsetFirstColorRecord + numColorRecords * 4) goto FAIL; |
| 52 |
if (length < tableHeaderLength + 2 * numPalettes) goto FAIL; |
| 53 |
|
| 54 |
// read color list |
| 55 |
cpal_Color *colorList; |
| 56 |
NEW(colorList, numColorRecords); |
| 57 |
for (uint16_t j = 0; j < numColorRecords; j++) { |
| 58 |
colorList[j] = (cpal_Color){.blue = read_8u(data + offsetFirstColorRecord + j * 4), |
| 59 |
.green = read_8u(data + offsetFirstColorRecord + j * 4 + 1), |
| 60 |
.red = read_8u(data + offsetFirstColorRecord + j * 4 + 2), |
| 61 |
.alpha = read_8u(data + offsetFirstColorRecord + j * 4 + 3), |
| 62 |
.label = 0xFFFF}; |
| 63 |
} |
| 64 |
|
| 65 |
// read palettes |
| 66 |
for (tableid_t j = 0; j < numPalettes; j++) { |
| 67 |
cpal_Palette palette; |
| 68 |
cpal_iPalette.init(&palette); |
| 69 |
tableid_t paletteStartIndex = read_16u(data + 12 + j * 2); |
| 70 |
for (colorid_t j = 0; j < numPalettesEntries; j++) { |
| 71 |
if (paletteStartIndex + j < numColorRecords) { |
| 72 |
cpal_iColorSet.push(&palette.colorset, colorList[j + paletteStartIndex]); |
| 73 |
} else { |
| 74 |
cpal_iColorSet.push(&palette.colorset, white); |
| 75 |
} |
| 76 |
} |
| 77 |
cpal_iPaletteSet.push(&t->palettes, palette); |
| 78 |
} |
| 79 |
|
| 80 |
if (version > 0) { |
| 81 |
uint32_t offsetPaletteTypeArray = read_32u(data + 16 + 2 * numPalettes); |
| 82 |
if (offsetPaletteTypeArray && length >= offsetPaletteTypeArray + 4 * numPalettes) { |
| 83 |
for (tableid_t j = 0; j < numPalettes; j++) { |
| 84 |
uint32_t type = read_32u(data + j * 4 + offsetPaletteTypeArray); |
| 85 |
t->palettes.items[j].type = type; |
| 86 |
} |
| 87 |
} |
| 88 |
uint32_t offsetPaletteLabelArray = read_32u(data + 20 + 2 * numPalettes); |
| 89 |
if (offsetPaletteLabelArray && length >= offsetPaletteLabelArray + 2 * numPalettes) { |
| 90 |
for (tableid_t j = 0; j < numPalettes; j++) { |
| 91 |
uint16_t label = read_16u(data + j * 2 + offsetPaletteLabelArray); |
| 92 |
t->palettes.items[j].label = label; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (version > 0) { |
| 97 |
uint32_t offsetPaletteEntryLabelArray = read_32u(data + 24 + 2 * numPalettes); |
| 98 |
if (offsetPaletteEntryLabelArray && |
| 99 |
length >= offsetPaletteEntryLabelArray + 4 * numPalettesEntries) { |
| 100 |
for (colorid_t j = 0; j < numPalettesEntries; j++) { |
| 101 |
uint16_t label = read_16u(data + j * 2 + offsetPaletteEntryLabelArray); |
| 102 |
for (tableid_t k = 0; k < numPalettes; k++) { |
| 103 |
t->palettes.items[k].colorset.items[j].label = label; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
FREE(colorList); |
| 111 |
return t; |
| 112 |
|
| 113 |
FAIL: |
| 114 |
table_iCPAL.free(t); |
| 115 |
t = NULL; |
| 116 |
} |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
static INLINE json_value *dumpColor(cpal_Color *color) { |
| 121 |
json_value *_color = json_object_new(5); |
| 122 |
json_object_push(_color, "red", json_integer_new(color->red)); |
| 123 |
json_object_push(_color, "green", json_integer_new(color->green)); |
| 124 |
json_object_push(_color, "blue", json_integer_new(color->blue)); |
| 125 |
if (color->alpha != 0xFF) json_object_push(_color, "alpha", json_integer_new(color->alpha)); |
| 126 |
if (color->label != 0xFFFF) json_object_push(_color, "label", json_integer_new(color->label)); |
| 127 |
return preserialize(_color); |
| 128 |
} |
| 129 |
|
| 130 |
static INLINE json_value *dumpPalette(cpal_Palette *palette) { |
| 131 |
json_value *_palette = json_object_new(3); |
| 132 |
if (palette->type) { json_object_push(_palette, "type", json_integer_new(palette->type)); } |
| 133 |
if (palette->label != 0xFFFF) { |
| 134 |
json_object_push(_palette, "label", json_integer_new(palette->label)); |
| 135 |
} |
| 136 |
json_value *a = json_array_new(palette->colorset.length); |
| 137 |
for (colorid_t j = 0; j < palette->colorset.length; j++) { |
| 138 |
json_array_push(a, dumpColor(&palette->colorset.items[j])); |
| 139 |
} |
| 140 |
json_object_push(_palette, "colors", a); |
| 141 |
return _palette; |
| 142 |
} |
| 143 |
|
| 144 |
void otfcc_dumpCPAL(const table_CPAL *table, json_value *root, const otfcc_Options *options) { |
| 145 |
if (!table) return; |
| 146 |
loggedStep("CPAL") { |
| 147 |
json_value *_t = json_object_new(2); |
| 148 |
json_object_push(_t, "version", json_integer_new(table->version)); |
| 149 |
json_value *_a = json_array_new(table->palettes.length); |
| 150 |
for (tableid_t j = 0; j < table->palettes.length; j++) { |
| 151 |
json_array_push(_a, dumpPalette(&table->palettes.items[j])); |
| 152 |
} |
| 153 |
json_object_push(_t, "palettes", _a); |
| 154 |
json_object_push(root, "CPAL", _t); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static INLINE cpal_Color parseColor(const json_value *_color) { |
| 159 |
cpal_Color color = white; |
| 160 |
if (!_color || _color->type != json_object) return color; |
| 161 |
color.red = json_obj_getint_fallback(_color, "red", 0); |
| 162 |
color.green = json_obj_getint_fallback(_color, "green", 0); |
| 163 |
color.blue = json_obj_getint_fallback(_color, "blue", 0); |
| 164 |
color.alpha = json_obj_getint_fallback(_color, "alpha", 0xFF); |
| 165 |
color.label = json_obj_getint_fallback(_color, "label", 0xFFFF); |
| 166 |
return color; |
| 167 |
} |
| 168 |
|
| 169 |
table_CPAL *otfcc_parseCPAL(const json_value *root, const otfcc_Options *options) { |
| 170 |
json_value *table = NULL; |
| 171 |
if (!(table = json_obj_get_type(root, "CPAL", json_object))) return NULL; |
| 172 |
table_CPAL *cpal = NULL; |
| 173 |
loggedStep("CPAL") { |
| 174 |
json_value *_palettes = json_obj_get_type(table, "palettes", json_array); |
| 175 |
if (!_palettes || !_palettes->u.array.length) return NULL; |
| 176 |
cpal = table_iCPAL.create(); |
| 177 |
cpal->version = json_obj_getint(table, "version"); |
| 178 |
for (tableid_t j = 0; j < _palettes->u.array.length; j++) { |
| 179 |
json_value *_palette = _palettes->u.array.values[j]; |
| 180 |
if (!_palette || _palette->type != json_object) continue; |
| 181 |
json_value *_colors = json_obj_get_type(_palette, "colors", json_array); |
| 182 |
if (!_colors) continue; |
| 183 |
|
| 184 |
// palette parser |
| 185 |
cpal_Palette palette; |
| 186 |
cpal_iPalette.init(&palette); |
| 187 |
palette.type = json_obj_getint(_palette, "type"); |
| 188 |
palette.label = json_obj_getint_fallback(_palette, "type", 0xFFFF); |
| 189 |
|
| 190 |
// color list parser |
| 191 |
for (colorid_t k = 0; k < _colors->u.array.length; k++) { |
| 192 |
cpal_iColorSet.push(&palette.colorset, parseColor(_colors->u.array.values[k])); |
| 193 |
} |
| 194 |
|
| 195 |
// push into cpal |
| 196 |
cpal_iPaletteSet.push(&cpal->palettes, palette); |
| 197 |
} |
| 198 |
} |
| 199 |
return cpal; |
| 200 |
} |
| 201 |
|
| 202 |
static INLINE bk_Block *buildPaletteType(const table_CPAL *cpal) { |
| 203 |
bool needsPaletteType = false; |
| 204 |
for (tableid_t j = 0; j < cpal->palettes.length; j++) { |
| 205 |
if (cpal->palettes.items[j].type) needsPaletteType = true; |
| 206 |
} |
| 207 |
if (!needsPaletteType) return NULL; |
| 208 |
bk_Block *block = bk_new_Block(bkover); |
| 209 |
for (tableid_t j = 0; j < cpal->palettes.length; j++) { |
| 210 |
bk_push(block, b32, cpal->palettes.items[j].type, bkover); |
| 211 |
} |
| 212 |
return block; |
| 213 |
} |
| 214 |
static INLINE bk_Block *buildPaletteLabel(const table_CPAL *cpal) { |
| 215 |
bool needsPaletteLabel = false; |
| 216 |
for (tableid_t j = 0; j < cpal->palettes.length; j++) { |
| 217 |
if (cpal->palettes.items[j].label != 0xFFFF) needsPaletteLabel = true; |
| 218 |
} |
| 219 |
if (!needsPaletteLabel) return NULL; |
| 220 |
bk_Block *block = bk_new_Block(bkover); |
| 221 |
for (tableid_t j = 0; j < cpal->palettes.length; j++) { |
| 222 |
bk_push(block, b16, cpal->palettes.items[j].label, bkover); |
| 223 |
} |
| 224 |
return block; |
| 225 |
} |
| 226 |
static INLINE bk_Block *buildPaletteEntryLabel(const table_CPAL *cpal) { |
| 227 |
bool needsPaletteEntryLabel = false; |
| 228 |
cpal_Palette *palette = &cpal->palettes.items[0]; |
| 229 |
for (colorid_t j = 0; j < palette->colorset.length; j++) { |
| 230 |
if (palette->colorset.items[j].label != 0xFFFF) needsPaletteEntryLabel = true; |
| 231 |
} |
| 232 |
if (!needsPaletteEntryLabel) return NULL; |
| 233 |
bk_Block *block = bk_new_Block(bkover); |
| 234 |
for (colorid_t j = 0; j < palette->colorset.length; j++) { |
| 235 |
bk_push(block, b16, palette->colorset.items[j].label, bkover); |
| 236 |
} |
| 237 |
return block; |
| 238 |
} |
| 239 |
caryll_Buffer *otfcc_buildCPAL(const table_CPAL *cpal, const otfcc_Options *options) { |
| 240 |
if (!cpal || !cpal->palettes.length) return NULL; |
| 241 |
uint16_t numPalettes = cpal->palettes.length; |
| 242 |
uint16_t numPalettesEntries = cpal->palettes.items[0].colorset.length; |
| 243 |
uint16_t numColorRecords = numPalettes * numPalettesEntries; |
| 244 |
|
| 245 |
bk_Block *colorRecords = bk_new_Block(bkover); |
| 246 |
for (tableid_t j = 0; j < numPalettes; j++) { |
| 247 |
cpal_Palette *palette = &cpal->palettes.items[j]; |
| 248 |
colorid_t totalColors = palette->colorset.length; |
| 249 |
for (colorid_t k = 0; k < numPalettesEntries; k++) { |
| 250 |
const cpal_Color *color = NULL; |
| 251 |
if (k < totalColors) { |
| 252 |
color = &palette->colorset.items[k]; |
| 253 |
} else { |
| 254 |
color = &white; |
| 255 |
} |
| 256 |
bk_push(colorRecords, b8, color->blue, // blue |
| 257 |
b8, color->green, // green |
| 258 |
b8, color->red, // red |
| 259 |
b8, color->alpha, // alpha |
| 260 |
bkover); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
bk_Block *root = bk_new_Block(b16, cpal->version, // Version |
| 265 |
b16, numPalettesEntries, // numPalettesEntries |
| 266 |
b16, numPalettes, // numPalettes |
| 267 |
b16, numColorRecords, // numColorRecords |
| 268 |
p32, colorRecords, // offsetFirstColorRecord |
| 269 |
bkover); |
| 270 |
for (tableid_t j = 0; j < numPalettes; j++) { |
| 271 |
bk_push(root, b16, numPalettesEntries * j, bkover); // colorRecordIndices |
| 272 |
} |
| 273 |
if (cpal->version > 0) { |
| 274 |
bk_push(root, p32, buildPaletteType(cpal), // offsetPaletteTypeArray |
| 275 |
p32, buildPaletteLabel(cpal), // offsetPaletteLabelArray |
| 276 |
p32, buildPaletteEntryLabel(cpal), // offsetPaletteEntryLabelArray |
| 277 |
bkover); |
| 278 |
} |
| 279 |
return bk_build_Block(root); |
| 280 |
} |