Move drawing to origin for SVGs
This commit is contained in:
parent
635a42a25e
commit
31d46088c2
|
@ -101,8 +101,7 @@ class Formats:
|
||||||
|
|
||||||
if fmt in ['svg', 'svg_Ponoko']:
|
if fmt in ['svg', 'svg_Ponoko']:
|
||||||
svg = svgutil.SVGFile(filename)
|
svg = svgutil.SVGFile(filename)
|
||||||
svg.getEnvelope()
|
svg.fix()
|
||||||
svg.rewriteViewPort()
|
|
||||||
else:
|
else:
|
||||||
ps = PSFile(filename)
|
ps = PSFile(filename)
|
||||||
ps.adjustDocumentMedia()
|
ps.adjustDocumentMedia()
|
||||||
|
|
|
@ -60,6 +60,11 @@ class SVGFile(object):
|
||||||
self.tree = ElementTree.parse(filename)
|
self.tree = ElementTree.parse(filename)
|
||||||
self.symbol_extends = {}
|
self.symbol_extends = {}
|
||||||
|
|
||||||
|
def fix(self):
|
||||||
|
self.getEnvelope()
|
||||||
|
self.moveOrigin()
|
||||||
|
self.rewriteViewPort()
|
||||||
|
|
||||||
def getExtend(self, element, extend):
|
def getExtend(self, element, extend):
|
||||||
if element.tag.endswith("}path"):
|
if element.tag.endswith("}path"):
|
||||||
minx = maxx = miny = maxy = None
|
minx = maxx = miny = maxy = None
|
||||||
|
@ -98,6 +103,37 @@ class SVGFile(object):
|
||||||
root = self.tree.getroot()
|
root = self.tree.getroot()
|
||||||
self.extend = self.getExtend(root, Extend())
|
self.extend = self.getExtend(root, Extend())
|
||||||
|
|
||||||
|
|
||||||
|
def _moveElement(self, e, dx, dy):
|
||||||
|
if e.tag.endswith("}symbol"):
|
||||||
|
return
|
||||||
|
if e.tag.endswith("}path"):
|
||||||
|
minx = maxx = miny = maxy = None
|
||||||
|
m = self.transformre.match(e.attrib.get("transform", ""))
|
||||||
|
|
||||||
|
if m:
|
||||||
|
matrix = [float(m.group(i)) for i in range(1, 12, 2)]
|
||||||
|
else:
|
||||||
|
matrix = [1, 0,
|
||||||
|
0, 1,
|
||||||
|
0, 0]
|
||||||
|
matrix[4] += dx
|
||||||
|
matrix[5] += dy
|
||||||
|
e.attrib["transform"] = "matrix(%s)" % (",".join(("%.4f" % m for m in matrix)))
|
||||||
|
if e.tag.endswith("}use"):
|
||||||
|
e.attrib["x"] = "%.4f" % (float(e.attrib["x"])+dx)
|
||||||
|
e.attrib["y"] = "%.4f" % (float(e.attrib["y"])+dy)
|
||||||
|
|
||||||
|
for child in e:
|
||||||
|
self._moveElement(child, dx, dy)
|
||||||
|
|
||||||
|
def moveOrigin(self):
|
||||||
|
self._moveElement(self.tree.getroot(),
|
||||||
|
-self.extend.minx+10, -self.extend.miny+10)
|
||||||
|
self.extend.maxx -= self.extend.minx-20
|
||||||
|
self.extend.maxy -= self.extend.miny-20
|
||||||
|
self.extend.minx = self.extend.miny = 0.0
|
||||||
|
|
||||||
def rewriteViewPort(self):
|
def rewriteViewPort(self):
|
||||||
"""
|
"""
|
||||||
Modify SVG file to have the correct width, height and viewPort attributes.
|
Modify SVG file to have the correct width, height and viewPort attributes.
|
||||||
|
|
Loading…
Reference in New Issue