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):
47 sys.stderr.write ("Reading checksum from `%s'\n" % fn)
55 cs = cs + (ord (b) << shift)
60 def parse_logfile (fn):
61 (autolines, deps) = read_log_file (fn)
67 tags = string.split(l, '@:')
68 if tags[0] == 'group':
70 elif tags[0] == 'char':
72 'description': tags[1],
73 'name': group + '-' + tags[7],
75 'code': string.atoi (tags[2]),
76 'breapth':string.atof (tags[3]),
77 'width': string.atof (tags[4]),
78 'depth':string.atof (tags[5]),
79 'height':string.atof (tags[6])
81 charmetrics.append (m)
82 elif tags[0] == 'font':
83 global_info['FontName'] = string.join (tags[1:])
84 global_info['FontFamily']=tags[1]
86 return (global_info, charmetrics, deps)
89 def write_afm_char_metric(file, charmetric):
92 tup = (charmetric['code'],
93 charmetric['width'] + charmetric['breapth'],
95 -charmetric['breapth'] *f,
96 -charmetric['depth']*f,
97 charmetric['width']*f,
98 charmetric['height']*f)
101 file.write ('C %d ; WX %d ; N %s ; B %d %d %d %d ;\n'% tup)
103 def write_afm_metric (file, global_info, charmetrics):
106 Comment Automatically generated by mf-to-table.py
108 for (k,v) in global_info.items():
109 file.write ("%s %s\n" % (k,v))
110 file.write ('StartCharMetrics %d\n' % len(charmetrics ))
111 for m in charmetrics:
112 write_afm_char_metric (file,m)
113 file.write ('EndCharMetrics\n')
114 file.write ('EndFontMetrics %d\n')
117 def write_tex_defs (file, global_info, charmetrics):
118 nm = global_info['FontFamily']
119 for m in charmetrics:
120 file.write (r'''\def\%s%s{\char%d}%s''' % (nm, m['tex'], m['code'],'\n'))
123 def write_deps (file, deps, targets):
125 file.write ('%s '% t)
128 file.write ('%s ' % d)
132 sys.stdout.write(r"""Usage: mf-to-table [options] LOGFILEs
133 Generate feta metrics table from preparated feta log\n
135 -a, --afm=FILE .afm file
136 -d, --dep=FILE print dependency info to FILE
137 -h, --help print this help
138 -l, --ly=FILE name output table
139 -o, --outdir=DIR prefix for dependency info
140 -p, --package=DIR specify package
141 -t, --tex=FILE name output tex chardefs"""
147 (options, files) = getopt.getopt(
148 sys.argv[1:], 'a:d:hl:o:p:t:',
149 ['afm=', 'outdir=', 'dep=', 'tex=', 'debug', 'help', 'package='])
160 if o == '--dep' or o == '-d':
162 elif o == '--outdir' or o == '-o':
164 elif o == '--tex' or o == '-t':
166 elif o== '--help' or o == '-h':
168 elif o=='--afm' or o == '-a':
172 elif o == '-p' or o == '--package':
179 (g,m, deps) = parse_logfile (filenm)
180 cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
181 afm = open (afmfile_nm, 'w')
183 afm.write ("TfmCheckSum %u\n" % cs)
185 write_afm_metric (afm, g,m)
186 write_tex_defs (open (texfile_nm, 'w'), g, m)
187 write_deps (open (depfile_nm, 'wb'), deps, [texfile_nm, afmfile_nm])