| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
{-# LANGUAGE TypeFamilies #-} |
| 3 |
|
| 4 |
----------------------------------------------------------------------------- |
| 5 |
-- | |
| 6 |
-- Module : Diagrams.Backend.Cairo |
| 7 |
-- Copyright : (c) 2011 Diagrams-cairo team (see LICENSE) |
| 8 |
-- License : BSD-style (see LICENSE) |
| 9 |
-- Maintainer : [email protected] |
| 10 |
-- |
| 11 |
-- A full-featured rendering backend for diagrams using the |
| 12 |
-- cairo rendering engine. |
| 13 |
-- |
| 14 |
-- To invoke the cairo backend, you have three options. |
| 15 |
-- |
| 16 |
-- * You can use the "Diagrams.Backend.Cairo.CmdLine" module to create |
| 17 |
-- standalone executables which output images when invoked. |
| 18 |
-- |
| 19 |
-- * You can use the 'renderCairo' function provided by this module, |
| 20 |
-- which gives you more flexible programmatic control over when and |
| 21 |
-- how images are output (making it easy to, for example, write a |
| 22 |
-- single program that outputs multiple images, or one that outputs |
| 23 |
-- images dynamically based on user input, and so on). |
| 24 |
-- |
| 25 |
-- * Finally, for the most flexibility, you can directly |
| 26 |
-- use methods from the |
| 27 |
-- 'Diagrams.Core.Types.Backend' instance for @Cairo@. In particular, |
| 28 |
-- 'Diagrams.Core.Types.renderDia' has the generic type |
| 29 |
-- |
| 30 |
-- > renderDia :: b -> Options b v n -> QDiagram b v n m -> Result b v n |
| 31 |
-- |
| 32 |
-- (omitting a few type class constraints). @b@ represents the |
| 33 |
-- backend type, @v@ the vector space, @n@ the numeric field, and @m@ |
| 34 |
-- the type of monoidal query annotations on the diagram. 'Options' |
| 35 |
-- and 'Result' are associated data and type families, respectively, |
| 36 |
-- which yield the type of option records and rendering results |
| 37 |
-- specific to any particular backend. For @b ~ Cairo@, @v ~ V2@, and |
| 38 |
-- @n ~ Double@, we have |
| 39 |
-- |
| 40 |
-- > data family Options Cairo V2 Double = CairoOptions |
| 41 |
-- > { _cairoFileName :: String -- ^ The name of the file you want generated |
| 42 |
-- > , _cairoSizeSpec :: SizeSpec V2 Double -- ^ The requested size of the output |
| 43 |
-- > , _cairoOutputType :: OutputType -- ^ the output format and associated options |
| 44 |
-- > , _cairoBypassAdjust :: Bool -- ^ Should the 'adjustDia' step be bypassed during rendering? |
| 45 |
-- > } |
| 46 |
-- |
| 47 |
-- @ |
| 48 |
-- type family Result Cairo V2 Double = (IO (), 'Graphics.Rendering.Cairo.Render' ()) |
| 49 |
-- @ |
| 50 |
-- |
| 51 |
-- So the type of 'renderDia' resolves to |
| 52 |
-- |
| 53 |
-- @ |
| 54 |
-- renderDia :: Cairo -> Options Cairo V2 Double -> QQDiagram Cairo V2 Double Any m -> (IO (), 'Graphics.Rendering.Cairo.Render' ()) |
| 55 |
-- @ |
| 56 |
-- |
| 57 |
-- which you could call like so: |
| 58 |
-- |
| 59 |
-- @ |
| 60 |
-- renderDia Cairo (CairoOptions \"foo.png\" (Width 250) PNG False) (myDiagram :: QDiagram Cairo V2 Double Any) |
| 61 |
-- @ |
| 62 |
-- |
| 63 |
-- This would return a pair; the first element is an @IO ()@ action |
| 64 |
-- which will write out @foo.png@ to disk, and the second is a cairo |
| 65 |
-- rendering action which can be used, for example, to directly draw |
| 66 |
-- to a Gtk window. Note the type annotation on @myDiagram@ which may |
| 67 |
-- be necessary to fix the type variable @m@; this example uses the |
| 68 |
-- type synonym @Diagram b = QDiagram b (V b) (N b) Any@ to fix @m = Any@ |
| 69 |
-- and fix @v@ and @n@ to backend specific types. |
| 70 |
-- |
| 71 |
----------------------------------------------------------------------------- |
| 72 |
module Diagrams.Backend.Cairo |
| 73 |
|
| 74 |
( -- * Rendering |
| 75 |
renderCairo |
| 76 |
|
| 77 |
-- * Cairo-supported output formats |
| 78 |
, OutputType(..) |
| 79 |
|
| 80 |
-- * Cairo-specific options |
| 81 |
-- $CairoOptions |
| 82 |
|
| 83 |
-- The below CPP hack is needed because GHC 7.0.x has a bug regarding |
| 84 |
-- (re?)export of data family constructors; in particular the below |
| 85 |
-- export causes the error "Not in scope: type constructor or class |
| 86 |
-- `Options'" even though |
| 87 |
-- http://www.haskell.org/haskellwiki/GHC/Type_families#Import_and_export |
| 88 |
-- seems to indicate it should be supported. When using 7.0.x one |
| 89 |
-- must import Diagrams.Backend.Cairo.Internal in order to bring |
| 90 |
-- CairoOptions into scope. |
| 91 |
-- GHC 7.4.0 regression? |
| 92 |
#if __GLASGOW_HASKELL__ >= 702 && __GLASGOW_HASKELL__ < 704 |
| 93 |
, Options(..) |
| 94 |
#endif |
| 95 |
|
| 96 |
-- * Backend token |
| 97 |
, Cairo(..) |
| 98 |
, B |
| 99 |
) where |
| 100 |
|
| 101 |
import System.FilePath (takeExtension) |
| 102 |
|
| 103 |
import Diagrams.Backend.Cairo.Internal |
| 104 |
import Diagrams.Prelude |
| 105 |
|
| 106 |
-- $CairoOptions |
| 107 |
-- |
| 108 |
-- Unfortunately, Haddock does not yet support documentation for |
| 109 |
-- associated data families, so we must just provide it manually. |
| 110 |
-- This module defines |
| 111 |
-- |
| 112 |
-- > data family Options Cairo V2 Double = CairoOptions |
| 113 |
-- > { _cairoFileName :: String -- ^ The name of the file you want generated |
| 114 |
-- > , _cairoSizeSpec :: SizeSpec V2 Double -- ^ The requested size of the output |
| 115 |
-- > , _cairoOutputType :: OutputType -- ^ the output format and associated options |
| 116 |
-- > , _cairoBypassAdjust :: Bool -- ^ Should the 'adjustDia' step be bypassed during rendering? |
| 117 |
-- > } |
| 118 |
-- |
| 119 |
-- See the documentation at the top of "Diagrams.Backend.Cairo" for |
| 120 |
-- information on how to make use of this. |
| 121 |
|
| 122 |
|
| 123 |
-- | Render a diagram using the cairo backend, writing to the given |
| 124 |
-- output file and using the requested size. The output type (PNG, |
| 125 |
-- PS, PDF, or SVG) is determined automatically from the output file |
| 126 |
-- extension. |
| 127 |
-- |
| 128 |
-- This function is provided as a convenience; if you need more |
| 129 |
-- flexibility than it provides, you can call 'renderDia' directly, |
| 130 |
-- as described above. |
| 131 |
renderCairo :: FilePath -> SizeSpec V2 Double -> QDiagram Cairo V2 Double Any -> IO () |
| 132 |
renderCairo outFile sizeSpec d |
| 133 |
= fst (renderDia Cairo (CairoOptions outFile sizeSpec outTy False) d) |
| 134 |
where |
| 135 |
outTy = |
| 136 |
case takeExtension outFile of |
| 137 |
".png" -> PNG |
| 138 |
".ps" -> PS |
| 139 |
".pdf" -> PDF |
| 140 |
".svg" -> SVG |
| 141 |
_ -> PNG |