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.search(line) == -1 and not self.eof():
67 line = self.read_autometricline()
72 return autometric_re.group(1);
73 def __init__(self, nm):
74 File.__init__(self, nm)
75 def do_file(infile_nm):
79 # FIXME: should parse output for {} to do indenting.
81 class Indentable_file(File):
82 """Output file with support for indentation"""
83 def __init__(self,nm, mode):
84 File.__init__(self,nm,mode)
85 self.current_indent_ = 0
86 self.delta_indent_ = 4
87 def writeline (self, str):
90 File.write(self, '\n')
91 File.write(self, ' '* self.current_indent_)
94 self.current_indent_ = self.delta_indent_ + self.current_indent_;
96 self.current_indent_ = self.current_indent_ - self.delta_indent_;
97 if self.current_indent_ < 0:
100 def write(self, str):
101 lines = split(str, '\n')
105 self.writeline (lines[-1])
107 class Ly_file(Indentable_file):
108 """extra provisions for mozarella quirks"""
109 def print_lit(self, str):
110 self.write('\"%s\"\t' % str)
112 def print_f_dimen(self, f):
113 self.write( '%.2f\\pt\t' % f);
115 def print_dimen(self, str):
116 self.print_f_dimen(atof(str))
118 def neg_print_dimen(self, str):
119 self.print_f_dimen(-atof(str));
121 def def_symbol(self, lily_id, tex_id, dims):
122 self.print_lit(lily_id)
123 self.print_lit('\\\\' + tex_id)
125 self.neg_print_dimen(dims [0])
126 self.print_dimen(dims [1])
127 self.neg_print_dimen(dims [2])
128 self.print_dimen(dims [3])
133 """Read logs, destill info, and put into output files"""
134 def output_label(self, line):
138 tags = split(line, '@:')
142 if tags[0] == 'font':
144 ly.write("% name=\\symboltables {\n")
145 self.texfile.write("% name\n")
146 elif label == "group":
149 ly.write(' = \\table {\n')
150 self.texfile.write("% " + name + "\n")
151 elif label == "puorg":
154 self.texfile.write("\n")
155 elif label == "tnof":
157 ly.write("% } % $name\n")
158 elif label == "char":
163 ly.def_symbol(id, texstr, tags[3:7])
165 self.texfile.write("\\fetdef\\%s{%s}\n" % (texstr, code))
167 raise 'unknown label: ' + label
170 def do_file(self,filenm):
171 self.lyfile.write('\n% input from ' + filenm + '\n')
172 self.texfile.write('\n% input from ' + filenm + '\n')
173 feta = Feta_file(filenm)
174 while not feta.eof():
175 line = feta.readline()
176 self.output_label(line)
179 def __init__(self, lyfile_nm, texfile_nm):
180 self.lyfile = Ly_file(lyfile_nm, 'w')
181 self.texfile = Indentable_file(texfile_nm, 'w')
184 headerstr = '%% Creator: %s\n%% Automatically generated on %s\n%% Do not edit' % \
185 (program_id(), today_str())
187 self.lyfile.write(headerstr)
188 self.texfile.write(headerstr)
198 return time.asctime(time.localtime(time.time()))
201 return 'mf-to-table.py version ' + version;
204 sys.stdout.write(program_id() + '\n')
207 sys.stdout.write("Usage: mf-to-table [options] LOGFILEs\n"
208 + "Generate mozarella metrics table from preparated feta log\n\n"
210 + " -h, --help print this help\n"
211 + " -l, --ly=FILE name output table\n"
212 + " -t, --tex=FILE name output tex chardefs\n")
217 (options, files) = getopt.getopt(
218 sys.argv[1:], 'hl:t:', ['ly=', 'tex=', 'debug', 'help'])
220 lyfile_nm = texfile_nm = '';
224 if o == '--ly' or o == '-l':
226 elif o == '--tex' or o == '-t':
228 elif o== '--help' or o == '-h':
235 log_reader = Log_reader(lyfile_nm, texfile_nm)
237 log_reader.do_file(filenm)