diff --git a/extensions/xvi_input.py b/extensions/xvi_input.py index 4d01639..d4dc134 100755 --- a/extensions/xvi_input.py +++ b/extensions/xvi_input.py @@ -20,7 +20,6 @@ binarystdout = sys.stdout.buffer from lxml import etree -from inkex import NSS def xvi2svg(handle, fullsvg=True, strokewidth=6, XVIroot='', @@ -57,12 +56,19 @@ def invert_str(x): root_translate = None stationcoords = set(tuple(line.split()[:2]) for line in stations) - if fullsvg: - root = etree.Element('svg', nsmap=NSS) - else: - root = etree.Element('g', nsmap=NSS) + root = etree.fromstring(""" + + +""") + + if not fullsvg: + root = etree.SubElement(root, th2ex.svg_g) - g_shots = etree.SubElement(root, 'g', {th2ex.inkscape_label: 'Shots'}) + 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 @@ -75,7 +81,7 @@ def process_shots(splays: bool): continue coords[1::2] = map(invert_str, coords[1::2]) coords_str = ' '.join(coords) - e = etree.SubElement(g_shots, 'path', { + etree.SubElement(g_shots, th2ex.svg_path, { 'd': 'M ' + coords_str, 'style': f'fill:none;' + style, }) @@ -89,23 +95,23 @@ def process_shots(splays: bool): coords[1::2] = map(invert_str, coords[1::2]) coords_str = ' '.join(coords) if len(coords) == 2: - e = etree.SubElement(root, 'circle', { + 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, 'path', { + 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, 'g', {th2ex.inkscape_label: 'Stations'}) + g_stations = etree.SubElement(root, th2ex.svg_g, {th2ex.inkscape_label: 'Stations'}) for line in stations: x, y, label = line.split() y = invert_str(y) - e = etree.SubElement(g_stations, 'text', { + e = etree.SubElement(g_stations, th2ex.svg_text, { 'style': f'font-size: {strokewidth*10}', 'x': x, 'y': y, diff --git a/tests/test_xvi_input.py b/tests/test_xvi_input.py new file mode 100644 index 0000000..9a3915c --- /dev/null +++ b/tests/test_xvi_input.py @@ -0,0 +1,37 @@ +import xvi_input + +import sys +import subprocess +from pathlib import Path +from lxml import etree + +TESTS_DATA = Path(__file__).resolve().parent / "data" + + +def test_xvi_input_script(): + svgcontent = subprocess.check_output([ + sys.executable, + xvi_input.__file__, + str(TESTS_DATA / "create.xvi"), + ]) + root = etree.fromstring(svgcontent) + assert root.tag == "{http://www.w3.org/2000/svg}svg" + assert root.get("width") == "13.0cm" + assert root.get("height") == "12.0cm" + assert root.get("viewBox") == "-254.921000 -233.660000 511.810000 472.440000" + assert root.get("{http://therion.speleo.sk/therion}xvi-dx") == "19.685" + assert root.get("{http://therion.speleo.sk/therion}xvi-dy") == "19.685" + assert root[0].tag == "{http://www.w3.org/2000/svg}g" + assert root[0].get("{http://www.inkscape.org/namespaces/inkscape}label") == "Shots" + assert root[0][0].tag == "{http://www.w3.org/2000/svg}path" + assert root[0][0].get("d") == "M 197.83 179.72 174.41 103.15" + + +def test_xvi2svg(): + with open(TESTS_DATA / "create.xvi") as handle: + root = xvi_input.xvi2svg(handle, fullsvg=False) + assert root.tag == "{http://www.w3.org/2000/svg}g" + assert root[0].tag == "{http://www.w3.org/2000/svg}g" + assert root[0].get("{http://www.inkscape.org/namespaces/inkscape}label") == "Shots" + assert root[0][0].tag == "{http://www.w3.org/2000/svg}path" + assert root[0][0].get("d") == "M 197.83 179.72 174.41 103.15"