~alcinnz/fontconfig-pure

8c6d796da7e050bb5defd863170828a1db7146c9 — Adrian Cochrane 2 years ago fe82ac9
Fix remaining segfaults.
2 files changed, 17 insertions(+), 8 deletions(-)

M Graphics/Text/Font/Choose/FontSet.hs
M cbits/pattern.c
M Graphics/Text/Font/Choose/FontSet.hs => Graphics/Text/Font/Choose/FontSet.hs +10 -6
@@ 52,15 52,19 @@ withFontSets' (fonts:fontss) i fontss' cb = withFontSet fonts $ \fonts' -> do

thawFontSet :: FontSet_ -> IO FontSet
thawFontSet fonts' = do
    -- Very hacky, but these debug statements must be in here to avoid segfaults.
    -- FIXME: Is there an alternative?
    print "a"
    n <- get_fontSet_nfont fonts'
    array <- get_fontSet_fonts fonts'
    if n == 0 || array == nullPtr
    then return []
    print "b"
    if n == 0 then return []
    else do
        list <- peekArray n array
        forM list (thawPattern_ . pure)
        print "c"
        ret <- forM [0..pred n] (thawPattern_ . get_fontSet_font fonts')
        print "d"
        return ret
foreign import ccall "get_fontSet_nfont" get_fontSet_nfont :: FontSet_ -> IO Int
foreign import ccall "get_fontSet_fonts" get_fontSet_fonts :: FontSet_ -> IO (Ptr Pattern_)
foreign import ccall "get_fontSet_font" get_fontSet_font :: FontSet_ -> Int -> IO Pattern_

thawFontSet_ :: IO FontSet_ -> IO FontSet
thawFontSet_ cb = bracket (throwNull <$> cb) fcFontSetDestroy thawFontSet

M cbits/pattern.c => cbits/pattern.c +7 -2
@@ 1,4 1,5 @@
#include <fontconfig/fontconfig.h>
#include <stddef.h>

int my_FcCHARSET_MAP_SIZE() {
    return FC_CHARSET_MAP_SIZE;


@@ 31,6 32,10 @@ int get_fontSet_nfont(FcFontSet *fonts) {
    return fonts->nfont;
}

FcPattern **get_fontSet_fonts(FcFontSet *fonts) {
    return fonts->fonts;
FcPattern *get_fontSet_font(FcFontSet *fonts, int i) {
    if (i < 0) return NULL;
    if (i >= fonts->nfont) return NULL;
    if (i >= fonts->sfont) return NULL;
    if (fonts->fonts == NULL) return NULL;
    return fonts->fonts[i];
}