module Graphics.Rendering.Pango.Attributes (
withAttrList,
parseMarkup,
fromAttrList,
readAttrList
) where
import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.GError
import System.Glib.GList
import Graphics.Rendering.Pango.Structs
import Graphics.Rendering.Pango.BasicTypes
import Data.List ( sortBy )
import Data.Char ( ord, chr )
import Control.Monad ( liftM )
foreign import ccall unsafe "pango_attr_list_unref"
pango_attr_list_unref :: PangoAttrList -> IO ()
withAttrList :: PangoString -> [PangoAttribute] -> (Ptr () -> IO a) -> IO a
withAttrList _ [] act = act nullPtr
withAttrList (PangoString correct _ _) pas act = do
alPtr <- pango_attr_list_new
let pas' = sortBy (\pa1 pa2 -> case compare (paStart pa1) (paStart pa2) of
EQ -> compare (paEnd pa1) (paEnd pa2)
other -> other) pas
mapM_ (\pa -> do
paPtr <- crAttr correct pa
pango_attr_list_insert alPtr (castPtr paPtr)) pas'
res <- act alPtr
pango_attr_list_unref alPtr
return res
crAttr :: UTFCorrection -> PangoAttribute -> IO CPangoAttribute
crAttr c AttrLanguage { paStart=s, paEnd=e, paLang = lang } =
setAttrPos c s e $ (\(Language arg1) -> pango_attr_language_new arg1) lang
crAttr c AttrFamily { paStart=s, paEnd=e, paFamily = fam } =
setAttrPos c s e $ withUTFString fam $ pango_attr_family_new
crAttr c AttrStyle { paStart=s, paEnd=e, paStyle = style } =
setAttrPos c s e $
pango_attr_style_new (fromIntegral (fromEnum style))
crAttr c AttrWeight { paStart=s, paEnd=e, paWeight = weight } =
setAttrPos c s e $
pango_attr_weight_new (fromIntegral (fromEnum weight))
crAttr c AttrVariant { paStart=s, paEnd=e, paVariant = variant } =
setAttrPos c s e $
pango_attr_variant_new (fromIntegral (fromEnum variant))
crAttr c AttrStretch { paStart=s, paEnd=e, paStretch = stretch } =
setAttrPos c s e $
pango_attr_stretch_new (fromIntegral (fromEnum stretch))
crAttr c AttrSize { paStart=s, paEnd=e, paSize = pu } =
setAttrPos c s e $ pango_attr_size_new (puToInt pu)
crAttr c AttrAbsSize { paStart=s, paEnd=e, paSize = pu } =
setAttrPos c s e $ pango_attr_size_new_absolute (puToInt pu)
crAttr c AttrFontDescription { paStart=s, paEnd=e, paFontDescription = fd } =
setAttrPos c s e $ (\(FontDescription arg1) -> withForeignPtr arg1 $ \argPtr1 ->pango_attr_font_desc_new argPtr1) fd
crAttr c AttrForeground { paStart=s, paEnd=e, paColor = Color r g b } =
setAttrPos c s e $ pango_attr_foreground_new
(fromIntegral r) (fromIntegral g) (fromIntegral b)
crAttr c AttrBackground { paStart=s, paEnd=e, paColor = Color r g b } =
setAttrPos c s e $ pango_attr_background_new
(fromIntegral r) (fromIntegral g) (fromIntegral b)
crAttr c AttrUnderline { paStart=s, paEnd=e, paUnderline = underline } =
setAttrPos c s e $ do
pango_attr_underline_new (fromIntegral (fromEnum underline))
crAttr c AttrUnderlineColor {paStart=s, paEnd=e, paColor = Color r g b } =
setAttrPos c s e $ pango_attr_underline_color_new
(fromIntegral r) (fromIntegral g) (fromIntegral b)
crAttr c AttrStrikethrough { paStart=s, paEnd=e, paStrikethrough = st } =
setAttrPos c s e $ do
pango_attr_strikethrough_new (fromIntegral (fromEnum st))
crAttr c AttrStrikethroughColor {paStart=s, paEnd=e, paColor = Color r g b } =
setAttrPos c s e $ pango_attr_strikethrough_color_new
(fromIntegral r) (fromIntegral g) (fromIntegral b)
crAttr c AttrRise { paStart=s, paEnd=e, paRise = pu } =
setAttrPos c s e $ pango_attr_rise_new (puToInt pu)
crAttr c AttrShape { paStart=s, paEnd=e, paInk = rect1, paLogical = rect2 } =
setAttrPos c s e $ alloca $ \rect1Ptr -> alloca $ \rect2Ptr -> do
poke rect1Ptr rect1
poke rect2Ptr rect2
pango_attr_shape_new (castPtr rect1Ptr) (castPtr rect2Ptr)
crAttr c AttrScale { paStart=s, paEnd=e, paScale = scale } =
setAttrPos c s e $
pango_attr_scale_new (realToFrac scale)
crAttr c AttrFallback { paStart=s, paEnd=e, paFallback = fb } =
setAttrPos c s e $
pango_attr_fallback_new (fromBool fb)
crAttr c AttrLetterSpacing { paStart=s, paEnd=e, paLetterSpacing = pu } =
setAttrPos c s e $
pango_attr_letter_spacing_new (puToInt pu)
crAttr c AttrGravity { paStart=s, paEnd=e, paGravity = g } =
setAttrPos c s e $
pango_attr_gravity_new (fromIntegral (fromEnum g))
crAttr c AttrGravityHint { paStart=s, paEnd=e, paGravityHint = g } =
setAttrPos c s e $
pango_attr_gravity_hint_new (fromIntegral (fromEnum g))
parseMarkup ::
(GlibString markup, GlibString string)
=> markup
-> Char
-> IO ([[PangoAttribute]], Char, string)
parseMarkup markup accelMarker = propagateGError $ \errPtr ->
withUTFStringLen markup $ \(markupPtr,markupLen) ->
alloca $ \attrListPtr ->
alloca $ \strPtrPtr ->
alloca $ \accelPtr -> do
poke accelPtr (fromIntegral (ord accelMarker))
success <- pango_parse_markup markupPtr
(fromIntegral markupLen) (fromIntegral (ord accelMarker))
(castPtr attrListPtr) strPtrPtr accelPtr errPtr
if not (toBool success) then return undefined else do
accel <- peek accelPtr
strPtr <- peek strPtrPtr
str <- peekUTFString strPtr
g_free (castPtr strPtr)
attrList <- peek attrListPtr
attrs <- fromAttrList (genUTFOfs str) attrList
return (attrs, chr (fromIntegral accel), str)
type PangoAttrIterator = Ptr (())
fromAttrList :: UTFCorrection -> PangoAttrList -> IO [[PangoAttribute]]
fromAttrList correct attrListPtr = do
iter <- pango_attr_list_get_iterator attrListPtr
let readIter = do
list <- pango_attr_iterator_get_attrs iter
attrs <- if list==nullPtr then return [] else do
attrPtrs <- fromGSList list
mapM (fromAttr correct) attrPtrs
more <- pango_attr_iterator_next iter
if toBool more then liftM ((:) attrs) $ readIter else return []
elems <- readIter
pango_attr_iterator_destroy iter
return elems
fromAttr :: UTFCorrection -> CPangoAttribute -> IO PangoAttribute
fromAttr correct attrPtr = do
attr <- readAttr correct attrPtr
pango_attribute_destroy attrPtr
return attr
readAttrList :: UTFCorrection -> PangoAttrList -> IO [[PangoAttribute]]
readAttrList correct attrListPtr = do
elems <- fromAttrList correct attrListPtr
pango_attr_list_unref attrListPtr
return elems
foreign import ccall unsafe "pango_attr_list_new"
pango_attr_list_new :: (IO (Ptr ()))
foreign import ccall unsafe "pango_attr_list_insert"
pango_attr_list_insert :: ((Ptr ()) -> ((Ptr ()) -> (IO ())))
foreign import ccall unsafe "pango_attr_language_new"
pango_attr_language_new :: ((Ptr Language) -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_family_new"
pango_attr_family_new :: ((Ptr CChar) -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_style_new"
pango_attr_style_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_weight_new"
pango_attr_weight_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_variant_new"
pango_attr_variant_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_stretch_new"
pango_attr_stretch_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_size_new"
pango_attr_size_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_size_new_absolute"
pango_attr_size_new_absolute :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_font_desc_new"
pango_attr_font_desc_new :: ((Ptr FontDescription) -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_foreground_new"
pango_attr_foreground_new :: (CUShort -> (CUShort -> (CUShort -> (IO (Ptr ())))))
foreign import ccall unsafe "pango_attr_background_new"
pango_attr_background_new :: (CUShort -> (CUShort -> (CUShort -> (IO (Ptr ())))))
foreign import ccall unsafe "pango_attr_underline_new"
pango_attr_underline_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_underline_color_new"
pango_attr_underline_color_new :: (CUShort -> (CUShort -> (CUShort -> (IO (Ptr ())))))
foreign import ccall unsafe "pango_attr_strikethrough_new"
pango_attr_strikethrough_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_strikethrough_color_new"
pango_attr_strikethrough_color_new :: (CUShort -> (CUShort -> (CUShort -> (IO (Ptr ())))))
foreign import ccall unsafe "pango_attr_rise_new"
pango_attr_rise_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_shape_new"
pango_attr_shape_new :: ((Ptr ()) -> ((Ptr ()) -> (IO (Ptr ()))))
foreign import ccall unsafe "pango_attr_scale_new"
pango_attr_scale_new :: (CDouble -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_fallback_new"
pango_attr_fallback_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_letter_spacing_new"
pango_attr_letter_spacing_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_gravity_new"
pango_attr_gravity_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_gravity_hint_new"
pango_attr_gravity_hint_new :: (CInt -> (IO (Ptr ())))
foreign import ccall unsafe "pango_parse_markup"
pango_parse_markup :: ((Ptr CChar) -> (CInt -> (CUInt -> ((Ptr ()) -> ((Ptr (Ptr CChar)) -> ((Ptr CUInt) -> ((Ptr (Ptr ())) -> (IO CInt))))))))
foreign import ccall unsafe "g_free"
g_free :: ((Ptr ()) -> (IO ()))
foreign import ccall unsafe "pango_attr_list_get_iterator"
pango_attr_list_get_iterator :: ((Ptr ()) -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_iterator_get_attrs"
pango_attr_iterator_get_attrs :: ((Ptr ()) -> (IO (Ptr ())))
foreign import ccall unsafe "pango_attr_iterator_next"
pango_attr_iterator_next :: ((Ptr ()) -> (IO CInt))
foreign import ccall unsafe "pango_attr_iterator_destroy"
pango_attr_iterator_destroy :: ((Ptr ()) -> (IO ()))
foreign import ccall unsafe "pango_attribute_destroy"
pango_attribute_destroy :: ((Ptr ()) -> (IO ()))