Skip to content

Commit

Permalink
fix(docx): improve DPI on SVG fallback PNG
Browse files Browse the repository at this point in the history
Pass --width and --height to `rsvg-convert` where available.

Signed-off-by: Edwin Török <edwin@etorok.net>
  • Loading branch information
edwintorok committed Dec 27, 2023
1 parent ce30f8b commit a03569e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Text/Pandoc/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import qualified Text.Pandoc.UTF8 as UTF8
#ifndef _WINDOWS
import System.Posix.IO (stdOutput)
import System.Posix.Terminal (queryTerminal)
import Text.Pandoc.ImageSize (desiredSizeInPoints)
import qualified Text.Pandoc.ImageSize as ImageSize
#endif

convertWithOpts :: ScriptingEngine -> Opt -> IO ()
Expand Down Expand Up @@ -382,7 +384,9 @@ createPngFallbacks opts = do
case T.takeWhile (/=';') mt of
"image/svg+xml" -> do
let attr = Data.Map.findWithDefault nullAttr fp attributes
res <- svgToPng (writerDpi opts) bs
let imageSize = ImageSize.imageSize opts (BL.toStrict bs)
let dims = either (const Nothing) (Just . desiredSizeInPoints opts attr) imageSize
res <- svgToPng (writerDpi opts) dims bs
case res of
Right bs' -> do
let fp' = fp <> ".png"
Expand Down
6 changes: 5 additions & 1 deletion src/Text/Pandoc/Image.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ import qualified Control.Exception as E
import Control.Monad.IO.Class (MonadIO(liftIO))
import Text.Pandoc.Class.PandocMonad
import qualified Data.Text as T
import Text.Printf (printf)

-- | Convert svg image to png. rsvg-convert
-- is used and must be available on the path.
svgToPng :: (PandocMonad m, MonadIO m)
=> Int -- ^ DPI
-> Maybe (Double, Double) -- desired size in points
-> L.ByteString -- ^ Input image as bytestring
-> m (Either Text L.ByteString)
svgToPng dpi bs = do
svgToPng dpi dims bs = do
let dpi' = show dpi
let whArg (w, h) = ["--width", printf "%.6fpt" w, "--height", printf "%.6fpt" h]
let args = ["-f","png","-a","--dpi-x",dpi',"--dpi-y",dpi']
++ maybe [] whArg dims
trace (T.intercalate " " $ map T.pack $ "rsvg-convert" : args)
liftIO $ E.catch
(do (exit, out) <- pipeProcess Nothing "rsvg-convert"
Expand Down

0 comments on commit a03569e

Please sign in to comment.