Improve SVG image size code.

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

See #3580.
This commit is contained in:
Marc Schreiber 2017-04-20 11:11:01 +02:00 committed by John MacFarlane
parent 5c44fd554f
commit 03cb05f4c6
7 changed files with 323 additions and 1 deletions

View file

@ -136,6 +136,10 @@ Extra-Source-Files:
test/*.native
test/command/*.md
test/command/abbrevs
test/command/SVG_logo-without-xml-declaration.svg
test/command/SVG_logo.svg
test/command/corrupt.svg
test/command/inkscape-cube.svg
test/command/sub-file-chapter-1.tex
test/command/sub-file-chapter-2.tex
test/command/3510-subdoc.org

View file

@ -116,7 +116,7 @@ imageType img = case B.take 4 img of
_ -> mzero
findSvgTag :: ByteString -> Bool
findSvgTag img = B.null $ snd (B.breakSubstring img "<svg")
findSvgTag img = "<svg" `B.isInfixOf` img || "<SVG" `B.isInfixOf` img
imageSize :: WriterOptions -> ByteString -> Either String ImageSize
imageSize opts img =

View file

@ -0,0 +1,32 @@
<svg viewBox="-50 -50 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>SVG Logo</title>
<rect id="background" x="-50" y="-50" width="100" height="100" rx="4" fill="#f90"/>
<rect id="top-left" x="-50" y="-50" width="50" height="50" rx="4" fill="#ffb13b"/>
<rect id="bottom-right" width="50" height="50" rx="4" fill="#de8500"/>
<use stroke="#f90" stroke-width="22.6" xlink:href="#a"/>
<circle r="26"/>
<use stroke="#000" stroke-width="12" xlink:href="#a"/>
<g id="a">
<g id="b">
<g id="c">
<circle id="n" cy="-31.6" r="7.1" fill="#fff"/>
<path d="m0 31.6v-63.2" stroke="#fff" stroke-width="10"/>
<use y="63.2" xlink:href="#n"/>
</g>
<use transform="rotate(90)" xlink:href="#c"/>
</g>
<use transform="rotate(45)" xlink:href="#b"/>
</g>
<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"/>
<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"/>
<use stroke="#000" stroke-width="7.4" xlink:href="#s"/>
<g id="svg-text" stroke="#fff" stroke-width="6.4">
<g id="s">
<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"/>
<g stroke-width=".5" stroke="#000">
<path d="m4.76 3h6.83l-8.24 39.8h-6.85l-8.26-39.8h6.85l4.84 23.3z" fill="#fff"/>
<path d="m23.23 19.55v6.9m4.838-11.71h6.9m-70.16 16.43h6.9m9.62-16.52h6.9" stroke-linecap="square"/>
</g>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

33
test/command/SVG_logo.svg Normal file
View file

@ -0,0 +1,33 @@
<?xml version="1.0"?>
<svg viewBox="-50 -50 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>SVG Logo</title>
<rect id="background" x="-50" y="-50" width="100" height="100" rx="4" fill="#f90"/>
<rect id="top-left" x="-50" y="-50" width="50" height="50" rx="4" fill="#ffb13b"/>
<rect id="bottom-right" width="50" height="50" rx="4" fill="#de8500"/>
<use stroke="#f90" stroke-width="22.6" xlink:href="#a"/>
<circle r="26"/>
<use stroke="#000" stroke-width="12" xlink:href="#a"/>
<g id="a">
<g id="b">
<g id="c">
<circle id="n" cy="-31.6" r="7.1" fill="#fff"/>
<path d="m0 31.6v-63.2" stroke="#fff" stroke-width="10"/>
<use y="63.2" xlink:href="#n"/>
</g>
<use transform="rotate(90)" xlink:href="#c"/>
</g>
<use transform="rotate(45)" xlink:href="#b"/>
</g>
<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"/>
<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"/>
<use stroke="#000" stroke-width="7.4" xlink:href="#s"/>
<g id="svg-text" stroke="#fff" stroke-width="6.4">
<g id="s">
<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"/>
<g stroke-width=".5" stroke="#000">
<path d="m4.76 3h6.83l-8.24 39.8h-6.85l-8.26-39.8h6.85l4.84 23.3z" fill="#fff"/>
<path d="m23.23 19.55v6.9m4.838-11.71h6.9m-70.16 16.43h6.9m9.62-16.52h6.9" stroke-linecap="square"/>
</g>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

5
test/command/corrupt.svg Normal file
View file

@ -0,0 +1,5 @@
Lorem ipsum dolor sit amet etiam. A pede dolor neque pretium luctus pharetra vel rutrum. Orci nonummy ac. At eu est tempor
proin wisi. Nunc tincidunt proin. Suspendisse lorem commodo. Integer diam diam semper commodo dictum et tellus eu ultrices
nec erat pulvinar porttitor nulla nulla mauris orci libero eros elementum et possimus voluptate. Velit morbi et. Luctus diam
in. Lorem tincidunt sem dolor rerum mauris. Dis taciti posuere pellentesque sed rutrum. Lectus donec fusce in dictum pede.
In etiam congue. Aliquam aliquet elit arcu mauris enim. Risus at enim.

View file

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="38.772217mm"
height="46.163891mm"
viewBox="0 0 38.772217 46.163891"
version="1.1"
id="svg8"
inkscape:version="0.92.1 r"
sodipodi:docname="cube.svg">
<defs
id="defs2">
<inkscape:perspective
sodipodi:type="inkscape:persp3d"
inkscape:vp_x="-48.380952 : -45.023815 : 1"
inkscape:vp_y="0 : 1000 : 0"
inkscape:vp_z="161.61905 : -45.023817 : 1"
inkscape:persp3d-origin="56.619048 : -94.523816 : 1"
id="perspective4485" />
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.98994949"
inkscape:cx="-63.181251"
inkscape:cy="-116.38602"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:window-width="1920"
inkscape:window-height="1136"
inkscape:window-x="1920"
inkscape:window-y="30"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-149.67857,78.746839)">
<g
sodipodi:type="inkscape:box3d"
id="g4487"
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"
inkscape:perspectiveID="#perspective4485"
inkscape:corner0="1.1045097 : 0.18860662 : 0 : 1"
inkscape:corner7="0.52634769 : 0.15538942 : 0.25 : 1">
<path
sodipodi:type="inkscape:box3dside"
id="path4489"
style="fill:#353564;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
inkscape:box3dsidetype="6"
d="m 151.19047,-53.658435 v 15.783818 l 17.00006,5.342459 v -14.107905 z"
points="151.19047,-37.874617 168.19053,-32.532158 168.19053,-46.640063 151.19047,-53.658435 " />
<path
sodipodi:type="inkscape:box3dside"
id="path4499"
style="fill:#e9e9ff;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
inkscape:box3dsidetype="11"
d="m 168.19053,-46.640063 21.77216,-19.229539 v 18.699717 l -21.77216,14.637727 z"
points="189.96269,-65.869602 189.96269,-47.169885 168.19053,-32.532158 168.19053,-46.640063 " />
<path
sodipodi:type="inkscape:box3dside"
id="path4491"
style="fill:#4d4d9f;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
inkscape:box3dsidetype="5"
d="m 151.19047,-53.658435 18.89881,-25.037614 19.87341,12.826447 -21.77216,19.229539 z"
points="170.08928,-78.696049 189.96269,-65.869602 168.19053,-46.640063 151.19047,-53.658435 " />
<path
sodipodi:type="inkscape:box3dside"
id="path4497"
style="fill:#afafde;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
inkscape:box3dsidetype="13"
d="m 151.19047,-37.874617 18.89881,-19.058894 19.87341,9.763626 -21.77216,14.637727 z"
points="170.08928,-56.933511 189.96269,-47.169885 168.19053,-32.532158 151.19047,-37.874617 " />
<path
sodipodi:type="inkscape:box3dside"
id="path4495"
style="fill:#d7d7ff;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
inkscape:box3dsidetype="14"
d="m 170.08928,-78.696049 v 21.762538 l 19.87341,9.763626 v -18.699717 z"
points="170.08928,-56.933511 189.96269,-47.169885 189.96269,-65.869602 170.08928,-78.696049 " />
<path
sodipodi:type="inkscape:box3dside"
id="path4493"
style="fill:#8686bf;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
inkscape:box3dsidetype="3"
d="m 151.19047,-53.658435 18.89881,-25.037614 v 21.762538 l -18.89881,19.058894 z"
points="170.08928,-78.696049 170.08928,-56.933511 151.19047,-37.874617 151.19047,-53.658435 " />
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.1 KiB

129
test/command/svg.md Normal file
View file

@ -0,0 +1,129 @@
```
% pandoc -f latex -t icml
\includegraphics{command/corrupt.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
<PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
<PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
<PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
<PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
<Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
<Properties>
<Profile type="string">
$ID/Embedded
</Profile>
</Properties>
<Link Self="ueb" LinkResourceURI="file:command/corrupt.svg" />
</Image>
</Rectangle>
</CharacterStyleRange>
</ParagraphStyleRange>
```
```
% pandoc -f latex -t icml
\includegraphics{command/SVG_logo.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
<PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
<PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
<PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
<PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
<Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
<Properties>
<Profile type="string">
$ID/Embedded
</Profile>
</Properties>
<Link Self="ueb" LinkResourceURI="file:command/SVG_logo.svg" />
</Image>
</Rectangle>
</CharacterStyleRange>
</ParagraphStyleRange>
```
```
% pandoc -f latex -t icml
\includegraphics{command/SVG_logo-without-xml-declaration.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
<PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
<PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
<PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
<PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
<Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
<Properties>
<Profile type="string">
$ID/Embedded
</Profile>
</Properties>
<Link Self="ueb" LinkResourceURI="file:command/SVG_logo-without-xml-declaration.svg" />
</Image>
</Rectangle>
</CharacterStyleRange>
</ParagraphStyleRange>
```
```
% pandoc -f latex -t icml
\includegraphics{command/inkscape-cube.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 54.75000 -65.25000">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
<PathPointType Anchor="-54.75000 -65.25000" LeftDirection="-54.75000 -65.25000" RightDirection="-54.75000 -65.25000" />
<PathPointType Anchor="-54.75000 65.25000" LeftDirection="-54.75000 65.25000" RightDirection="-54.75000 65.25000" />
<PathPointType Anchor="54.75000 65.25000" LeftDirection="54.75000 65.25000" RightDirection="54.75000 65.25000" />
<PathPointType Anchor="54.75000 -65.25000" LeftDirection="54.75000 -65.25000" RightDirection="54.75000 -65.25000" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
<Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -54.75000 -65.25000">
<Properties>
<Profile type="string">
$ID/Embedded
</Profile>
</Properties>
<Link Self="ueb" LinkResourceURI="file:command/inkscape-cube.svg" />
</Image>
</Rectangle>
</CharacterStyleRange>
</ParagraphStyleRange>
```