Convert attribute 'd' and 'transform' to a transformed 'd'

Discussion about writing code for Inkscape.
knobba
Posts: 27
Joined: Tue Mar 02, 2010 4:35 am
Contact:

Convert attribute 'd' and 'transform' to a transformed 'd'

Postby knobba » Wed Jan 21, 2015 9:40 am

I have path with a transform attribute which I need to convert to the same transformed path, but having the transform incorporated in the path string. I guess somebody has written that code already, but I have now got lost in cyberspace and in simplePath.py, cubicSuperPath.py, simpleTransform.py and cannot figure our how to write this code:

something like

newD = convert(originalD, transform)


fx

originalD = 'M 10 10 ....'
transform = 'matrix( 10 0 0 10 0 0)'
newD = 'M 100 100 ... '

tylerdurden
Posts: 2344
Joined: Sun Apr 14, 2013 12:04 pm
Location: Michigan, USA

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby tylerdurden » Wed Jan 21, 2015 10:05 am

One quick cheat, is to set the Inkscape Transform preferences to "optimised" and move the path one arrow key right and back one arrow key. That should re-write the path and remove the transform attribute.
Have a nice day.

I'm using Inkscape 0.92.2 (5c3e80d, 2017-08-06), 64 bit win8.1

The Inkscape manual has lots of helpful info! http://tavmjong.free.fr/INKSCAPE/MANUAL/html/

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby hulf2012 » Wed Jan 21, 2015 1:02 pm

Hello:
Wish you could share your code

On the other hande, here is some information about how transformations work:
https://developer.mozilla.org/en-US/doc ... /transform

Some information about the libraries used by the extensions in Inkscape
http://wiki.inkscape.org/wiki/index.php ... ansform.py
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

knobba
Posts: 27
Joined: Tue Mar 02, 2010 4:35 am
Contact:

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby knobba » Wed Jan 21, 2015 5:51 pm

Thanks for the tips. The cheat to drop the transform is good to learn for other purposes, but will not work in my case as I will not change the original paths that I can see and select. I want to change the copy that I paste into glyphs, which I cannot select in that manner.

Thank you also for the links, hulf2012. The links are good and I have had good use of them to create the transformations

The code mainly copies paths and paste them into glyphs after a transform. There are more transformation than I show here and I have checked that the transformation workes as expected if pasted into a path, but for the glyphs the svg font seemes to ignore the trasformation matrix and thus I would like to put the transformation incorporated into the path.
Here is bits and parts of the code. I appologize for novise ineffective and unpretty code. (I guess fx the cc into the for loop could be done better, but at least it works :D ) :

Code: Select all

...
      rescale=min([abs(rescaleAsc),abs(rescaleDesc)])
      
      #get paths
      pathList=[]
      transformList=[]
      cc=0
      for id, node in self.selected.iteritems():
         #read all selected paths
         if node.tag == inkex.addNS('path','svg'):
            tempNode=copy.deepcopy(node)
            #move path to x=0
            transformation = 'translate('+str(-minxList[cc])+' 0)'
            transform = simpletransform.parseTransform(transformation)
            simpletransform.applyTransformToNode(transform, tempNode)
            transformation = 'scale('+str(rescale)+' -'+str(rescale)+')'
            transform = simpletransform.parseTransform(transformation)
            simpletransform.applyTransformToNode(transform, tempNode)
            d = tempNode.get('d')
            d = simplepath.formatPath( simplepath.parsePath(d) )
            # close curves from the close_curves.py
            d = re.sub(r'(?i)(m[^mz]+)',r'\1 Z ',d)
            d = re.sub(r'(?i)\s*z\s*z\s*',r' Z ',d)
            
            pathList.append(d)
            transformList.append(tempNode.get('transform'))
            cc=cc+1

      #fill glyphs with the paths
                cc=0
      for node in self.document.xpath('//svg:glyph',namespaces=inkex.NSS):
         
         node.set('d',pathList[cc])
         node.set('unicode',glyphList[cc])
         node.set('transform',transformList[cc])
         cc=cc+1


So I'd like to drop the node.set('transform',transformList[cc]) and change the pathList[] to include the transformation in the path.

knobba
Posts: 27
Joined: Tue Mar 02, 2010 4:35 am
Contact:

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby knobba » Thu Jan 22, 2015 7:57 am

I tried once more with fresh courage and fresh ideas to search the cupicsuperpath/simplepath/simpletransform.py and I found something that sounded helpful and tried it and it did work:

simpletransform.fuseTransform(tempNode)

I takes the d and the transform attributes and calculates a new path d.

Hurray! :)

But quite strange that the glyphs do not take the transform into account. I tested with different approaches and it really ignores the transform attribute.

Lazur
Posts: 4717
Joined: Tue Jun 14, 2016 10:38 am

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby Lazur » Thu Jan 22, 2015 12:12 pm

tylerdurden wrote:One quick cheat, is to set the Inkscape Transform preferences to "optimised" and move the path one arrow key right and back one arrow key. That should re-write the path and remove the transform attribute.



In that vein, to "transorm" all objects a simple page resize could do the trick to all paths.

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby hulf2012 » Thu Jan 22, 2015 10:59 pm

Hello Knooba,

Good to find your answer about transformation.

It's not clear what is hapening with those glyphs you mention. INMHO, posting an example file always help to clarify the problem.

But anyway, it's a valuable information what you have found. I see that also you have a subroutine to close paths, in your code.

Lazur URH: Some times I've tried that trick, but it seems to me that for some special shapes it may not work, like an ellipse which also has been rotated and scaled.
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

Lazur
Posts: 4717
Joined: Tue Jun 14, 2016 10:38 am

Re: Convert attribute 'd' and 'transform' to a transformed '

Postby Lazur » Fri Jan 23, 2015 3:45 am

I remembered there was a problem with shapes like rectangles too, so I wasn't brave enough to write all objects, but paths.
Confusing enough ellipses are labeled as paths too -in 0.48 at least.


Return to “Programming”