diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-03-20 12:22:17 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-03-20 12:25:09 -0700 |
commit | 957314143faec08b4687822557dac6ac32216cb9 (patch) | |
tree | caa0343bd37edd28280b06bd1faa3ba424c2e6e0 /src/Text | |
parent | 6be8f4e953b6ba9a7af1b707ef3902f8a5b7b8e2 (diff) | |
download | pandoc-957314143faec08b4687822557dac6ac32216cb9.tar.gz |
Improve pdfSize in ImageSize.
Improves fix to #4322.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/ImageSize.hs | 45 |
1 files changed, 25 insertions, 20 deletions
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 |