4 # mf-to-table.py -- convert spacing info in MF logs .ly and .tex
6 # source file of the GNU LilyPond music typesetter
8 # (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
19 begin_autometric_re = regex.compile('@{')
20 end_autometric_re = regex.compile('@}')
21 autometric_re = regex.compile('@{\([^@]*\)@}')
25 """silly wrapper for Python file object."""
26 def __init__(self,nm, mode='r'):
28 self.file_ = open(nm, mode);
30 self.file_ = sys.stdout
32 self.file_ = sys.stdin
36 l= self.file_.readline();
49 class Feta_file(File):
50 """Read Feta metrics from a metafont log-file."""
51 def read_autometricline(self):
53 while end_autometric_re.search(line) == -1 and not self.eof():
54 suf = File.readline(self)
55 if begin_autometric_re.search(line) == -1:
57 line = line + regsub.sub('\n','', suf)
64 """return what is enclosed in one @{ @} pair"""
66 while autometric_re.match(line) == -1 and not self.eof():
67 line = self.read_autometricline()
70 return autometric_re.group(1);
71 def __init__(self, nm):
72 File.__init__(self, nm)
73 def do_file(infile_nm):
77 # FIXME: should parse output for {} to do indenting.
79 class Indentable_file(File):
80 """Output file with support for indentation"""
81 def __init__(self,nm, mode):
82 File.__init__(self,nm,mode)
83 self.current_indent_ = 0
84 self.delta_indent_ = 4
85 def writeline (self, str):
88 File.write(self, '\n')
89 File.write(self, ' '* self.current_indent_)
92 self.current_indent_ = self.delta_indent_ + self.current_indent_;
94 self.current_indent_ = self.current_indent_ - self.delta_indent_;
95 if self.current_indent_ < 0:
99 lines = split(str, '\n')
103 self.writeline (lines[-1])
105 class Ly_file(Indentable_file):
106 """extra provisions for mozarella quirks"""
107 def print_lit(self, str):
108 self.write('\"' + str + '\" ')
110 def print_dimen(self, str):
111 self.write( '%.2f' % atof(str) + '\\pt ');
113 def neg_print_dimen(self, str):
114 self.write( '%.2f' % -atof(str) + '\\pt ');
115 def def_symbol(self, lily_id, tex_id, dims):
116 self.print_lit(lily_id)
117 self.print_lit('\\' + tex_id)
119 self.neg_print_dimen(dims [0])
120 self.print_dimen(dims [1])
121 self.neg_print_dimen(dims [2])
122 self.print_dimen(dims [3])
127 """Read logs, destill info, and put into output files"""
128 def output_label(self, line):
131 tags = split(line, ':')
135 if tags[0] == 'font':
137 ly.write("% name=\\symboltables {\n")
138 self.texfile.write("% name\n")
139 elif label == "group":
142 ly.write(' = \\table {\n')
143 self.texfile.write("% " + name + "\n")
144 elif label == "puorg":
147 self.texfile.write("\n")
148 elif label == "tnof":
150 ly.write("% } % $name\n")
151 elif label == "char":
156 ly.def_symbol(id, texstr, tags[3:7])
158 self.texfile.write("\\fetdef\\" + texstr + '{' + code + '}\n')
160 raise 'unknown label: ' + label
163 def do_file(self,filenm):
164 self.lyfile.write('\n% input from ' + filenm + '\n')
165 self.texfile.write('\n% input from ' + filenm + '\n')
166 feta = Feta_file(filenm)
167 while not feta.eof():
168 line = feta.readline()
169 self.output_label(line)
172 def __init__(self, lyfile_nm, texfile_nm):
173 self.lyfile = Ly_file(lyfile_nm, 'w')
174 self.texfile = Indentable_file(texfile_nm, 'w')
176 headerstr = '% generated automatically by ' + program_id()
177 headerstr = headerstr + '\n% on ' + today_str()
178 headerstr = headerstr + '\n% Do not edit\n'
180 self.lyfile.write(headerstr)
181 self.texfile.write(headerstr)
191 return time.asctime(time.localtime(time.time()))
194 return 'mf-to-table.py version ' + version;
197 sys.stdout.write(program_id() + '\n')
200 sys.stdout.write("Usage: mf-to-table [options] LOGFILEs\n"
201 + "Generate mozarella metrics table from preparated feta log\n\n"
203 + " -h, --help print this help\n"
204 + " -l, --ly=FILE name output table\n"
205 + " -t, --tex=FILE name output tex chardefs\n")
210 (options, files) = getopt.getopt(
211 sys.argv[1:], 'hl:t:', ['ly=', 'tex=', 'debug', 'help'])
213 lyfile_nm = texfile_nm = '';
217 if o == '--ly' or o == '-l':
219 elif o == '--tex' or o == '-t':
221 elif o== '--help' or o == '-h':
228 log_reader = Log_reader(lyfile_nm, texfile_nm)
230 log_reader.do_file(filenm)