SVG export fixes:

- unicode support for text objects
  - always export in scene coordinates with offset from root item
This commit is contained in:
Luke Campagnola 2012-12-28 16:23:28 -05:00
parent b06204b492
commit ea86665dc6
2 changed files with 31 additions and 15 deletions

View File

@ -83,7 +83,7 @@ class SVGExporter(Exporter):
return bytes(xml)
else:
with open(fileName, 'w') as fh:
fh.write(xml)
fh.write(xml.encode('UTF-8'))
xmlHeader = """\
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
@ -171,8 +171,16 @@ def _generateItemSvg(item, nodes=None, root=None):
doc = xml.parseString(xmlStr)
else:
childs = item.childItems()
tr = itemTransform(item, root)
tr = itemTransform(item, item.scene())
## offset to corner of root item
if isinstance(root, QtGui.QGraphicsScene):
rootPos = QtCore.QPoint(0,0)
else:
rootPos = root.scenePos()
tr2 = QtGui.QTransform()
tr2.translate(-rootPos.x(), -rootPos.y())
tr = tr * tr2
#print item, pg.SRTTransform(tr)
#tr.translate(item.pos().x(), item.pos().y())
@ -239,17 +247,23 @@ def _generateItemSvg(item, nodes=None, root=None):
nodes[name] = g1
g1.setAttribute('id', name)
## If this item clips its children, we need to take car of that.
## If this item clips its children, we need to take care of that.
childGroup = g1 ## add children directly to this node unless we are clipping
if not isinstance(item, QtGui.QGraphicsScene):
## See if this item clips its children
if int(item.flags() & item.ItemClipsChildrenToShape) > 0:
## Generate svg for just the path
if isinstance(root, QtGui.QGraphicsScene):
#if isinstance(root, QtGui.QGraphicsScene):
#path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape()))
#else:
#path = QtGui.QGraphicsPathItem(root.mapToParent(item.mapToItem(root, item.shape())))
path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape()))
else:
path = QtGui.QGraphicsPathItem(root.mapToParent(item.mapToItem(root, item.shape())))
item.scene().addItem(path)
try:
pathNode = _generateItemSvg(path, root=root).getElementsByTagName('path')[0]
finally:
item.scene().removeItem(path)
## and for the clipPath element
clip = name + '_clip'
clipNode = g1.ownerDocument.createElement('clipPath')
@ -294,7 +308,10 @@ def correctCoordinates(node, item):
elif ch.tagName == 'path':
removeTransform = True
newCoords = ''
for c in ch.getAttribute('d').strip().split(' '):
oldCoords = ch.getAttribute('d').strip()
if oldCoords == '':
continue
for c in oldCoords.split(' '):
x,y = c.split(',')
if x[0].isalpha():
t = x[0]
@ -317,8 +334,6 @@ def correctCoordinates(node, item):
#fs = c[1]-c[2]
#fs = (fs**2).sum()**0.5
#ch.setAttribute('font-size', str(fs))
else:
print('warning: export not implemented for SVG tag %s (from item %s)' % (ch.tagName, item))
## correct line widths if needed
if removeTransform and ch.getAttribute('vector-effect') != 'non-scaling-stroke':

View File

@ -115,6 +115,7 @@ class TextItem(UIGraphicsItem):
self.viewRangeChanged()
self.lastTransform = tr
if self.border.style() != QtCore.Qt.NoPen or self.fill.style() != QtCore.Qt.NoBrush:
p.setPen(self.border)
p.setBrush(self.fill)
p.setRenderHint(p.Antialiasing, True)