]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mf-to-table.py
Robustness fixes: generate entire string, then write.
[lilypond.git] / buildscripts / mf-to-table.py
1 #!@PYTHON@
2
3 # mf-to-table.py -- convert spacing info in MF logs . 
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1997--2007 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9 import os
10 import sys
11 import getopt
12 import string
13 import re
14 import time
15
16 def read_log_file (fn):
17     str = open (fn).read ()
18     str = re.sub ('\n', '', str)
19     str = re.sub ('[\t ]+', ' ', str)
20
21     deps = []
22     autolines = []
23     def include_func (match, d = deps):
24         d.append (match.group (1))
25         return ''
26
27     def auto_func (match, a = autolines):
28         a.append (match.group (1))
29         return ''
30
31     str = re.sub ('\\(([/a-z.A-Z_0-9-]+\\.mf)', include_func, str)
32     str = re.sub ('@{(.*?)@}', auto_func, str)
33
34     return (autolines, deps)
35
36
37 class Char_metric:
38     def __init__ (self):
39         pass
40
41 font_family = 'feta'
42
43 def parse_logfile (fn):
44     (autolines, deps) = read_log_file (fn)
45     charmetrics = []
46     
47     global_info = {
48         'filename' : os.path.splitext (os.path.basename (fn))[0]
49         }
50     group = ''
51
52     for l in autolines:
53         tags = string.split (l, '@:')
54         if tags[0] == 'group':
55             group = tags[1]
56         elif tags[0] == 'puorg':
57             group = ''
58         elif tags[0] == 'char':
59             name = tags[9]
60
61             if group:
62                 name = group + '.' + name
63             m = {
64                 'description': tags[1],
65                 'name': name,
66                 'code': string.atoi (tags[2]),
67                 'breapth': string.atof (tags[3]),
68                 'width': string.atof (tags[4]),
69                 'depth': string.atof (tags[5]),
70                 'height': string.atof (tags[6]),
71                 'wx': string.atof (tags[7]),
72                 'wy': string.atof (tags[8]),
73             }
74             charmetrics.append (m)
75         elif tags[0] == 'font':
76             global font_family
77             font_family = (tags[3])
78             # To omit 'GNU' (foundry) from font name proper:
79             # name = tags[2:]
80             #urg
81             if 0: # testing
82                 tags.append ('Regular')
83
84             encoding = re.sub (' ','-', tags[5])
85             tags = tags[:-1]
86             name = tags[1:]
87             global_info['design_size'] = string.atof (tags[4])
88             global_info['FontName'] = string.join (name, '-')
89             global_info['FullName'] = string.join (name,' ')
90             global_info['FamilyName'] = string.join (name[1:-1],
91                                 '-')
92             if 1:
93                 global_info['Weight'] = tags[4]
94             else: # testing
95                 global_info['Weight'] = tags[-1]
96
97             global_info['FontBBox'] = '0 0 1000 1000'
98             global_info['Ascender'] = '0'
99             global_info['Descender'] = '0'
100             global_info['EncodingScheme'] = encoding
101
102         elif tags[0] == 'parameter':
103             global_info[tags[1]] = tags[2];
104             
105     return (global_info, charmetrics, deps)
106
107
108
109
110
111 def character_lisp_table (global_info, charmetrics):
112
113     def conv_char_metric (charmetric):
114         f = 1.0
115         s = """(%s .
116 ((bbox . (%f %f %f %f))
117 (subfont . "%s")
118 (subfont-index . %d)
119 (attachment . (%f . %f))))
120 """ %(charmetric['name'],
121    -charmetric['breapth'] * f,
122    -charmetric['depth'] * f,
123    charmetric['width'] * f,
124    charmetric['height'] * f,
125    global_info['filename'],
126    charmetric['code'],
127    charmetric['wx'],
128    charmetric['wy'])
129
130         return s
131
132     s = ''
133     for c in charmetrics:
134         s += conv_char_metric (c)
135
136     return s
137
138
139 def global_lisp_table (global_info):
140     str = ''
141
142     keys = ['staffsize', 'stafflinethickness', 'staff_space',
143         'linethickness', 'black_notehead_width', 'ledgerlinethickness',
144         'design_size', 
145         'blot_diameter'
146         ]
147     for k in keys:
148         if global_info.has_key (k):
149             str = str + "(%s . %s)\n" % (k,global_info[k])
150
151     return str
152
153     
154 def ps_encoding (name, global_info, charmetrics):
155     encs = ['.notdef'] * 256
156     for m in charmetrics:
157         encs[m['code']] = m['name']
158
159
160     s =  ('/%s [\n' % name)
161     for m in range (0, 256):
162         s += ('  /%s %% %d\n' % (encs[m], m))
163     s +=  ('] def\n')
164     return s
165
166 def get_deps (deps, targets):
167     s = ''
168     for t in targets:
169         t = re.sub ( '^\\./', '', t)
170         s += ('%s '% t)
171     s += (": ")
172     for d in deps:
173         s += ('%s ' % d)
174     s += ('\n')
175     return s
176
177 def help ():
178     sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
179
180 Generate feta metrics table from preparated feta log.
181
182 Options:
183  -d, --dep=FILE         print dependency info to FILE
184  -h, --help             print this help
185  -l, --ly=FILE          name output table
186  -o, --outdir=DIR       prefix for dependency info
187  -p, --package=DIR      specify package
188
189  """)
190     sys.exit (0)
191
192
193 (options, files) = \
194  getopt.getopt (sys.argv[1:],
195         'a:d:ho:p:t:',
196         ['enc=',  'outdir=', 'dep=', 'lisp=',
197          'global-lisp=',
198          'debug', 'help', 'package='])
199
200 global_lisp_nm = ''
201 char_lisp_nm = ''
202 enc_nm = ''
203 depfile_nm = ''
204 lyfile_nm = ''
205 outdir_prefix = '.'
206
207 for opt in options:
208     o = opt[0]
209     a = opt[1]
210     if o == '--dep' or o == '-d':
211         depfile_nm = a
212     elif o == '--outdir' or o == '-o':
213         outdir_prefix = a
214     elif o == '--lisp': 
215         char_lisp_nm = a
216     elif o == '--global-lisp': 
217         global_lisp_nm = a
218     elif o == '--enc':
219         enc_nm = a
220     elif o== '--help' or o == '-h':
221         help()
222     elif o == '--debug':
223         debug_b = 1
224     else:
225         print o
226         raise getopt.error
227
228 base = os.path.splitext (lyfile_nm)[0]
229
230 for filenm in files:
231     (g, m, deps) = parse_logfile (filenm)
232
233     enc_name = 'FetaEncoding'
234     if re.search ('parmesan', filenm):
235         enc_name = 'ParmesanEncoding'
236     elif re.search ('feta-brace', filenm):
237         enc_name = 'FetaBraceEncoding'
238     elif re.search ('feta-alphabet', filenm):
239         enc_name = 'FetaAlphabetEncoding';
240
241     open (enc_nm, 'w').write (ps_encoding (enc_name, g, m))
242     open (char_lisp_nm, 'w').write (character_lisp_table (g, m))
243     open (global_lisp_nm, 'w').write (global_lisp_table (g))
244     if depfile_nm:
245         open (depfile_nm, 'wb').write (get_deps (deps,
246                                                  [base + '.log', base + '.dvi', base + '.pfa',
247                                                   depfile_nm,
248                                                   base + '.pfb']))