~alcinnz/fontconfig-pure

d242b0657c8f076ccb7ae8cc6954c0731dbf74fa — Adrian Cochrane 8 months ago 677feaa
Continue implementing C-side language bindings.
2 files changed, 61 insertions(+), 3 deletions(-)

M cbits/transcode.c
M fontconfig-pure.cabal
M cbits/transcode.c => cbits/transcode.c +60 -2
@@ 373,6 373,27 @@ fail:
    return NULL;
}

FcFontSet **decodeFontSets(cmp_ctx_t *bytes, size_t *nsets) {
    if (bytes == NULL) return NULL;

    uint32_t size;
    if (!cmp_read_array(bytes, &size)) return NULL;
    if (nsets != NULL) nsets = size;

    FcFontSet **ret = calloc(sizeof(FcFontSet *), size);
    uint32_t i;
    for (i = 0; i < size; i++) {
        ret[i] = decodeFontSet(bytes);
        if (ret[i] == NULL) goto fail;
        i++;
    }
    return ret;
fail:
    for (uint32_t j = 0; j < i; j++) FcFontSetDestroy(ret[j]);
    free(ret);
    return NULL;
}

bool encodeFontSet(cmp_ctx_t *bytes, FcFontSet *data) {
    if (bytes == NULL || data == NULL) return NULL;



@@ 383,6 404,23 @@ bool encodeFontSet(cmp_ctx_t *bytes, FcFontSet *data) {
    return true;
}

bool encodeResult(cmp_ctx_t *bytes, FcResult res) {
    switch (res) {
    case FcResultMatch: // Should be handled by caller! Can't do anything sensible here.
    case FcResultNoMatch: // May be handled by caller.
        return cmp_write_nil(bytes);
    case FcResultTypeMismatch:
        return cmp_write_str(bytes, "ErrType", strlen("ErrType"));
    case FcResultNoId:
        return cmp_write_str(bytes, "ErrNoId", strlen("ErrNoId"));
    case FcResultOutOfMemory:
        return cmp_write_str(bytes, "ErrOOM", strlen("ErrOOM"));
    default:
        // Should never happen!
        return cmp_write_str(bytes, "ErrOther", strlen("ErrOther"));
    }
}

struct bytes {
    uint8_t *start;
    size_t offset;


@@ 406,18 444,30 @@ bool bytes_skipper(struct cmp_ctx_s *ctx, size_t count) {

size_t bytes_writer(struct cmp_ctx_s *ctx, const void *data, size_t count) {
    struct bytes *bytes = ctx->buf;
    if (bytes->offset + count > bytes->length) return 0;
    if (bytes->offset + count > bytes->length) {
        bytes->start = realloc(bytes->start, 2*bytes->length);
        if (bytes->start == NULL) return 0;
        bytes->length = 2*bytes->length;
    }
    memcpy(bytes->start + bytes->offset, data, count);
    bytes->offset += count;
    return count;
}

void cmp_bytes_init(cmp_ctx_t *ctx, uint8_t *buf, size_t length) {
bool cmp_bytes_init(cmp_ctx_t *ctx, uint8_t *buf, size_t length) {
    struct bytes *inner = malloc(sizeof(struct bytes));
    if (inner == NULL) return false;
    inner->start = buf;
    inner->offset = 0;
    inner->length = length;
    cmp_init(ctx, inner, bytes_reader, bytes_skipper, bytes_writer);
    return true;
}

bool cmp_bytes_alloc(cmp_ctx_t *ctx, size_t length) {
    struct uint8_t *bytes = malloc(length);
    if (bytes == NULL) return false;
    return cmp_bytes_init(ctx, bytes, length);
}

void cmp_bytes_free(cmp_ctx_t *ctx) {


@@ 425,3 475,11 @@ void cmp_bytes_free(cmp_ctx_t *ctx) {
    free(bytes->start);
    free(bytes);
}

uint8_t *cmp_bytes_take(cmp_ctx_t *ctx, size_t *length) {
    struct bytes *bytes = ctx->buf;
    uint8_t *ret = bytes->start;
    if (length != NULL) *length = bytes->offset;
    free(bytes);
    return ret;
}

M fontconfig-pure.cabal => fontconfig-pure.cabal +1 -1
@@ 68,7 68,7 @@ library
            Graphics.Text.Font.Choose.StrSet, Graphics.Text.Font.Choose.Value,
            Graphics.Text.Font.Choose.Pattern, Graphics.Text.Font.Choose.FontSet

    c-sources: cbits/cmp.c, cbits/transcode.c
    c-sources: cbits/cmp.c, cbits/transcode.c, cbits/freetype-wrap.c

    -- Modules included in this library but not exported.
    -- other-modules: