Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. # -*- coding: windows-1251 -*-
  2. #!/usr/bin/env python
  3.  
  4. import sys
  5. sys.path.append('/usr/share/inkscape/extensions')
  6. import inkex
  7. from simplestyle import *
  8.  
  9. class HelloWorldEffect(inkex.Effect):
  10. def __init__(self):
  11. """
  12. Constructor.
  13. Defines the "--what" option of a script.
  14. """
  15. # Call the base class constructor.
  16. inkex.Effect.__init__(self)
  17.  
  18. # Define string option "--what" with "-w" shortcut and default value "World".
  19. self.OptionParser.add_option('-w', '--what', action = 'store',
  20. type = 'string', dest = 'what', default = 'World',
  21. help = 'What would you like to greet?')
  22.  
  23. def effect(self):
  24. """
  25. Effect behaviour.
  26. Overrides base class' method and inserts "Hello World" text into SVG document.
  27. """
  28. # Get script's "--what" option value.
  29. what = self.options.what
  30.  
  31. # Get access to main SVG document element and get its dimensions.
  32. svg = self.document.getroot()
  33. # or alternatively
  34. # svg = self.document.xpath('//svg:svg',namespaces=inkex.NSS)[0]
  35.  
  36. # Again, there are two ways to get the attibutes:
  37. width = inkex.unittouu(svg.get('width'))
  38. height = inkex.unittouu(svg.attrib['height'])
  39.  
  40. # Create a new layer.
  41. layer = inkex.etree.SubElement(svg, 'g')
  42. layer.set(inkex.addNS('label', 'inkscape'), 'MyLayer')
  43. layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')
  44.  
  45. # Create text element
  46. text = inkex.etree.Element(inkex.addNS('text','svg'))
  47. text.text = text.text = unicode('Hello '+what+'!','windows-1251')
  48.  
  49. # Set text position to center of document.
  50. text.set('x', str(width / 2))
  51. text.set('y', str(height / 2))
  52.  
  53. # Center text horizontally with CSS style.
  54. style = {'text-align' : 'center', 'text-anchor': 'middle'}
  55. text.set('style', formatStyle(style))
  56.  
  57. # Connect elements together.
  58. layer.append(text)
  59.  
  60. # Create effect instance and apply it.
  61. effect = HelloWorldEffect()
  62. effect.affect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement