Code

standardize indents on 4 spaces
[inkscape.git] / share / extensions / fretfind.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
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.
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.
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 '''
19 import sys
20 import inkex, simplestyle, simplepath
21 import ffscale, ffproc
23 def seg2path(seg):
24     return "M%s,%sL%s,%s" % (seg[0]['x'],seg[0]['y'],seg[1]['x'],seg[1]['y'])
26 class FretFind(inkex.Effect):
27     def __init__(self):
28         inkex.Effect.__init__(self)
29         self.doScala = False
30         self.doMultiScale = False
31         self.OptionParser.add_option("--scalafile",
32                         action="store", type="string", 
33                         dest="scalafile", default=None,
34                         help="")
35         self.OptionParser.add_option("--scalascale",
36                         action="store", type="string", 
37                         dest="scalascale", default=None,
38                         help="")
39         self.OptionParser.add_option("--etbase",
40                         action="store", type="float", 
41                         dest="etbase", default=2.0,
42                         help="")
43         self.OptionParser.add_option("--etroot",
44                         action="store", type="float", 
45                         dest="etroot", default=12.0,
46                         help="")
47         self.OptionParser.add_option("--tuning",
48                         action="store", type="string", 
49                         dest="tuning", default="0;0;0;0;0;0",
50                         help="")
51         self.OptionParser.add_option("--perpdist",
52                         action="store", type="float", 
53                         dest="perpdist", default=0.5,
54                         help="")
55         self.OptionParser.add_option("--offset",
56                         action="store", type="float", 
57                         dest="offset", default=0.0,
58                         help="")
59         self.OptionParser.add_option("--scalelength",
60                         action="store", type="float", 
61                         dest="scalelength", default=25.0,
62                         help="")
63         self.OptionParser.add_option("--firstscalelength",
64                         action="store", type="float", 
65                         dest="firstscalelength", default=None,
66                         help="")
67         self.OptionParser.add_option("--lastscalelength",
68                         action="store", type="float", 
69                         dest="lastscalelength", default=None,
70                         help="")
71         self.OptionParser.add_option("--nutwidth",
72                         action="store", type="float", 
73                         dest="nutwidth", default=1.5,
74                         help="")
75         self.OptionParser.add_option("--bridgewidth",
76                         action="store", type="float", 
77                         dest="bridgewidth", default=2.5,
78                         help="")
79         self.OptionParser.add_option("--frets",
80                         action="store", type="int", 
81                         dest="frets", default=24,
82                         help="")
83         self.OptionParser.add_option("--strings",
84                         action="store", type="int", 
85                         dest="strings", default=6,
86                         help="")
87         self.OptionParser.add_option("--fbedges",
88                         action="store", type="float", 
89                         dest="fbedges", default=0.0975,
90                         help="")
91         self.OptionParser.add_option("--pxperunit",
92                         action="store", type="float", 
93                         dest="pxperunit", default=90,
94                         help="")
95     def checkopts(self):
96         #scale type
97         if self.options.scalascale is not None:
98             self.doScala = True
99         if self.options.scalafile is not None:
100             if self.doScala:
101                 sys.stderr.write('Mutually exclusive options: if found scalafile will override scalascale.')
102             else:
103                 self.options.scalascale = ""
104             self.doScala = True
105             try:
106                 f = open(self.options.scalafile,'r')
107                 self.options.scalascale = f.read()
108             except: 
109                 sys.exit('Scala scale description file expected.')
110         #scale
111         if self.doScala:
112             self.scale = ffscale.ScalaScale(self.options.scalascale)
113         else:
114             self.scale = ffscale.ETScale(self.options.etroot,self.options.etbase)
115     
116         #string length
117         first = self.options.firstscalelength is not None
118         last = self.options.lastscalelength is not None
119         if first and last:
120             self.doMultiScale = True
121         elif first:
122             sys.stderr.write('Missing lastscalelength: overriding scalelength with firstscalelength.')
123             self.options.scalelength = self.options.firstscalelength
124         elif last:
125             sys.stderr.write('Missing firstscalelength: overriding scalelength with lastscalelength.')
126             self.options.scalelength = self.options.lastscalelength
127         
128         #tuning
129         self.tuning = [int(t.strip()) for t in self.options.tuning.split(";")]
130         self.tuning.extend([0 for i in range(self.options.strings - len(self.tuning))])
132     def effect(self):
133         self.checkopts()
135         o = self.options.fbedges
136         oNF,oBF,oNL,oBL = o,o,o,o
138         if self.doMultiScale:
139             strings, meta = ffproc.FindStringsMultiScale(self.options.strings,self.options.firstscalelength,
140                 self.options.lastscalelength,self.options.nutwidth,self.options.bridgewidth,
141                 self.options.perpdist,oNF,oBF,oNL,oBL)
142         else:
143             strings, meta = ffproc.FindStringsSingleScale(self.options.strings,self.options.scalelength,
144                 self.options.nutwidth,self.options.bridgewidth,
145                 oNF,oBF,oNL,oBL)
146         
147         frets = ffproc.FindFrets(strings, meta, self.scale, self.tuning, self.options.frets)
148         
149         edgepath = seg2path(meta[0]) + seg2path(meta[-1])
150         stringpath = "".join([seg2path(s) for s in strings])
151         fretpath = "".join(["".join([seg2path(f) for f in s]) for s in frets])
152     
153         group = self.document.createElement('svg:g')
154         group.setAttribute('transform',"scale(%s,%s)" % (self.options.pxperunit,self.options.pxperunit))
155         self.document.documentElement.appendChild(group)
156         
157         edge = self.document.createElement('svg:path')
158         s = {'stroke-linejoin': 'miter', 'stroke-width': '0.01px', 
159             'stroke-opacity': '1.0', 'fill-opacity': '1.0', 
160             'stroke': '#0000FF', 'stroke-linecap': 'butt', 
161             'fill': 'none'}
162         edge.setAttribute('style', simplestyle.formatStyle(s))
163         edge.setAttribute('d', edgepath)
165         string = self.document.createElement('svg:path')
166         s = {'stroke-linejoin': 'miter', 'stroke-width': '0.01px', 
167             'stroke-opacity': '1.0', 'fill-opacity': '1.0', 
168             'stroke': '#FF0000', 'stroke-linecap': 'butt', 
169             'fill': 'none'}
170         string.setAttribute('style', simplestyle.formatStyle(s))
171         string.setAttribute('d', stringpath)
173         fret = self.document.createElement('svg:path')
174         s = {'stroke-linejoin': 'miter', 'stroke-width': '0.01px', 
175             'stroke-opacity': '1.0', 'fill-opacity': '1.0', 
176             'stroke': '#000000', 'stroke-linecap': 'butt', 
177             'fill': 'none'}
178         fret.setAttribute('style', simplestyle.formatStyle(s))
179         fret.setAttribute('d', fretpath)
181         group.appendChild(edge)
182         group.appendChild(string)
183         group.appendChild(fret)
185 e = FretFind()
186 e.affect()