~alcinnz/fontconfig-pure

db90fbd0f2143c28aaa9d49f91e6376109518b57 — Adrian Cochrane 2 years ago 160a358
Get FontSet module compiling (need to review memory management responsibilities).
M Graphics/Text/Font/Choose/FontSet.hs => Graphics/Text/Font/Choose/FontSet.hs +13 -9
@@ 2,10 2,11 @@
module Graphics.Text.Font.Choose.FontSet where

import Graphics.Text.Font.Choose.Pattern
import Graphics.Text.Font.Choose.Config
--import Graphics.Text.Font.Choose.Config
import Graphics.Text.Font.Choose.ObjectSet
import Graphics.Text.Font.Choose.Result (throwFalse)

import Foreign.Ptr (Ptr, castPtr)
import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.Storable (pokeElemOff, sizeOf, peek)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Marshal.Array (advancePtr)


@@ 14,7 15,7 @@ import Control.Exception (bracket)

type FontSet = [Pattern]

fontSetList :: Config -> [FontSet] -> Pattern -> ObjectSet
{-fontSetList :: Config -> [FontSet] -> Pattern -> ObjectSet
fontSetList config fontss pattern objs = withConfig config $ \config' ->
    withFontSets fontss $ \fontss' -> withPattern $ \pattern' ->
        withObjectSet objs $ \objs' -> do


@@ 39,7 40,7 @@ fontSetSort config fontss pattern trim csp cb = withConfig config $ \config' ->
                res <- peek res'
                ret <- if res == 0 then Just <$> thawFontSet ret' else return Nothing
                fcFontSetDestroy ret'
                return ret
                return ret-}

------
--- Low-level


@@ 53,19 54,22 @@ foreign import ccall "FcFontSetCreate" fcFontSetCreate :: IO FontSet_
foreign import ccall "FcFontSetDestroy" fcFontSetDestroy :: FontSet_ -> IO ()

withFontSet :: FontSet -> (FontSet_ -> IO a) -> IO a
withFontSet fonts cb = withNewFontSet $ \fonts' ->
    forM fonts $ \font -> (fcFontSetAdd fonts' =<< patternAsPointer font)
withFontSet fonts cb = withNewFontSet $ \fonts' -> do
    forM fonts $ \font -> do
        font' <- patternAsPointer font
        throwFalse <$> fcFontSetAdd fonts' font'
    cb fonts'
foreign import ccall "FcFontSetAdd" fcFontSetAdd :: FontSet_ -> Pattern_ -> IO Bool

withFontSets :: [FontSet] -> (Ptr FontSet_ -> IO a) -> IO a
withFontSets fontss cb = let n = length fontss in
    allocaBytes (sizeOf (undefined :: FontSet_) * n) $ \fontss' ->
        withFontSets' fontss 0 fontss' -> cb fontss'
        withFontSets' fontss 0 fontss' $ cb fontss'
withFontSets' :: [FontSet] -> Int -> Ptr FontSet_ -> IO a -> IO a
withFontSets' [] _ _ cb = cb
withFontSets' (fonts:fontss) i fontss' cb = withFontSet fonts $ \fonts' -> do
    pokeElemOff fontss' i fonts'
    withFontSets fontss (succ i) fontss' cb
    withFontSets' fontss (succ i) fontss' cb

thawFontSet :: FontSet_ -> IO FontSet
thawFontSet fonts' = do


@@ 73,4 77,4 @@ thawFontSet fonts' = do
    array <- peek $ castPtr $ advancePtr fonts' 2
    if n == 0 || array == nullPtr
    then return []
    else forM [0..pred n] $ \i -> thawPattern (advancePtr array i)
    else forM [0..pred n] $ \i -> thawPattern =<< peek (advancePtr array i)

M Graphics/Text/Font/Choose/Pattern.hs => Graphics/Text/Font/Choose/Pattern.hs +1 -1
@@ 1,6 1,6 @@
{-# LANGUAGE DeriveGeneric #-}
module Graphics.Text.Font.Choose.Pattern (Pattern(..), Binding(..), equalSubset,
    Pattern_, withPattern) where
    Pattern_, withPattern, thawPattern, patternAsPointer) where

import Prelude hiding (filter)
import Data.List (nub)

M fontconfig-pure.cabal => fontconfig-pure.cabal +1 -1
@@ 55,7 55,7 @@ library
                Graphics.Text.Font.Choose.ObjectSet, Graphics.Text.Font.Choose.CharSet,
                Graphics.Text.Font.Choose.Strings, Graphics.Text.Font.Choose.Range,
                Graphics.Text.Font.Choose.LangSet, Graphics.Text.Font.Choose.Value,
                Graphics.Text.Font.Choose.Pattern
                Graphics.Text.Font.Choose.Pattern, Graphics.Text.Font.Choose.FontSet

  c-sources:    cbits/pattern.c