Mercurial > hg > python
view vec2svg-2.py @ 52:10f17205908f
debug info for bad subdict
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Mon, 29 May 2023 21:57:13 +0100 |
parents | f67f9ea191b7 |
children |
line wrap: on
line source
""" vec2svg.py Copyright 2009 XEROX """ import libxml2 from optparse import OptionParser sSVGNs = "svg" sSVGNsURI = "http://www.w3.org/2000/svg" gnsSVG = None def generatePath(doc,node): pathNode = libxml2.newNode("path") pathNode.setNs(gnsSVG) ctxt = doc.xpathNewContext() xpath = "./M|L|C" ctxt.setContextNode(node) lInstruction= ctxt.xpathEval(xpath) ctxt.xpathFreeContext() sPath="" for inst in lInstruction: if inst.name =="M" or inst.name =="L": sPath = sPath + "%s %g %g " % (inst.name, float(inst.prop("x")), float(inst.prop("y"))) elif inst.name == "C": sPath = sPath + "%s %g %g %g %g %g %g " % (inst.name, float(inst.prop("x1")),\ float(inst.prop("y1")), float(inst.prop("x2")),\ float(inst.prop("y2")),float(inst.prop("x3")),float(inst.prop("y3"))) pathNode.newProp("d", sPath) return pathNode def generateSVG(node, doc): ## vectors ctxt = doc.xpathNewContext() xpath = "./%s" % ("GROUP") ctxt.setContextNode(node) lGroup= ctxt.xpathEval(xpath) ctxt.xpathFreeContext() image = libxml2.newDoc("1.0") svgRoot = libxml2.newNode("svg") svgRoot.newNs(sSVGNsURI, None) svgRoot.newNs("http://www.w3.org/1999/xlink","xlink") svgRoot.setNs(gnsSVG) image.setRootElement(svgRoot) for elt in lGroup: svgg = libxml2.newNode("g") svgRoot.addChild(svgg) svgg.setNs(gnsSVG) svgg.setProp("style", elt.prop("style")) path = generatePath(doc,elt) svgg.addChild(path) return image def loadDom(filename): doc = libxml2.parseFile(filename) return doc def add_option(parser,*args, **kwargs): """add a new command line option to the parser""" parser.add_option(*args, **kwargs) def parseCommandLine(parser): (options, args) = parser.parse_args() dOptions = {} for k,v in options.__dict__.items(): if v != None: dOptions[k] = v return dOptions def writeDom(outFile,doc, bIndent=False): if bIndent: doc.saveFormatFile(outFile, bIndent) else: doc.saveFile(outFile) if __name__ == "__main__": dParams = {} usage="python vec2svg.py -i inputFile -o outputFile" parser = OptionParser(usage, version="0.1") parser.description = ".vec to .svg conversion" add_option(parser,"-i", "--input", dest="input", default="", action="store", type="string", help="input file (.vec)", metavar="<file>") add_option(parser,"-o", "--output", dest="output", default="-", action="store", type="string", help="output file (SVG)", metavar="<file>") dParams = parseCommandLine(parser) bInput = "input" in dParams if bInput: inputFileName = dParams["input"] print(inputFileName) bOutput = "output" in dParams if bOutput: outputFileName = dParams["output"] else: output = "-" doc = loadDom(inputFileName) svgFile = generateSVG(doc.getRootElement(), doc) writeDom(outputFileName,svgFile) else: print(usage)