Skip to content

Commit

Permalink
Text.Pandoc.ImageSize: Handle EPS.
Browse files Browse the repository at this point in the history
Closes #903.  This change will make EPS images properly sized
on conversion to Word.
  • Loading branch information
jgm committed Jul 17, 2013
1 parent 94c9825 commit b2385d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Text/Pandoc/ImageSize.hs
Expand Up @@ -34,11 +34,12 @@ import Data.ByteString (ByteString, unpack)
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Data.Bits
import Text.Pandoc.Shared (safeRead)

-- quick and dirty functions to get image sizes
-- algorithms borrowed from wwwis.pl

data ImageType = Png | Gif | Jpeg | Pdf deriving Show
data ImageType = Png | Gif | Jpeg | Pdf | Eps deriving Show

data ImageSize = ImageSize{
pxX :: Integer
Expand All @@ -54,6 +55,9 @@ imageType img = case B.take 4 img of
"\x47\x49\x46\x38" -> return Gif
"\xff\xd8\xff\xe0" -> return Jpeg
"%PDF" -> return Pdf
"%!PS"
| (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
-> return Eps
_ -> fail "Unknown image type"

imageSize :: ByteString -> Maybe ImageSize
Expand All @@ -63,6 +67,7 @@ imageSize img = do
Png -> pngSize img
Gif -> gifSize img
Jpeg -> jpegSize img
Eps -> epsSize img
Pdf -> Nothing -- TODO

sizeInPixels :: ImageSize -> (Integer, Integer)
Expand All @@ -71,6 +76,23 @@ sizeInPixels s = (pxX s, pxY s)
sizeInPoints :: ImageSize -> (Integer, Integer)
sizeInPoints s = (pxX s * 72 `div` dpiX s, pxY s * 72 `div` dpiY s)

epsSize :: ByteString -> Maybe ImageSize
epsSize img = do
let ls = takeWhile ("%" `B.isPrefixOf`) $ B.lines img
let ls' = dropWhile (not . ("%%BoundingBox:" `B.isPrefixOf`)) ls
case ls' of
[] -> mzero
(x:_) -> case B.words x of
(_:_:_:ux:uy:[]) -> do
ux' <- safeRead $ B.unpack ux
uy' <- safeRead $ B.unpack uy
return ImageSize{
pxX = ux'
, pxY = uy'
, dpiX = 72
, dpiY = 72 }
_ -> mzero

pngSize :: ByteString -> Maybe ImageSize
pngSize img = do
let (h, rest) = B.splitAt 8 img
Expand Down
1 change: 1 addition & 0 deletions src/Text/Pandoc/Writers/Docx.hs
Expand Up @@ -776,6 +776,7 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
Just Jpeg -> ".jpeg"
Just Gif -> ".gif"
Just Pdf -> ".pdf"
Just Eps -> ".eps"
Nothing -> takeExtension src
if null imgext
then -- without an extension there is no rule for content type
Expand Down

0 comments on commit b2385d0

Please sign in to comment.