@@ -39,6 +39,7 @@ module Text.Pandoc.ImageSize ( ImageType(..)
39 , Direction(..)
39 , Direction(..)
40 , dimension
40 , dimension
42 , inPoints
43 , inPoints
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
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,17 +103,25 @@ 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
108+ | "<svg " == (B.take 5 $ last $ B.groupBy openingTag $ B.drop 7 img)
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
115+ -- B.groupBy openingTag matches first "<svg" or "<html" but not "<!--"
116+ openingTag x y = x == '<' && y /= '!'
108−imageSize :: ByteString -> Either String ImageSize
118+imageSize :: WriterOptions -> ByteString -> Either String ImageSize
109−imageSize img =
119+imageSize opts img =
110 case imageType img of
120 case imageType img of
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 opts 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
175+inPixel :: WriterOptions -> Dimension -> Integer
179+ (Centimeter a) -> floor $ dpi * a * 0.3937007874 :: Integer
180+ (Inch a) -> floor $ dpi * a :: Integer
183+ dpi = fromIntegral $ writerDpi opts
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
176− (Centimeter a) -> show (floor $ dpi * a * 0.3937007874 :: Int)
177− (Inch a) -> show (floor $ dpi * a :: Int)
180− dpi = fromIntegral $ writerDpi opts
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"
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
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
214epsSize :: ByteString -> Maybe ImageSize
229epsSize :: ByteString -> Maybe ImageSize
@@ -271,6 +286,22 @@ gifSize img = do
272 _ -> Nothing -- "GIF parse error"
287 _ -> Nothing -- "GIF parse error"
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"
274jpegSize :: ByteString -> Either String ImageSize
305jpegSize :: ByteString -> Either String ImageSize
275jpegSize img =
306jpegSize img =
276 let (hdr, rest) = B.splitAt 4 img
307 let (hdr, rest) = B.splitAt 4 img