scaling text with extensions

Discuss SVG code, accessible via the XML Editor.
lazygeorge
Posts: 8
Joined: Wed Mar 02, 2011 8:00 am

scaling text with extensions

Postby lazygeorge » Fri Mar 04, 2011 6:09 am

Hi,

I have been learning how to script with python a few hours and looking at the svg codes for about the same time, so I admit I'm a bit clueless.

I want to create an extension that scales text depending on it's virtical position on the canvas using a simple algorithm

Here is my code so far:

Code: Select all

#!/usr/bin/python

import sys, copy

sys.path.append('C:\Program Files\Inkscape\share\extensions')
import inkex, simpletransform, simplepath, math
from math import cos, fabs

class GlobeStretcher(inkex.Effect):
   def __init__(self):
      inkex.Effect.__init__(self)
      
   def effect(self):

      if self.selected:
         for id, node in self.selected.iteritems():
            if node.tag == inkex.addNS('text','svg'):
               y = node.get('y')
               scale = 'scale(' + str((1/cos(fabs(y-90)))) + ', ' + str(1)  + ')'
               transform = simpletransform.parseTransform(scale)
               simpletransform.applyTransformToNode(transform, node)
               
effect = GlobeStretcher()
effect.affect()


At the moment it doesn't work, I get:

Traceback (most recent call last):

File "stretcher.py", line 24, in <module>

effect.affect()

File "C:\Program Files\Inkscape\share\extensions\inkex.py", line 215, in affect

self.effect()

File "stretcher.py", line 19, in effect

scale = 'scale(' + str((1/cos(fabs(y-90)))) + ', ' + str(1) + ')'

TypeError: unsupported operand type(s) for -: 'str' and 'int'


The intention is that the extension scales text horizontally depending on their position relative to 90mm from the bottom of the page. I suspect I have more than one thing wrong in the code.

Could someone please give me some idea about how to do this - I know I'm missing something in the code.



As an almost non-coder, I must say it is a little difficult for complete beginners to learn how to code extensions using the help pages. Please don't take it as a criticism, I know many people do great work on inkscape, I just wonder if there might be the argument for having a forum section for coding or extensions as I'm sure I'm not alone in attempting to write extensions with limited coding skills.

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

Re: scaling text with extensions

Postby brynn » Fri Mar 04, 2011 6:29 am

Hi lazygeorge,
Image
Welcome to InkscapeForum!

As an almost non-coder, I must say it is a little difficult for complete beginners to learn how to code extensions using the help pages. Please don't take it as a criticism, I know many people do great work on inkscape, I just wonder if there might be the argument for having a forum section for coding or extensions as I'm sure I'm not alone in attempting to write extensions with limited coding skills.

Well, it is true that we have been getting a lot more messages lately, from people trying to write extensions. So on that side, a new forum would be nice. On the other hand, I'm not sure if we have very many members who could answer the messages. It may well be that we have plenty....honestly I haven't taken notice of those who are answering these recent messages. Hopefully micro (board admin) will comment :?

So clearly I'm not one of those who could help with coding, lol! But hang in there, I think you will get some help soon :D

lazygeorge
Posts: 8
Joined: Wed Mar 02, 2011 8:00 am

Re: scaling text with extensions

Postby lazygeorge » Fri Mar 04, 2011 7:19 am

Hi Brynn,

Thanks for your quick reply.

Although I would appreciate some help from a coder, I aknowledge the frustration someone may feel with dealing with beginners like myself! On the other hand, making the writings of extensions more accessible would surely be good for all of us.

Even in the last few minutes I appreciate that my previous coding was wrong. Here is my more recent code:

Code: Select all

#!/usr/bin/python

import sys, copy

sys.path.append('C:\Program Files\Inkscape\share\extensions')
import inkex, simpletransform, math,

class GlobeStretcher(inkex.Effect):
   def __init__(self):
      inkex.Effect.__init__(self)
      
   def effect(self):
      
      for id in self.options.ids:
         node = self.selected[id]
         if node.tagName =='text':
            y = node.attributes.getNamedItem('y').value
         else:
            self.options.ids.remove(id)

         scale = 'scale(' + str((1/cos(fabs(y-90)))) + ', ' + str(1)  + ')'
         transform = simpletransform.parseTransform(scale)
         simpletransform.applyTransformToNode(transform, node)
               
effect = GlobeStretcher()
effect.affect()


I have pinched bits of code from other extensions and online but I think I am getting there slowly. The error I get now is

Traceback (most recent call last):

File "stretcher.py", line 26, in <module>

effect.affect()

File "C:\Program Files\Inkscape\share\extensions\inkex.py", line 215, in affect

self.effect()

File "stretcher.py", line 16, in effect

if node.tagName =='text':

AttributeError: 'lxml.etree._Element' object has no attribute 'tagName'


~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: scaling text with extensions

Postby ~suv » Fri Mar 04, 2011 9:52 am

lazygeorge wrote:if node.tagName =='text':

AttributeError: 'lxml.etree._Element' object has no attribute 'tagName'

Why did you change the test [1]?
from

Code: Select all

if node.tag == inkex.addNS('text','svg'):
to

Code: Select all

if node.tagName =='text':("text", "svg"):

The first version does look correct to me (caveat: untested and an uneducated guess, based on the explanations in Generating objects from extensions (Inkscape Wiki) and for example 'split.py' - I'm not a coder myself, and barely know the basics about Python)


[1] If you took it from the ''FillStroke' example extension in the Inkscape Wiki - that one fails with the same error if run in a current Inkscape version. Possibly it was coded for an older version…


Return to “SVG / XML Code”