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 ce05e6fb8a
commit f32a04a433
2 changed files with 31 additions and 15 deletions

View File

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

View File

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