lilypond-1.3.87
[lilypond.git] / buildscripts / mf-to-table.py
blob4c87408117cd194c885b7aa023647ceedd1bc05c
1 #!@PYTHON@
3 # mf-to-table.py -- convert spacing info in MF logs .afm and .tex
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1997 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 import os
10 import sys
11 import getopt
12 import string
13 import re
14 import time
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)
24 deps = []
25 autolines = []
26 def include_func (match, d = deps):
27 d.append (match.group (1))
28 return ''
30 def auto_func (match, a = autolines):
31 a.append (match.group (1))
32 return ''
34 str = re.sub ('\(([a-zA-Z_0-9-]+\.mf)', include_func, str)
35 str = re.sub ('@{(.*?)@}', auto_func, str)
37 return (autolines, deps)
41 class Char_metric:
42 def __init__ (self):
43 pass
46 def parse_logfile (fn):
47 (autolines, deps) = read_log_file (fn)
48 charmetrics = []
49 global_info = {}
50 group = ''
52 for l in autolines:
53 tags = string.split(l, '@:')
54 if tags[0] == 'group':
55 group = tags[1]
56 elif tags[0] == 'char':
57 m = {
58 'description': tags[1],
59 'name': group + '-' + tags[7],
60 'tex': tags[8],
61 'code': string.atoi (tags[2]),
62 'breapth':string.atof (tags[3]),
63 'width': string.atof (tags[4]),
64 'depth':string.atof (tags[5]),
65 'height':string.atof (tags[6])
67 charmetrics.append (m)
68 elif tags[0] == 'font':
69 global_info['FontName'] = string.join (tags[1:])
70 global_info['FontFamily']=tags[1]
72 return (global_info, charmetrics, deps)
75 def write_afm_char_metric(file, charmetric):
77 f = 1000;
78 tup = (charmetric['code'],
79 charmetric['width'] + charmetric['breapth'],
80 charmetric['name'],
81 -charmetric['breapth'] *f,
82 -charmetric['depth']*f,
83 charmetric['width']*f,
84 charmetric['height']*f)
87 file.write ('C %d ; WX %d ; N %s ; B %d %d %d %d ;\n'% tup)
89 def write_afm_metric (file, global_info, charmetrics):
90 file.write (r"""
91 StartFontMetrics 2.0
92 Comment Automatically generated by mf-to-table.py
93 """)
94 for (k,v) in global_info.items():
95 file.write ("%s %s\n" % (k,v))
96 file.write ('StartCharMetrics %d\n' % len(charmetrics ))
97 for m in charmetrics:
98 write_afm_char_metric (file,m)
99 file.write ('EndCharMetrics\n')
100 file.write ('EndFontMetrics %d\n')
103 def write_tex_defs (file, global_info, charmetrics):
104 nm = global_info['FontFamily']
105 for m in charmetrics:
106 file.write (r'''\def\%s%s{\char%d}%s''' % (nm, m['tex'], m['code'],'\n'))
109 def write_deps (file, deps, targets):
110 for t in targets:
111 file.write ('%s '% t)
112 file.write (": ")
113 for d in deps:
114 file.write ('%s ' % d)
115 file.write ('\n')
117 def help():
118 sys.stdout.write(r"""Usage: mf-to-table [options] LOGFILEs
119 Generate feta metrics table from preparated feta log\n
120 Options:
121 -a, --afm=FILE .afm file
122 -d, --dep=FILE print dependency info to FILE
123 -h, --help print this help
124 -l, --ly=FILE name output table
125 -o, --outdir=DIR prefix for dependency info
126 -p, --package=DIR specify package
127 -t, --tex=FILE name output tex chardefs"""
129 sys.exit (0)
133 (options, files) = getopt.getopt(
134 sys.argv[1:], 'a:d:hl:o:p:t:',
135 ['afm=', 'outdir=', 'dep=', 'tex=', 'debug', 'help', 'package='])
138 texfile_nm = '';
139 depfile_nm = ''
140 afmfile_nm = ''
141 outdir_prefix = '.'
143 for opt in options:
144 o = opt[0]
145 a = opt[1]
146 if o == '--dep' or o == '-d':
147 depfile_nm = a
148 elif o == '--outdir' or o == '-o':
149 outdir_prefix = a
150 elif o == '--tex' or o == '-t':
151 texfile_nm = a
152 elif o== '--help' or o == '-h':
153 help()
154 elif o=='--afm' or o == '-a':
155 afmfile_nm = a
156 elif o == '--debug':
157 debug_b = 1
158 elif o == '-p' or o == '--package':
159 topdir = a
160 else:
161 print o
162 raise getopt.error
164 for filenm in files:
165 (g,m, deps) = parse_logfile (filenm)
166 afm = open (afmfile_nm, 'w')
167 write_afm_metric (afm, g,m)
168 write_tex_defs (open (texfile_nm, 'w'), g, m)
169 write_deps (open (depfile_nm, 'wb'), deps, [texfile_nm, afmfile_nm])