Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: windows-1251 -*-
- #!/usr/bin/env python
- import sys
- sys.path.append('/usr/share/inkscape/extensions')
- import inkex
- from simplestyle import *
- class HelloWorldEffect(inkex.Effect):
- def __init__(self):
- """
- Constructor.
- Defines the "--what" option of a script.
- """
- # Call the base class constructor.
- inkex.Effect.__init__(self)
- # Define string option "--what" with "-w" shortcut and default value "World".
- self.OptionParser.add_option('-w', '--what', action = 'store',
- type = 'string', dest = 'what', default = 'World',
- help = 'What would you like to greet?')
- def effect(self):
- """
- Effect behaviour.
- Overrides base class' method and inserts "Hello World" text into SVG document.
- """
- # Get script's "--what" option value.
- what = self.options.what
- # Get access to main SVG document element and get its dimensions.
- svg = self.document.getroot()
- # or alternatively
- # svg = self.document.xpath('//svg:svg',namespaces=inkex.NSS)[0]
- # Again, there are two ways to get the attibutes:
- width = inkex.unittouu(svg.get('width'))
- height = inkex.unittouu(svg.attrib['height'])
- # Create a new layer.
- layer = inkex.etree.SubElement(svg, 'g')
- layer.set(inkex.addNS('label', 'inkscape'), 'MyLayer')
- layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')
- # Create text element
- text = inkex.etree.Element(inkex.addNS('text','svg'))
- text.text = text.text = unicode('Hello '+what+'!','windows-1251')
- # Set text position to center of document.
- text.set('x', str(width / 2))
- text.set('y', str(height / 2))
- # Center text horizontally with CSS style.
- style = {'text-align' : 'center', 'text-anchor': 'middle'}
- text.set('style', formatStyle(style))
- # Connect elements together.
- layer.append(text)
- # Create effect instance and apply it.
- effect = HelloWorldEffect()
- effect.affect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement