-- NOTE: Untested!
module Graphics.Text.Font.Choose.Pattern (Pattern(..), Strength(..), equalSubset,
Pattern_, withPattern) where
import Prelude hiding (filter)
import Graphics.Text.Font.Choose.Value (Value, withValue, thawValue, Value_)
import Graphics.Text.Font.Choose.ObjectSet (ObjectSet, ObjectSet_, withObjectSet)
import Data.Hashable (Hashable)
import Foreign.Ptr (Ptr)
import Foreign.C.String (CString, withCString, peekCString)
import Debug.Trace (trace) -- For reporting internal errors!
type Pattern = [(String, [(Binding, Value)])] deriving (Eq, Ord, Hashable, Show)
data Binding = Strong | Weak | Same deriving (Eq, Ord, Enum, Show)
instance Hashable Binding where
hash Strong = 0
hash Weak = 1
hash Same = 2
normalizePattern pat =
[(key, [val | (key', val) <- pat, key' == key]) | key <- nub $ map fst pat]
equalSubset :: Pattern -> Pattern -> ObjectSet -> Bool
equalSubset a b objs = unsafePerformIO $ withPattern a $ \a' -> withPattern b $ \b' ->
withObjectSet objs $ fcPatternEqualSubset a' b'
foreign import ccall "FcPatternEqualSubset" fcPatternEqualSubset ::
Pattern_ -> Pattern_ -> ObjectSet_ -> IO Bool
filter :: Pattern -> ObjectSet -> Pattern
filter pat objs =
unsafePerformIO $ withPattern pat $ \pat' -> withObjectSet objs $ \objs' -> do
ret <- fcPatternFilter pat' objs'
thawPattern ret
foreign import ccall "FcPatternFilter" fcPatternFilter ::
Pattern_ -> ObjectSet_ -> IO Pattern_
substitute :: Pattern -> Pattern
substitute pat = unsafePerformIO $ withPattern pat $ \pat' -> do
ret <- fcDefaultSubstitute pat'
thawPattern pat'
foreign import ccall "FcPatternSubstitute" fcPatternSubstitute :: Pattern_ -> IO ()
nameParse :: String -> Pattern
nameParse name = unsafePerformIO $ withCString name $ \name' -> do
ret <- fcNameParse name'
thawPattern ret
foreign import ccall "FcNameParse" fcNameParse :: CString -> IO Pattern_
nameUnparse :: Pattern -> String
nameUnparse pat = unsafePerformIO $ withPattern pat $ \pat' -> do
ret <- fcNameUnparse pat'
peekCString ret
format :: Pattern -> String -> String
format pat fmt =
unsafePerformIO $ withPattern pat $ \pat' -> withCString fmt $ \fmt' -> do
ret <- fcPatternFormat pat' fmt'
peekCString ret
foreign import ccall "FcPatternFormat" fcPatternFormat ::
Pattern_ -> CString -> IO CString
------
--- Low-level
------
data Pattern'
type Pattern_ = Ptr Pattern'
withPattern :: Pattern -> (Pattern_ -> IO a) -> IO a
withPattern pat cb = withNewPattern $ \pat' -> do
forM pat $ \(obj, vals) -> withCString obj $ \obj' -> do
forM vals $ \(strength, val) -> withValue val $
fcPatternAdd_ pat' obj' (strength == Strong) True
cb pat'
-- Does Haskell FFI support unboxed structs? Do I really need to write a C wrapper?
foreign import ccall "my_FCPatternAdd" fcPatternAdd_ ::
Pattern_ -> CString -> Bool -> Bool -> Value_ -> IO Bool
patternAsPointer :: Pattern -> IO Pattern_
patternAsPointer = withPattern fcPatternCopy
foreign import ccall "FcPatternCopy" fcPatternCopy :: Pattern_ -> IO Pattern_
data PatternIter'
type PatternIter_ = Ptr PatternIter
thawPattern :: Pattern_ -> IO Pattern
thawPattern pat' = alloca $ \iter' -> do
fcPatternIterStart pat' iter'
ret <- go iter'
return $ normalizePattern ret
where
go iter' = do
ok <- fcPatternIterNext pat' iter'
if ok then do
x <- thawPattern' pat' iter'
xs <- go iter'
return (x : xs)
else return []
foreign import ccall "FcPatternIterStart" fcPatternIterStart ::
Pattern_ -> PatternIter_ -> IO ()
foreign import ccall "FcPatternIterNext" fcPatternIterNext ::
Pattern_ -> PatternIter_ -> IO Bool
thawPattern' :: Pattern_ -> PatternIter_ -> IO (String, [(Binding, Value)])
thawPattern' pat' iter' = do
obj <- peekCString $ fcPatternIterGetObject pat' iter'
count <- fcPatternIterValueCount pat' iter'
values <- forM [0..pred count] $ \i -> alloca $ \val' -> alloca $ \binding' -> do
res <- fcPatternIterGetValue pat' iter' i val' binding'
if res then do
binding <- peek binding'
val <- thawValue val'
return $ Just (toEnum binding, val)
else trace
("FontConfig: Error retrieving value for " ++ obj ++
" code: " ++ show res) $
return Nothing
return (obj, catMaybes values)
withNewPattern cb = bracket fcPatternCreate fcPatternDestroy cb
foreign import ccall "FcPatternCreate" fcPatternCreate :: IO Pattern_
foreign import ccall "FcPatternDestroy" fcPatternDestroy :: Pattern_ -> IO ()