#include "cmp.h"
#include <fontconfig/fontconfig.h>
#include <assert.h>
FcCharSet *decodeCharSet(cmp_ctx_t *bytes) {
uint32_t size;
if (!cmp_read_array(bytes, &size)) return NULL;
FcCharSet *ret = FcCharSetCreate();
if (ret == NULL) return NULL;
FcChar32 prev = 0;
for (uint32_t i = 0; i < size; i++) {
uint32_t x;
if (!cmp_read_uint(bytes, &x)) goto fail;
prev += x;
if (!FcCharSetAddChar(ret, prev)) goto fail;
}
return ret;
fail:
FcCharSetDestroy(ret);
return NULL;
}
bool encodeCharSet(cmp_ctx_t *bytes, FcCharSet *data) {
FcChar32 size = FcCharSetCount(data);
FcChar32 count = 0; // For validation
if (!cmp_write_array(bytes, size)) return false;
FcChar32 map[FC_CHARSET_MAP_SIZE];
FcChar32 next;
FcChar32 c = FcCharSetFirstPage(data, map, &next);
FcChar32 prev = 0;
while (c != FC_CHARSET_DONE) {
if (!cmp_write_uinteger(bytes, c - prev)) return false;
prev = c;
count++;
c = FcCharSetNextPage(data, map, &next);
}
assert(size == count);
return true;
}