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):
24 d.append (match.group (1))
27 def auto_func (match, a = autolines):
28 a.append (match.group (1))
31 str = re.sub ('\(([a-zA-Z_0-9-]+\.mf)', include_func, str)
32 str = re.sub ('@{(.*?)@}', auto_func, str)
34 return (autolines, deps)
43 def parse_logfile (fn):
44 (autolines, deps) = read_log_file (fn)
48 'filename' : os.path.splitext (os.path.basename (fn))[0]
53 tags = string.split (l, '@:')
54 if tags[0] == 'group':
56 elif tags[0] == 'puorg':
58 elif tags[0] == 'char':
61 name = re.sub ('-', 'M', name)
63 name = group + '.' + name
65 'description': tags[1],
67 'code': string.atoi (tags[2]),
68 'breapth': string.atof (tags[3]),
69 'width': string.atof (tags[4]),
70 'depth': string.atof (tags[5]),
71 'height': string.atof (tags[6]),
72 'wx': string.atof (tags[7]),
73 'wy': string.atof (tags[8]),
75 charmetrics.append (m)
76 elif tags[0] == 'font':
78 font_family = (tags[3])
79 # To omit 'GNU' (foundry) from font name proper:
83 tags.append ('Regular')
85 encoding = re.sub (' ','-', tags[5])
88 global_info['design_size'] = string.atof (tags[4])
89 global_info['FontName'] = string.join (name, '-')
90 global_info['FullName'] = string.join (name,' ')
91 global_info['FamilyName'] = string.join (name[1:-1],
94 global_info['Weight'] = tags[4]
96 global_info['Weight'] = tags[-1]
98 global_info['FontBBox'] = '0 0 1000 1000'
99 global_info['Ascender'] = '0'
100 global_info['Descender'] = '0'
101 global_info['EncodingScheme'] = encoding
103 elif tags[0] == 'parameter':
104 global_info[tags[1]] = tags[2];
106 return (global_info, charmetrics, deps)
110 def write_tex_defs (file, global_info, charmetrics):
112 for m in charmetrics:
114 texname = re.sub ('[_.]', 'X', m['name'])
115 def digit_to_letter (match):
116 return chr (ord (match.group(1)) - ord ('0') + ord ('A'))
117 texname = re.sub ('([0-9])', digit_to_letter, texname)
118 file.write (r'''\gdef\%s%s{\char%d}%%%s''' % \
119 (nm, texname, m['code'],'\n'))
120 file.write ('\\endinput\n')
123 def write_character_lisp_table (file, global_info, charmetrics):
125 def conv_char_metric (charmetric):
128 ((bbox . (%f %f %f %f))
131 (attachment . (%f . %f))))
132 """ %(charmetric['name'],
133 -charmetric['breapth'] * f,
134 -charmetric['depth'] * f,
135 charmetric['width'] * f,
136 charmetric['height'] * f,
137 global_info['filename'],
144 for c in charmetrics:
145 file.write (conv_char_metric (c))
148 def write_global_lisp_table (file, global_info):
151 keys = ['staffsize', 'stafflinethickness', 'staff_space',
152 'linethickness', 'black_notehead_width', 'ledgerlinethickness',
157 if global_info.has_key (k):
158 str = str + "(%s . %s)\n" % (k,global_info[k])
163 def write_ps_encoding (name, file, global_info, charmetrics):
164 encs = ['.notdef'] * 256
165 for m in charmetrics:
166 encs[m['code']] = m['name']
168 file.write ('/%s [\n' % name)
169 for m in range (0, 256):
170 file.write (' /%s %% %d\n' % (encs[m], m))
171 file.write ('] def\n')
174 def write_deps (file, deps, targets):
176 t = re.sub ( '^\\./', '', t)
177 file.write ('%s '% t)
180 file.write ('%s ' % d)
185 sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
187 Generate feta metrics table from preparated feta log.
190 -d, --dep=FILE print dependency info to FILE
191 -h, --help print this help
192 -l, --ly=FILE name output table
193 -o, --outdir=DIR prefix for dependency info
194 -p, --package=DIR specify package
195 -t, --tex=FILE name output tex chardefs
202 getopt.getopt (sys.argv[1:],
204 ['enc=', 'outdir=', 'dep=', 'lisp=',
206 'tex=', 'debug', 'help', 'package='])
219 if o == '--dep' or o == '-d':
221 elif o == '--outdir' or o == '-o':
223 elif o == '--tex' or o == '-t':
227 elif o == '--global-lisp':
231 elif o== '--help' or o == '-h':
239 base = re.sub ('.tex$', '', texfile_nm)
242 (g, m, deps) = parse_logfile (filenm)
244 write_tex_defs (open (texfile_nm, 'w'), g, m)
245 enc_name = 'FetaEncoding'
246 if re.search ('parmesan', filenm):
247 enc_name = 'ParmesanEncoding'
248 elif re.search ('feta-brace', filenm):
249 enc_name = 'FetaBraceEncoding'
250 elif re.search ('feta-alphabet', filenm):
251 enc_name = 'FetaAlphabetEncoding';
253 write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
254 write_character_lisp_table (open (char_lisp_nm, 'w'), g, m)
255 write_global_lisp_table (open (global_lisp_nm, 'w'), g)
257 write_deps (open (depfile_nm, 'wb'), deps,
258 [base + '.log', base + '.dvi', base + '.pfa',
260 base + '.pfb', texfile_nm])