git.iko.soy/typeable/pandoc
on master help sign in
ImageSize: use viewBox for svg if no length, width.

This change allows pandoc to extract size information from more SVGs. Closes #7045.

commit198ce0cde99731680b62294a7dff1b218b29bd4d authorJohn MacFarlane <jgm@berkeley.edu> dateFri 22 Jan 2021, 20:49 (6 years ago) parentf5e3c1d
2 files changed+20−18
src/Text/Pandoc/ImageSize.hs+6−2 test/command/svg.md+14−16
src/Text/Pandoc/ImageSize.hs+6−2
@@ -328,12 +328,16 @@ getSize img =
328328svgSize :: WriterOptions -> ByteString -> Maybe ImageSize
329329svgSize opts img = do
330330 doc <- Xml.parseXMLDoc $ UTF8.toString img
331+ let viewboxSize = do
332+ vb <- Xml.findAttrBy (== Xml.QName "viewBox" Nothing Nothing) doc
333+ [_,_,w,h] <- mapM safeRead (T.words (T.pack vb))
334+ return (w,h)
331335 let dpi = fromIntegral $ writerDpi opts
332336 let dirToInt dir = do
333337 dim <- Xml.findAttrBy (== Xml.QName dir Nothing Nothing) doc >>= lengthToDim . T.pack
334338 return $ inPixel opts dim
335− w <- dirToInt "width"
336− h <- dirToInt "height"
339+ w <- dirToInt "width" <|> (fst <$> viewboxSize)
340+ h <- dirToInt "height" <|> (snd <$> viewboxSize)
337341 return ImageSize {
338342 pxX = w
339343 , pxY = h
test/command/svg.md+14−16
@@ -36,28 +36,27 @@
3636% pandoc -f latex -t icml
3737\includegraphics{command/SVG_logo.svg}
3838^D
39−[WARNING] Could not determine image size for 'command/SVG_logo.svg': could not determine SVG size
4039<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
4140 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
42− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
41+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 37.5 -37.5">
4342 <Properties>
4443 <PathGeometry>
4544 <GeometryPathType PathOpen="false">
4645 <PathPointArray>
47− <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
48− <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
49− <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
50− <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
46+ <PathPointType Anchor="-37.5 -37.5" LeftDirection="-37.5 -37.5" RightDirection="-37.5 -37.5" />
47+ <PathPointType Anchor="-37.5 37.5" LeftDirection="-37.5 37.5" RightDirection="-37.5 37.5" />
48+ <PathPointType Anchor="37.5 37.5" LeftDirection="37.5 37.5" RightDirection="37.5 37.5" />
49+ <PathPointType Anchor="37.5 -37.5" LeftDirection="37.5 -37.5" RightDirection="37.5 -37.5" />
5150 </PathPointArray>
5251 </GeometryPathType>
5352 </PathGeometry>
5453 </Properties>
55− <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
54+ <Image Self="ue6" ItemTransform="1 0 0 1 -37.5 -37.5">
5655 <Properties>
5756 <Profile type="string">
5857 $ID/Embedded
5958 </Profile>
60− <GraphicBounds Left="0" Top="0" Right="300" Bottom="200" />
59+ <GraphicBounds Left="0" Top="0" Right="75" Bottom="75" />
6160 </Properties>
6261 <Link Self="ueb" LinkResourceURI="file:command/SVG_logo.svg" />
6362 </Image>
@@ -70,28 +69,27 @@
7069% pandoc -f latex -t icml
7170\includegraphics{command/SVG_logo-without-xml-declaration.svg}
7271^D
73−[WARNING] Could not determine image size for 'command/SVG_logo-without-xml-declaration.svg': could not determine SVG size
7472<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
7573 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
76− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
74+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 37.5 -37.5">
7775 <Properties>
7876 <PathGeometry>
7977 <GeometryPathType PathOpen="false">
8078 <PathPointArray>
81− <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
82− <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
83− <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
84− <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
79+ <PathPointType Anchor="-37.5 -37.5" LeftDirection="-37.5 -37.5" RightDirection="-37.5 -37.5" />
80+ <PathPointType Anchor="-37.5 37.5" LeftDirection="-37.5 37.5" RightDirection="-37.5 37.5" />
81+ <PathPointType Anchor="37.5 37.5" LeftDirection="37.5 37.5" RightDirection="37.5 37.5" />
82+ <PathPointType Anchor="37.5 -37.5" LeftDirection="37.5 -37.5" RightDirection="37.5 -37.5" />
8583 </PathPointArray>
8684 </GeometryPathType>
8785 </PathGeometry>
8886 </Properties>
89− <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
87+ <Image Self="ue6" ItemTransform="1 0 0 1 -37.5 -37.5">
9088 <Properties>
9189 <Profile type="string">
9290 $ID/Embedded
9391 </Profile>
94− <GraphicBounds Left="0" Top="0" Right="300" Bottom="200" />
92+ <GraphicBounds Left="0" Top="0" Right="75" Bottom="75" />
9593 </Properties>
9694 <Link Self="ueb" LinkResourceURI="file:command/SVG_logo-without-xml-declaration.svg" />
9795 </Image>