module Graphics.UI.SDL.TTF.Management
( tryOpenFont
, openFont
, closeFont
, tryOpenFontRW
, openFontRW
, tryOpenFontIndex
, openFontIndex
) where
import Graphics.UI.SDL.TTF.Types
import Graphics.UI.SDL.General (unwrapMaybe)
import Graphics.UI.SDL.Types
import Foreign
import Foreign.C
foreign import ccall unsafe "&TTF_CloseFont" ttfCloseFontFinal :: FunPtr (Ptr FontStruct -> IO ())
mkFinalizedFont :: Ptr FontStruct -> IO Font
mkFinalizedFont = newForeignPtr ttfCloseFontFinal
foreign import ccall unsafe "TTF_CloseFont" ttfCloseFont :: Ptr FontStruct -> IO ()
closeFont :: Font -> IO ()
closeFont font = withForeignPtr font ttfCloseFont
foreign import ccall unsafe "TTF_OpenFont" ttfOpenFont :: CString -> CInt -> IO (Ptr FontStruct)
tryOpenFont :: String -> Int -> IO (Maybe Font)
tryOpenFont path ptsize
= withCString path $ \cPath ->
ttfOpenFont cPath (fromIntegral ptsize) >>= maybePeek mkFinalizedFont
openFont :: String -> Int -> IO Font
openFont path ptsize = unwrapMaybe "TTF_OpenFont" (tryOpenFont path ptsize)
foreign import ccall unsafe "TTF_OpenFontRW" ttfOpenFontRW :: Ptr RWopsStruct -> CInt -> Int -> IO (Ptr FontStruct)
tryOpenFontRW :: RWops -> Bool -> Int -> IO (Maybe Font)
tryOpenFontRW rw freesrc ptsize
= withForeignPtr rw $ \rwPtr ->
ttfOpenFontRW rwPtr (fromBool freesrc) ptsize >>= maybePeek mkFinalizedFont
openFontRW :: RWops -> Bool -> Int -> IO Font
openFontRW rw freesrc ptsize
= unwrapMaybe "TTF_OpenFontRW" (tryOpenFontRW rw freesrc ptsize)
foreign import ccall unsafe "TTF_OpenFontIndex" ttfOpenFontIndex :: CString -> CInt -> Int -> IO (Ptr FontStruct)
tryOpenFontIndex :: String -> Int -> Int -> IO (Maybe Font)
tryOpenFontIndex file ptsize index
= withCString file $ \cFile ->
ttfOpenFontIndex cFile (fromIntegral ptsize) (fromIntegral index) >>= maybePeek mkFinalizedFont
openFontIndex :: String -> Int -> Int -> IO Font
openFontIndex file ptsize index = unwrapMaybe "TTF_OpenFontIndex" (tryOpenFontIndex file ptsize index)