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