module SFML.Graphics.Font
(
module SFML.Utils
, FontException(..)
, fontFromFile
, fontFromMemory
, fontFromStream
, copy
, destroy
, getGlyph
, getKerning
, getLineSpacing
, getFontTexture
)
where
import SFML.Graphics.Glyph
import SFML.Graphics.Types
import SFML.SFCopyable
import SFML.SFResource
import SFML.System.InputStream
import SFML.Utils
import Control.Exception
import Data.Typeable
import Data.Word
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr
import Foreign.Storable
checkNull :: Font -> Maybe Font
checkNull font@(Font ptr) = if ptr == nullPtr then Nothing else Just font
data FontException = FontException String deriving (Show, Typeable)
instance Exception FontException
fontFromFile :: FilePath -> IO (Either FontException Font)
fontFromFile path =
let err = FontException $ "Failed loading font from file " ++ show path
in fmap (tagErr err . checkNull) $ withCAString path sfFont_createFromFile
foreign import ccall unsafe "sfFont_createFromFile"
sfFont_createFromFile :: CString -> IO Font
fontFromMemory
:: Ptr Char
-> Int
-> IO (Either FontException Font)
fontFromMemory pixels size =
let err = FontException $ "Failed loading font from memory address " ++ show pixels
in fmap (tagErr err . checkNull) $ sfFont_createFromMemory pixels (fromIntegral size)
foreign import ccall unsafe "sfFont_createFromMemory"
sfFont_createFromMemory :: Ptr a -> CInt -> IO Font
fontFromStream :: InputStream -> IO (Either FontException Font)
fontFromStream stream =
let err = FontException $ "Failed loading font from stream " ++ show stream
in fmap (tagErr err . checkNull) $ with stream sfFont_createFromStream
foreign import ccall "sfFont_createFromStream"
sfFont_createFromStream :: Ptr InputStream -> IO Font
instance SFCopyable Font where
copy = sfFont_copy
foreign import ccall unsafe "sfFont_copy"
sfFont_copy :: Font -> IO Font
instance SFResource Font where
destroy = sfFont_destroy
foreign import ccall unsafe "sfFont_destroy"
sfFont_destroy :: Font -> IO ()
getGlyph
:: Font
-> Int
-> Int
-> Bool
-> IO Glyph
getGlyph font codePoint size bold =
alloca $ \glyphPtr -> do
sfFont_getGlyph_helper
font (fromIntegral codePoint) (fromIntegral size) (fromIntegral . fromEnum $ bold) glyphPtr
peek glyphPtr
foreign import ccall unsafe "sfFont_getGlyph_helper"
sfFont_getGlyph_helper :: Font -> Word32 -> CUInt -> CInt -> Ptr Glyph -> IO ()
getKerning
:: Font
-> Int
-> Int
-> Int
-> IO Int
getKerning font first second size =
fmap fromIntegral $ sfFont_getKerning font (fromIntegral first) (fromIntegral second) (fromIntegral size)
foreign import ccall unsafe "sfFont_getKerning"
sfFont_getKerning :: Font -> Word32 -> Word32 -> CUInt -> IO CInt
getLineSpacing
:: Font
-> Int
-> IO Int
getLineSpacing font size = fmap fromIntegral $ sfFont_getLineSpacing font (fromIntegral size)
foreign import ccall unsafe "sfFont_getLineSpacing"
sfFont_getLineSpacing :: Font -> CUInt -> IO CInt
getFontTexture
:: Font
-> Int
-> IO Texture
getFontTexture font size = sfFont_getTexture font (fromIntegral size)
foreign import ccall unsafe "sfFont_getTexture"
sfFont_getTexture :: Font -> CUInt -> IO Texture