Chart-cairo 1.0 → 1.1
raw patch · 2 files changed
+83/−70 lines, 2 filesdep +lensdep ~Chartdep ~basedep ~colour
Dependencies added: lens
Dependency ranges changed: Chart, base, colour, operational
Files
Chart-cairo.cabal view
@@ -1,5 +1,5 @@ Name: Chart-cairo-Version: 1.0+Version: 1.1 License: BSD3 License-file: LICENSE Copyright: Tim Docker, 2006-2010@@ -17,10 +17,11 @@ , old-locale , time, mtl, array , cairo >= 0.9.11- , colour >= 2.2.1+ , colour >= 2.2.1 && < 2.4 , data-default-class < 0.1- , operational >= 0.2.2- , Chart >= 1.0+ , operational >= 0.2.2 && < 0.3+ , lens >= 3.9 && < 3.11+ , Chart >= 1.1 && < 1.2 Exposed-modules: Graphics.Rendering.Chart.Backend.Cairo
Graphics/Rendering/Chart/Backend/Cairo.hs view
@@ -1,20 +1,27 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GADTs #-}+{-# LANGUAGE TemplateHaskell #-} -- | The backend to render charts with cairo. module Graphics.Rendering.Chart.Backend.Cairo- ( CairoBackend(..)+ ( FileFormat(..)+ , FileOptions(..) , runBackend- , renderToFile+ , renderableToFile , defaultEnv - , renderableToPNGFile- , renderableToPDFFile- , renderableToPSFile- , renderableToSVGFile+ , fo_size+ , fo_format++ , cBackendToFile++ , renderableToPNGFile -- deprecated+ , renderableToPDFFile -- deprecated+ , renderableToPSFile -- deprecated+ , renderableToSVGFile -- deprecated - , sparkLineToPDF- , sparkLineToPNG+ , sparkLineToPDF -- deprecated+ , sparkLineToPNG -- deprecated ) where import Data.Default.Class@@ -24,6 +31,7 @@ import Data.List (unfoldr) import Data.Monoid +import Control.Lens(makeLenses) import Control.Monad.Reader import Control.Monad.Operational @@ -162,46 +170,74 @@ -- Output rendering functions -- ----------------------------------------------------------------------- -data CairoBackend = CairoPNG | CairoSVG | CairoPS | CairoPDF+data FileFormat = PNG+ | SVG+ | PS+ | PDF -renderToFile :: ChartBackend a -> CairoBackend -> Int -> Int -> FilePath -> IO ()-renderToFile m b = case b of- CairoPNG -> \w h f -> cRenderToPNGFile m w h f >> return ()- CairoSVG -> cRenderToSVGFile m- CairoPS -> cRenderToPSFile m- CairoPDF -> cRenderToPDFFile m+data FileOptions = FileOptions {+ _fo_size :: (Int,Int),+ _fo_format :: FileFormat+} --- | Output the given renderable to a PNG file of the specifed size--- (in pixels), to the specified file.-renderableToPNGFile :: Renderable a -> Int -> Int -> FilePath -> IO (PickFn a)-renderableToPNGFile r width height path =- cRenderToPNGFile cr width height path+instance Default FileOptions where+ def = FileOptions (800,600) PNG++-- | Generate an image file for the given renderable, at the specified path. Size and+-- format are set through the `FileOptions` parameter.+renderableToFile :: FileOptions -> Renderable a -> FilePath -> IO (PickFn a)+renderableToFile fo r path = cBackendToFile fo cr path where cr = render r (fromIntegral width, fromIntegral height)+ (width,height) = _fo_size fo +-- | Generate an image file for the given drawing instructions, at the specified path. Size and+-- format are set through the `FileOptions` parameter.+cBackendToFile :: FileOptions -> ChartBackend a -> FilePath -> IO a+cBackendToFile fo cr path = do+ case (_fo_format fo) of+ PS -> write C.withPSSurface+ PDF -> write C.withPDFSurface+ SVG -> write C.withSVGSurface+ PNG -> writePNG+ where+ write withSurface = do+ withSurface path (fromIntegral width) (fromIntegral height) $ \result -> do+ pf <- C.renderWith result $ do+ pf <- runBackend (defaultEnv vectorAlignmentFns) cr+ C.showPage+ return pf+ C.surfaceFinish result+ return pf++ writePNG = C.withImageSurface C.FormatARGB32 width height $ \result -> do+ pf <- C.renderWith result $ runBackend (defaultEnv bitmapAlignmentFns) cr+ C.surfaceWriteToPNG result path+ return pf++ (width,height) = _fo_size fo++{-# DEPRECATED renderableToPNGFile "use renderableToFile" #-}+renderableToPNGFile :: Renderable a -> Int -> Int -> FilePath -> IO (PickFn a)+renderableToPNGFile r width height path = renderableToFile (FileOptions (width,height) PNG) r path+ -- | Output the given renderable to a PDF file of the specifed size -- (in points), to the specified file.+{-# DEPRECATED renderableToPDFFile "use renderableToFile" #-} renderableToPDFFile :: Renderable a -> Int -> Int -> FilePath -> IO ()-renderableToPDFFile r width height path =- cRenderToPDFFile cr width height path- where- cr = render r (fromIntegral width, fromIntegral height)+renderableToPDFFile r width height path = void $ renderableToFile (FileOptions (width,height) PDF) r path -- | Output the given renderable to a postscript file of the specifed size -- (in points), to the specified file.+{-# DEPRECATED renderableToPSFile "use renderableToFile" #-} renderableToPSFile :: Renderable a -> Int -> Int -> FilePath -> IO ()-renderableToPSFile r width height path = - cRenderToPSFile cr width height path- where- cr = render r (fromIntegral width, fromIntegral height)+renderableToPSFile r width height path = void $ renderableToFile (FileOptions (width,height) PS) r path -- | Output the given renderable to an SVG file of the specifed size -- (in points), to the specified file.+{-# DEPRECATED renderableToSVGFile "use renderableToFile" #-} renderableToSVGFile :: Renderable a -> Int -> Int -> FilePath -> IO ()-renderableToSVGFile r width height path =- cRenderToSVGFile cr width height path- where- cr = render r (fromIntegral width, fromIntegral height)+renderableToSVGFile r width height path = void $ renderableToFile (FileOptions (width,height) SVG) r path -- ----------------------------------------------------------------------- -- Type Conversions: Chart -> Cairo@@ -283,46 +319,23 @@ cArcNegative :: Point -> Double -> Double -> Double -> C.Render () cArcNegative p r a1 a2 = C.arcNegative (p_x p) (p_y p) r a1 a2 -cRenderToPNGFile :: ChartBackend a -> Int -> Int -> FilePath -> IO a-cRenderToPNGFile cr width height path = - C.withImageSurface C.FormatARGB32 width height $ \result -> do- a <- C.renderWith result $ runBackend' (defaultEnv bitmapAlignmentFns) cr - C.surfaceWriteToPNG result path- return a--cRenderToPDFFile :: ChartBackend a -> Int -> Int -> FilePath -> IO ()-cRenderToPDFFile = cRenderToFile C.withPDFSurface--cRenderToPSFile :: ChartBackend a -> Int -> Int -> FilePath -> IO ()-cRenderToPSFile = cRenderToFile C.withPSSurface--cRenderToSVGFile :: ChartBackend a -> Int -> Int -> FilePath -> IO ()-cRenderToSVGFile = cRenderToFile C.withSVGSurface--cRenderToFile withSurface cr width height path = - withSurface path (fromIntegral width) (fromIntegral height) $ \result -> do- C.renderWith result $ do- runBackend' (defaultEnv vectorAlignmentFns) cr- C.showPage- C.surfaceFinish result- -- ----------------------------------------------------------------------- -- Simple Instances -- ----------------------------------------------------------------------- instance PlotPDFType (IO a) where pld fn args = do- renderableToPDFFile (layout1DddToRenderable $ uplot (reverse args)) 640 480 fn+ renderableToPDFFile (layoutDddToRenderable $ uplot (reverse args)) 640 480 fn return undefined instance PlotPSType (IO a) where pls fn args = do- renderableToPSFile (layout1DddToRenderable $ uplot (reverse args)) 640 480 fn+ renderableToPSFile (layoutDddToRenderable $ uplot (reverse args)) 640 480 fn return undefined instance PlotPNGType (IO a) where plp fn args = do- renderableToPNGFile (layout1DddToRenderable $ uplot (reverse args)) 640 480 fn+ renderableToPNGFile (layoutDddToRenderable $ uplot (reverse args)) 640 480 fn return undefined -- -----------------------------------------------------------------------@@ -330,14 +343,13 @@ -- ----------------------------------------------------------------------- -- | Generate a PNG for the sparkline, using its natural size.+{-# DEPRECATED sparkLineToPNG "use renderableToFile" #-} sparkLineToPNG :: FilePath -> SparkLine -> IO (PickFn ())-sparkLineToPNG fp sp = renderableToPNGFile (sparkLineToRenderable sp)- (sparkWidth sp)- (so_height (sl_options sp))- fp+sparkLineToPNG fp sp = renderableToFile (FileOptions (sparkSize sp) PNG) (sparkLineToRenderable sp) fp+ -- | Generate a PDF for the sparkline, using its natural size.+{-# DEPRECATED sparkLineToPDF "use renderableToFile" #-} sparkLineToPDF :: FilePath -> SparkLine -> IO ()-sparkLineToPDF fp sp = renderableToPDFFile (sparkLineToRenderable sp)- (sparkWidth sp)- (so_height (sl_options sp))- fp+sparkLineToPDF fp sp = void $ renderableToFile (FileOptions (sparkSize sp) PDF) (sparkLineToRenderable sp) fp++$( makeLenses ''FileOptions )