git.iko.soy/typeable/pandoc
on master help sign in
Improve pdfSize in ImageSize.

Improves fix to #4322.

commit957314143faec08b4687822557dac6ac32216cb9 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 20 Mar 2019, 12:22 (8 years ago) parent6be8f4e
2 files changed+27−21
pandoc.cabal+2−1 src/Text/Pandoc/ImageSize.hs+25−20
pandoc.cabal+2−1
@@ -404,7 +404,8 @@ library
404404 case-insensitive >= 1.2 && < 1.3,
405405 unicode-transforms >= 0.3 && < 0.4,
406406 HsYAML >= 0.1.1.1 && < 0.2,
407− ipynb >= 0.1 && < 0.2
407+ ipynb >= 0.1 && < 0.2,
408+ attoparsec >= 0.12 && < 0.14
408409 if impl(ghc < 8.0)
409410 build-depends: semigroups == 0.18.*,
410411 -- basement 0.0.8 and foundation 0.0.21, transitive
src/Text/Pandoc/ImageSize.hs+25−20
@@ -50,7 +50,9 @@ import qualified Text.Pandoc.UTF8 as UTF8
5050import qualified Text.XML.Light as Xml
5151import qualified Data.Map as M
5252import Control.Monad.Except
53+import Control.Applicative
5354import Data.Maybe (fromMaybe)
55+import qualified Data.Attoparsec.ByteString.Char8 as A
5456
5557-- quick and dirty functions to get image sizes
5658-- algorithms borrowed from wwwis.pl
@@ -267,26 +269,29 @@ epsSize img = do
267269
268270pdfSize :: ByteString -> Maybe ImageSize
269271pdfSize img =
270− case dropWhile (\l -> not (l == "stream" ||
271− "/MediaBox" `B.isPrefixOf` l)) (B.lines img) of
272− (x:_)
273− | "/MediaBox" `B.isPrefixOf` x
274− -> case B.words . B.takeWhile (/=']')
275− . B.drop 1
276− . B.dropWhile (/='[')
277− $ x of
278− [x1, y1, x2, y2] -> do
279− x1' <- safeRead $ B.unpack x1
280− x2' <- safeRead $ B.unpack x2
281− y1' <- safeRead $ B.unpack y1
282− y2' <- safeRead $ B.unpack y2
283− return ImageSize{
284− pxX = x2' - x1'
285− , pxY = y2' - y1'
286− , dpiX = 72
287− , dpiY = 72 }
288− _ -> mzero
289− _ -> mzero
272+ case A.parseOnly pPdfSize img of
273+ Left _ -> Nothing
274+ Right sz -> Just sz
275+
276+pPdfSize :: A.Parser ImageSize
277+pPdfSize = do
278+ A.skipWhile (/='/')
279+ A.char8 '/'
280+ (do A.string "MediaBox"
281+ A.char8 '['
282+ [x1,y1,x2,y2] <- A.count 4 $ do
283+ A.skipWhile (==' ')
284+ raw <- A.many1 $ A.satisfy (\c -> isDigit c || c == '.')
285+ case safeRead raw of
286+ Just (r :: Double) -> return $ floor r
287+ Nothing -> mzero
288+ A.char8 ']'
289+ return $ ImageSize{
290+ pxX = x2 - x1
291+ , pxY = y2 - y1
292+ , dpiX = 72
293+ , dpiY = 72 }
294+ ) <|> pPdfSize
290295
291296pngSize :: ByteString -> Maybe ImageSize
292297pngSize img = do