From 35586a37128df60a71044132e409ccaede29c5b9 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Wed, 31 Jan 2024 17:58:35 +1300 Subject: [PATCH] Encode & decode charsets on C side (fix Haskell side) --- cbits/transcode.c | 42 ++++++++++++++++++++++++ fontconfig-pure.cabal | 2 +- lib/Graphics/Text/Font/Choose/CharSet.hs | 2 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 cbits/transcode.c diff --git a/cbits/transcode.c b/cbits/transcode.c new file mode 100644 index 0000000..52469fb --- /dev/null +++ b/cbits/transcode.c @@ -0,0 +1,42 @@ +#include "cmp.h" +#include +#include + +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; +} diff --git a/fontconfig-pure.cabal b/fontconfig-pure.cabal index a44fefe..2b6ec4f 100644 --- a/fontconfig-pure.cabal +++ b/fontconfig-pure.cabal @@ -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 + c-sources: cbits/cmp.c, cbits/transcode.c -- Modules included in this library but not exported. -- other-modules: diff --git a/lib/Graphics/Text/Font/Choose/CharSet.hs b/lib/Graphics/Text/Font/Choose/CharSet.hs index c6f94dd..78c6ffd 100644 --- a/lib/Graphics/Text/Font/Choose/CharSet.hs +++ b/lib/Graphics/Text/Font/Choose/CharSet.hs @@ -47,7 +47,7 @@ diffCompress :: Int -> [Int] -> [Int] diffCompress prev (x:xs) = x - prev:diffCompress x xs diffCompress _ [] = [] diffDecompress :: Int -> [Int] -> [Int] -diffDecompress prev (x:xs) = prev + x:diffDecompress x xs +diffDecompress prev (x:xs) = let y = prev + x in y:diffDecompress y xs diffDecompress _ [] = [] newtype CharSet' = CharSet' { unCharSet :: CharSet } -- 2.30.2