@@ -138,7 +138,7 @@ imageSize opts img =
138138 Just Jpeg -> jpegSize img
139139 Just Svg -> mbToEither "could not determine SVG size" $ svgSize opts img
140140 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
141− Just Pdf -> Left "could not determine PDF size" -- TODO
141+ Just Pdf -> mbToEither "Could not determine PDF size" $ pdfSize img
142142 Nothing -> Left "could not determine image type"
143143 where mbToEither msg Nothing = Left msg
144144 mbToEither _ (Just x) = Right x
@@ -277,6 +277,27 @@ epsSize img = do
277277 , dpiY = 72 }
278278 _ -> mzero
279279
280+pdfSize :: ByteString -> Maybe ImageSize
281+pdfSize img =
282+ case dropWhile (\l -> not (l == "stream" ||
283+ "/MediaBox" `B.isPrefixOf` l)) (B.lines img) of
284+ (x:_)
285+ | "/MediaBox" `B.isPrefixOf` x
286+ -> case B.words $ B.filter (\c -> c /= '[' && c /= ']')
287+ $ B.drop 10 x of
288+ [x1, y1, x2, y2] -> do
289+ x1' <- safeRead $ B.unpack x1
290+ x2' <- safeRead $ B.unpack x2
291+ y1' <- safeRead $ B.unpack y1
292+ y2' <- safeRead $ B.unpack y2
293+ return ImageSize{
294+ pxX = x2' - x1'
295+ , pxY = y2' - y1'
296+ , dpiX = 72
297+ , dpiY = 72 }
298+ _ -> mzero
299+ _ -> mzero
300+
280301pngSize :: ByteString -> Maybe ImageSize
281302pngSize img = do
282303 let (h, rest) = B.splitAt 8 img