Skip to content

Commit

Permalink
xvi -> svg: Fix XML namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed Jan 17, 2024
1 parent 3f32a22 commit dd12861
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
28 changes: 17 additions & 11 deletions extensions/xvi_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
binarystdout = sys.stdout.buffer

from lxml import etree
from inkex import NSS


def xvi2svg(handle, fullsvg=True, strokewidth=6, XVIroot='',
Expand Down Expand Up @@ -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("""<?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, '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
Expand All @@ -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,
})
Expand All @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions tests/test_xvi_input.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit dd12861

Please sign in to comment.