3 # mf-to-table.py -- convert spacing info in MF logs .
5 # This file is part of LilyPond, the GNU music typesetter.
7 # Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 # LilyPond is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # LilyPond is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
28 def read_log_file (fn):
29 str = open (fn).read ()
30 str = re.sub ('[\n\r]', '', str)
31 str = re.sub ('[\t ]+', ' ', str)
35 def include_func (match, d = deps):
36 d.append (match.group (1))
39 def auto_func (match, a = autolines):
40 a.append (match.group (1))
43 str = re.sub ('\\(([/a-z.A-Z_0-9-]+\\.mf)', include_func, str)
44 str = re.sub ('@{(.*?)@}', auto_func, str)
46 return (autolines, deps)
55 def parse_logfile (fn):
56 autolines, deps = read_log_file (fn)
60 'filename' : os.path.splitext (os.path.basename (fn))[0]
64 for i, l in enumerate(autolines):
66 if tags[0] == 'group':
68 elif tags[0] == 'puorg':
70 elif tags[0] == 'char':
74 print 'Error in mf-to-table while processing file', fn
75 print 'Index 9 >', len(tags)-1, 'on line', i
80 name = group + '.' + name
82 'description': tags[1],
84 'code': int (tags[2]),
85 'breapth': float (tags[3]),
86 'width': float (tags[4]),
87 'depth': float (tags[5]),
88 'height': float (tags[6]),
89 'wx': float (tags[7]),
90 'wy': float (tags[8]),
92 charmetrics.append (m)
93 elif tags[0] == 'font':
95 font_family = (tags[3])
96 # To omit 'GNU' (foundry) from font name proper:
100 tags.append ('Regular')
102 encoding = re.sub (' ','-', tags[5])
105 global_info['design_size'] = float (tags[4])
106 global_info['FontName'] = '-'.join (name)
107 global_info['FullName'] = ' '.join (name)
108 global_info['FamilyName'] = '-'.join (name[1:-1])
110 global_info['Weight'] = tags[4]
112 global_info['Weight'] = tags[-1]
114 global_info['FontBBox'] = '0 0 1000 1000'
115 global_info['Ascender'] = '0'
116 global_info['Descender'] = '0'
117 global_info['EncodingScheme'] = encoding
119 elif tags[0] == 'parameter':
120 global_info[tags[1]] = tags[2];
122 return (global_info, charmetrics, deps)
126 def character_lisp_table (global_info, charmetrics):
128 def conv_char_metric (charmetric):
131 ((bbox . (%f %f %f %f))
134 (attachment . (%f . %f))))
135 """ %(charmetric['name'],
136 -charmetric['breapth'] * f,
137 -charmetric['depth'] * f,
138 charmetric['width'] * f,
139 charmetric['height'] * f,
140 global_info['filename'],
148 for c in charmetrics:
149 s += conv_char_metric (c)
154 def global_lisp_table (global_info):
157 keys = ['staffsize', 'stafflinethickness', 'staff_space',
158 'linethickness', 'black_notehead_width', 'ledgerlinethickness',
163 if global_info.has_key (k):
164 str = str + "(%s . %s)\n" % (k,global_info[k])
169 def ps_encoding (name, global_info, charmetrics):
170 encs = ['.notdef'] * 256
171 for m in charmetrics:
172 encs[m['code']] = m['name']
175 s = ('/%s [\n' % name)
176 for m in range (0, 256):
177 s += (' /%s %% %d\n' % (encs[m], m))
181 def get_deps (deps, targets):
184 t = re.sub ( '^\\./', '', t)
193 sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
195 Generate feta metrics table from preparated feta log.
198 -d, --dep=FILE print dependency info to FILE
199 -h, --help print this help
200 -l, --ly=FILE name output table
201 -o, --outdir=DIR prefix for dependency info
202 -p, --package=DIR specify package
209 getopt.getopt (sys.argv[1:],
211 ['enc=', 'outdir=', 'dep=', 'lisp=',
213 'debug', 'help', 'package='])
225 if o == '--dep' or o == '-d':
227 elif o == '--outdir' or o == '-o':
231 elif o == '--global-lisp':
235 elif o== '--help' or o == '-h':
243 base = os.path.splitext (lyfile_nm)[0]
246 (g, m, deps) = parse_logfile (filenm)
248 enc_name = 'FetaEncoding'
249 if re.search ('parmesan', filenm):
250 enc_name = 'ParmesanEncoding'
251 elif re.search ('parmesan-noteheads', filenm):
252 enc_name = 'ParmesanNoteheadsEncoding'
253 elif re.search ('feta-noteheads', filenm):
254 enc_name = 'FetaNoteheadsEncoding'
255 elif re.search ('feta-flags', filenm):
256 enc_name = 'FetaFlagsEncoding'
257 elif re.search ('feta-brace', filenm):
258 enc_name = 'FetaBraceEncoding'
259 elif re.search ('feta-alphabet', filenm):
260 enc_name = 'FetaAlphabetEncoding';
262 open (enc_nm, 'w').write (ps_encoding (enc_name, g, m))
263 open (char_lisp_nm, 'w').write (character_lisp_table (g, m))
264 open (global_lisp_nm, 'w').write (global_lisp_table (g))
266 open (depfile_nm, 'wb').write (get_deps (deps,
267 [base + '.log', base + '.dvi', base + '.pfa',