-- Here to break recursive imports...
module Graphics.Text.Font.Choose.FontSet.API where

import Graphics.Text.Font.Choose.FontSet
import Graphics.Text.Font.Choose.Pattern
import Graphics.Text.Font.Choose.Config
import Graphics.Text.Font.Choose.ObjectSet
import Graphics.Text.Font.Choose.CharSet
import Graphics.Text.Font.Choose.Result (Word8, throwPtr)

import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Alloc (alloca)
import System.IO.Unsafe (unsafePerformIO)

-- | Selects fonts matching pattern from sets,
-- creates patterns from those fonts containing only the objects in object_set
-- and returns the set of unique such patterns.
fontSetList :: Config -> [FontSet] -> Pattern -> ObjectSet -> FontSet
fontSetList :: Config -> [FontSet] -> Pattern -> ObjectSet -> FontSet
fontSetList Config
config [FontSet]
fontss Pattern
pattern ObjectSet
objs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr Config
config forall a b. (a -> b) -> a -> b
$ \Ptr Config'
config' ->
    forall a. [FontSet] -> (Ptr FontSet_ -> Int -> IO a) -> IO a
withFontSets [FontSet]
fontss forall a b. (a -> b) -> a -> b
$ \Ptr FontSet_
fontss' Int
n -> forall a. Pattern -> (Pattern_ -> IO a) -> IO a
withPattern Pattern
pattern forall a b. (a -> b) -> a -> b
$ \Pattern_
pattern' ->
        forall a. ObjectSet -> (ObjectSet_ -> IO a) -> IO a
withObjectSet ObjectSet
objs forall a b. (a -> b) -> a -> b
$ \ObjectSet_
objs' ->
            IO FontSet_ -> IO FontSet
thawFontSet_ forall a b. (a -> b) -> a -> b
$ Ptr Config'
-> Ptr FontSet_ -> Int -> Pattern_ -> ObjectSet_ -> IO FontSet_
fcFontSetList Ptr Config'
config' Ptr FontSet_
fontss' Int
n Pattern_
pattern' ObjectSet_
objs'
-- | Variant of `fontSetList` operating upon register default `Config`.
fontSetList' :: [FontSet] -> Pattern -> ObjectSet -> FontSet
fontSetList' :: [FontSet] -> Pattern -> ObjectSet -> FontSet
fontSetList' [FontSet]
fontss Pattern
pattern ObjectSet
objs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. [FontSet] -> (Ptr FontSet_ -> Int -> IO a) -> IO a
withFontSets [FontSet]
fontss forall a b. (a -> b) -> a -> b
$ \Ptr FontSet_
fontss' Int
n ->
    forall a. Pattern -> (Pattern_ -> IO a) -> IO a
withPattern Pattern
pattern forall a b. (a -> b) -> a -> b
$ \Pattern_
pattern' -> forall a. ObjectSet -> (ObjectSet_ -> IO a) -> IO a
withObjectSet ObjectSet
objs forall a b. (a -> b) -> a -> b
$ \ObjectSet_
objs' ->
        IO FontSet_ -> IO FontSet
thawFontSet_ forall a b. (a -> b) -> a -> b
$ Ptr Config'
-> Ptr FontSet_ -> Int -> Pattern_ -> ObjectSet_ -> IO FontSet_
fcFontSetList forall a. Ptr a
nullPtr Ptr FontSet_
fontss' Int
n Pattern_
pattern' ObjectSet_
objs'
foreign import ccall "FcFontSetList" fcFontSetList ::
    Config_ -> Ptr FontSet_ -> Int -> Pattern_ -> ObjectSet_ -> IO FontSet_

-- | Finds the font in sets most closely matching pattern
-- and returns the result of `fontRenderPrepare` for that font and
-- the provided pattern. This function should be called only after
-- `configSubstitute` and `defaultSubstitute` have been called for pattern;
-- otherwise the results will not be correct.
fontSetMatch :: Config -> [FontSet] -> Pattern -> Maybe Pattern
fontSetMatch :: Config -> [FontSet] -> Pattern -> Maybe Pattern
fontSetMatch Config
config [FontSet]
fontss Pattern
pattern = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr Config
config forall a b. (a -> b) -> a -> b
$ \Ptr Config'
config' ->
    forall a. [FontSet] -> (Ptr FontSet_ -> Int -> IO a) -> IO a
withFontSets [FontSet]
fontss forall a b. (a -> b) -> a -> b
$ \Ptr FontSet_
fontss' Int
n -> forall a. Pattern -> (Pattern_ -> IO a) -> IO a
withPattern Pattern
pattern forall a b. (a -> b) -> a -> b
$ \Pattern_
pattern' -> 
        forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr Word8
res' -> do
            Pattern_
ret <- Ptr Config'
-> Ptr FontSet_ -> Int -> Pattern_ -> Ptr Word8 -> IO Pattern_
fcFontSetMatch Ptr Config'
config' Ptr FontSet_
fontss' Int
n Pattern_
pattern' Ptr Word8
res'
            forall a. Ptr Word8 -> IO a -> IO (Maybe a)
throwPtr Ptr Word8
res' forall a b. (a -> b) -> a -> b
$ IO Pattern_ -> IO Pattern
thawPattern_ forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure Pattern_
ret
-- | Variant of `fontSetMatch` operating upon registered default `Config`.
fontSetMatch' :: [FontSet] -> Pattern -> Maybe Pattern
fontSetMatch' :: [FontSet] -> Pattern -> Maybe Pattern
fontSetMatch' [FontSet]
fontss Pattern
pattern = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. [FontSet] -> (Ptr FontSet_ -> Int -> IO a) -> IO a
withFontSets [FontSet]
fontss forall a b. (a -> b) -> a -> b
$ \Ptr FontSet_
fontss' Int
n ->
    forall a. Pattern -> (Pattern_ -> IO a) -> IO a
withPattern Pattern
pattern forall a b. (a -> b) -> a -> b
$ \Pattern_
pattern' -> forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr Word8
res' -> do
        Pattern_
ret <- Ptr Config'
-> Ptr FontSet_ -> Int -> Pattern_ -> Ptr Word8 -> IO Pattern_
fcFontSetMatch forall a. Ptr a
nullPtr Ptr FontSet_
fontss' Int
n Pattern_
pattern' Ptr Word8
res'
        forall a. Ptr Word8 -> IO a -> IO (Maybe a)
throwPtr Ptr Word8
res' forall a b. (a -> b) -> a -> b
$ IO Pattern_ -> IO Pattern
thawPattern_ forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure Pattern_
ret
foreign import ccall "FcFontSetMatch" fcFontSetMatch ::
    Config_ -> Ptr FontSet_ -> Int -> Pattern_ -> Ptr Word8 -> IO Pattern_

-- | Returns the list of fonts from sets sorted by closeness to pattern.
-- If trim is `True`, elements in the list which don't include Unicode coverage
-- not provided by earlier elements in the list are elided.
-- The union of Unicode coverage of all of the fonts is returned in csp.
-- This function should be called only after `configSubstitute` and
-- `defaultSubstitute` have been called for p;
-- otherwise the results will not be correct.
-- The returned FcFontSet references `Pattern` structures which may be shared by
-- the return value from multiple `fontSort` calls, applications cannot modify
-- these patterns. Instead, they should be passed, along with pattern to
-- `fontRenderPrepare` which combines them into a complete pattern.
fontSetSort :: Config -> [FontSet] -> Pattern -> Bool -> Maybe (FontSet, CharSet)
fontSetSort :: Config -> [FontSet] -> Pattern -> Bool -> Maybe (FontSet, CharSet)
fontSetSort Config
config [FontSet]
fontss Pattern
pattern Bool
trim = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr Config
config forall a b. (a -> b) -> a -> b
$ \Ptr Config'
config' -> forall a. [FontSet] -> (Ptr FontSet_ -> Int -> IO a) -> IO a
withFontSets [FontSet]
fontss forall a b. (a -> b) -> a -> b
$ \Ptr FontSet_
fontss' Int
n ->
        forall a. Pattern -> (Pattern_ -> IO a) -> IO a
withPattern Pattern
pattern forall a b. (a -> b) -> a -> b
$ \Pattern_
pattern' -> forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr CharSet_
csp' -> forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr Word8
res' -> do
            FontSet_
ret' <- Ptr Config'
-> Ptr FontSet_
-> Int
-> Pattern_
-> Bool
-> Ptr CharSet_
-> Ptr Word8
-> IO FontSet_
fcFontSetSort Ptr Config'
config' Ptr FontSet_
fontss' Int
n Pattern_
pattern' Bool
trim Ptr CharSet_
csp' Ptr Word8
res'
            forall a. Ptr Word8 -> IO a -> IO (Maybe a)
throwPtr Ptr Word8
res' forall a b. (a -> b) -> a -> b
$ do
                FontSet
x <- IO FontSet_ -> IO FontSet
thawFontSet_ forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure FontSet_
ret'
                CharSet
y <- Ptr CharSet_ -> IO CharSet
thawCharSet' Ptr CharSet_
csp'
                forall (m :: * -> *) a. Monad m => a -> m a
return (FontSet
x, CharSet
y)
-- | Variant of `fontSetSort` operating upon registered default `Config`.
fontSetSort' :: [FontSet] -> Pattern -> Bool -> Maybe (FontSet, CharSet)
fontSetSort' :: [FontSet] -> Pattern -> Bool -> Maybe (FontSet, CharSet)
fontSetSort' [FontSet]
fontss Pattern
pattern Bool
trim = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
    forall a. [FontSet] -> (Ptr FontSet_ -> Int -> IO a) -> IO a
withFontSets [FontSet]
fontss forall a b. (a -> b) -> a -> b
$ \Ptr FontSet_
fontss' Int
n -> forall a. Pattern -> (Pattern_ -> IO a) -> IO a
withPattern Pattern
pattern forall a b. (a -> b) -> a -> b
$ \Pattern_
pattern' ->
        forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr CharSet_
csp' -> forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr Word8
res' -> do
            FontSet_
ret' <- Ptr Config'
-> Ptr FontSet_
-> Int
-> Pattern_
-> Bool
-> Ptr CharSet_
-> Ptr Word8
-> IO FontSet_
fcFontSetSort forall a. Ptr a
nullPtr Ptr FontSet_
fontss' Int
n Pattern_
pattern' Bool
trim Ptr CharSet_
csp' Ptr Word8
res'
            forall a. Ptr Word8 -> IO a -> IO (Maybe a)
throwPtr Ptr Word8
res' forall a b. (a -> b) -> a -> b
$ do
                FontSet
x <- IO FontSet_ -> IO FontSet
thawFontSet_ forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure FontSet_
ret'
                CharSet
y <- Ptr CharSet_ -> IO CharSet
thawCharSet' Ptr CharSet_
csp'
                forall (m :: * -> *) a. Monad m => a -> m a
return (FontSet
x, CharSet
y)
foreign import ccall "FcFontSetSort" fcFontSetSort :: Config_ -> Ptr FontSet_
    -> Int -> Pattern_ -> Bool -> Ptr CharSet_ -> Ptr Word8 -> IO FontSet_