~alcinnz/fontconfig-pure

64bcb432397d1fdfdf9f136ec3c5cdc4990bf920 — Adrian Cochrane 2 years ago 77762b2
Refine first-draft language bindings!
6 files changed, 38 insertions(+), 10 deletions(-)

D Graphics/Text/Font/Choose/Constant.hs
M Graphics/Text/Font/Choose/Init.hs
D Graphics/Text/Font/Choose/LangResult.hs
M Graphics/Text/Font/Choose/LangSet.hs
D Graphics/Text/Font/Choose/ObjectType.hs
M Graphics/Text/Font/Choose/Result.hs
D Graphics/Text/Font/Choose/Constant.hs => Graphics/Text/Font/Choose/Constant.hs +0 -3
@@ 1,3 0,0 @@
module Graphics.Text.Font.Choose.Constant where

data Constant = Constant { name :: String, object :: String, value :: Int }

M Graphics/Text/Font/Choose/Init.hs => Graphics/Text/Font/Choose/Init.hs +4 -2
@@ 3,8 3,10 @@ module Graphics.Text.Font.Choose.Init ({-Config, initLoadConfig, initLoadConfigA

import Prelude hiding (init)

{-foreign import ccall "FcInitLoadConfig" initLoadConfig :: IO Config
foreign import ccall "FcInitLoadConfigAndFonts" initLoadConfigAndFonts :: IO Config-}
import Graphics.Text.Font.Choose.Config

foreign import ccall "FcInitLoadConfig" initLoadConfig :: IO Config
foreign import ccall "FcInitLoadConfigAndFonts" initLoadConfigAndFonts :: IO Config

foreign import ccall "FcInit" init :: IO Bool
foreign import ccall "FcFini" fini :: IO ()

D Graphics/Text/Font/Choose/LangResult.hs => Graphics/Text/Font/Choose/LangResult.hs +0 -0
M Graphics/Text/Font/Choose/LangSet.hs => Graphics/Text/Font/Choose/LangSet.hs +7 -0
@@ 21,6 21,13 @@ langs :: LangSet
langs = unsafePerformIO (thawStrSet =<< fcGetLangs)
foreign import ccall "FcGetLangs" fcGetLangs :: IO StrSet_

data LangResult = SameLang | DifferentTerritory | DifferentLang deriving Enum
langSetCompare :: LangSet -> LangSet -> LangResult
langSetCompare a b = unsafePerformIO $ withLangSet a $ \a' -> withLangSet b $ \b' ->
    (toEnum <$> fcLangSetCompare a' b')
foreign import ccall "FcLangSetCompare" fcLangSetCompare ::
    LangSet_ -> LangSet_ -> IO Int

langNormalize :: String -> String
langNormalize = unsafePerformIO $ flip withCString (peekCString . fcLangNormalize)
foreign import ccall "FcLangNormalize" fcLangNormalize :: CString -> CString

D Graphics/Text/Font/Choose/ObjectType.hs => Graphics/Text/Font/Choose/ObjectType.hs +0 -5
@@ 1,5 0,0 @@
module Graphics.Text.Font.Choose.ObjectType

import Graphics.Text.Font.Choose.Value (Value)

data ObjectType = ObjectType String Value

M Graphics/Text/Font/Choose/Result.hs => Graphics/Text/Font/Choose/Result.hs +27 -0
@@ 1,9 1,36 @@
module Graphics.Text.Font.Choose.Result (Result(..), resultFromPointer) where

import Foreign.Storable (peek)
import Foreign.Ptr (Ptr, ptrNull)
import Control.Exception (throwIO, throw, Exception)

data Result = Match | NoMatch | TypeMismatch | ResultNoId | OutOfMemory
    deriving (Eq, Show, Read, Enum)

resultFromPointer :: Ptr Int -> IO Result
resultFromPointer res = toEnum <$> peek res

data Error = ErrTypeMismatch | ErrResultNoId | ErrOutOfMemory deriving (Eq, Show, Read)
instance Exception Error

throwResult :: Result -> IO a -> IO (Maybe a)
throwResult Match x = Just <$> x
throwResult NoMatch _ = return Nothing
throwResult TypeMismatch = throwIO ErrTypeMismatch
throwResult ResultNoId = throwIO ErrResultNoId
throwResult OutOfMemory = throwIO ErrOutOfMemory

throwInt :: Int -> IO a -> IO (Maybe a)
throwInt = throwResult . toEnum
throwPtr :: Ptr Int -> IO a -> IO (Maybe a)
throwPtr a b = resultFromPointer a >>= flip throwResult b

throwFalse :: Bool -> IO ()
throwFalse True = return ()
throwFalse False = throwIO ErrOutOfMemory
throwFalse' :: IO Bool -> IO ()
throwFalse' = (>>= throwFalse)

throwNull :: Ptr a -> Ptr a
throwNull ptr | ptr == ptrNull = throw ErrOutOfMemory
    | otherwise = ptr