3 # mf-to-table.py -- convert spacing info in MF logs .afm and .tex
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
17 postfixes = ['log', 'dvi', '2602gf', 'tfm']
19 def read_log_file (fn):
20 str = open (fn).read ()
21 str = re.sub ('\n', '', str)
22 str = re.sub ('[\t ]+', ' ', str)
26 def include_func (match, d = deps):
27 d.append (match.group (1))
30 def auto_func (match, a = autolines):
31 a.append (match.group (1))
34 str = re.sub ('\(([a-zA-Z_0-9-]+\.mf)', include_func, str)
35 str = re.sub ('@{(.*?)@}', auto_func, str)
37 return (autolines, deps)
45 def tfm_checksum (fn):
46 sys.stderr.write ("Reading checksum from `%s'\n" % fn)
54 cs = cs + (long (ord (b)) << shift)
60 ## ugh. What's font_family supposed to be? It's not an afm thing.
63 def parse_logfile (fn):
64 (autolines, deps) = read_log_file (fn)
70 tags = string.split (l, '@:')
71 if tags[0] == 'group':
73 elif tags[0] == 'puorg':
75 elif tags[0] == 'char':
78 name = re.sub ('-', 'M', name)
80 name = group + '.' + name
82 'description': tags[1],
84 'code': string.atoi (tags[2]),
85 'breapth': string.atof (tags[3]),
86 'width': string.atof (tags[4]),
87 'depth': string.atof (tags[5]),
88 'height': string.atof (tags[6]),
89 'wx': string.atof (tags[7]),
90 'wy': string.atof (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['DesignSize'] = string.atof (tags[4])
106 global_info['FontName'] = string.join (name, '-')
107 global_info['FullName'] = string.join (name,' ')
108 global_info['FamilyName'] = string.join (name[1:-1],
111 global_info['Weight'] = tags[4]
113 global_info['Weight'] = tags[-1]
115 global_info['FontBBox'] = '0 0 1000 1000'
116 global_info['Ascender'] = '0'
117 global_info['Descender'] = '0'
118 global_info['EncodingScheme'] = encoding
120 elif tags[0] == 'parameter':
121 global_info[tags[1]] = tags[2];
123 return (global_info, charmetrics, deps)
126 def write_afm_char_metric (file, charmetric):
128 tup = (charmetric['code'],
130 -charmetric['breapth'] * f,
131 -charmetric['depth'] * f,
132 charmetric['width'] * f,
133 charmetric['height'] * f,
134 charmetric['wx'] * f,
135 charmetric['wy'] * f)
137 file.write ('C %d ; N %s ; B %d %d %d %d ; W %d %d ;\n' % tup)
140 def write_afm_header (file):
141 file.write ("StartFontMetrics 2.0\n")
142 file.write ("Comment Automatically generated by mf-to-table.py\n")
145 def write_afm_metric (file, global_info, charmetrics):
146 for (k, v) in global_info.items():
147 file.write ("%s %s\n" % (k, v))
148 file.write ('StartCharMetrics %d\n' % len(charmetrics ))
149 for m in charmetrics:
150 write_afm_char_metric (file, m)
151 file.write ('EndCharMetrics\n')
152 file.write ('EndFontMetrics\n')
155 def write_tex_defs (file, global_info, charmetrics):
156 nm = global_info['font
157 ## nm = global_info['FontFamily']
159 for m in charmetrics:
161 texname = re.sub ('[_.]', 'X', m['name'])
162 def digit_to_letter (match):
163 return chr (ord (match.group(1)) - ord ('0') + ord ('A'))
164 texname = re.sub ('([0-9])', digit_to_letter, texname)
165 file.write (r'''\gdef\%s%s{\char%d}%%%s''' % \
166 (nm, texname, m['code'],'\n'))
167 file.write ('\\endinput\n')
170 def write_character_lisp_table (file, global_info, charmetrics):
172 def conv_char_metric (charmetric):
175 ((bbox . (%f %f %f %f))
176 (attachment . (%f . %f))))
177 """ %(charmetric['name'],
178 -charmetric['breapth'] * f,
179 -charmetric['depth'] * f,
180 charmetric['width'] * f,
181 charmetric['height'] * f,
187 for c in charmetrics:
188 file.write (conv_char_metric (c))
191 def write_global_lisp_table (file, global_info):
194 keys = ['staffsize', 'stafflinethickness', 'staff_space',
195 'linethickness', 'black_notehead_width', 'ledgerlinethickness',
199 if global_info.has_key (k):
200 str = str + "(%s . %s)\n" % (k,global_info[k])
205 def write_ps_encoding (name, file, global_info, charmetrics):
206 encs = ['.notdef'] * 256
207 for m in charmetrics:
208 encs[m['code']] = m['name']
210 file.write ('/%s [\n' % name)
211 for m in range (0, 256):
212 file.write (' /%s %% %d\n' % (encs[m], m))
213 file.write ('] def\n')
216 def write_fontlist (file, global_info, charmetrics):
217 ## nm = global_info['FontFamily']
221 r"""%% LilyPond file to list all font symbols and the corresponding names
222 %% Automatically generated by mf-to-table.py
226 """ % (2 * per_line + 1))
229 for m in charmetrics:
232 ## \musicglyph and \markup require "_" to be escaped
234 scm_string = re.sub ('_', r'_', m['name'])
235 tex_string = re.sub ('_', r'\\_' , m['name'])
237 ## prevent TeX from interpreting "--" as long dash
238 tex_string = re.sub ('--','-{}-', tex_string)
240 file.write (''' \\markup { \\raise #0.75 \\vcenter
242 \\typewriter " %s" } 4\n''' % (scm_string, tex_string))
244 if (count % per_line) == 0:
245 file.write (' \\skip 8 \\break\n')
253 \override SeparationItem #'padding = #2
254 minimumVerticalExtent = ##f
258 \remove "Bar_number_engraver"
265 def write_deps (file, deps, targets):
267 t = re.sub ( '^\\./', '', t)
268 file.write ('%s '% t)
271 file.write ('%s ' % d)
276 sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
278 Generate feta metrics table from preparated feta log.
281 -a, --afm=FILE specify .afm file
282 -d, --dep=FILE print dependency info to FILE
283 -h, --help print this help
284 -l, --ly=FILE name output table
285 -o, --outdir=DIR prefix for dependency info
286 -p, --package=DIR specify package
287 -t, --tex=FILE name output tex chardefs
294 getopt.getopt (sys.argv[1:],
296 ['enc=', 'afm=', 'outdir=', 'dep=', 'lisp=',
298 'tex=', 'ly=', 'debug', 'help', 'package='])
312 if o == '--dep' or o == '-d':
314 elif o == '--outdir' or o == '-o':
316 elif o == '--tex' or o == '-t':
320 elif o == '--global-lisp':
324 elif o == '--ly' or o == '-l':
326 elif o== '--help' or o == '-h':
328 elif o=='--afm' or o == '-a':
336 base = re.sub ('.tex$', '', texfile_nm)
339 (g, m, deps) = parse_logfile (filenm)
340 cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
341 afm = open (afmfile_nm, 'w')
343 write_afm_header (afm)
344 afm.write ("Comment TfmCheckSum %d\n" % cs)
345 afm.write ("Comment DesignSize %.2f\n" % g['DesignSize'])
349 write_afm_metric (afm, g, m)
351 write_tex_defs (open (texfile_nm, 'w'), g, m)
352 enc_name = 'FetaEncoding'
353 if re.search ('parmesan', filenm):
354 enc_name = 'ParmesanEncoding'
355 elif re.search ('feta-brace', filenm):
356 enc_name = 'FetaBraceEncoding'
358 write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
359 write_character_lisp_table (open (char_lisp_nm, 'w'), g, m)
360 write_global_lisp_table (open (global_lisp_nm, 'w'), g)
362 write_deps (open (depfile_nm, 'wb'), deps,
363 [base + '.dvi', base + '.pfa', base + '.pfb',
364 texfile_nm, afmfile_nm])
366 write_fontlist (open (lyfile_nm, 'w'), g, m)