module Graphics.Rendering.FTGL (
createBitmapFont
, createBufferFont
, createOutlineFont
, createPixmapFont
, createPolygonFont
, createTextureFont
, createExtrudeFont
, Glyph
, Font
, Layout
, createSimpleLayout
, getLayoutFont
, setLayoutFont
, getLayoutAlignment
, setLayoutAlignment
, getLayoutLineLength
, setLayoutLineLength
, setLayoutLineSpacing
, destroyFont
, attachData
, attachFile
, setFontCharMap
, setFontFaceSize
, getFontFaceSize
, setFontFaceDepth
, getFontBBox
, getFontAdvance
, renderFont
, getFontError
, destroyLayout
, renderLayout
, getLayoutError
, RenderMode(All,Back,Front,Side)
, TextAlignment(AlignLeft,AlignRight,AlignCenter,Justify)
)
where
import Foreign (unsafePerformIO)
import Foreign.C
import Foreign.Ptr
import Foreign.Marshal.Alloc
import Foreign.Marshal.Array
import qualified Graphics.Rendering.OpenGL.GL as GL
import Control.Applicative ((<$>))
foreign import ccall unsafe "ftglCreateBitmapFont" fcreateBitmapFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreateBufferFont" fcreateBufferFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreateOutlineFont" fcreateOutlineFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreatePixmapFont" fcreatePixmapFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreatePolygonFont" fcreatePolygonFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreateTextureFont" fcreateTextureFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreateExtrudeFont" fcreateExtrudeFont :: CString -> IO Font
foreign import ccall unsafe "ftglCreateSimpleLayout" createSimpleLayout :: IO Layout
foreign import ccall unsafe "ftglSetLayoutFont" setLayoutFont :: Layout -> Font -> IO ()
foreign import ccall unsafe "ftglGetLayoutFont" fgetLayoutFont :: Layout -> IO Font
foreign import ccall unsafe "ftglSetLayoutLineLength" setLayoutLineLength :: Layout -> CFloat -> IO ()
foreign import ccall unsafe "ftglGetLayoutLineLength" fgetLayoutLineLength :: Layout -> IO CFloat
foreign import ccall unsafe "ftglSetLayoutAlignment" setLayoutAlignment :: Layout -> CInt -> IO ()
foreign import ccall unsafe "ftglGetLayoutAlignement" fgetLayoutAlignment :: Layout -> IO CInt
foreign import ccall unsafe "ftglSetLayoutLineSpacing" setLayoutLineSpacing :: Layout -> CFloat -> IO ()
foreign import ccall unsafe "ftglDestroyFont" destroyFont :: Font -> IO ()
foreign import ccall unsafe "ftglAttachFile" fattachFile :: Font -> CString -> IO ()
foreign import ccall unsafe "ftglAttachData" attachData :: Font -> Ptr () -> IO ()
foreign import ccall unsafe "ftglSetFontCharMap" setFontCharMap :: Font -> CInt -> IO ()
foreign import ccall unsafe "ftglGetFontCharMapCount" fgetFontCharMapCount :: Font -> IO CInt
foreign import ccall unsafe "ftglGetFontCharMapList" fgetFontCharMapList :: Font -> IO (Ptr CInt)
foreign import ccall unsafe "ftglSetFontFaceSize" fsetFontFaceSize :: Font -> CInt -> CInt -> IO CInt
foreign import ccall unsafe "ftglGetFontFaceSize" fgetFontFaceSize :: Font -> IO CInt
foreign import ccall unsafe "ftglSetFontDepth" setFontFaceDepth :: Font -> CInt -> IO ()
foreign import ccall unsafe "ftglSetFontOutset" setFontOutset :: Font -> CInt -> CInt -> IO ()
foreign import ccall unsafe "ftglGetFontBBox" fgetFontBBox :: Font -> CString -> Ptr CFloat -> IO ()
foreign import ccall unsafe "ftglGetFontAdvance" fgetFontAdvance :: Font -> CString -> IO CFloat
foreign import ccall unsafe "ftglRenderFont" frenderFont :: Font -> CString -> CInt -> IO ()
foreign import ccall unsafe "ftglGetFontError" fgetFontError :: Font -> IO CInt
foreign import ccall unsafe "ftglDestroyLayout" destroyLayout :: Layout -> IO ()
foreign import ccall unsafe "ftglRenderLayout" renderLayout_foreign :: Layout -> CString -> IO ()
foreign import ccall unsafe "ftglGetLayoutError" fgetLayoutError :: Layout -> IO CInt
setFontFaceSize :: Font -> Int -> Int -> IO CInt
setFontFaceSize f s x = fsetFontFaceSize f (fromIntegral s) (fromIntegral x)
data RenderMode = Front | Back | Side | All
data TextAlignment = AlignLeft | AlignCenter | AlignRight | Justify
marshalRenderMode :: RenderMode -> CInt
marshalRenderMode Front = 0x0001
marshalRenderMode Back = 0x0002
marshalRenderMode Side = 0x004
marshalRenderMode All = 0xffff
marshalTextAlignment :: TextAlignment -> CInt
marshalTextAlignment AlignLeft = 0
marshalTextAlignment AlignCenter = 1
marshalTextAlignment AlignRight = 2
marshalTextAlignment Justify = 3
readTextAlignment :: CInt -> TextAlignment
readTextAlignment 0 = AlignLeft
readTextAlignment 1 = AlignCenter
readTextAlignment 2 = AlignRight
readTextAlignment 3 = Justify
data Glyph_Opaque
data Font_Opaque
data Layout_Opaque
type Glyph = Ptr Glyph_Opaque
type Font = Ptr Font_Opaque
type Layout = Ptr Layout_Opaque
createBitmapFont :: String -> IO Font
createBitmapFont file = withCStringLen file $ \(p,l) -> fcreateBitmapFont p
createBufferFont :: String -> IO Font
createBufferFont file = withCStringLen file $ \(p,l) -> fcreateBufferFont p
createOutlineFont :: String -> IO Font
createOutlineFont file = withCStringLen file $ \(p,l) -> fcreateOutlineFont p
createPixmapFont :: String -> IO Font
createPixmapFont file = withCStringLen file $ \(p,l) -> fcreatePixmapFont p
createPolygonFont :: String -> IO Font
createPolygonFont file = withCStringLen file $ \(p,l) -> fcreatePolygonFont p
createTextureFont :: String -> IO Font
createTextureFont file = withCStringLen file $ \(p,l) -> fcreateTextureFont p
createExtrudeFont :: String -> IO Font
createExtrudeFont file = withCStringLen file $ \(p,l) -> fcreateExtrudeFont p
getLayoutFont f = unsafePerformIO $ fgetLayoutFont f
getLayoutLineLength :: Layout -> Float
getLayoutLineLength f = realToFrac . unsafePerformIO $ fgetLayoutLineLength f
getLayoutAlignment :: Layout -> TextAlignment
getLayoutAlignment f = readTextAlignment . unsafePerformIO $ fgetLayoutAlignment f
getFontCharMapCount :: Font -> Int
getFontCharMapCount f = fromIntegral . unsafePerformIO $ fgetFontCharMapCount f
getFontCharMapList f = unsafePerformIO $ fgetFontCharMapList f
getFontFaceSize :: Font -> Int
getFontFaceSize f = fromIntegral . unsafePerformIO $ fgetFontFaceSize f
getFontError :: Font -> Int
getFontError f = fromIntegral . unsafePerformIO $ fgetFontError f
attachFile :: Font -> String -> IO ()
attachFile font str = withCString str $ \p -> fattachFile font p
getFontAdvance :: Font -> String -> Float
getFontAdvance font str = realToFrac . unsafePerformIO $ withCString str $ \p -> fgetFontAdvance font p
renderFont :: Font -> String -> RenderMode -> IO ()
renderFont font str mode = withCString str $ \p -> do
frenderFont font p (marshalRenderMode mode)
getFontBBox :: Font -> String -> [Float]
getFontBBox f s = unsafePerformIO $
allocaBytes 16 $ \pf ->
withCString s $ \ps -> do
fgetFontBBox f ps pf
map realToFrac <$> peekArray 4 pf
getLayoutError f = unsafePerformIO $ fgetLayoutError f
renderLayout layout str = withCString str $ \strPtr -> do renderLayout_foreign layout strPtr