changeset 43:f67f9ea191b7

from http://sourceforge.net/projects/pdf2xml/files/misc/vec2svg-2.py/download
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 24 Feb 2022 15:43:26 +0000
parents 59517f60826d
children 737cd0fd5adb 6faea25a69b3
files vec2svg-2.py
diffstat 1 files changed, 111 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vec2svg-2.py	Thu Feb 24 15:43:26 2022 +0000
@@ -0,0 +1,111 @@
+"""
+    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)
+        
+