aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Image.hs
blob: e0c938938de85a9f61fe3696d5f2c68c074f0166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, CPP #-}
{- |
Module      : Text.Pandoc.Image
Copyright   : Copyright (C) 2020-2021 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 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
           return $ if exit == ExitSuccess
              then Right out
              else Left "conversion from SVG failed")
       (\(e :: E.SomeException) -> return $ Left $
           "check that rsvg-convert is in path.\n" <> tshow e)