-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxvi_input.py
executable file
·152 lines (127 loc) · 4.75 KB
/
xvi_input.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
#!/usr/bin/env python
'''
Convert XVI file to SVG
Copyright (C) Thomas Holder
Distributed under the terms of the GNU General Public License v2 or later
'''
import contextlib
import sys
import th2ex
import tkinter
from lxml import etree
def xvi2svg(handle, fullsvg=True, strokewidth=6, XVIroot='',
scale: float = 200.0):
"""
Args:
scale: Scale as 1:scale (ignored with XVIroot or fullsvg=False)
"""
# file contents
filecontents = ''.join(handle)
tk_instance = tkinter.Tcl().tk.eval
tk_instance(filecontents)
# methods
def str_tcl2py(name):
if tk_instance('info exists ' + name) == '0':
return ''
return tk_instance('lindex $%s' % (name))
def list_tcl2py(name):
if tk_instance('info exists ' + name) == '0':
return []
L = int(tk_instance('llength $%s' % (name)))
return [tk_instance('lindex $%s %d' % (name, i)) for i in range(L)]
def invert_str(x):
if x[0] == '-':
return x[1:]
return '-' + x
# parse into python objects
stations = list_tcl2py('XVIstations')
shots = list_tcl2py('XVIshots')
sketchlines = list_tcl2py('XVIsketchlines')
grid = list_tcl2py('XVIgrid')
root_translate = None
stationcoords = set(tuple(line.split()[:2]) for line in stations)
root = etree.fromstring("""<?xml version="1.0" ?>
<svg
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:therion="http://therion.speleo.sk/therion"
xmlns="http://www.w3.org/2000/svg">
</svg>
""")
if not fullsvg:
root = etree.SubElement(root, th2ex.svg_g)
g_shots = etree.SubElement(root, th2ex.svg_g, {th2ex.inkscape_label: 'Shots'})
def process_shots(splays: bool):
style = (f"stroke:#f00;stroke-width:{strokewidth / 2}" if (not splays) else
f"stroke:#fc0;stroke-width:{strokewidth / 4}")
for line in shots:
coords = line.split()
is_leg = (tuple(coords[0:2]) in stationcoords and
tuple(coords[2:4]) in stationcoords)
if is_leg == splays:
continue
coords[1::2] = map(invert_str, coords[1::2])
coords_str = ' '.join(coords)
etree.SubElement(g_shots, th2ex.svg_path, {
'd': 'M ' + coords_str,
'style': f'fill:none;' + style,
})
process_shots(True)
process_shots(False)
for line in sketchlines:
color, coords_str = line.split(None, 1)
coords = coords_str.split()
coords[1::2] = map(invert_str, coords[1::2])
coords_str = ' '.join(coords)
if len(coords) == 2:
e = etree.SubElement(root, th2ex.svg_circle, {
'cx': coords[0],
'cy': coords[1],
'r': str(strokewidth),
'style': 'fill:%s;stroke:none' % (color),
})
elif len(coords) > 2:
e = etree.SubElement(root, th2ex.svg_path, {
'd': 'M ' + coords_str,
'style': 'fill:none;stroke:%s;stroke-width:%f' % (color, strokewidth),
})
g_stations = etree.SubElement(root, th2ex.svg_g, {th2ex.inkscape_label: 'Stations'})
for line in stations:
x, y, label = line.split(None, 2)
y = invert_str(y)
e = etree.SubElement(g_stations, th2ex.svg_text, {
'style': f'font-size: {strokewidth * 10}',
'x': x,
'y': y,
})
e.text = label
# station could exist multiple times, take first one
if root_translate is None and XVIroot == label:
root_translate = -float(x), -float(y)
x, y, dx, _, _, dy, nx, ny = map(float, grid)
width = dx * nx
height = dy * ny
y_top = -y - height
root.set(th2ex.therion_xvi_dx, str(dx))
root.set(th2ex.therion_xvi_dy, str(dy))
if root_translate is not None:
root.set('transform', 'translate(%f,%f)' % root_translate)
elif fullsvg:
scale /= 100 # cm
root.set('width', f"{nx / scale}cm")
root.set('height', f"{ny / scale}cm")
root.set('viewBox', '%f %f %f %f' % (x, y_top, width, height))
else:
root.set('transform', 'translate(%f,%f)' % (-float(x), -float(y)))
root.set("style", "stroke-linecap:round;stroke-linejoin:round")
return root
def _get_input_context():
if len(sys.argv) > 1:
return open(sys.argv[-1], encoding="utf-8")
return contextlib.nullcontext(sys.stdin)
if __name__ == '__main__':
with _get_input_context() as handle:
root = xvi2svg(handle)
binarystdout = sys.stdout.buffer
binarystdout.write(etree.tostring(root, encoding='utf-8', xml_declaration=True))
binarystdout.write(b'\n')