coordinate transforms in Inkscape extensions

Post questions on how to use or achieve an effect in Inkscape.
cl84
Posts: 4
Joined: Sun Dec 06, 2015 7:59 am

coordinate transforms in Inkscape extensions

Postby cl84 » Sun Dec 06, 2015 8:32 am

Hi,

my first post in this forum :-)

I'm currently working on an extension based on inkex and Python which exports the coordinates of all selected text objects.

The code works fine, however, I do not manage to obtain the coordinates as they are displayed on the screen. The coordinates I get using .get('x') and .get('y') appear to undergo some coordinate transform (translation) before the objects are drawn at the desired position. Obviously, the code of my extension has to do the same transform to get the right values. How do I achieve that?

I'm sure that the solution already exists, but I did not find anything useful.

My code is also in the attachment.

Thanks in advance
Christoph

Code: Select all

#!/usr/bin/env python

# Exports the texts in the document in format understood by
# LaTeX overpic package.
#
# (c) Christoph Lechner, Dec-5, 2015
# http://www.cl-projects.de/

import inkex
import sys
import simpletransform

class TemplateEffect(inkex.Effect):
   def __init__(self):
      # Call base class construtor.
      inkex.Effect.__init__(self)
       
   def effect(self):

      # obtain dimensions of SVG document
      svg = self.document.getroot()
      page_w = svg.get('width')
      page_h = svg.get('height')
       
           #Create the string variable which will hold the formatted data (note that '\n' defines a line break)
           outputString = ""
         
           #Iterate through all the selected objects in Inkscape
           for id, node in self.selected.iteritems():
         outputString += "***id="+id+"\n";

              #Check if the node is a text
                if node.tag == inkex.addNS('text','svg'):
            # collect all texts
            # loop over all elements (there should be a tspan)
            txt=""
            for stuff in node:
               txt += stuff.text + " "
               #for attr in dir(stuff):
               #   outputString += "obj.%s = %s\n" % (attr, getattr(stuff, attr)) 

            # write format needed by LaTeX overpic
            outputString += "\\put(%s,%s){%s}\n" % (node.get('x'),node.get('y'), txt)

      outputString += "width=%s, height=%s\n" % (page_w,page_h)

      sys.stderr.write(outputString)

# Create effect instance and apply it.
effect = TemplateEffect()
effect.affect()
Attachments
EO.zip
(1.39 KiB) Downloaded 149 times

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: coordinate transforms in Inkscape extensions

Postby Moini » Sun Dec 06, 2015 8:56 am

Hi Christoph,

there's a helper python file inside Inkscape's extension directory called 'simpletransform.py'.
I think it probably provides everything you'd need for this, and it takes transforms into account.
Just compare how other extensions use it to find out more (there aren't any comments in it, unfortunately). There's one function that computes the bounding box, which is maybe what you need.

Or you could just use the command line - there are arguments for querying an object's x and y coordinates, as well as width and height: https://inkscape.org/en/doc/inkscape-man.html
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

cl84
Posts: 4
Joined: Sun Dec 06, 2015 7:59 am

Re: coordinate transforms in Inkscape extensions

Postby cl84 » Sun Dec 06, 2015 8:14 pm

Hi Moini,

thanks a lot for your hint. That pointed in the right direction and so I solved the problem.
Now I can finish the actual coding ...

Cheers

cl84
Posts: 4
Joined: Sun Dec 06, 2015 7:59 am

Re: coordinate transforms in Inkscape extensions

Postby cl84 » Wed Dec 09, 2015 5:20 am

Hi,

I finished coding and testing the first version of my extension. Thanks for the support from this forum.

This Inkscape extension converts the selected text elements of your svg image into a format understood by the Latex package overpic. To be specific, the text is the text in Inkscape and the coordinates on the Inkscape page are the coordinates of the Latex put command.

The attached image (screenshot from the example document on the web site) illustrates what can be done with this workflow. All information needed to prepare the image comes from the Inkscape document.

It works for me on Windows and Linux (version 0.48r5). Nevertheless I consider it alpha software and release it without any warranty. So use it at your own risk. I set up a website for it: http://www.cl-projects.de/projects/misc/exportoverpic/

Cheers
Christoph

Inkscape svg file:
Image

After processing with Latex:
Image

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: coordinate transforms in Inkscape extensions

Postby Moini » Wed Dec 09, 2015 5:49 am

Looks nice!

I'm not a tex user myself, but this seems to be similar to the pdf+latex export for me.
(when you select export to pdf in 0.91, there's an option to export a text-less pdf and an additional file.)
For someone who doesn't really know tex, can you explain the difference?

(For compatibility with 0.91, look at getUnittouu here: https://github.com/Moini/inkscape-reals ... alscale.py - I 'stole' it from a jpg generation extension).
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: coordinate transforms in Inkscape extensions

Postby brynn » Wed Dec 09, 2015 6:15 am

Off topic:
You didn't happen to fix that JPG export extension? (assuming it's the same one I'm thinking of)

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: coordinate transforms in Inkscape extensions

Postby Moini » Wed Dec 09, 2015 6:58 am

Do you mean this one: https://github.com/giacmir/Inkscape-JPE ... -extension ? I didn't test, but it was only recently updated for 0.91, so I'd guess it works. Does it not?
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

cl84
Posts: 4
Joined: Sun Dec 06, 2015 7:59 am

Re: coordinate transforms in Inkscape extensions

Postby cl84 » Wed Dec 09, 2015 7:13 am

Hi,

I would say that pdf+latex export option is quite similar to what I did.

When I tried pdf+latex very long time ago, I had a hard time to get text alignment correct. The key difference is that I added a \makebox(0,0)[b]{ ... } command, which instructs Latex to center the text at the text position in Inkscape. If you center the latex expression also in Inkscape, you get identical adjustment.

"pdf+latex" is based on the 'picture' environment. The 'overlay' package that I use is based on the picture environment. To this end, both extensions write "\put" commands. So there is redundancy.

Of course another reason to create it was to learn how to create extensions. :-)

Best
Christoph

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: coordinate transforms in Inkscape extensions

Postby Moini » Wed Dec 09, 2015 8:13 am

Danke, Christoph ;) - so yours looks better for figures, when the length of the text before may be very different from the length after the tex-ification.

Learning how to write extensions is a worthy goal, too - I've only written my first one a few weeks ago, still learning, too ;)
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: coordinate transforms in Inkscape extensions

Postby brynn » Thu Dec 10, 2015 4:47 am

Moini wrote:Do you mean this one: https://github.com/giacmir/Inkscape-JPE ... -extension ? I didn't test, but it was only recently updated for 0.91, so I'd guess it works. Does it not?


There was a recent message on the dev list asking for JPG export. I referred them to that extension, but they reported that it doesn't work. First message dated 11-18-15. I'm sure they'd be happy to know, if it's been fixed! And if so, I'll change my notes, re the master list of extensions project.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: coordinate transforms in Inkscape extensions

Postby Moini » Thu Dec 10, 2015 9:40 am

Works as is (on Linux). Can't debug on Windows - the screenshots in the thread (http://sourceforge.net/p/inkscape/mailman/message/34629123/) suggest that it's a bug, probably with opening Inkscape or imagemagick on the Windows commandline.

I've played with it a little, though, and added a few error messages that catch wrong user entries. Won't help the user on the mailing list, but I'll ask the github author if he'd like to have my changes.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)


Return to “Help with using Inkscape”