aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2020-02-12 21:37:42 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2020-02-12 21:57:00 -0800
commit3a181f0a978cb838615b86ffbc705255d92fdf66 (patch)
tree2462e3740d139b85a9b7288daa909eb24121697e /src/Text/Pandoc
parentf5ea5f0aad1d5000b326ce4c45c92fdfb1a4b5d3 (diff)
downloadpandoc-3a181f0a978cb838615b86ffbc705255d92fdf66.tar.gz
Add Text.Pandoc.Image with unexported svgToPng.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Image.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Image.hs b/src/Text/Pandoc/Image.hs
new file mode 100644
index 000000000..634d37769
--- /dev/null
+++ b/src/Text/Pandoc/Image.hs
@@ -0,0 +1,40 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, CPP #-}
+{-# LANGUAGE ViewPatterns #-}
+{- |
+Module : Text.Pandoc.Image
+Copyright : Copyright (C) 2020 John MacFarlane
+License : GNU GPL, version 2 or above
+
+Maintainer : John MacFarlane <jgm@berkeley.edu>
+Stability : alpha
+Portability : portable
+
+Functions for converting images.
+-}
+module Text.Pandoc.Image ( svgToPng ) where
+import Prelude
+import Text.Pandoc.Options (WriterOptions(..))
+import Text.Pandoc.Process (pipeProcess)
+import qualified Data.ByteString.Lazy as L
+import System.Exit
+import Data.Text (Text)
+import Text.Pandoc.Shared (tshow)
+import qualified Control.Exception as E
+
+-- | Convert svg image to png. rsvg-convert
+-- is used and must be available on the path.
+svgToPng :: WriterOptions
+ -> L.ByteString -- ^ Input image as bytestring
+ -> IO (Either Text L.ByteString)
+svgToPng opts bs = do
+ let dpi = show $ writerDpi opts
+ E.catch
+ (do (exit, out) <- pipeProcess Nothing "rsvg-convert"
+ ["-f","png","-a","--dpi-x",dpi,"--dpi-y",dpi]
+ bs
+ if exit == ExitSuccess
+ then return $ Right out
+ else return $ Left "conversion from SVG failed")
+ (\(e :: E.SomeException) -> return $ Left $
+ "check that rsvg-convert is in path.\n" <> tshow e)