xbox-board-re icon indicating copy to clipboard operation
xbox-board-re copied to clipboard

Restrict to a more lightweight SVG subset

Open JayFoxRox opened this issue 4 years ago • 0 comments

Inkscape 1.0 has different element and attribute order when exporting.

When I export this file in Inkscape 1.0 (compared to the existing Inkscape 0.92 file), almost all of it will have been touched, leading to unreadable git diff.

I wrote a small (and ugly) script to deal with this:

#!/usr/bin/python3

import sys
from xml.dom import minidom

old_svg = minidom.parse(sys.argv[1])
new_svg = minidom.parse(sys.argv[2])
old_paths = [path.getAttribute('id') for path in old_svg.getElementsByTagName('path')]
new_paths = [path.getAttribute('id') for path in new_svg.getElementsByTagName('path')]

removed = []
for old_path in old_paths:
  if not old_path in new_paths:
    removed += [old_path]
added = []
for new_path in new_paths:
  if not new_path in old_paths:
    added += [new_path]

#print(removed)

#FIXME: Check modified

#print(added)
clean_xml = minidom.Document()
clean_svg = clean_xml.createElement("svg")
for added_id in added:
  #FIXME: Instead, save the full element path so we also know which layer it came from
  for path in new_svg.getElementsByTagName('path'):
    if path.getAttribute('id') == added_id:
      path_id = path.getAttribute('id')
      path_d = path.getAttribute('d')
      path_style = path.getAttribute('style') #FIXME: Remove most of this
      #FIXME: Warn about any other attribute  

      clean_path = clean_xml.createElement("path")
      clean_path.setAttribute('id', path_id)
      clean_path.setAttribute('d', path_d)
      clean_path.setAttribute('style', "fill:none;stroke:#ff0000;stroke-width:1.25;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1")
      clean_svg.appendChild(clean_path)



      print(clean_path.toxml())

open("clean.svg", "w").write(clean_svg.toxml())

old_svg.unlink()
new_svg.unlink()

So this lists the changes and allows me to create a more standardized file format.

The script above was a crude prototype. We probably want a pair of scripts to resolve this:

  • A script to reduce the number of SVG features, so that the file in the repository is always dumbed down.
  • A script to generate a diff from 2 SVGs, so we can highlight changes done between 2 revisions (to ease review).

Feature wise, our "dumb SVG" will only have to support:

  • Paths (for traces): either stroked OR filled (so the "style" is one of these two), stroke width (maybe just a fixed number of allowed ones?), the path ID and geometry; the rest of the features (including various attributes) can be dropped or added on demand.
  • Rectangles / Circles (for vias and pads): features to be determined.
  • Groups / Layers (for organizing the file): features to be determined.
  • Images (for the background, so this can be very simple): features to be determined.

The elements would have to be sorted by their ID, so that re-arranging anything wouldn't lead to trouble.

This would help with creating consistency and allow other tools to be used.

JayFoxRox avatar Jun 11 '20 15:06 JayFoxRox