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)
46 def tfm_checksum (fn):
47 sys.stderr.write ("Reading checksum from `%s'\n" % fn)
55 cs = cs + (long (ord (b)) << shift)
60 ## ugh. What's font_family supposed to be? It's not an afm thing.
62 def parse_logfile (fn):
63 (autolines, deps) = read_log_file (fn)
69 tags = string.split(l, '@:')
70 if tags[0] == 'group':
72 elif tags[0] == 'char':
75 name = group + '-' + name
77 'description': tags[1],
80 'code': string.atoi (tags[2]),
81 'breapth':string.atof (tags[3]),
82 'width': string.atof (tags[4]),
83 'depth':string.atof (tags[5]),
84 'height':string.atof (tags[6]),
85 'wx': string.atof (tags[7]),
86 'wy':string.atof (tags[8]),
88 charmetrics.append (m)
89 elif tags[0] == 'font':
91 font_family = (tags[3])
92 # To omit 'GNU' (foundry) from font name proper:
96 tags.append ('Regular')
99 encoding = re.sub (' ','-', tags[5])
102 global_info['DesignSize'] = string.atof (tags[4])
103 global_info['FontName'] = string.join (name,'-')
104 global_info['FullName'] = string.join (name,' ')
105 global_info['FamilyName'] = string.join (name[1:-1],
108 global_info['Weight'] = tags[4]
110 global_info['Weight'] = tags[-1]
112 global_info['FontBBox'] = '0 0 1000 1000'
113 global_info['Ascender'] = '0'
114 global_info['Descender'] = '0'
115 global_info['EncodingScheme'] = encoding
117 return (global_info, charmetrics, deps)
120 def write_afm_char_metric(file, charmetric):
123 tup = (charmetric['code'],
125 -charmetric['breapth'] *f,
126 -charmetric['depth']*f,
127 charmetric['width']*f,
128 charmetric['height']*f,
129 charmetric['wx'] * f,
130 charmetric['wy'] * f)
132 file.write ('C %d ; N %s ; B %d %d %d %d ; W %d %d ;\n'% tup)
134 def write_afm_header (file):
135 file.write ("StartFontMetrics 2.0\n")
136 file.write ("Comment Automatically generated by mf-to-table.py\n")
138 def write_afm_metric (file, global_info, charmetrics):
139 for (k,v) in global_info.items():
140 file.write ("%s %s\n" % (k,v))
141 file.write ('StartCharMetrics %d\n' % len(charmetrics ))
142 for m in charmetrics:
143 write_afm_char_metric (file,m)
144 file.write ('EndCharMetrics\n')
145 file.write ('EndFontMetrics\n')
148 def write_tex_defs (file, global_info, charmetrics):
149 ##nm = global_info['FontFamily']
151 for m in charmetrics:
152 file.write (r'''\gdef\%s%s{\char%d}%%%s''' % (nm, m['tex'], m['code'],'\n'))
153 file.write ('\\endinput\n')
155 def write_ps_encoding (name, file, global_info, charmetrics):
156 encs = ['.notdef'] * 256
157 for m in charmetrics:
158 encs[m['code']] = m['tex']
160 file.write ('/%s [\n' % name)
161 for m in range(0,256):
162 file.write (' /%s %% %d\n' % (encs[m], m))
163 file.write ('] def\n')
165 def write_fontlist (file, global_info, charmetrics):
166 ##nm = global_info['FontFamily']
170 %% LilyPond file to list all font symbols and the corresponding names
171 %% Automatically generated by mf-to-table.py
172 \score{ \lyrics \new Lyrics { \time %d/8
173 """ % (2*per_line+1))
176 for m in charmetrics:
180 ## \musicglyph and \markup require "_" to be escaped differently:
183 scm_string = re.sub('_', r'_', m['name'])
184 tex_string = re.sub ('_', r'\\_' , m['name'])
186 ## prevent TeX from interpreting "--" as long dash:
187 tex_string=re.sub('--','-{}-', tex_string)
189 file.write (' \\markup { \\raise #0.75 \\vcenter \\musicglyph #"%s" " %s" } 4 \n' % (scm_string, tex_string))
192 file.write ('\skip 8 \\break\n')
200 \override SeparationItem #'padding = #2
201 minimumVerticalExtent = ##f
205 \remove "Bar_number_engraver"
211 def write_deps (file, deps, targets):
215 t = re.sub ( '^\\./', '', t)
216 file.write ('%s '% t)
219 file.write ('%s ' % d)
223 sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
224 Generate feta metrics table from preparated feta log.
227 -a, --afm=FILE specify .afm file
228 -d, --dep=FILE print dependency info to FILE
229 -h, --help print this help
230 -l, --ly=FILE name output table
231 -o, --outdir=DIR prefix for dependency info
232 -p, --package=DIR specify package
233 -t, --tex=FILE name output tex chardefs
241 (options, files) = getopt.getopt(
242 sys.argv[1:], 'a:d:hl:o:p:t:',
243 ['enc=', 'afm=', 'outdir=', 'dep=', 'tex=', 'ly=', 'debug', 'help', 'package='])
256 if o == '--dep' or o == '-d':
258 elif o == '--outdir' or o == '-o':
260 elif o == '--tex' or o == '-t':
264 elif o == '--ly' or o == '-':
266 elif o== '--help' or o == '-h':
268 elif o=='--afm' or o == '-a':
272 elif o == '-p' or o == '--package':
278 base = re.sub ('.tex$', '', texfile_nm)
281 (g,m, deps) = parse_logfile (filenm)
282 cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
283 afm = open (afmfile_nm, 'w')
285 write_afm_header (afm)
286 afm.write ("Comment TfmCheckSum %d\n" % cs)
287 afm.write ("Comment DesignSize %.2f\n" % g['DesignSize'])
291 write_afm_metric (afm, g, m)
293 write_tex_defs (open (texfile_nm, 'w'), g, m)
294 enc_name = 'FetaEncoding'
295 if re.search ('parmesan', filenm) :
296 enc_name = 'ParmesanEncoding'
297 elif re.search ('feta-brace', filenm) :
298 enc_name = 'FetaBraceEncoding'
301 write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
303 write_deps (open (depfile_nm, 'wb'), deps, [base + '.dvi', base + '.pfa', base + '.pfb', texfile_nm, afmfile_nm])
305 write_fontlist(open (lyfile_nm, 'w'), g, m)