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