From 957314143faec08b4687822557dac6ac32216cb9 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 20 Mar 2019 12:22:17 -0700 Subject: Improve pdfSize in ImageSize. Improves fix to #4322. --- src/Text/Pandoc/ImageSize.hs | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/ImageSize.hs b/src/Text/Pandoc/ImageSize.hs index 802959484..c5289bbc2 100644 --- a/src/Text/Pandoc/ImageSize.hs +++ b/src/Text/Pandoc/ImageSize.hs @@ -50,7 +50,9 @@ import qualified Text.Pandoc.UTF8 as UTF8 import qualified Text.XML.Light as Xml import qualified Data.Map as M import Control.Monad.Except +import Control.Applicative import Data.Maybe (fromMaybe) +import qualified Data.Attoparsec.ByteString.Char8 as A -- quick and dirty functions to get image sizes -- algorithms borrowed from wwwis.pl @@ -267,26 +269,29 @@ epsSize img = do 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.takeWhile (/=']') - . B.drop 1 - . B.dropWhile (/='[') - $ 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 + case A.parseOnly pPdfSize img of + Left _ -> Nothing + Right sz -> Just sz + +pPdfSize :: A.Parser ImageSize +pPdfSize = do + A.skipWhile (/='/') + A.char8 '/' + (do A.string "MediaBox" + A.char8 '[' + [x1,y1,x2,y2] <- A.count 4 $ do + A.skipWhile (==' ') + raw <- A.many1 $ A.satisfy (\c -> isDigit c || c == '.') + case safeRead raw of + Just (r :: Double) -> return $ floor r + Nothing -> mzero + A.char8 ']' + return $ ImageSize{ + pxX = x2 - x1 + , pxY = y2 - y1 + , dpiX = 72 + , dpiY = 72 } + ) <|> pPdfSize pngSize :: ByteString -> Maybe ImageSize pngSize img = do -- cgit v1.2.3