git.iko.soy/typeable/pandoc
on master help sign in
make imageSize recognize basic SVG dimensions, see #3462
b312ac6mb21
src/Text/Pandoc/ImageSize.hs+48−16
@@ -39,6 +39,7 @@ module Text.Pandoc.ImageSize ( ImageType(..)
39 , Direction(..)
39 , Direction(..)
40 , dimension
40 , dimension
41 , inInch
41 , inInch
42+ , inPixel
42 , inPoints
43 , inPoints
43 , numUnit
44 , numUnit
44 , showInInch
45 , showInInch
@@ -58,6 +59,8 @@ import Data.Default (Default)
58import Numeric (showFFloat)
59import Numeric (showFFloat)
59import Text.Pandoc.Definition
60import Text.Pandoc.Definition
60import Text.Pandoc.Options
61import Text.Pandoc.Options
62+import qualified Text.Pandoc.UTF8 as UTF8
63+import qualified Text.XML.Light as Xml
61import qualified Data.Map as M
64import qualified Data.Map as M
62import Control.Monad.Except
65import Control.Monad.Except
63import Data.Maybe (fromMaybe)
66import Data.Maybe (fromMaybe)
@@ -65,7 +68,7 @@ import Data.Maybe (fromMaybe)
65-- quick and dirty functions to get image sizes
68-- quick and dirty functions to get image sizes
66-- algorithms borrowed from wwwis.pl
69-- algorithms borrowed from wwwis.pl
67
70
68−data ImageType = Png | Gif | Jpeg | Pdf | Eps deriving Show
71+data ImageType = Png | Gif | Jpeg | Svg | Pdf | Eps deriving Show
69data Direction = Width | Height
72data Direction = Width | Height
70instance Show Direction where
73instance Show Direction where
71 show Width = "width"
74 show Width = "width"
@@ -100,10 +103,17 @@ imageType img = case B.take 4 img of
100 "\xff\xd8\xff\xe0" -> return Jpeg -- JFIF
103 "\xff\xd8\xff\xe0" -> return Jpeg -- JFIF
101 "\xff\xd8\xff\xe1" -> return Jpeg -- Exif
104 "\xff\xd8\xff\xe1" -> return Jpeg -- Exif
102 "%PDF" -> return Pdf
105 "%PDF" -> return Pdf
106+ "<svg" -> return Svg
107+ "<?xm"
108+ | "<svg " == (B.take 5 $ last $ B.groupBy openingTag $ B.drop 7 img)
109+ -> return Svg
103 "%!PS"
110 "%!PS"
104 | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
111 | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
105 -> return Eps
112 -> return Eps
106 _ -> mzero
113 _ -> mzero
114+ where
115+ -- B.groupBy openingTag matches first "<svg" or "<html" but not "<!--"
116+ openingTag x y = x == '<' && y /= '!'
107
117
108imageSize :: ByteString -> Either String ImageSize
118imageSize :: ByteString -> Either String ImageSize
109imageSize img =
119imageSize img =
@@ -111,6 +121,7 @@ imageSize img =
111 Just Png -> mbToEither "could not determine PNG size" $ pngSize img
121 Just Png -> mbToEither "could not determine PNG size" $ pngSize img
112 Just Gif -> mbToEither "could not determine GIF size" $ gifSize img
122 Just Gif -> mbToEither "could not determine GIF size" $ gifSize img
113 Just Jpeg -> jpegSize img
123 Just Jpeg -> jpegSize img
124+ Just Svg -> mbToEither "could not determine SVG size" $ svgSize img
114 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
125 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
115 Just Pdf -> Left "could not determine PDF size" -- TODO
126 Just Pdf -> Left "could not determine PDF size" -- TODO
116 Nothing -> Left "could not determine image type"
127 Nothing -> Left "could not determine image type"
@@ -161,6 +172,16 @@ inInch opts dim =
161 (Inch a) -> a
172 (Inch a) -> a
162 (Percent _) -> 0
173 (Percent _) -> 0
163
174
175+inPixel :: WriterOptions -> Dimension -> Integer
176+inPixel opts dim =
177+ case dim of
178+ (Pixel a) -> a
179+ (Centimeter a) -> floor $ dpi * a * 0.3937007874 :: Integer
180+ (Inch a) -> floor $ dpi * a :: Integer
181+ _ -> 0
182+ where
183+ dpi = fromIntegral $ writerDpi opts
184+
164-- | Convert a Dimension to a String denoting its equivalent in inches, for example "2.00000".
185-- | Convert a Dimension to a String denoting its equivalent in inches, for example "2.00000".
165-- Note: Dimensions in percentages are converted to the empty string.
186-- Note: Dimensions in percentages are converted to the empty string.
166showInInch :: WriterOptions -> Dimension -> String
187showInInch :: WriterOptions -> Dimension -> String
@@ -170,14 +191,8 @@ showInInch opts dim = showFl $ inInch opts dim
170-- | Convert a Dimension to a String denoting its equivalent in pixels, for example "600".
191-- | Convert a Dimension to a String denoting its equivalent in pixels, for example "600".
171-- Note: Dimensions in percentages are converted to the empty string.
192-- Note: Dimensions in percentages are converted to the empty string.
172showInPixel :: WriterOptions -> Dimension -> String
193showInPixel :: WriterOptions -> Dimension -> String
173−showInPixel opts dim =
194+showInPixel _ (Percent _) = ""
174− case dim of
195+showInPixel opts dim = show $ inPixel opts dim
175− (Pixel a) -> show a
176− (Centimeter a) -> show (floor $ dpi * a * 0.3937007874 :: Int)
177− (Inch a) -> show (floor $ dpi * a :: Int)
178− (Percent _) -> ""
179− where
180− dpi = fromIntegral $ writerDpi opts
181
196
182-- | Maybe split a string into a leading number and trailing unit, e.g. "3cm" to Just (3.0, "cm")
197-- | Maybe split a string into a leading number and trailing unit, e.g. "3cm" to Just (3.0, "cm")
183numUnit :: String -> Maybe (Double, String)
198numUnit :: String -> Maybe (Double, String)
@@ -195,13 +210,11 @@ dimension dir (_, _, kvs) =
195 Width -> extractDim "width"
210 Width -> extractDim "width"
196 Height -> extractDim "height"
211 Height -> extractDim "height"
197 where
212 where
198− extractDim key =
213+ extractDim key = lookup key kvs >>= lengthToDim
199− case lookup key kvs of
214+
200− Just str ->
215+lengthToDim :: String -> Maybe Dimension
201− case numUnit str of
216+lengthToDim s = numUnit s >>= uncurry toDim
202− Just (num, unit) -> toDim num unit
217+ where
203− Nothing -> Nothing
204− Nothing -> Nothing
205 toDim a "cm" = Just $ Centimeter a
218 toDim a "cm" = Just $ Centimeter a
206 toDim a "mm" = Just $ Centimeter (a / 10)
219 toDim a "mm" = Just $ Centimeter (a / 10)
207 toDim a "in" = Just $ Inch a
220 toDim a "in" = Just $ Inch a
@@ -209,6 +222,8 @@ dimension dir (_, _, kvs) =
209 toDim a "%" = Just $ Percent a
222 toDim a "%" = Just $ Percent a
210 toDim a "px" = Just $ Pixel (floor a::Integer)
223 toDim a "px" = Just $ Pixel (floor a::Integer)
211 toDim a "" = Just $ Pixel (floor a::Integer)
224 toDim a "" = Just $ Pixel (floor a::Integer)
225+ toDim a "pt" = Just $ Inch (a / 72)
226+ toDim a "pc" = Just $ Inch (a / 6)
212 toDim _ _ = Nothing
227 toDim _ _ = Nothing
213
228
214epsSize :: ByteString -> Maybe ImageSize
229epsSize :: ByteString -> Maybe ImageSize
@@ -271,6 +286,23 @@ gifSize img = do
271 }
286 }
272 _ -> Nothing -- "GIF parse error"
287 _ -> Nothing -- "GIF parse error"
273
288
289+svgSize :: ByteString -> Maybe ImageSize
290+svgSize img = do
291+ doc <- Xml.parseXMLDoc $ UTF8.toString img
292+ let opts = def --TODO: use proper opts instead of def, which simply contains dpi=72
293+ let dpi = fromIntegral $ writerDpi opts
294+ let dirToInt dir = do
295+ dim <- Xml.findAttrBy (== Xml.QName dir Nothing Nothing) doc >>= lengthToDim
296+ return $ inPixel opts dim
297+ w <- dirToInt "width"
298+ h <- dirToInt "height"
299+ return ImageSize {
300+ pxX = w
301+ , pxY = h
302+ , dpiX = dpi
303+ , dpiY = dpi
304+ }
305+
274jpegSize :: ByteString -> Either String ImageSize
306jpegSize :: ByteString -> Either String ImageSize
275jpegSize img =
307jpegSize img =
276 let (hdr, rest) = B.splitAt 4 img
308 let (hdr, rest) = B.splitAt 4 img
src/Text/Pandoc/Writers/Docx.hs+1
@@ -1228,6 +1228,7 @@ inlineToOpenXML' opts (Image attr alt (src, title)) = do
1228 Just Gif -> ".gif"
1228 Just Gif -> ".gif"
1229 Just Pdf -> ".pdf"
1229 Just Pdf -> ".pdf"
1230 Just Eps -> ".eps"
1230 Just Eps -> ".eps"
1231+ Just Svg -> ".svg"
1231 Nothing -> ""
1232 Nothing -> ""
1232 if null imgext
1233 if null imgext
1233 then -- without an extension there is no rule for content type
1234 then -- without an extension there is no rule for content type