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 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):
54 cs = cs + (ord (b) << shift)
59 def parse_logfile (fn):
60 (autolines, deps) = read_log_file (fn)
66 tags = string.split(l, '@:')
67 if tags[0] == 'group':
69 elif tags[0] == 'char':
71 'description': tags[1],
72 'name': group + '-' + tags[7],
74 'code': string.atoi (tags[2]),
75 'breapth':string.atof (tags[3]),
76 'width': string.atof (tags[4]),
77 'depth':string.atof (tags[5]),
78 'height':string.atof (tags[6])
80 charmetrics.append (m)
81 elif tags[0] == 'font':
82 global_info['FontName'] = string.join (tags[1:])
83 global_info['FontFamily']=tags[1]
85 return (global_info, charmetrics, deps)
88 def write_afm_char_metric(file, charmetric):
91 tup = (charmetric['code'],
92 charmetric['width'] + charmetric['breapth'],
94 -charmetric['breapth'] *f,
95 -charmetric['depth']*f,
96 charmetric['width']*f,
97 charmetric['height']*f)
100 file.write ('C %d ; WX %d ; N %s ; B %d %d %d %d ;\n'% tup)
102 def write_afm_metric (file, global_info, charmetrics):
105 Comment Automatically generated by mf-to-table.py
107 for (k,v) in global_info.items():
108 file.write ("%s %s\n" % (k,v))
109 file.write ('StartCharMetrics %d\n' % len(charmetrics ))
110 for m in charmetrics:
111 write_afm_char_metric (file,m)
112 file.write ('EndCharMetrics\n')
113 file.write ('EndFontMetrics %d\n')
116 def write_tex_defs (file, global_info, charmetrics):
117 nm = global_info['FontFamily']
118 for m in charmetrics:
119 file.write (r'''\def\%s%s{\char%d}%s''' % (nm, m['tex'], m['code'],'\n'))
122 def write_deps (file, deps, targets):
124 file.write ('%s '% t)
127 file.write ('%s ' % d)
131 sys.stdout.write(r"""Usage: mf-to-table [options] LOGFILEs
132 Generate feta metrics table from preparated feta log\n
134 -a, --afm=FILE .afm file
135 -d, --dep=FILE print dependency info to FILE
136 -h, --help print this help
137 -l, --ly=FILE name output table
138 -o, --outdir=DIR prefix for dependency info
139 -p, --package=DIR specify package
140 -t, --tex=FILE name output tex chardefs"""
146 (options, files) = getopt.getopt(
147 sys.argv[1:], 'a:d:hl:o:p:t:',
148 ['afm=', 'outdir=', 'dep=', 'tex=', 'debug', 'help', 'package='])
159 if o == '--dep' or o == '-d':
161 elif o == '--outdir' or o == '-o':
163 elif o == '--tex' or o == '-t':
165 elif o== '--help' or o == '-h':
167 elif o=='--afm' or o == '-a':
171 elif o == '-p' or o == '--package':
178 (g,m, deps) = parse_logfile (filenm)
179 cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
180 afm = open (afmfile_nm, 'w')
182 afm.write ("TfmCheckSum %u\n" % cs)
184 write_afm_metric (afm, g,m)
185 write_tex_defs (open (texfile_nm, 'w'), g, m)
186 write_deps (open (depfile_nm, 'wb'), deps, [texfile_nm, afmfile_nm])