git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.ImageSize: Handle EPS.

Closes #903. This change will make EPS images properly sized on conversion to Word.

commitb2385d0e9bf13f2fc152a3983893c47f2ab5d4c0 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 16 Jul 2013, 22:04 (13 years ago) parent94c9825
2 files changed+24−1
src/Text/Pandoc/ImageSize.hs+23−1 src/Text/Pandoc/Writers/Docx.hs+1
src/Text/Pandoc/ImageSize.hs+23−1
@@ -34,11 +34,12 @@ import Data.ByteString (ByteString, unpack)
3434import qualified Data.ByteString.Char8 as B
3535import Control.Monad
3636import Data.Bits
37+import Text.Pandoc.Shared (safeRead)
3738
3839-- quick and dirty functions to get image sizes
3940-- algorithms borrowed from wwwis.pl
4041
41−data ImageType = Png | Gif | Jpeg | Pdf deriving Show
42+data ImageType = Png | Gif | Jpeg | Pdf | Eps deriving Show
4243
4344data ImageSize = ImageSize{
4445 pxX :: Integer
@@ -54,6 +55,9 @@ imageType img = case B.take 4 img of
5455 "\x47\x49\x46\x38" -> return Gif
5556 "\xff\xd8\xff\xe0" -> return Jpeg
5657 "%PDF" -> return Pdf
58+ "%!PS"
59+ | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
60+ -> return Eps
5761 _ -> fail "Unknown image type"
5862
5963imageSize :: ByteString -> Maybe ImageSize
@@ -63,6 +67,7 @@ imageSize img = do
6367 Png -> pngSize img
6468 Gif -> gifSize img
6569 Jpeg -> jpegSize img
70+ Eps -> epsSize img
6671 Pdf -> Nothing -- TODO
6772
6873sizeInPixels :: ImageSize -> (Integer, Integer)
@@ -71,6 +76,23 @@ sizeInPixels s = (pxX s, pxY s)
7176sizeInPoints :: ImageSize -> (Integer, Integer)
7277sizeInPoints s = (pxX s * 72 `div` dpiX s, pxY s * 72 `div` dpiY s)
7378
79+epsSize :: ByteString -> Maybe ImageSize
80+epsSize img = do
81+ let ls = takeWhile ("%" `B.isPrefixOf`) $ B.lines img
82+ let ls' = dropWhile (not . ("%%BoundingBox:" `B.isPrefixOf`)) ls
83+ case ls' of
84+ [] -> mzero
85+ (x:_) -> case B.words x of
86+ (_:_:_:ux:uy:[]) -> do
87+ ux' <- safeRead $ B.unpack ux
88+ uy' <- safeRead $ B.unpack uy
89+ return ImageSize{
90+ pxX = ux'
91+ , pxY = uy'
92+ , dpiX = 72
93+ , dpiY = 72 }
94+ _ -> mzero
95+
7496pngSize :: ByteString -> Maybe ImageSize
7597pngSize img = do
7698 let (h, rest) = B.splitAt 8 img
src/Text/Pandoc/Writers/Docx.hs+1
@@ -776,6 +776,7 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
776776 Just Jpeg -> ".jpeg"
777777 Just Gif -> ".gif"
778778 Just Pdf -> ".pdf"
779+ Just Eps -> ".eps"
779780 Nothing -> takeExtension src
780781 if null imgext
781782 then -- without an extension there is no rule for content type