~inkscape.dev/inkscape/trunk

1 by mental
moving trunk for module inkscape
1
#!/usr/bin/env python 
2
'''
3
Copyright (C) 2005 Aaron Spike, aaron@ekips.org
4
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
'''
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
19
import inkex, simplestyle, simplepath, math
1 by mental
moving trunk for module inkscape
20
21
class Dots(inkex.Effect):
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
22
770 by acspike
standardize indents on 4 spaces
23
    def __init__(self):
24
        inkex.Effect.__init__(self)
25
        self.OptionParser.add_option("-d", "--dotsize",
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
26
                        action="store", type="string",
770 by acspike
standardize indents on 4 spaces
27
                        dest="dotsize", default="10px",
28
                        help="Size of the dots placed at path nodes")
29
        self.OptionParser.add_option("-f", "--fontsize",
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
30
                        action="store", type="string",
770 by acspike
standardize indents on 4 spaces
31
                        dest="fontsize", default="20",
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
32
                        help="Size of node label numbers")
9735 by JazzyNico
Extensions. Number dots improvements (Bug #615313).
33
        self.OptionParser.add_option("-s", "--start",
34
                        action="store", type="int",
35
                        dest="start", default="1",
36
                        help="First number in the sequence, assigned to the first node")
37
        self.OptionParser.add_option("-t", "--step",
38
                        action="store", type="int",
39
                        dest="step", default="1",
40
                        help="Numbering step between two nodes")
9736 by JazzyNico
Extensions:
41
        self.OptionParser.add_option("--tab",
42
                        action="store", type="string",
43
                        dest="tab",
44
                        help="The selected UI-tab when OK was pressed")
45
46
    def effect(self):
47
        selection = self.selected
48
        if (selection):
49
            for id, node in selection.iteritems():
50
                if node.tag == inkex.addNS('path','svg'):
51
                    self.addDot(node)
52
        else:
53
            inkex.errormsg("Please select an object.")
54
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
55
    def separateLastAndFirst(self, p):
56
        # Separate the last and first dot if they are togheter
57
        lastDot = -1
58
        if p[lastDot][1] == []: lastDot = -2
59
        if round(p[lastDot][1][-2]) == round(p[0][1][-2]) and \
60
                round(p[lastDot][1][-1]) == round(p[0][1][-1]):
61
                x1 = p[lastDot][1][-2]
62
                y1 = p[lastDot][1][-1]
63
                x2 = p[lastDot-1][1][-2]
64
                y2 = p[lastDot-1][1][-1]
65
                dx = abs( max(x1,x2) - min(x1,x2) )
66
                dy = abs( max(y1,y2) - min(y1,y2) )
67
                dist = math.sqrt( dx**2 + dy**2 )
68
                x = dx/dist
69
                y = dy/dist
70
                if x1 > x2: x *= -1
71
                if y1 > y2: y *= -1
12722 by Johan B. C. Engelen
fix unit handling of all python extensions. did not test with all extensions
72
                p[lastDot][1][-2] += x * self.unittouu(self.options.dotsize)
73
                p[lastDot][1][-1] += y * self.unittouu(self.options.dotsize)
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
74
9736 by JazzyNico
Extensions:
75
    def addDot(self, node):
76
        self.group = inkex.etree.SubElement( node.getparent(), inkex.addNS('g','svg') )
77
        self.dotGroup = inkex.etree.SubElement( self.group, inkex.addNS('g','svg') )
78
        self.numGroup = inkex.etree.SubElement( self.group, inkex.addNS('g','svg') )
79
        
80
        try:
81
            t = node.get('transform')
82
            self.group.set('transform', t)
83
        except:
84
            pass
85
86
        style = simplestyle.formatStyle({ 'stroke': 'none', 'fill': '#000' })
87
        a = []
88
        p = simplepath.parsePath(node.get('d'))
89
90
        self.separateLastAndFirst(p)
91
92
        num = self.options.start
93
        for cmd,params in p:
94
            if cmd != 'Z' and cmd != 'z':
95
                dot_att = {
96
                  'style': style,
12722 by Johan B. C. Engelen
fix unit handling of all python extensions. did not test with all extensions
97
                  'r':  str( self.unittouu(self.options.dotsize) / 2 ),
9736 by JazzyNico
Extensions:
98
                  'cx': str( params[-2] ),
99
                  'cy': str( params[-1] )
100
                }
101
                inkex.etree.SubElement(
102
                  self.dotGroup,
103
                  inkex.addNS('circle','svg'),
104
                  dot_att )
105
                self.addText(
106
                  self.numGroup,
12722 by Johan B. C. Engelen
fix unit handling of all python extensions. did not test with all extensions
107
                  params[-2] + ( self.unittouu(self.options.dotsize) / 2 ),
108
                  params[-1] - ( self.unittouu(self.options.dotsize) / 2 ),
9736 by JazzyNico
Extensions:
109
                  num )
110
                num += self.options.step
111
        node.getparent().remove( node )
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
112
770 by acspike
standardize indents on 4 spaces
113
    def addText(self,node,x,y,text):
3077 by acspike
begin converting python extensions to use lxml. this might take me a while and it will break unconverted extensions in the meanwhile.
114
                new = inkex.etree.SubElement(node,inkex.addNS('text','svg'))
13030 by apenner
Extensions. Scaling of stroke and some sizes into document units, Part 5 (Bug 1240455)
115
                s = {'font-size': self.unittouu(self.options.fontsize), 'fill-opacity': '1.0', 'stroke': 'none',
7237 by aurium
extensions/dots.py : Now dots are circles and not a multi-path to correct the "where is the dot" bug and make easy to change a dot position; The number is moved to be separated from the dot; The original path is now deleted, not only cleared; The last dot is not more over the first - now is better for kids (and some adults) to understand the start and end dots
116
                    'font-weight': 'normal', 'font-style': 'normal', 'fill': '#999'}
3077 by acspike
begin converting python extensions to use lxml. this might take me a while and it will break unconverted extensions in the meanwhile.
117
                new.set('style', simplestyle.formatStyle(s))
118
                new.set('x', str(x))
119
                new.set('y', str(y))
120
                new.text = str(text)
1 by mental
moving trunk for module inkscape
121
5742 by pjrm
share/extensions/*.py: Wrap ‘e = MyEffect(); e.affect()’ in ‘if __name__ == '__main__':’, to allow use of pychecker.
122
if __name__ == '__main__':
123
    e = Dots()
124
    e.affect()
5739 by pjrm
noop: Add vim modeline for all share/extensions/*.py files that use four-space indentation.
125
126
9900 by Chris Morgan
Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in all 1074 Vim modelines.
127
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99