ihaskell-diagrams 0.2.0.0 → 0.2.2.0
raw patch · 3 files changed
+85/−23 lines, 3 filesdep +activedep ~basedep ~classy-preludedep ~diagrams
Dependencies added: active
Dependency ranges changed: base, classy-prelude, diagrams, ihaskell
Files
- IHaskell/Display/Diagrams.hs +20/−16
- IHaskell/Display/Diagrams/Animation.hs +55/−0
- ihaskell-diagrams.cabal +10/−7
IHaskell/Display/Diagrams.hs view
@@ -1,24 +1,26 @@-{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}-module IHaskell.Display.Diagrams (diagram) where+{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-} -import ClassyPrelude+module IHaskell.Display.Diagrams (diagram, animation) where -import System.Directory+import ClassyPrelude++import System.Directory import qualified Data.ByteString.Char8 as Char-import System.IO.Unsafe+import System.IO.Unsafe -import Diagrams.Prelude-import Diagrams.Backend.Cairo+import Diagrams.Prelude+import Diagrams.Backend.Cairo -import IHaskell.Display+import IHaskell.Display+import IHaskell.Display.Diagrams.Animation -instance IHaskellDisplay (Diagram Cairo R2) where+instance IHaskellDisplay (QDiagram Cairo V2 Double Any) where display renderable = do png <- diagramData renderable PNG svg <- diagramData renderable SVG return $ Display [png, svg] -diagramData :: Diagram Cairo R2 -> OutputType -> IO DisplayData+diagramData :: Diagram Cairo -> OutputType -> IO DisplayData diagramData renderable format = do switchToTmpDir @@ -31,19 +33,21 @@ -- Write the image. let filename = ".ihaskell-diagram." ++ extension format- renderCairo filename (Height imgHeight) renderable+ renderCairo filename (mkSizeSpec2D (Just imgWidth) (Just imgHeight)) renderable -- Convert to base64.- imgData <- readFile $ fpFromString filename- let value = case format of- PNG -> png (floor imgWidth) (floor imgHeight) $ base64 imgData- SVG -> svg $ Char.unpack imgData+ imgData <- readFile filename+ let value =+ case format of+ PNG -> png (floor imgWidth) (floor imgHeight) $ base64 imgData+ SVG -> svg $ Char.unpack imgData return value+ where extension SVG = "svg" extension PNG = "png" -- Rendering hint.-diagram :: Diagram Cairo R2 -> Diagram Cairo R2+diagram :: Diagram Cairo -> Diagram Cairo diagram = id
+ IHaskell/Display/Diagrams/Animation.hs view
@@ -0,0 +1,55 @@+{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}++module IHaskell.Display.Diagrams.Animation (animation) where++import ClassyPrelude hiding (filename)++import Diagrams.Prelude+import Diagrams.Backend.Cairo+import Diagrams.Backend.Cairo.CmdLine (GifOpts(..))+import Diagrams.Backend.CmdLine (DiagramOpts(..), mainRender)++import IHaskell.Display++instance IHaskellDisplay (QAnimation Cairo V2 Double Any) where+ display renderable = do+ gif <- animationData renderable+ return $ Display [html $ "<img src=\"data:image/gif;base64,"+ ++ gif ++ "\" />"]++animationData :: Animation Cairo V2 Double -> IO String+animationData renderable = do+ switchToTmpDir++ -- Generate the frames+ let fps = 30+ animAdjusted = animEnvelope' fps renderable+ frames = simulate fps animAdjusted+ timediff = 100 `div` ceiling fps :: Int+ frameSet = map (\x -> (x # bg white, timediff)) frames++ -- Compute width and height.+ let shape = activeStart animAdjusted+ w = width shape+ h = height shape+ aspect = w / h+ imgHeight = 300+ imgWidth = aspect * imgHeight++ -- Write the image.+ let filename = ".ihaskell-diagram.gif"+ diagOpts = DiagramOpts+ { _width = Just . ceiling $ imgWidth+ , _height = Just . ceiling $ imgHeight+ , _output = filename+ }+ gifOpts = GifOpts { _dither = True, _noLooping = False, _loopRepeat = Nothing }+ mainRender (diagOpts, gifOpts) frameSet++ -- Convert to ascii represented base64 encoding+ imgData <- readFile filename+ return . unpack . base64 $ imgData++-- Rendering hint.+animation :: Animation Cairo V2 Double -> Animation Cairo V2 Double+animation = id
ihaskell-diagrams.cabal view
@@ -7,7 +7,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.2.0.0+version: 0.2.2.0 -- A short (one-line) description of the package. synopsis: IHaskell display instances for diagram types@@ -51,23 +51,26 @@ exposed-modules: IHaskell.Display.Diagrams -- Modules included in this library but not exported.- -- other-modules: + other-modules: IHaskell.Display.Diagrams.Animation -- Language extensions. default-extensions: DoAndIfThenElse OverloadedStrings -- Other library packages from which modules are imported.- build-depends: base ==4.6.* || ==4.7.*,- classy-prelude >=0.6,+ build-depends: base >=4.6 && <4.9,+ classy-prelude >=0.10.5, bytestring, directory, -- Use diagrams wrapper package to ensure all same versions of subpackages- diagrams==1.2.*,+ diagrams >= 1.3, diagrams-lib, diagrams-cairo,- ihaskell >= 0.5- + ihaskell >= 0.6.2,++ -- The active package, used to represent animations+ active >= 0.2+ -- Directories containing source files. -- hs-source-dirs: