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],
85 'code': string.atoi (tags[2]),
86 'breapth': string.atof (tags[3]),
87 'width': string.atof (tags[4]),
88 'depth': string.atof (tags[5]),
89 'height': string.atof (tags[6]),
90 'wx': string.atof (tags[7]),
91 'wy': string.atof (tags[8]),
93 charmetrics.append (m)
94 elif tags[0] == 'font':
96 font_family = (tags[3])
97 # To omit 'GNU' (foundry) from font name proper:
101 tags.append ('Regular')
103 encoding = re.sub (' ','-', tags[5])
106 global_info['DesignSize'] = string.atof (tags[4])
107 global_info['FontName'] = string.join (name,'-')
108 global_info['FullName'] = string.join (name,' ')
109 global_info['FamilyName'] = string.join (name[1:-1],
112 global_info['Weight'] = tags[4]
114 global_info['Weight'] = tags[-1]
116 global_info['FontBBox'] = '0 0 1000 1000'
117 global_info['Ascender'] = '0'
118 global_info['Descender'] = '0'
119 global_info['EncodingScheme'] = encoding
121 return (global_info, charmetrics, deps)
124 def write_afm_char_metric (file, charmetric):
126 tup = (charmetric['code'],
128 -charmetric['breapth'] * f,
129 -charmetric['depth'] * f,
130 charmetric['width'] * f,
131 charmetric['height'] * f,
132 charmetric['wx'] * f,
133 charmetric['wy'] * f)
135 file.write ('C %d ; N %s ; B %d %d %d %d ; W %d %d ;\n' % tup)
138 def write_afm_header (file):
139 file.write ("StartFontMetrics 2.0\n")
140 file.write ("Comment Automatically generated by mf-to-table.py\n")
143 def write_afm_metric (file, global_info, charmetrics):
144 for (k, v) in global_info.items():
145 file.write ("%s %s\n" % (k, v))
146 file.write ('StartCharMetrics %d\n' % len(charmetrics ))
147 for m in charmetrics:
148 write_afm_char_metric (file, m)
149 file.write ('EndCharMetrics\n')
150 file.write ('EndFontMetrics\n')
153 def write_tex_defs (file, global_info, charmetrics):
154 ## nm = global_info['FontFamily']
156 for m in charmetrics:
157 file.write (r'''\gdef\%s%s{\char%d}%%%s''' % \
158 (nm, m['tex'], m['code'],'\n'))
159 file.write ('\\endinput\n')
162 def write_otf_lisp_table (file, global_info, charmetrics):
164 def conv_char_metric (charmetric):
167 (bbox . (%f %f %f %f))
168 (attachment . (%f %f))))
169 """ %(charmetric['name'],
170 -charmetric['breapth'] * f,
171 -charmetric['depth'] * f,
172 charmetric['width'] * f,
173 charmetric['height'] * f,
179 for c in charmetrics:
180 file.write (conv_char_metric (c))
182 def write_ps_encoding (name, file, global_info, charmetrics):
183 encs = ['.notdef'] * 256
184 for m in charmetrics:
185 encs[m['code']] = m['name']
187 file.write ('/%s [\n' % name)
188 for m in range (0, 256):
189 file.write (' /%s %% %d\n' % (encs[m], m))
190 file.write ('] def\n')
193 def write_fontlist (file, global_info, charmetrics):
194 ## nm = global_info['FontFamily']
198 r"""%% LilyPond file to list all font symbols and the corresponding names
199 %% Automatically generated by mf-to-table.py
203 """ % (2 * per_line + 1))
206 for m in charmetrics:
209 ## \musicglyph and \markup require "_" to be escaped
211 scm_string = re.sub ('_', r'_', m['name'])
212 tex_string = re.sub ('_', r'\\_' , m['name'])
214 ## prevent TeX from interpreting "--" as long dash
215 tex_string = re.sub ('--','-{}-', tex_string)
217 file.write (''' \\markup { \\raise #0.75 \\vcenter
219 \\typewriter " %s" } 4\n''' % (scm_string, tex_string))
221 if (count % per_line) == 0:
222 file.write (' \\skip 8 \\break\n')
230 \override SeparationItem #'padding = #2
231 minimumVerticalExtent = ##f
235 \remove "Bar_number_engraver"
242 def write_deps (file, deps, targets):
244 t = re.sub ( '^\\./', '', t)
245 file.write ('%s '% t)
248 file.write ('%s ' % d)
253 sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
255 Generate feta metrics table from preparated feta log.
258 -a, --afm=FILE specify .afm file
259 -d, --dep=FILE print dependency info to FILE
260 -h, --help print this help
261 -l, --ly=FILE name output table
262 -o, --outdir=DIR prefix for dependency info
263 -p, --package=DIR specify package
264 -t, --tex=FILE name output tex chardefs
271 getopt.getopt (sys.argv[1:],
273 ['enc=', 'afm=', 'outdir=', 'dep=', 'lisp=',
274 'tex=', 'ly=', 'debug', 'help', 'package='])
288 if o == '--dep' or o == '-d':
290 elif o == '--outdir' or o == '-o':
292 elif o == '--tex' or o == '-t':
298 elif o == '--ly' or o == '-l':
300 elif o== '--help' or o == '-h':
302 elif o=='--afm' or o == '-a':
310 base = re.sub ('.tex$', '', texfile_nm)
313 (g, m, deps) = parse_logfile (filenm)
314 cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
315 afm = open (afmfile_nm, 'w')
317 write_afm_header (afm)
318 afm.write ("Comment TfmCheckSum %d\n" % cs)
319 afm.write ("Comment DesignSize %.2f\n" % g['DesignSize'])
323 write_afm_metric (afm, g, m)
325 write_tex_defs (open (texfile_nm, 'w'), g, m)
326 enc_name = 'FetaEncoding'
327 if re.search ('parmesan', filenm):
328 enc_name = 'ParmesanEncoding'
329 elif re.search ('feta-brace', filenm):
330 enc_name = 'FetaBraceEncoding'
332 write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
333 write_otf_lisp_table (open (lisp_nm, 'w'), g, m)
335 write_deps (open (depfile_nm, 'wb'), deps,
336 [base + '.dvi', base + '.pfa', base + '.pfb',
337 texfile_nm, afmfile_nm])
339 write_fontlist (open (lyfile_nm, 'w'), g, m)