3 # mf-to-table.py -- convert spacing info in MF logs . and .tex
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1997--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
16 def read_log_file (fn):
17 str = open (fn).read ()
18 str = re.sub ('\n', '', str)
19 str = re.sub ('[\t ]+', ' ', str)
23 def include_func (match, d = deps):
25 d.append (match.group (1))
28 def auto_func (match, a = autolines):
29 a.append (match.group (1))
32 str = re.sub ('\\(([/a-z.A-Z_0-9-]+\\.mf)', include_func, str)
33 str = re.sub ('@{(.*?)@}', auto_func, str)
35 return (autolines, deps)
44 def parse_logfile (fn):
45 (autolines, deps) = read_log_file (fn)
49 'filename' : os.path.splitext (os.path.basename (fn))[0]
54 tags = string.split (l, '@:')
55 if tags[0] == 'group':
57 elif tags[0] == 'puorg':
59 elif tags[0] == 'char':
62 name = re.sub ('-', 'M', name)
64 name = group + '.' + name
66 'description': tags[1],
68 'code': string.atoi (tags[2]),
69 'breapth': string.atof (tags[3]),
70 'width': string.atof (tags[4]),
71 'depth': string.atof (tags[5]),
72 'height': string.atof (tags[6]),
73 'wx': string.atof (tags[7]),
74 'wy': string.atof (tags[8]),
76 charmetrics.append (m)
77 elif tags[0] == 'font':
79 font_family = (tags[3])
80 # To omit 'GNU' (foundry) from font name proper:
84 tags.append ('Regular')
86 encoding = re.sub (' ','-', tags[5])
89 global_info['design_size'] = string.atof (tags[4])
90 global_info['FontName'] = string.join (name, '-')
91 global_info['FullName'] = string.join (name,' ')
92 global_info['FamilyName'] = string.join (name[1:-1],
95 global_info['Weight'] = tags[4]
97 global_info['Weight'] = tags[-1]
99 global_info['FontBBox'] = '0 0 1000 1000'
100 global_info['Ascender'] = '0'
101 global_info['Descender'] = '0'
102 global_info['EncodingScheme'] = encoding
104 elif tags[0] == 'parameter':
105 global_info[tags[1]] = tags[2];
107 return (global_info, charmetrics, deps)
111 def write_tex_defs (file, global_info, charmetrics):
113 for m in charmetrics:
115 texname = re.sub ('[_.]', 'X', m['name'])
116 def digit_to_letter (match):
117 return chr (ord (match.group(1)) - ord ('0') + ord ('A'))
118 texname = re.sub ('([0-9])', digit_to_letter, texname)
119 file.write (r'''\gdef\%s%s{\char%d}%%%s''' % \
120 (nm, texname, m['code'],'\n'))
121 file.write ('\\endinput\n')
124 def write_character_lisp_table (file, global_info, charmetrics):
126 def conv_char_metric (charmetric):
129 ((bbox . (%f %f %f %f))
132 (attachment . (%f . %f))))
133 """ %(charmetric['name'],
134 -charmetric['breapth'] * f,
135 -charmetric['depth'] * f,
136 charmetric['width'] * f,
137 charmetric['height'] * f,
138 global_info['filename'],
145 for c in charmetrics:
146 file.write (conv_char_metric (c))
149 def write_global_lisp_table (file, global_info):
152 keys = ['staffsize', 'stafflinethickness', 'staff_space',
153 'linethickness', 'black_notehead_width', 'ledgerlinethickness',
158 if global_info.has_key (k):
159 str = str + "(%s . %s)\n" % (k,global_info[k])
164 def write_ps_encoding (name, file, global_info, charmetrics):
165 encs = ['.notdef'] * 256
166 for m in charmetrics:
167 encs[m['code']] = m['name']
169 file.write ('/%s [\n' % name)
170 for m in range (0, 256):
171 file.write (' /%s %% %d\n' % (encs[m], m))
172 file.write ('] def\n')
175 def write_deps (file, deps, targets):
177 t = re.sub ( '^\\./', '', t)
178 file.write ('%s '% t)
181 file.write ('%s ' % d)
186 sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
188 Generate feta metrics table from preparated feta log.
191 -d, --dep=FILE print dependency info to FILE
192 -h, --help print this help
193 -l, --ly=FILE name output table
194 -o, --outdir=DIR prefix for dependency info
195 -p, --package=DIR specify package
196 -t, --tex=FILE name output tex chardefs
203 getopt.getopt (sys.argv[1:],
205 ['enc=', 'outdir=', 'dep=', 'lisp=',
207 'tex=', 'debug', 'help', 'package='])
220 if o == '--dep' or o == '-d':
222 elif o == '--outdir' or o == '-o':
224 elif o == '--tex' or o == '-t':
228 elif o == '--global-lisp':
232 elif o== '--help' or o == '-h':
240 base = re.sub ('.tex$', '', texfile_nm)
243 (g, m, deps) = parse_logfile (filenm)
245 write_tex_defs (open (texfile_nm, 'w'), g, m)
246 enc_name = 'FetaEncoding'
247 if re.search ('parmesan', filenm):
248 enc_name = 'ParmesanEncoding'
249 elif re.search ('feta-brace', filenm):
250 enc_name = 'FetaBraceEncoding'
251 elif re.search ('feta-alphabet', filenm):
252 enc_name = 'FetaAlphabetEncoding';
254 write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
255 write_character_lisp_table (open (char_lisp_nm, 'w'), g, m)
256 write_global_lisp_table (open (global_lisp_nm, 'w'), g)
258 write_deps (open (depfile_nm, 'wb'), deps,
259 [base + '.log', base + '.dvi', base + '.pfa',
261 base + '.pfb', texfile_nm])