@@ -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