]> git.donarmstrong.com Git - lilypond.git/blob - scripts/musa.py
partial: 1.3.25.jcn
[lilypond.git] / scripts / musa.py
1 #!/usr/bin/python
2 #!@PYTHON@
3
4 program_name = 'musa.py'
5 version = '@TOPLEVEL_VERSION@'
6 if version == '@' + 'TOPLEVEL_VERSION' + '@':
7         version = '(unknown version)'           # uGUHGUHGHGUGH
8
9 import __main__
10 import getopt
11 import sys
12 import re
13 import string
14 import os
15
16 #names = ["One", "Two", "Three"]
17 DIGITS='0123456789'
18
19 def dump_score (outf):
20         outf.write (r"""\score{
21         \notes <
22 """)
23
24 def set_default_len_from_time_sig (s):
25         m =  re.search ('([0-9]+)/([0-9]+)', s)
26         if m:
27                 n = string.atoi (m.group (1))
28                 d = string.atoi (m.group (2))
29                 if (n * 1.0 )/(d * 1.0) <  0.75:
30                         __main__.default_len =  16
31                 else:
32                         __main__.default_len = 8
33
34 def gulp_file (f):
35         try:
36                 i = open (f)
37                 i.seek (0, 2)
38                 n = i.tell ()
39                 i.seek (0,0)
40         except:
41                 sys.stderr.write ("can't open file: %s\n" % f)
42                 return ''
43         s = i.read (n)
44         if len (s) <= 0:
45                 sys.stderr.write ("gulped emty file: %s\n" % f)
46         i.close ()
47         return s
48
49 def identify():
50         sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
51
52 def help ():
53         print r"""
54 Musa.
55
56 Usage: musa [OPTION]...
57
58 Options:
59   -h, --help          this help
60   -o, --output=FILE   set output filename to FILE
61   -v, --version       version information
62 """
63
64 def print_version ():
65         print r"""musa (GNU lilypond) %s""" % version
66
67
68 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
69 out_filename = ''
70
71 for opt in options:
72         o = opt[0]
73         a = opt[1]
74         if o== '--help' or o == '-h':
75                 help ()
76                 sys.exit (0)
77         if o == '--version' or o == '-v':
78                 print_version ()
79                 sys.exit(0)
80                 
81         if o == '--output' or o == '-o':
82                 out_filename = a
83         else:
84                 print o
85                 raise getopt.error
86
87 identify ()
88
89 #header['tagline'] = 'Lily was here %s -- automatically converted from ABC' % version
90
91 #if not out_filename:
92         #out_filename = os.path.basename (os.path.splitext (f)[0]) + ".ly"
93 #sys.stderr.write ('Ly output to: %s...' % out_filename)
94 #outf = open (out_filename, 'w')
95 #sys.stderr.write ('\n')
96
97 outf = sys.stdout
98
99 # display width
100 width = 65
101
102 # font database
103 fonts = {}
104
105 # cursor
106 x = 0
107 y = 0
108
109 # current font
110 font = ""
111
112 def print_font (name):
113         global fonts
114         font = fonts[name]
115         for k in font.keys ():
116                 c = font[k]
117                 print ("Character: %s" % k)
118                 for i in range (len (c)):
119                         print (c[i])
120
121 def put_char (x, y, c):
122         global line, width
123         height = len (line[0])
124         y = -y
125         if x >= 0 and x < width - 2 and y >= 0 and y < height:
126                 try:
127                         line[y] = line[y][:x] + c + line[y][x+1:]
128                 except:
129                         print ("%d, %d: %c" % (x, y, c))
130         else:
131                 print ("%d, %d: %c" % (x, y, c))
132
133 def put_string (x, y, s):
134         global line, width
135         height = len (line[0])
136         y = -y
137         if x >= 0 and x < width and y >= 0 and y < height:
138                 try:
139                         line[y] = line[y][:x] + s + line[y][x+len (s):]
140                 except:
141                         print ("%d, %d: %s" % (x, y, s))
142         else:
143                 print ("%d, %d: %s" % (x, y, s))
144
145 def header (creator, generate):
146         print (creator, generate)
147
148 def header_end ():
149         return
150
151 def load_font (name, mag):
152         global fonts
153         font_str = gulp_file (name + ".af");
154         i = 0
155         font = {}
156         for c in string.split (font_str, '\f')[1:]:
157                 id = 0
158                 code = 0
159                 char = []
160                 for line in string.split (c, '\n')[:-1]:
161                         if not id:
162                                 id = line
163                                 #code = string.atoi (id[string.index (id, 
164                                 #       'C')+2:string.index (id, ';')])
165                                 code = id[string.index (id, 
166                                         'C')+2:string.index (id, ';')]
167                                 code = string.strip (code)
168                                 bbox = string.split (string.strip (id[string.rindex (id, 'B')+1: string.rindex (id, ';')]), ' ')
169                                 char.append (bbox)
170                         else:
171                                 char.append (line)
172                 font[code] = char
173         fonts[name] = font
174         #print_font (name)
175
176 def start_line (height):
177         global line
178         line = []
179         # urg
180         for i in range (height+2):
181                 line.append (" " * width)
182
183 def move_to (new_x, new_y):
184         global x, y
185         x = new_x
186         y = new_y
187
188 def move_relative (dx, dy):
189         global x, y
190         x = x + dx
191         y = y + dy
192
193 def hline (length):
194         global x, y
195         for i in range (length):
196                 put_char (x+i, y, '-')
197         return
198
199 def select_font (name):
200         global font
201         font = name
202
203 def char (i):
204         global x, y, width, fonts, font
205         height = len (line[0])
206         #y = -y
207         #if x >= 0 and x < width and y >= 0 and y < height:
208         c = fonts[font][`i`]
209         bbox = c[0]
210         #print ("Bbox: %s " % `bbox`)
211         c = c[1:]
212         for i in range (len (c)):
213                 put_string (x-string.atoi (bbox[1]), y-i+ string.atoi (bbox[3]), c[i])
214
215 def text (s):
216         global x, y
217         put_string (x, y, s)
218
219 def vline (length):
220         global x, y
221         for i in range (length):
222                 put_char (x, y+i, '|')
223
224 def stop_line ():
225         global line
226         for i in range (len (line)):
227                 print (line[i])
228         print ("=== ===")
229
230 def end_output ():
231         return
232