@@ -39,6 +39,7 @@ module Text.Pandoc.ImageSize ( ImageType(..)
3939 , Direction(..)
4040 , dimension
4141 , inInch
42+ , inPixel
4243 , inPoints
4344 , numUnit
4445 , showInInch
@@ -58,6 +59,8 @@ import Data.Default (Default)
5859import Numeric (showFFloat)
5960import Text.Pandoc.Definition
6061import Text.Pandoc.Options
62+import qualified Text.Pandoc.UTF8 as UTF8
63+import qualified Text.XML.Light as Xml
6164import qualified Data.Map as M
6265import Control.Monad.Except
6366import Data.Maybe (fromMaybe)
@@ -65,7 +68,7 @@ import Data.Maybe (fromMaybe)
6568-- quick and dirty functions to get image sizes
6669-- algorithms borrowed from wwwis.pl
6770
68−data ImageType = Png | Gif | Jpeg | Pdf | Eps deriving Show
71+data ImageType = Png | Gif | Jpeg | Svg | Pdf | Eps deriving Show
6972data Direction = Width | Height
7073instance Show Direction where
7174 show Width = "width"
@@ -100,17 +103,25 @@ imageType img = case B.take 4 img of
100103 "\xff\xd8\xff\xe0" -> return Jpeg -- JFIF
101104 "\xff\xd8\xff\xe1" -> return Jpeg -- Exif
102105 "%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
103110 "%!PS"
104111 | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
105112 -> return Eps
106113 _ -> mzero
114+ where
115+ -- B.groupBy openingTag matches first "<svg" or "<html" but not "<!--"
116+ openingTag x y = x == '<' && y /= '!'
107117
108−imageSize :: ByteString -> Either String ImageSize
109−imageSize img =
118+imageSize :: WriterOptions -> ByteString -> Either String ImageSize
119+imageSize opts img =
110120 case imageType img of
111121 Just Png -> mbToEither "could not determine PNG size" $ pngSize img
112122 Just Gif -> mbToEither "could not determine GIF size" $ gifSize img
113123 Just Jpeg -> jpegSize img
124+ Just Svg -> mbToEither "could not determine SVG size" $ svgSize opts img
114125 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
115126 Just Pdf -> Left "could not determine PDF size" -- TODO
116127 Nothing -> Left "could not determine image type"
@@ -161,6 +172,16 @@ inInch opts dim =
161172 (Inch a) -> a
162173 (Percent _) -> 0
163174
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+
164185-- | Convert a Dimension to a String denoting its equivalent in inches, for example "2.00000".
165186-- Note: Dimensions in percentages are converted to the empty string.
166187showInInch :: WriterOptions -> Dimension -> String
@@ -170,14 +191,8 @@ showInInch opts dim = showFl $ inInch opts dim
170191-- | Convert a Dimension to a String denoting its equivalent in pixels, for example "600".
171192-- Note: Dimensions in percentages are converted to the empty string.
172193showInPixel :: WriterOptions -> Dimension -> String
173−showInPixel opts dim =
174− case dim of
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
194+showInPixel _ (Percent _) = ""
195+showInPixel opts dim = show $ inPixel opts dim
181196
182197-- | Maybe split a string into a leading number and trailing unit, e.g. "3cm" to Just (3.0, "cm")
183198numUnit :: String -> Maybe (Double, String)
@@ -195,13 +210,11 @@ dimension dir (_, _, kvs) =
195210 Width -> extractDim "width"
196211 Height -> extractDim "height"
197212 where
198− extractDim key =
199− case lookup key kvs of
200− Just str ->
201− case numUnit str of
202− Just (num, unit) -> toDim num unit
203− Nothing -> Nothing
204− Nothing -> Nothing
213+ extractDim key = lookup key kvs >>= lengthToDim
214+
215+lengthToDim :: String -> Maybe Dimension
216+lengthToDim s = numUnit s >>= uncurry toDim
217+ where
205218 toDim a "cm" = Just $ Centimeter a
206219 toDim a "mm" = Just $ Centimeter (a / 10)
207220 toDim a "in" = Just $ Inch a
@@ -209,6 +222,8 @@ dimension dir (_, _, kvs) =
209222 toDim a "%" = Just $ Percent a
210223 toDim a "px" = Just $ Pixel (floor a::Integer)
211224 toDim a "" = Just $ Pixel (floor a::Integer)
225+ toDim a "pt" = Just $ Inch (a / 72)
226+ toDim a "pc" = Just $ Inch (a / 6)
212227 toDim _ _ = Nothing
213228
214229epsSize :: ByteString -> Maybe ImageSize
@@ -271,6 +286,22 @@ gifSize img = do
271286 }
272287 _ -> Nothing -- "GIF parse error"
273288
289+svgSize :: WriterOptions -> ByteString -> Maybe ImageSize
290+svgSize opts img = do
291+ doc <- Xml.parseXMLDoc $ UTF8.toString img
292+ let dpi = fromIntegral $ writerDpi opts
293+ let dirToInt dir = do
294+ dim <- Xml.findAttrBy (== Xml.QName dir Nothing Nothing) doc >>= lengthToDim
295+ return $ inPixel opts dim
296+ w <- dirToInt "width"
297+ h <- dirToInt "height"
298+ return ImageSize {
299+ pxX = w
300+ , pxY = h
301+ , dpiX = dpi
302+ , dpiY = dpi
303+ }
304+
274305jpegSize :: ByteString -> Either String ImageSize
275306jpegSize img =
276307 let (hdr, rest) = B.splitAt 4 img