@@ -50,7 +50,9 @@ import qualified Text.Pandoc.UTF8 as UTF8
50import qualified Text.XML.Light as Xml
50import qualified Text.XML.Light as Xml
51import qualified Data.Map as M
51import qualified Data.Map as M
52import Control.Monad.Except
52import Control.Monad.Except
53+import Control.Applicative
53import Data.Maybe (fromMaybe)
54import Data.Maybe (fromMaybe)
55+import qualified Data.Attoparsec.ByteString.Char8 as A
55-- quick and dirty functions to get image sizes
57-- quick and dirty functions to get image sizes
56-- algorithms borrowed from wwwis.pl
58-- algorithms borrowed from wwwis.pl
@@ -267,26 +269,29 @@ epsSize img = do
268pdfSize :: ByteString -> Maybe ImageSize
270pdfSize :: ByteString -> Maybe ImageSize
269pdfSize img =
271pdfSize img =
270− case dropWhile (\l -> not (l == "stream" ||
272+ case A.parseOnly pPdfSize img of
271− "/MediaBox" `B.isPrefixOf` l)) (B.lines img) of
273+ Left _ -> Nothing
272− (x:_)
274+ Right sz -> Just sz
273− | "/MediaBox" `B.isPrefixOf` x
275+
274− -> case B.words . B.takeWhile (/=']')
276+pPdfSize :: A.Parser ImageSize
275− . B.drop 1
277+pPdfSize = do
276− . B.dropWhile (/='[')
278+ A.skipWhile (/='/')
277− $ x of
279+ A.char8 '/'
278− [x1, y1, x2, y2] -> do
280+ (do A.string "MediaBox"
279− x1' <- safeRead $ B.unpack x1
281+ A.char8 '['
280− x2' <- safeRead $ B.unpack x2
282+ [x1,y1,x2,y2] <- A.count 4 $ do
281− y1' <- safeRead $ B.unpack y1
283+ A.skipWhile (==' ')
282− y2' <- safeRead $ B.unpack y2
284+ raw <- A.many1 $ A.satisfy (\c -> isDigit c || c == '.')
285+ case safeRead raw of
286+ Just (r :: Double) -> return $ floor r
283− return ImageSize{
289+ return $ ImageSize{
284− pxX = x2' - x1'
290+ pxX = x2 - x1
285− , pxY = y2' - y1'
291+ , pxY = y2 - y1
286− , dpiX = 72
292+ , dpiX = 72
287− , dpiY = 72 }
293+ , dpiY = 72 }
288− _ -> mzero
294+ ) <|> pPdfSize
291pngSize :: ByteString -> Maybe ImageSize
296pngSize :: ByteString -> Maybe ImageSize
292pngSize img = do
297pngSize img = do