comparison vec2svg-2.py @ 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
children
comparison
equal deleted inserted replaced
42:59517f60826d 43:f67f9ea191b7
1 """
2 vec2svg.py
3 Copyright 2009 XEROX
4 """
5
6 import libxml2
7 from optparse import OptionParser
8
9 sSVGNs = "svg"
10 sSVGNsURI = "http://www.w3.org/2000/svg"
11 gnsSVG = None
12 def generatePath(doc,node):
13
14
15 pathNode = libxml2.newNode("path")
16 pathNode.setNs(gnsSVG)
17
18 ctxt = doc.xpathNewContext()
19 xpath = "./M|L|C"
20 ctxt.setContextNode(node)
21 lInstruction= ctxt.xpathEval(xpath)
22 ctxt.xpathFreeContext()
23
24 sPath=""
25 for inst in lInstruction:
26 if inst.name =="M" or inst.name =="L":
27 sPath = sPath + "%s %g %g " % (inst.name, float(inst.prop("x")), float(inst.prop("y")))
28 elif inst.name == "C":
29 sPath = sPath + "%s %g %g %g %g %g %g " % (inst.name, float(inst.prop("x1")),\
30 float(inst.prop("y1")), float(inst.prop("x2")),\
31 float(inst.prop("y2")),float(inst.prop("x3")),float(inst.prop("y3")))
32
33 pathNode.newProp("d", sPath)
34 return pathNode
35
36 def generateSVG(node, doc):
37
38 ## vectors
39 ctxt = doc.xpathNewContext()
40 xpath = "./%s" % ("GROUP")
41 ctxt.setContextNode(node)
42 lGroup= ctxt.xpathEval(xpath)
43 ctxt.xpathFreeContext()
44
45 image = libxml2.newDoc("1.0")
46 svgRoot = libxml2.newNode("svg")
47 svgRoot.newNs(sSVGNsURI, None)
48 svgRoot.newNs("http://www.w3.org/1999/xlink","xlink")
49 svgRoot.setNs(gnsSVG)
50 image.setRootElement(svgRoot)
51
52 for elt in lGroup:
53 svgg = libxml2.newNode("g")
54 svgRoot.addChild(svgg)
55 svgg.setNs(gnsSVG)
56 svgg.setProp("style", elt.prop("style"))
57 path = generatePath(doc,elt)
58 svgg.addChild(path)
59
60 return image
61
62
63 def loadDom(filename):
64 doc = libxml2.parseFile(filename)
65 return doc
66
67 def add_option(parser,*args, **kwargs):
68 """add a new command line option to the parser"""
69 parser.add_option(*args, **kwargs)
70
71 def parseCommandLine(parser):
72 (options, args) = parser.parse_args()
73 dOptions = {}
74 for k,v in options.__dict__.items():
75 if v != None: dOptions[k] = v
76 return dOptions
77
78 def writeDom(outFile,doc, bIndent=False):
79 if bIndent:
80 doc.saveFormatFile(outFile, bIndent)
81 else:
82 doc.saveFile(outFile)
83
84 if __name__ == "__main__":
85
86 dParams = {}
87 usage="python vec2svg.py -i inputFile -o outputFile"
88 parser = OptionParser(usage, version="0.1")
89 parser.description = ".vec to .svg conversion"
90 add_option(parser,"-i", "--input", dest="input", default="", action="store", type="string", help="input file (.vec)", metavar="<file>")
91 add_option(parser,"-o", "--output", dest="output", default="-", action="store", type="string", help="output file (SVG)", metavar="<file>")
92
93 dParams = parseCommandLine(parser)
94 bInput = "input" in dParams
95 if bInput:
96 inputFileName = dParams["input"]
97 print(inputFileName)
98 bOutput = "output" in dParams
99 if bOutput:
100 outputFileName = dParams["output"]
101 else:
102 output = "-"
103
104 doc = loadDom(inputFileName)
105 svgFile = generateSVG(doc.getRootElement(), doc)
106 writeDom(outputFileName,svgFile)
107
108 else:
109 print(usage)
110
111