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