git.iko.soy/typeable/pandoc
on master help sign in
Improve SVG image size code.

The old code made some unwise assumptions about how the svg file would look. See #3580.

commit03cb05f4c614f08600bcd8e90a7fd1ca13ae33a2 authorMarc Schreiber <marc.schreiber@fh-aachen.de> dateThu 20 Apr 2017, 11:11 (10 years ago) parent5c44fd5
7 files changed+323−1
pandoc.cabal+4 src/Text/Pandoc/ImageSize.hs+1−1 test/command/SVG_logo-without-xml-declaration.svg+32 test/command/SVG_logo.svg+33 test/command/corrupt.svg+5 test/command/inkscape-cube.svg+119 test/command/svg.md+129
pandoc.cabal+4
@@ -136,6 +136,10 @@ Extra-Source-Files:
136136 test/*.native
137137 test/command/*.md
138138 test/command/abbrevs
139+ test/command/SVG_logo-without-xml-declaration.svg
140+ test/command/SVG_logo.svg
141+ test/command/corrupt.svg
142+ test/command/inkscape-cube.svg
139143 test/command/sub-file-chapter-1.tex
140144 test/command/sub-file-chapter-2.tex
141145 test/command/3510-subdoc.org
src/Text/Pandoc/ImageSize.hs+1−1
@@ -116,7 +116,7 @@ imageType img = case B.take 4 img of
116116 _ -> mzero
117117
118118findSvgTag :: ByteString -> Bool
119−findSvgTag img = B.null $ snd (B.breakSubstring img "<svg")
119+findSvgTag img = "<svg" `B.isInfixOf` img || "<SVG" `B.isInfixOf` img
120120
121121imageSize :: WriterOptions -> ByteString -> Either String ImageSize
122122imageSize opts img =
test/command/SVG_logo-without-xml-declaration.svgnew file+32
@@ -0,0 +1,32 @@
1+<svg viewBox="-50 -50 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2+<title>SVG Logo</title>
3+<rect id="background" x="-50" y="-50" width="100" height="100" rx="4" fill="#f90"/>
4+<rect id="top-left" x="-50" y="-50" width="50" height="50" rx="4" fill="#ffb13b"/>
5+<rect id="bottom-right" width="50" height="50" rx="4" fill="#de8500"/>
6+<use stroke="#f90" stroke-width="22.6" xlink:href="#a"/>
7+<circle r="26"/>
8+<use stroke="#000" stroke-width="12" xlink:href="#a"/>
9+<g id="a">
10+ <g id="b">
11+ <g id="c">
12+ <circle id="n" cy="-31.6" r="7.1" fill="#fff"/>
13+ <path d="m0 31.6v-63.2" stroke="#fff" stroke-width="10"/>
14+ <use y="63.2" xlink:href="#n"/>
15+ </g>
16+ <use transform="rotate(90)" xlink:href="#c"/>
17+ </g>
18+ <use transform="rotate(45)" xlink:href="#b"/>
19+</g>
20+<path id="text-backdrop" d="m44.68 0v40c0 3.333-1.667 5-5 5h-79.38c-3.333 0-5-1.667-5-5v-40"/>
21+<path id="shine" d="m36 4.21c2.9 0 5.3 2.4 5.3 5.3v18c-27.6-3.4-54.9-8-82-7.7v-10.2c0-2.93 2.4-5.3 5.3-5.3z" fill="#3f3f3f"/>
22+<use stroke="#000" stroke-width="7.4" xlink:href="#s"/>
23+<g id="svg-text" stroke="#fff" stroke-width="6.4">
24+ <g id="s">
25+ <path fill="none" d="m-31.74 31.17a8.26 8.26 0 1 0 8.26 -8.26 8.26 8.26 0 1 1 8.26 -8.26M23.23 23h8.288v 8.26a8.26 8.26 0 0 1 -16.52 0v-16.52a8.26 8.26 0 0 1 16.52 0"/>
26+ <g stroke-width=".5" stroke="#000">
27+ <path d="m4.76 3h6.83l-8.24 39.8h-6.85l-8.26-39.8h6.85l4.84 23.3z" fill="#fff"/>
28+ <path d="m23.23 19.55v6.9m4.838-11.71h6.9m-70.16 16.43h6.9m9.62-16.52h6.9" stroke-linecap="square"/>
29+ </g>
30+ </g>
31+</g>
32+</svg>
test/command/SVG_logo.svgnew file+33
@@ -0,0 +1,33 @@
1+<?xml version="1.0"?>
2+<svg viewBox="-50 -50 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3+<title>SVG Logo</title>
4+<rect id="background" x="-50" y="-50" width="100" height="100" rx="4" fill="#f90"/>
5+<rect id="top-left" x="-50" y="-50" width="50" height="50" rx="4" fill="#ffb13b"/>
6+<rect id="bottom-right" width="50" height="50" rx="4" fill="#de8500"/>
7+<use stroke="#f90" stroke-width="22.6" xlink:href="#a"/>
8+<circle r="26"/>
9+<use stroke="#000" stroke-width="12" xlink:href="#a"/>
10+<g id="a">
11+ <g id="b">
12+ <g id="c">
13+ <circle id="n" cy="-31.6" r="7.1" fill="#fff"/>
14+ <path d="m0 31.6v-63.2" stroke="#fff" stroke-width="10"/>
15+ <use y="63.2" xlink:href="#n"/>
16+ </g>
17+ <use transform="rotate(90)" xlink:href="#c"/>
18+ </g>
19+ <use transform="rotate(45)" xlink:href="#b"/>
20+</g>
21+<path id="text-backdrop" d="m44.68 0v40c0 3.333-1.667 5-5 5h-79.38c-3.333 0-5-1.667-5-5v-40"/>
22+<path id="shine" d="m36 4.21c2.9 0 5.3 2.4 5.3 5.3v18c-27.6-3.4-54.9-8-82-7.7v-10.2c0-2.93 2.4-5.3 5.3-5.3z" fill="#3f3f3f"/>
23+<use stroke="#000" stroke-width="7.4" xlink:href="#s"/>
24+<g id="svg-text" stroke="#fff" stroke-width="6.4">
25+ <g id="s">
26+ <path fill="none" d="m-31.74 31.17a8.26 8.26 0 1 0 8.26 -8.26 8.26 8.26 0 1 1 8.26 -8.26M23.23 23h8.288v 8.26a8.26 8.26 0 0 1 -16.52 0v-16.52a8.26 8.26 0 0 1 16.52 0"/>
27+ <g stroke-width=".5" stroke="#000">
28+ <path d="m4.76 3h6.83l-8.24 39.8h-6.85l-8.26-39.8h6.85l4.84 23.3z" fill="#fff"/>
29+ <path d="m23.23 19.55v6.9m4.838-11.71h6.9m-70.16 16.43h6.9m9.62-16.52h6.9" stroke-linecap="square"/>
30+ </g>
31+ </g>
32+</g>
33+</svg>
test/command/corrupt.svgnew file+5
@@ -0,0 +1,5 @@
1+Lorem ipsum dolor sit amet etiam. A pede dolor neque pretium luctus pharetra vel rutrum. Orci nonummy ac. At eu est tempor
2+proin wisi. Nunc tincidunt proin. Suspendisse lorem commodo. Integer diam diam semper commodo dictum et tellus eu ultrices
3+nec erat pulvinar porttitor nulla nulla mauris orci libero eros elementum et possimus voluptate. Velit morbi et. Luctus diam
4+in. Lorem tincidunt sem dolor rerum mauris. Dis taciti posuere pellentesque sed rutrum. Lectus donec fusce in dictum pede.
5+In etiam congue. Aliquam aliquet elit arcu mauris enim. Risus at enim.
test/command/inkscape-cube.svgnew file+119
@@ -0,0 +1,119 @@
1+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+<!-- Created with Inkscape (http://www.inkscape.org/) -->
3+
4+<svg
5+ xmlns:dc="http://purl.org/dc/elements/1.1/"
6+ xmlns:cc="http://creativecommons.org/ns#"
7+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
8+ xmlns:svg="http://www.w3.org/2000/svg"
9+ xmlns="http://www.w3.org/2000/svg"
10+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
11+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
12+ width="38.772217mm"
13+ height="46.163891mm"
14+ viewBox="0 0 38.772217 46.163891"
15+ version="1.1"
16+ id="svg8"
17+ inkscape:version="0.92.1 r"
18+ sodipodi:docname="cube.svg">
19+ <defs
20+ id="defs2">
21+ <inkscape:perspective
22+ sodipodi:type="inkscape:persp3d"
23+ inkscape:vp_x="-48.380952 : -45.023815 : 1"
24+ inkscape:vp_y="0 : 1000 : 0"
25+ inkscape:vp_z="161.61905 : -45.023817 : 1"
26+ inkscape:persp3d-origin="56.619048 : -94.523816 : 1"
27+ id="perspective4485" />
28+ </defs>
29+ <sodipodi:namedview
30+ id="base"
31+ pagecolor="#ffffff"
32+ bordercolor="#666666"
33+ borderopacity="1.0"
34+ inkscape:pageopacity="0.0"
35+ inkscape:pageshadow="2"
36+ inkscape:zoom="0.98994949"
37+ inkscape:cx="-63.181251"
38+ inkscape:cy="-116.38602"
39+ inkscape:document-units="mm"
40+ inkscape:current-layer="layer1"
41+ showgrid="false"
42+ fit-margin-top="0"
43+ fit-margin-left="0"
44+ fit-margin-right="0"
45+ fit-margin-bottom="0"
46+ inkscape:window-width="1920"
47+ inkscape:window-height="1136"
48+ inkscape:window-x="1920"
49+ inkscape:window-y="30"
50+ inkscape:window-maximized="1" />
51+ <metadata
52+ id="metadata5">
53+ <rdf:RDF>
54+ <cc:Work
55+ rdf:about="">
56+ <dc:format>image/svg+xml</dc:format>
57+ <dc:type
58+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
59+ <dc:title></dc:title>
60+ </cc:Work>
61+ </rdf:RDF>
62+ </metadata>
63+ <g
64+ inkscape:label="Ebene 1"
65+ inkscape:groupmode="layer"
66+ id="layer1"
67+ transform="translate(-149.67857,78.746839)">
68+ <g
69+ sodipodi:type="inkscape:box3d"
70+ id="g4487"
71+ style="opacity:0.2;fill:none;fill-opacity:1;stroke:#000000;stroke-width:0.53100002;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
72+ inkscape:perspectiveID="#perspective4485"
73+ inkscape:corner0="1.1045097 : 0.18860662 : 0 : 1"
74+ inkscape:corner7="0.52634769 : 0.15538942 : 0.25 : 1">
75+ <path
76+ sodipodi:type="inkscape:box3dside"
77+ id="path4489"
78+ style="fill:#353564;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
79+ inkscape:box3dsidetype="6"
80+ d="m 151.19047,-53.658435 v 15.783818 l 17.00006,5.342459 v -14.107905 z"
81+ points="151.19047,-37.874617 168.19053,-32.532158 168.19053,-46.640063 151.19047,-53.658435 " />
82+ <path
83+ sodipodi:type="inkscape:box3dside"
84+ id="path4499"
85+ style="fill:#e9e9ff;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
86+ inkscape:box3dsidetype="11"
87+ d="m 168.19053,-46.640063 21.77216,-19.229539 v 18.699717 l -21.77216,14.637727 z"
88+ points="189.96269,-65.869602 189.96269,-47.169885 168.19053,-32.532158 168.19053,-46.640063 " />
89+ <path
90+ sodipodi:type="inkscape:box3dside"
91+ id="path4491"
92+ style="fill:#4d4d9f;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
93+ inkscape:box3dsidetype="5"
94+ d="m 151.19047,-53.658435 18.89881,-25.037614 19.87341,12.826447 -21.77216,19.229539 z"
95+ points="170.08928,-78.696049 189.96269,-65.869602 168.19053,-46.640063 151.19047,-53.658435 " />
96+ <path
97+ sodipodi:type="inkscape:box3dside"
98+ id="path4497"
99+ style="fill:#afafde;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
100+ inkscape:box3dsidetype="13"
101+ d="m 151.19047,-37.874617 18.89881,-19.058894 19.87341,9.763626 -21.77216,14.637727 z"
102+ points="170.08928,-56.933511 189.96269,-47.169885 168.19053,-32.532158 151.19047,-37.874617 " />
103+ <path
104+ sodipodi:type="inkscape:box3dside"
105+ id="path4495"
106+ style="fill:#d7d7ff;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
107+ inkscape:box3dsidetype="14"
108+ d="m 170.08928,-78.696049 v 21.762538 l 19.87341,9.763626 v -18.699717 z"
109+ points="170.08928,-56.933511 189.96269,-47.169885 189.96269,-65.869602 170.08928,-78.696049 " />
110+ <path
111+ sodipodi:type="inkscape:box3dside"
112+ id="path4493"
113+ style="fill:#8686bf;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
114+ inkscape:box3dsidetype="3"
115+ d="m 151.19047,-53.658435 18.89881,-25.037614 v 21.762538 l -18.89881,19.058894 z"
116+ points="170.08928,-78.696049 170.08928,-56.933511 151.19047,-37.874617 151.19047,-53.658435 " />
117+ </g>
118+ </g>
119+</svg>
test/command/svg.mdnew file+129
@@ -0,0 +1,129 @@
1+```
2+% pandoc -f latex -t icml
3+\includegraphics{command/corrupt.svg}
4+^D
5+<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
6+ <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
7+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
8+ <Properties>
9+ <PathGeometry>
10+ <GeometryPathType PathOpen="false">
11+ <PathPointArray>
12+ <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
13+ <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
14+ <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
15+ <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
16+ </PathPointArray>
17+ </GeometryPathType>
18+ </PathGeometry>
19+ </Properties>
20+ <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
21+ <Properties>
22+ <Profile type="string">
23+ $ID/Embedded
24+ </Profile>
25+ </Properties>
26+ <Link Self="ueb" LinkResourceURI="file:command/corrupt.svg" />
27+ </Image>
28+ </Rectangle>
29+ </CharacterStyleRange>
30+</ParagraphStyleRange>
31+```
32+
33+```
34+% pandoc -f latex -t icml
35+\includegraphics{command/SVG_logo.svg}
36+^D
37+<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
38+ <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
39+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
40+ <Properties>
41+ <PathGeometry>
42+ <GeometryPathType PathOpen="false">
43+ <PathPointArray>
44+ <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
45+ <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
46+ <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
47+ <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
48+ </PathPointArray>
49+ </GeometryPathType>
50+ </PathGeometry>
51+ </Properties>
52+ <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
53+ <Properties>
54+ <Profile type="string">
55+ $ID/Embedded
56+ </Profile>
57+ </Properties>
58+ <Link Self="ueb" LinkResourceURI="file:command/SVG_logo.svg" />
59+ </Image>
60+ </Rectangle>
61+ </CharacterStyleRange>
62+</ParagraphStyleRange>
63+```
64+
65+```
66+% pandoc -f latex -t icml
67+\includegraphics{command/SVG_logo-without-xml-declaration.svg}
68+^D
69+<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
70+ <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
71+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
72+ <Properties>
73+ <PathGeometry>
74+ <GeometryPathType PathOpen="false">
75+ <PathPointArray>
76+ <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
77+ <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
78+ <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
79+ <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
80+ </PathPointArray>
81+ </GeometryPathType>
82+ </PathGeometry>
83+ </Properties>
84+ <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
85+ <Properties>
86+ <Profile type="string">
87+ $ID/Embedded
88+ </Profile>
89+ </Properties>
90+ <Link Self="ueb" LinkResourceURI="file:command/SVG_logo-without-xml-declaration.svg" />
91+ </Image>
92+ </Rectangle>
93+ </CharacterStyleRange>
94+</ParagraphStyleRange>
95+```
96+
97+
98+```
99+% pandoc -f latex -t icml
100+\includegraphics{command/inkscape-cube.svg}
101+^D
102+<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
103+ <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
104+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 54.75000 -65.25000">
105+ <Properties>
106+ <PathGeometry>
107+ <GeometryPathType PathOpen="false">
108+ <PathPointArray>
109+ <PathPointType Anchor="-54.75000 -65.25000" LeftDirection="-54.75000 -65.25000" RightDirection="-54.75000 -65.25000" />
110+ <PathPointType Anchor="-54.75000 65.25000" LeftDirection="-54.75000 65.25000" RightDirection="-54.75000 65.25000" />
111+ <PathPointType Anchor="54.75000 65.25000" LeftDirection="54.75000 65.25000" RightDirection="54.75000 65.25000" />
112+ <PathPointType Anchor="54.75000 -65.25000" LeftDirection="54.75000 -65.25000" RightDirection="54.75000 -65.25000" />
113+ </PathPointArray>
114+ </GeometryPathType>
115+ </PathGeometry>
116+ </Properties>
117+ <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -54.75000 -65.25000">
118+ <Properties>
119+ <Profile type="string">
120+ $ID/Embedded
121+ </Profile>
122+ </Properties>
123+ <Link Self="ueb" LinkResourceURI="file:command/inkscape-cube.svg" />
124+ </Image>
125+ </Rectangle>
126+ </CharacterStyleRange>
127+</ParagraphStyleRange>
128+```
129+