git.iko.soy/typeable/pandoc
on master help sign in
EMF Image size support (#4375)
commitbb7681a85af76b2b85b263d593f560caf5428def authorAndrew Pritchard <andrewjpritchard@gmail.com> dateMon 19 Feb 2018, 03:12 (9 years ago) parent3776404
3 files changed+39−1
src/Text/Pandoc/ImageSize.hs+37−1 src/Text/Pandoc/Writers/Docx.hs+1 src/Text/Pandoc/Writers/Powerpoint/Output.hs+1
src/Text/Pandoc/ImageSize.hs+37−1
@@ -71,7 +71,7 @@ import Data.Maybe (fromMaybe)
7171-- quick and dirty functions to get image sizes
7272-- algorithms borrowed from wwwis.pl
7373
74−data ImageType = Png | Gif | Jpeg | Svg | Pdf | Eps deriving Show
74+data ImageType = Png | Gif | Jpeg | Svg | Pdf | Eps | Emf deriving Show
7575data Direction = Width | Height
7676instance Show Direction where
7777 show Width = "width"
@@ -125,6 +125,9 @@ imageType img = case B.take 4 img of
125125 "%!PS"
126126 | B.take 4 (B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
127127 -> return Eps
128+ "\x01\x00\x00\x00"
129+ | B.take 4 (B.drop 40 img) == " EMF"
130+ -> return Emf
128131 _ -> mzero
129132
130133findSvgTag :: ByteString -> Bool
@@ -139,6 +142,7 @@ imageSize opts img =
139142 Just Svg -> mbToEither "could not determine SVG size" $ svgSize opts img
140143 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
141144 Just Pdf -> mbToEither "could not determine PDF size" $ pdfSize img
145+ Just Emf -> mbToEither "could not determine EMF size" $ emfSize img
142146 Nothing -> Left "could not determine image type"
143147 where mbToEither msg Nothing = Left msg
144148 mbToEither _ (Just x) = Right x
@@ -357,6 +361,38 @@ svgSize opts img = do
357361 , dpiX = dpi
358362 , dpiY = dpi
359363 }
364+
365+emfSize :: ByteString -> Maybe ImageSize
366+emfSize img =
367+ let
368+ parseheader = runGetOrFail $ do
369+ skip 0x18 -- 0x00
370+ frameL <- getWord32le -- 0x18 measured in 1/100 of a millimetre
371+ frameT <- getWord32le -- 0x1C
372+ frameR <- getWord32le -- 0x20
373+ frameB <- getWord32le -- 0x24
374+ skip 0x20 -- 0x28
375+ deviceX <- getWord32le -- 0x48 pixels of reference device
376+ deviceY <- getWord32le -- 0x4C
377+ mmX <- getWord32le -- 0x50 real mm of reference device (always 320*240?)
378+ mmY <- getWord32le -- 0x58
379+ -- end of header
380+ let
381+ w = (deviceX * (frameR - frameL)) `quot` (mmX * 100)
382+ h = (deviceY * (frameB - frameT)) `quot` (mmY * 100)
383+ dpiW = (deviceX * 254) `quot` (mmX * 10)
384+ dpiH = (deviceY * 254) `quot` (mmY * 10)
385+ return $ ImageSize
386+ { pxX = fromIntegral w
387+ , pxY = fromIntegral h
388+ , dpiX = fromIntegral dpiW
389+ , dpiY = fromIntegral dpiH
390+ }
391+ in
392+ case parseheader . BL.fromStrict $ img of
393+ Left _ -> Nothing
394+ Right (_, _, size) -> Just size
395+
360396
361397jpegSize :: ByteString -> Either String ImageSize
362398jpegSize img =
src/Text/Pandoc/Writers/Docx.hs+1
@@ -1339,6 +1339,7 @@ inlineToOpenXML' opts (Image attr alt (src, title)) = do
13391339 Just Pdf -> ".pdf"
13401340 Just Eps -> ".eps"
13411341 Just Svg -> ".svg"
1342+ Just Emf -> ".emf"
13421343 Nothing -> ""
13431344 if null imgext
13441345 then -- without an extension there is no rule for content type
src/Text/Pandoc/Writers/Powerpoint/Output.hs+1
@@ -475,6 +475,7 @@ registerMedia fp caption = do
475475 Just Pdf -> Just ".pdf"
476476 Just Eps -> Just ".eps"
477477 Just Svg -> Just ".svg"
478+ Just Emf -> Just ".emf"
478479 Nothing -> Nothing
479480
480481 let newGlobalId = case M.lookup fp globalIds of