summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlvin Penner <penner@vaxxine.com>2011-07-03 23:21:47 +0000
committerapenner <penner@vaxxine.com>2011-07-03 23:21:47 +0000
commit3043388b308a91f52ca9a838e79551301661cecc (patch)
tree7448eff1b8bcf7c29968f7908ba3845655b407af
parentGTK+ cleanup: gtk_timeout_remove (diff)
downloadinkscape-3043388b308a91f52ca9a838e79551301661cecc.tar.gz
inkscape-3043388b308a91f52ca9a838e79551301661cecc.zip
Extensions. Measure Path. use transform elements and viewBox (Bug 803791)
Fixed bugs: - https://launchpad.net/bugs/803791 (bzr r10414)
-rw-r--r--share/extensions/measure.py54
-rw-r--r--share/extensions/simpletransform.py8
2 files changed, 28 insertions, 34 deletions
diff --git a/share/extensions/measure.py b/share/extensions/measure.py
index 147027fb6..e1f8eab1d 100644
--- a/share/extensions/measure.py
+++ b/share/extensions/measure.py
@@ -30,7 +30,7 @@ TODO:
2. check direction >90 or <-90 Degrees
3. rotate by 180 degrees around text center
'''
-import inkex, simplestyle, simplepath, sys, cubicsuperpath, bezmisc, locale
+import inkex, simplestyle, simplepath, simpletransform, sys, cubicsuperpath, bezmisc, locale
# Set current system locale
locale.setlocale(locale.LC_ALL, '')
@@ -73,23 +73,16 @@ def csplength(csp):
return lengths, total
def csparea(csp):
area = 0.0
- n0 = 0.0
- x0 = 0.0
- y0 = 0.0
for sp in csp:
for i in range(len(sp)): # calculate polygon area
area += 0.5*sp[i-1][1][0]*(sp[i][1][1] - sp[i-2][1][1])
- if abs(sp[i-1][1][0]-sp[i][1][0]) > 0.001 or abs(sp[i-1][1][1]-sp[i][1][1]) > 0.001:
- n0 += 1.0
- x0 += sp[i][1][0]
- y0 += sp[i][1][1]
for i in range(1, len(sp)): # add contribution from cubic Bezier
bezarea = ( 0.0*sp[i-1][1][1] + 2.0*sp[i-1][2][1] + 1.0*sp[i][0][1] - 3.0*sp[i][1][1])*sp[i-1][1][0]
bezarea += (-2.0*sp[i-1][1][1] + 0.0*sp[i-1][2][1] + 1.0*sp[i][0][1] + 1.0*sp[i][1][1])*sp[i-1][2][0]
bezarea += (-1.0*sp[i-1][1][1] - 1.0*sp[i-1][2][1] + 0.0*sp[i][0][1] + 2.0*sp[i][1][1])*sp[i][0][0]
bezarea += ( 3.0*sp[i-1][1][1] - 1.0*sp[i-1][2][1] - 2.0*sp[i][0][1] + 0.0*sp[i][1][1])*sp[i][1][0]
area += 0.15*bezarea
- return abs(area), x0/n0, y0/n0
+ return abs(area)
class Length(inkex.Effect):
def __init__(self):
@@ -134,12 +127,20 @@ class Length(inkex.Effect):
def effect(self):
# get number of digits
prec = int(self.options.precision)
+ factor = 1.0
+ doc = self.document.getroot()
+ if doc.get('viewBox'):
+ [viewx, viewy, vieww, viewh] = doc.get('viewBox').split(' ')
+ factor = float(doc.get('width'))/float(vieww)
+ if float(doc.get('height'))/float(viewh) < factor:
+ factor = float(doc.get('height'))/float(viewh)
+ self.options.fontsize /= factor
# loop over all selected paths
for id, node in self.selected.iteritems():
if node.tag == inkex.addNS('path','svg'):
self.group = inkex.etree.SubElement(node.getparent(),inkex.addNS('text','svg'))
- t = node.get('transform')
+ # t = node.get('transform')
# Removed to fix LP #308183
# (Measure Path text shifted when used with a copied object)
#if t:
@@ -147,31 +148,30 @@ class Length(inkex.Effect):
a =[]
+ mat = simpletransform.composeParents(node, [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
p = cubicsuperpath.parsePath(node.get('d'))
- num = 1
- factor = 1.0/inkex.unittouu('1'+self.options.unit)
+ simpletransform.applyTransformToPath(mat, p)
+ factor = factor/inkex.unittouu('1'+self.options.unit)
if self.options.type == "length":
slengths, stotal = csplength(p)
else:
- stotal,x0,y0 = csparea(p)
- stotal *= factor*self.options.scale
+ stotal = csparea(p)*factor*self.options.scale
# Format the length as string
lenstr = locale.format("%(len)25."+str(prec)+"f",{'len':round(stotal*factor*self.options.scale,prec)}).strip()
if self.options.type == "length":
- self.addTextOnPath(self.group,0, 0,lenstr+' '+self.options.unit, id, self.options.offset)
+ self.addTextOnPath(self.group, 0, 0, lenstr+' '+self.options.unit, id, 'start', '50%', self.options.offset)
else:
- self.addTextWithTspan(self.group,x0,y0,lenstr+' '+self.options.unit+'^2', id, self.options.offset)
+ self.addTextOnPath(self.group, 0, 0, lenstr+' '+self.options.unit+'^2', id, 'start', '0%', self.options.offset)
-
- def addTextOnPath(self,node,x,y,text, id,dy=0):
+ def addTextOnPath(self, node, x, y, text, id, anchor, startOffset, dy = 0):
new = inkex.etree.SubElement(node,inkex.addNS('textPath','svg'))
s = {'text-align': 'center', 'vertical-align': 'bottom',
- 'text-anchor': 'middle', 'font-size': str(self.options.fontsize),
+ 'text-anchor': anchor, 'font-size': str(self.options.fontsize),
'fill-opacity': '1.0', 'stroke': 'none',
'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
new.set('style', simplestyle.formatStyle(s))
new.set(inkex.addNS('href','xlink'), '#'+id)
- new.set('startOffset', "50%")
+ new.set('startOffset', startOffset)
new.set('dy', str(dy)) # dubious merit
#new.append(tp)
new.text = str(text)
@@ -179,20 +179,6 @@ class Length(inkex.Effect):
node.set('x', str(x))
node.set('y', str(y))
- def addTextWithTspan(self,node,x,y,text,id,dy=0):
- new = inkex.etree.SubElement(node,inkex.addNS('tspan','svg'), {inkex.addNS('role','sodipodi'): 'line'})
- s = {'text-align': 'center', 'vertical-align': 'bottom',
- 'text-anchor': 'middle', 'font-size': str(self.options.fontsize),
- 'fill-opacity': '1.0', 'stroke': 'none',
- 'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
- new.set('style', simplestyle.formatStyle(s))
- new.set(inkex.addNS('href','xlink'), '#'+id)
- new.set('startOffset', "50%")
- new.set('dy', str(dy))
- new.text = str(text)
- node.set('x', str(x))
- node.set('y', str(y))
-
if __name__ == '__main__':
e = Length()
e.affect()
diff --git a/share/extensions/simpletransform.py b/share/extensions/simpletransform.py
index 8f8a811dc..5bb30070d 100644
--- a/share/extensions/simpletransform.py
+++ b/share/extensions/simpletransform.py
@@ -89,6 +89,14 @@ def composeTransform(M1,M2):
v2 = M1[1][0]*M2[0][2] + M1[1][1]*M2[1][2] + M1[1][2]
return [[a11,a12,v1],[a21,a22,v2]]
+def composeParents(node, mat):
+ trans = node.get('transform')
+ if trans:
+ mat = composeTransform(parseTransform(trans), mat)
+ if node.getparent().tag == inkex.addNS('g','svg'):
+ mat = composeParents(node.getparent(), mat)
+ return mat
+
def applyTransformToNode(mat,node):
m=parseTransform(node.get("transform"))
newtransf=formatTransform(composeTransform(mat,m))