forked from xyz2tex/svg2tikz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_svg2tikz.py
242 lines (196 loc) · 8.85 KB
/
test_svg2tikz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
import unittest
from svg2tikz.extensions.tikz_export import convert_svg, parse_transform
from svg2tikz.extensions.tikz_export import TikZPathExporter
basic_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<desc>Example rect01 - rectangle with sharp corners</desc>
<!-- Show outline of canvas using 'rect' element -->
<rect x="1" y="1" width="1198" height="398" id="rect1"
fill="none" stroke="blue" stroke-width="2"/>
<rect x="400" y="100" width="400" height="200" id="rect2"
fill="yellow" stroke="navy" stroke-width="10" />
</svg>
"""
basic2_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<desc>Example rect01 - rectangle with sharp corners</desc>
<!-- Show outline of canvas using 'rect' element -->
<rect x="1" y="1" width="1198" height="398" id="rect1"
fill="none" stroke="blue" stroke-width="2"/>
<rect x="400" y="100" width="400" height="200" id="rect2"
fill="yellow" stroke="navy" stroke-width="10" />
<rect x="400" y="100" width="400" height="200" id="rect3"
fill="none" stroke="green" stroke-width="10" />
</svg>
"""
class InterfaceTest(unittest.TestCase):
def test_basicsvg(self):
code = convert_svg(basic_svg)
assert 'rect' in code
def test_basic_codeonly(self):
code = convert_svg(basic_svg, codeoutput="codeonly")
assert 'documentclass' not in code
assert r'\begin{tikzpicture}' not in code
def test_basic_figonly(self):
code = convert_svg(basic_svg, codeoutput="figonly")
assert 'documentclass' not in code
assert r'\begin{tikzpicture}' in code
def test_no_ids(self):
code = convert_svg(basic2_svg, ids=[], verbose=True)
assert 'rect1' in code
assert 'rect2' in code
def test_select_id_rect1(self):
code = convert_svg(basic2_svg, ids=['rect1'], verbose=True)
assert 'rect1' in code
assert 'rect2' not in code
def test_select_id_rect1and3(self):
code = convert_svg(basic2_svg, ids=['rect1', 'rect3'], verbose=True)
assert 'rect1' in code
assert 'rect2' not in code
assert 'rect3' in code
paint_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
xmlns="http://www.w3.org/2000/svg" version="1.1"
fill="red">
<desc>Example rect01 - rectangle with sharp corners</desc>
<!-- Show outline of canvas using 'rect' element -->
<rect x="1" y="1" width="1198" height="398"
stroke="blue" stroke-width="2"/>
</svg>
"""
class PaintingTest(unittest.TestCase):
def test_inherited_fill(self):
code = convert_svg(paint_svg)
self.assertTrue('fill=red' in code)
class TestTransformation(unittest.TestCase):
def test_exponential_notation_bug(self):
converter = TikZPathExporter(inkscape_mode=False)
transform = "matrix(1,-0.43924987,0,1,-2.3578e-6,37.193992)"
trans1 = parse_transform(transform)
self.assertFalse('e-06' in converter._convert_transform_to_tikz(trans1)[0])
trans2 = parse_transform("translate(1e-6,0.03057816)")
self.assertFalse('e-06' in converter._convert_transform_to_tikz(trans2)[0])
text_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="10cm" height="3cm" viewBox="0 0 1000 300"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<desc>Example text01 - 'Hello, out there' in blue</desc>
<text x="250" y="150">a%b</text>
</svg>"""
class TestTextMode(unittest.TestCase):
def test_escape(self):
code = convert_svg(text_svg, codeoutput="codeonly")
self.assertTrue(r'a\%b' in code)
code = convert_svg(text_svg, codeoutput="codeonly", texmode='escape')
self.assertTrue(r'a\%b' in code)
def test_raw(self):
code = convert_svg(text_svg, codeoutput="codeonly", texmode='raw')
self.assertTrue(r'a%b' in code)
def test_math(self):
code = convert_svg(text_svg, codeoutput="codeonly", texmode='math')
self.assertTrue(r'$a%b$' in code)
no_height_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x=".01cm" y=".01cm" width="4.98cm" height="4.98cm"
fill="none" stroke="blue" stroke-width=".02cm"/>
</svg>"""
class BugsTest(unittest.TestCase):
def test_no_svgheight_error(self):
code = convert_svg(no_height_svg)
self.assertTrue('rectangle' in code)
defs_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg viewBox = "0 0 1000 1000" version = "1.1">
<defs>
<!-- A circle of radius 200 -->
<circle id = "s1" cx = "200" cy = "200" r = "200" fill = "yellow" stroke = "black" stroke-width = "3"/>
<!-- An ellipse (rx=200,ry=150) -->
<ellipse id = "s2" cx = "200" cy = "150" rx = "200" ry = "150" fill = "salmon" stroke = "black" stroke-width = "3"/>
</defs>
<use x = "100" y = "100" xlink:href="#s1"/>
<use x = "100" y = "650" xlink:href="#s2"/>
</svg>"""
defs1_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="10cm" height="3cm" viewBox="0 0 100 30" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<desc>Example Use01 - Simple case of 'use' on a 'rect'</desc>
<defs>
<rect id="MyRect" width="60" height="10"/>
<circle id = "s1" cx = "200" cy = "200" r = "200" fill = "yellow" stroke = "black" stroke-width = "3"/>
<ellipse id = "s2" cx = "200" cy = "150" rx = "200" ry = "150" fill = "salmon" stroke = "black" stroke-width = "3"/>
</defs>
<rect x=".1" y=".1" width="99.8" height="29.8"
fill="none" stroke="blue" stroke-width=".2" />
<use x="20" y="10" xlink:href="#MyRect" />
</svg>"""
class DefsTest(unittest.TestCase):
def test_no_used_defs(self):
code = convert_svg(defs1_svg)
self.assertTrue('circle' not in code)
arrows_svg = r"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="4in" height="2in"
viewBox="0 0 4000 2000" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="Triangle"
viewBox="0 0 10 10" refX="0" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
</defs>
<path d="M 1000 750 L 2000 750 L 2500 1250"
fill="none" stroke="black" stroke-width="100"
marker-end="url(#Triangle)" />
</svg>"""
class MarkersTest(unittest.TestCase):
def test_marker_options(self):
code = convert_svg(arrows_svg, markers="ignore", codeoutput="codeonly")
self.assertTrue('>' not in code)
def test_marker2_options(self):
code = convert_svg(arrows_svg, markers="arrows", codeoutput="codeonly")
self.assertTrue('->' in code, 'code="%s"' % code)
# https://github.com/kjellmf/svg2tikz/issues/20
class TestIssue20(unittest.TestCase):
test_svg = """<?xml version="1.0" standalone="no"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="370px"
width="710px">
<defs>
<path id="sum" d="m-20,0 h40 m-20,-20 v40 m0,10 a30,30 0 0,1 0,-60 30,30 0 0,1 0,60" fill="white" stroke="black"
stroke-width="3px"/>
</defs>
<use xlink:href="#sum" x="350px" y="50px"/>
<text x="10px" y="60px"><tspan font-style="italic">x[n]</tspan></text>
</svg>"""
test_svg2 = """<?xml version="1.0" standalone="no"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="370px"
width="710px">
<text x="10px" y="60px"><tspan font-style="italic">x[n]</tspan></text>
</svg>"""
def test_pxtransform(self):
try:
code = convert_svg(self.test_svg)
except ValueError:
self.fail("Failed to parse transform")
def test_px_in_output(self):
code = convert_svg(self.test_svg2, codeoutput="codeonly")
self.assertNotIn("px", code)
if __name__ == '__main__':
unittest.main()