don't embed the SVG inside the .shape, link to it

Dia doesn't seem to support complex SVG in the .shape? Or is the problem
elsewhere?
Anyway, just xlinking to the SVG file yields better results, and avoids
parsing the SVG ourselves, so it may be better.
Rely on ImageMagick to get the aspect ratio and dimensions then. Dia
supports abstract coordinates (we'll max them to 64x64).
This commit is contained in:
Hg 2020-10-08 00:00:11 +02:00
parent c396dde854
commit eb32cc96f2

View File

@ -6,7 +6,6 @@ import xml.etree.ElementTree as ET
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
import getpass import getpass
import re
import subprocess import subprocess
@ -38,19 +37,16 @@ def build_sheet(sheet):
return xsheet return xsheet
def get_float(s): def build_shape(svg_path, width, height):
return float(re.match(r'\d+([.]\d*)?', s)[0]) attrib = {
'xmlns:svg': "http://www.w3.org/2000/svg",
'xmlns:xlink': "http://www.w3.org/1999/xlink",
def build_shape(svg_path): 'xmlns': 'http://www.daa.com.au/~james/dia-shape-ns',
orig_xsvg = ET.parse(str(svg_path)).getroot() }
xshape = ET.Element('shape', attrib=attrib)
xshape = ET.Element('shape', attrib={'xmlns': 'http://www.daa.com.au/~james/dia-shape-ns'})
ET.SubElement(xshape, 'name').text = svg_path.stem ET.SubElement(xshape, 'name').text = svg_path.stem
ET.SubElement(xshape, 'icon').text = f"{svg_path.stem}.png" ET.SubElement(xshape, 'icon').text = f"{svg_path.stem}.png"
width = int(get_float(orig_xsvg.attrib['width']))
height = int(get_float(orig_xsvg.attrib['height']))
xconns = ET.SubElement(xshape, 'connections') xconns = ET.SubElement(xshape, 'connections')
points = [ points = [
(0, 0), (0, 0),
@ -66,7 +62,9 @@ def build_shape(svg_path):
ET.SubElement(xconns, 'point', attrib={'x': str(x), 'y': str(y)}) ET.SubElement(xconns, 'point', attrib={'x': str(x), 'y': str(y)})
ET.SubElement(xshape, 'aspectratio', attrib={'type': 'fixed'}) ET.SubElement(xshape, 'aspectratio', attrib={'type': 'fixed'})
xshape.append(orig_xsvg)
xsvg = ET.SubElement(xshape, 'svg:svg')
ET.SubElement(xsvg, 'svg:image', attrib={'xlink:href': str(svg_path), 'x': '0', 'y': '0', 'width': str(width), 'height': str(height)})
return xshape return xshape
@ -92,13 +90,6 @@ def main():
seen = set() seen = set()
for svg_file in args.svg_file: for svg_file in args.svg_file:
svg_file = Path(svg_file) svg_file = Path(svg_file)
xshape = build_shape(svg_file)
# ET.indent(xshape)
tshape = ET.tostring(xshape, encoding='unicode')
shape_path = pshapes.joinpath(f"{svg_file.stem}.shape")
shape_path.write_text(tshape)
print(f"wrote {shape_path}")
if svg_file.stem in seen: if svg_file.stem in seen:
print(f"ignoring duplicate {svg_file.stem}") print(f"ignoring duplicate {svg_file.stem}")
continue continue
@ -114,6 +105,24 @@ def main():
]) ])
print(f"wrote {png_file}") print(f"wrote {png_file}")
width, height = map(int, subprocess.check_output([
'identify',
'-format',
'%w %h',
png_file,
]).decode().split())
new_path = pshapes.joinpath(f"{svg_file.stem}.svg")
new_path.write_text(svg_file.read_text())
print(f"wrote {new_path}")
xshape = build_shape(svg_file, width, height)
# ET.indent(xshape)
tshape = ET.tostring(xshape, encoding='unicode')
shape_path = pshapes.joinpath(f"{svg_file.stem}.shape")
shape_path.write_text(tshape)
print(f"wrote {shape_path}")
sheet.objects[svg_file.stem] = f"TODO: fill description for {svg_file.stem}" sheet.objects[svg_file.stem] = f"TODO: fill description for {svg_file.stem}"
xsheet = build_sheet(sheet) xsheet = build_sheet(sheet)