module SFML.Graphics.Font
(
module SFML.Utils
, fontFromFile
, fontFromMemory
, fontFromStream
, copy
, destroy
, getGlyph
, getKerning
, getLineSpacing
, getUnderlinePosition
, getUnderlineThickness
, getFontTexture
, getInfo
)
where
import SFML.Graphics.FontInfo
import SFML.Graphics.Glyph
import SFML.Graphics.Types
import SFML.SFCopyable
import SFML.SFException
import SFML.SFResource
import SFML.System.InputStream
import SFML.Utils
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
fontFromFile :: FilePath -> IO (Either SFException Font)
fontFromFile path =
let err = SFException $ "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 SFException Font)
fontFromMemory pixels size =
let err = SFException $ "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 SFException Font)
fontFromStream stream =
let err = SFException $ "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 Float
getKerning font first second size =
fmap realToFrac $ sfFont_getKerning font (fromIntegral first) (fromIntegral second) (fromIntegral size)
foreign import ccall unsafe "sfFont_getKerning"
sfFont_getKerning :: Font -> Word32 -> Word32 -> CUInt -> IO CFloat
getLineSpacing
:: Font
-> Int
-> IO Float
getLineSpacing font size = fmap realToFrac $ sfFont_getLineSpacing font (fromIntegral size)
foreign import ccall unsafe "sfFont_getLineSpacing"
sfFont_getLineSpacing :: Font -> CUInt -> IO CFloat
getUnderlinePosition
:: Font
-> Int
-> IO Float
getUnderlinePosition font size
= fmap realToFrac $ sfFont_getUnderlinePosition font (fromIntegral size)
foreign import ccall unsafe "sfFont_getUnderlinePosition"
sfFont_getUnderlinePosition :: Font -> CUInt -> IO CFloat
getUnderlineThickness
:: Font
-> Int
-> IO Float
getUnderlineThickness font size
= fmap realToFrac $ sfFont_getUnderlineThickness font (fromIntegral size)
foreign import ccall unsafe "sfFont_getUnderlineThickness"
sfFont_getUnderlineThickness :: Font -> CUInt -> IO CFloat
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
getInfo :: Font -> IO FontInfo
getInfo font = alloca $ \ptr -> sfFont_getInfo_helper font ptr >> peek ptr
foreign import ccall unsafe "sfFont_getInfo_helper"
sfFont_getInfo_helper :: Font -> Ptr FontInfo -> IO ()