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.
This commit is contained in:
parent
39e74e67ba
commit
ff7ee22885
|
@ -15,6 +15,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue