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>
22 lilypath = os.environ['LILYPOND_SOURCEDIR'] + '/'
25 lilypath = os.environ['top_srcdir'] + '/'
27 print 'Please set LILYPOND_SOURCEDIR to the toplevel source, eg LILYPOND_SOURCEDIR=/home/foobar/lilypond-1.2.3/'
31 lilypath = lilypath + '/bin/'
32 sys.path.append(lilypath)
36 begin_autometric_re = regex.compile('@{')
37 end_autometric_re = regex.compile('@}')
38 autometric_re = regex.compile('@{\(.*\)@}')
41 class Feta_file(File):
42 """Read Feta metrics from a metafont log-file."""
43 def read_autometricline(self):
45 while end_autometric_re.search(line) == -1 and not self.eof():
46 suf = File.readline(self)
47 if begin_autometric_re.search(line) == -1:
49 line = line + regsub.sub('\n','', suf)
56 """return what is enclosed in one @{ @} pair"""
58 while autometric_re.search(line) == -1 and not self.eof():
59 line = self.read_autometricline()
64 return autometric_re.group(1);
65 def __init__(self, nm):
66 File.__init__(self, nm)
67 def do_file(infile_nm):
71 # FIXME: should parse output for {} to do indenting.
73 class Indentable_file(File):
74 """Output file with support for indentation"""
75 def __init__(self,nm, mode):
76 File.__init__(self,nm,mode)
77 self.current_indent_ = 0
78 self.delta_indent_ = 4
79 def writeline (self, str):
82 File.write(self, '\n')
83 File.write(self, ' '* self.current_indent_)
86 self.current_indent_ = self.delta_indent_ + self.current_indent_;
88 self.current_indent_ = self.current_indent_ - self.delta_indent_;
89 if self.current_indent_ < 0:
93 lines = split(str, '\n')
97 self.writeline (lines[-1])
99 class Ly_file(Indentable_file):
100 """extra provisions for mozarella quirks"""
101 def print_lit(self, str):
102 self.write('\"%s\"\t' % str)
104 def print_f_dimen(self, f):
107 # try to mask rounding errors
108 if (dimstr == '-0.00'):
110 self.write( dimstr +'\\pt\t');
112 def print_dimen(self, str):
113 self.print_f_dimen(atof(str))
115 def neg_print_dimen(self, str):
116 self.print_f_dimen(-atof(str));
118 def def_symbol(self, lily_id, tex_id, dims):
119 self.print_lit(lily_id)
120 self.print_lit('\\\\' + tex_id)
122 self.neg_print_dimen(dims [0])
123 self.print_dimen(dims [1])
124 self.neg_print_dimen(dims [2])
125 self.print_dimen(dims [3])
130 """Read logs, destill info, and put into output files"""
131 def output_label(self, line):
135 tags = split(line, '@:')
139 if tags[0] == 'font':
141 ly.write("% name=\\symboltables {\n")
142 self.texfile.write("% name\n")
143 elif label == "group":
146 ly.write(' = \\table {\n')
147 self.texfile.write("% " + name + "\n")
148 elif label == "puorg":
151 self.texfile.write("\n")
152 elif label == "tnof":
154 ly.write("% } % $name\n")
155 elif label == "char":
160 ly.def_symbol(id, texstr, tags[3:7])
162 self.texfile.write("\\fetdef\\%s{%s}\n" % (texstr, code))
164 raise 'unknown label: ' + label
167 def do_file(self,filenm):
168 self.lyfile.write('\n% input from ' + filenm + '\n')
169 self.texfile.write('\n% input from ' + filenm + '\n')
170 feta = Feta_file(filenm)
171 while not feta.eof():
172 line = feta.readline()
173 self.output_label(line)
176 def __init__(self, lyfile_nm, texfile_nm):
177 self.lyfile = Ly_file(lyfile_nm, 'w')
178 self.texfile = Indentable_file(texfile_nm, 'w')
181 headerstr = '%% Creator: %s\n%% Automatically generated on\n%% Do not edit' % \
184 self.lyfile.write(headerstr)
185 self.texfile.write(headerstr)
195 return time.asctime(time.localtime(time.time()))
198 return 'mf-to-table.py version ' + version;
201 sys.stdout.write(program_id() + '\n')
204 sys.stdout.write("Usage: mf-to-table [options] LOGFILEs\n"
205 + "Generate mozarella metrics table from preparated feta log\n\n"
207 + " -h, --help print this help\n"
208 + " -l, --ly=FILE name output table\n"
209 + " -t, --tex=FILE name output tex chardefs\n")
214 (options, files) = getopt.getopt(
215 sys.argv[1:], 'hl:t:', ['ly=', 'tex=', 'debug', 'help'])
217 lyfile_nm = texfile_nm = '';
221 if o == '--ly' or o == '-l':
223 elif o == '--tex' or o == '-t':
225 elif o== '--help' or o == '-h':
232 log_reader = Log_reader(lyfile_nm, texfile_nm)
234 log_reader.do_file(filenm)