~alcinnz/fontconfig-pure

ref: 35586a37128df60a71044132e409ccaede29c5b9 fontconfig-pure/cbits/transcode.c -rw-r--r-- 1.1 KiB
35586a37 — Adrian Cochrane Encode & decode charsets on C side (fix Haskell side) 10 months ago
                                                                                
35586a37 Adrian Cochrane
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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;
}