aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2023-04-07 09:13:49 -0700
committerJohn MacFarlane <[email protected]>2023-04-07 09:13:49 -0700
commit9bcf1ff8d6823c9980ff1b9959e7a48968de0275 (patch)
tree6f9c9ca5e7d4ecaac950c84332af4928a385c5eb /src
parente89a3edf24a025d5bb0fe8c4c7a8e6e0208fa846 (diff)
ImageSize: drop BOM at start of SVG if present.
Otherwise our code can fail to determine image size.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/ImageSize.hs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/Text/Pandoc/ImageSize.hs b/src/Text/Pandoc/ImageSize.hs
index 9db6744b4..727be238c 100644
--- a/src/Text/Pandoc/ImageSize.hs
+++ b/src/Text/Pandoc/ImageSize.hs
@@ -97,6 +97,12 @@ removeExtra0s s = case T.dropWhileEnd (=='0') s of
(T.unsnoc -> Just (xs, '.')) -> xs
xs -> xs
+dropBOM :: ByteString -> ByteString
+dropBOM bs =
+ if "\xEF\xBB\xBF" `B.isPrefixOf` bs
+ then B.drop 3 bs
+ else bs
+
imageType :: ByteString -> Maybe ImageType
imageType img = case B.take 4 img of
"\x89\x50\x4e\x47" -> return Png
@@ -116,7 +122,9 @@ imageType img = case B.take 4 img of
"\x01\x00\x00\x00"
| B.take 4 (B.drop 40 img) == " EMF"
-> return Emf
- _ -> mzero
+ "\xEF\xBB\xBF<" -- BOM before svg
+ -> imageType (B.drop 3 img)
+ _ -> mzero
findSvgTag :: ByteString -> Bool
findSvgTag img = "<svg" `B.isInfixOf` img || "<SVG" `B.isInfixOf` img
@@ -333,7 +341,7 @@ getSize img =
svgSize :: WriterOptions -> ByteString -> Maybe ImageSize
svgSize opts img = do
doc <- either (const mzero) return $ parseXMLElement
- $ TL.fromStrict $ UTF8.toText img
+ $ TL.fromStrict $ UTF8.toText $ dropBOM img
let viewboxSize = do
vb <- findAttrBy (== QName "viewBox" Nothing Nothing) doc
[_,_,w,h] <- mapM safeRead (T.words vb)