Skip to content

Commit 6da581d

Browse files
committed
fix(docx): improve DPI on SVG fallback PNG
Pass --width and --height to `rsvg-convert` where available. Signed-off-by: Edwin Török <[email protected]>
1 parent ce30f8b commit 6da581d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Text/Pandoc/App.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import Text.Pandoc.URI (isURI)
7171
import Text.Pandoc.Writers.Shared (lookupMetaString)
7272
import Text.Pandoc.Readers.Markdown (yamlToMeta)
7373
import qualified Text.Pandoc.UTF8 as UTF8
74+
import Text.Pandoc.ImageSize (desiredSizeInPoints, imageSize)
7475
#ifndef _WINDOWS
7576
import System.Posix.IO (stdOutput)
7677
import System.Posix.Terminal (queryTerminal)
@@ -382,7 +383,9 @@ createPngFallbacks opts = do
382383
case T.takeWhile (/=';') mt of
383384
"image/svg+xml" -> do
384385
let attr = Data.Map.findWithDefault nullAttr fp attributes
385-
res <- svgToPng (writerDpi opts) bs
386+
let imageSize' = imageSize opts (BL.toStrict bs)
387+
let dims = either (const Nothing) (Just . desiredSizeInPoints opts attr) imageSize'
388+
res <- svgToPng (writerDpi opts) dims bs
386389
case res of
387390
Right bs' -> do
388391
let fp' = fp <> ".png"

src/Text/Pandoc/Image.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ import qualified Control.Exception as E
2020
import Control.Monad.IO.Class (MonadIO(liftIO))
2121
import Text.Pandoc.Class.PandocMonad
2222
import qualified Data.Text as T
23+
import Text.Printf (printf)
2324

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

0 commit comments

Comments
 (0)