aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2018-02-02 10:00:14 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2018-02-02 10:00:14 -0800
commiteeafb3fa773e992174dd460d093653cd77255ce5 (patch)
treea2fdb6a1fb67e46c13fb334625e8c2095227f0f2
parente232faf5ee998c0d24169f003f010091996c0c8c (diff)
downloadpandoc-eeafb3fa773e992174dd460d093653cd77255ce5.tar.gz
Determine image size for PDFs.
Closes #4322.
-rw-r--r--src/Text/Pandoc/ImageSize.hs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/Text/Pandoc/ImageSize.hs b/src/Text/Pandoc/ImageSize.hs
index 65559e1ce..0a811d545 100644
--- a/src/Text/Pandoc/ImageSize.hs
+++ b/src/Text/Pandoc/ImageSize.hs
@@ -138,7 +138,7 @@ imageSize opts img =
Just Jpeg -> jpegSize img
Just Svg -> mbToEither "could not determine SVG size" $ svgSize opts img
Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
- Just Pdf -> Left "could not determine PDF size" -- TODO
+ Just Pdf -> mbToEither "Could not determine PDF size" $ pdfSize img
Nothing -> Left "could not determine image type"
where mbToEither msg Nothing = Left msg
mbToEither _ (Just x) = Right x
@@ -277,6 +277,27 @@ epsSize img = do
, dpiY = 72 }
_ -> mzero
+pdfSize :: ByteString -> Maybe ImageSize
+pdfSize img =
+ case dropWhile (\l -> not (l == "stream" ||
+ "/MediaBox" `B.isPrefixOf` l)) (B.lines img) of
+ (x:_)
+ | "/MediaBox" `B.isPrefixOf` x
+ -> case B.words $ B.filter (\c -> c /= '[' && c /= ']')
+ $ B.drop 10 x of
+ [x1, y1, x2, y2] -> do
+ x1' <- safeRead $ B.unpack x1
+ x2' <- safeRead $ B.unpack x2
+ y1' <- safeRead $ B.unpack y1
+ y2' <- safeRead $ B.unpack y2
+ return ImageSize{
+ pxX = x2' - x1'
+ , pxY = y2' - y1'
+ , dpiX = 72
+ , dpiY = 72 }
+ _ -> mzero
+ _ -> mzero
+
pngSize :: ByteString -> Maybe ImageSize
pngSize img = do
let (h, rest) = B.splitAt 8 img