From ff7ee228859ccca57c1766997897f2e214cf9735 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Thu, 23 Feb 2017 17:30:26 +0100 Subject: [PATCH] Add svgutil.svgMerge() allwing to add generated box into anoterh svg Implemetation has still many short comings. E.g. assumes units and layout of document passed in. Also assumes to be merged document to be generated by boxes.py. --- boxes/svgutil.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/boxes/svgutil.py b/boxes/svgutil.py index 06c12be..69c57a0 100755 --- a/boxes/svgutil.py +++ b/boxes/svgutil.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import xml.parsers.expat +from lxml import etree as et import re @@ -100,6 +101,31 @@ class SVGFile(object): raise ValueError("Could not understand SVG file") +def svgMerge(box, inkscape, output): + parser = et.XMLParser(remove_blank_text=True) + + src_tree = et.parse(box, parser) + dest_tree = et.parse(inkscape, parser) + dest_root = dest_tree.getroot() + + m = re.match(r"(\d+\.?\d*)(\D+)", dest_root.get("height")) + height, units = m.groups() + + scale = {"mm" : 1.0, + "pt" : 7.2, # XXX + }.get(units, 1.0) + + + for el in src_tree.getroot(): + import sys + dest_root.append(el) + if el.tag.endswith("g"): + el.set("transform", "matrix(%f,0,0,%f, 0, %i)" % ( + scale, scale, -10000+float(height)*scale)) + + # write the xml file + et.ElementTree(dest_root).write(output, pretty_print=True, encoding='utf-8', xml_declaration=True) + if __name__ == "__main__": svg = SVGFile("examples/box.svg") svg.getEnvelope()