]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mf-to-table.py
7febb8fa32310ac00ec168e32d048a42c2849d6d
[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--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         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 write_character_lisp_table (file, 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     for c in charmetrics:
133         file.write (conv_char_metric (c))
134
135
136 def write_global_lisp_table (file, global_info):
137     str = ''
138
139     keys = ['staffsize', 'stafflinethickness', 'staff_space',
140         'linethickness', 'black_notehead_width', 'ledgerlinethickness',
141         'design_size', 
142         'blot_diameter'
143         ]
144     for k in keys:
145         if global_info.has_key (k):
146             str = str + "(%s . %s)\n" % (k,global_info[k])
147
148     file.write (str)
149
150     
151 def write_ps_encoding (name, file, global_info, charmetrics):
152     encs = ['.notdef'] * 256
153     for m in charmetrics:
154         encs[m['code']] = m['name']
155
156     file.write ('/%s [\n' % name)
157     for m in range (0, 256):
158         file.write ('  /%s %% %d\n' % (encs[m], m))
159     file.write ('] def\n')
160
161
162 def write_deps (file, deps, targets):
163     for t in targets:
164         t = re.sub ( '^\\./', '', t)
165         file.write ('%s '% t)
166     file.write (": ")
167     for d in deps:
168         file.write ('%s ' % d)
169     file.write ('\n')
170
171
172 def help ():
173     sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
174
175 Generate feta metrics table from preparated feta log.
176
177 Options:
178  -d, --dep=FILE         print dependency info to FILE
179  -h, --help             print this help
180  -l, --ly=FILE          name output table
181  -o, --outdir=DIR       prefix for dependency info
182  -p, --package=DIR      specify package
183
184  """)
185     sys.exit (0)
186
187
188 (options, files) = \
189  getopt.getopt (sys.argv[1:],
190         'a:d:ho:p:t:',
191         ['enc=',  'outdir=', 'dep=', 'lisp=',
192          'global-lisp=',
193          'debug', 'help', 'package='])
194
195 global_lisp_nm = ''
196 char_lisp_nm = ''
197 enc_nm = ''
198 depfile_nm = ''
199 lyfile_nm = ''
200 outdir_prefix = '.'
201
202 for opt in options:
203     o = opt[0]
204     a = opt[1]
205     if o == '--dep' or o == '-d':
206         depfile_nm = a
207     elif o == '--outdir' or o == '-o':
208         outdir_prefix = a
209     elif o == '--lisp': 
210         char_lisp_nm = a
211     elif o == '--global-lisp': 
212         global_lisp_nm = a
213     elif o == '--enc':
214         enc_nm = a
215     elif o== '--help' or o == '-h':
216         help()
217     elif o == '--debug':
218         debug_b = 1
219     else:
220         print o
221         raise getopt.error
222
223 base = os.path.splitext (lyfile_nm)[0]
224
225 for filenm in files:
226     (g, m, deps) = parse_logfile (filenm)
227
228     enc_name = 'FetaEncoding'
229     if re.search ('parmesan', filenm):
230         enc_name = 'ParmesanEncoding'
231     elif re.search ('feta-brace', filenm):
232         enc_name = 'FetaBraceEncoding'
233     elif re.search ('feta-alphabet', filenm):
234         enc_name = 'FetaAlphabetEncoding';
235
236     write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
237     write_character_lisp_table (open (char_lisp_nm, 'w'), g, m)  
238     write_global_lisp_table (open (global_lisp_nm, 'w'), g)  
239     if depfile_nm:
240         write_deps (open (depfile_nm, 'wb'), deps,
241               [base + '.log', base + '.dvi', base + '.pfa',
242                depfile_nm,
243                base + '.pfb'])