]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/mf-to-table.py
Run grand-replace (issue 3765)
[lilypond.git] / scripts / build / mf-to-table.py
1 #!@PYTHON@
2
3 # mf-to-table.py -- convert spacing info in MF logs .
4
5 # This file is part of LilyPond, the GNU music typesetter.
6 #
7 # Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 #
9 # LilyPond is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # LilyPond is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
21
22 import os
23 import sys
24 import getopt
25 import re
26 import time
27
28 def read_log_file (fn):
29     str = open (fn).read ()
30     str = re.sub ('[\n\r]', '', str)
31     str = re.sub ('[\t ]+', ' ', str)
32
33     deps = []
34     autolines = []
35     def include_func (match, d = deps):
36         d.append (match.group (1))
37         return ''
38
39     def auto_func (match, a = autolines):
40         a.append (match.group (1))
41         return ''
42
43     str = re.sub ('\\(([/a-z.A-Z_0-9-]+\\.mf)', include_func, str)
44     str = re.sub ('@{(.*?)@}', auto_func, str)
45
46     return (autolines, deps)
47
48
49 class Char_metric:
50     def __init__ (self):
51         pass
52
53 font_family = 'feta'
54
55 def parse_logfile (fn):
56     autolines, deps = read_log_file (fn)
57     charmetrics = []
58
59     global_info = {
60         'filename' : os.path.splitext (os.path.basename (fn))[0]
61         }
62     group = ''
63
64     for i, l in enumerate(autolines):
65         tags = l.split ('@:')
66         if tags[0] == 'group':
67             group = tags[1]
68         elif tags[0] == 'puorg':
69             group = ''
70         elif tags[0] == 'char':
71             try:
72                 name = tags[9]
73             except IndexError:
74                 print 'Error in mf-to-table while processing file', fn
75                 print 'Index 9 >', len(tags)-1, 'on line', i
76                 print l
77                 raise
78
79             if group:
80                 name = group + '.' + name
81             m = {
82                 'description': tags[1],
83                 'name': name,
84                 'code': int (tags[2]),
85                 'breapth': float (tags[3]),
86                 'width': float (tags[4]),
87                 'depth': float (tags[5]),
88                 'height': float (tags[6]),
89                 'wx': float (tags[7]),
90                 'wy': float (tags[8]),
91             }
92             charmetrics.append (m)
93         elif tags[0] == 'font':
94             global font_family
95             font_family = (tags[3])
96             # To omit 'GNU' (foundry) from font name proper:
97             # name = tags[2:]
98             #urg
99             if 0: # testing
100                 tags.append ('Regular')
101
102             encoding = re.sub (' ','-', tags[5])
103             tags = tags[:-1]
104             name = tags[1:]
105             global_info['design_size'] = float (tags[4])
106             global_info['FontName'] = '-'.join (name)
107             global_info['FullName'] = ' '.join (name)
108             global_info['FamilyName'] = '-'.join (name[1:-1])
109             if 1:
110                 global_info['Weight'] = tags[4]
111             else: # testing
112                 global_info['Weight'] = tags[-1]
113
114             global_info['FontBBox'] = '0 0 1000 1000'
115             global_info['Ascender'] = '0'
116             global_info['Descender'] = '0'
117             global_info['EncodingScheme'] = encoding
118
119         elif tags[0] == 'parameter':
120             global_info[tags[1]] = tags[2];
121
122     return (global_info, charmetrics, deps)
123
124
125
126 def character_lisp_table (global_info, charmetrics):
127
128     def conv_char_metric (charmetric):
129         f = 1.0
130         s = """(%s .
131 ((bbox . (%f %f %f %f))
132 (subfont . "%s")
133 (subfont-index . %d)
134 (attachment . (%f . %f))))
135 """ %(charmetric['name'],
136    -charmetric['breapth'] * f,
137    -charmetric['depth'] * f,
138    charmetric['width'] * f,
139    charmetric['height'] * f,
140    global_info['filename'],
141    charmetric['code'],
142    charmetric['wx'],
143    charmetric['wy'])
144
145         return s
146
147     s = ''
148     for c in charmetrics:
149         s += conv_char_metric (c)
150
151     return s
152
153
154 def global_lisp_table (global_info):
155     str = ''
156
157     keys = ['staffsize', 'stafflinethickness', 'staff_space',
158         'linethickness', 'black_notehead_width', 'ledgerlinethickness',
159         'design_size',
160         'blot_diameter'
161         ]
162     for k in keys:
163         if global_info.has_key (k):
164             str = str + "(%s . %s)\n" % (k,global_info[k])
165
166     return str
167
168
169 def ps_encoding (name, global_info, charmetrics):
170     encs = ['.notdef'] * 256
171     for m in charmetrics:
172         encs[m['code']] = m['name']
173
174
175     s =  ('/%s [\n' % name)
176     for m in range (0, 256):
177         s += ('  /%s %% %d\n' % (encs[m], m))
178     s +=  ('] def\n')
179     return s
180
181 def get_deps (deps, targets):
182     s = ''
183     for t in targets:
184         t = re.sub ( '^\\./', '', t)
185         s += ('%s '% t)
186     s += (": ")
187     for d in deps:
188         s += ('%s ' % d)
189     s += ('\n')
190     return s
191
192 def help ():
193     sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
194
195 Generate feta metrics table from preparated feta log.
196
197 Options:
198  -d, --dep=FILE         print dependency info to FILE
199  -h, --help             print this help
200  -l, --ly=FILE          name output table
201  -o, --outdir=DIR       prefix for dependency info
202  -p, --package=DIR      specify package
203
204  """)
205     sys.exit (0)
206
207
208 (options, files) = \
209  getopt.getopt (sys.argv[1:],
210         'a:d:ho:p:t:',
211         ['enc=',  'outdir=', 'dep=', 'lisp=',
212          'global-lisp=',
213          'debug', 'help', 'package='])
214
215 global_lisp_nm = ''
216 char_lisp_nm = ''
217 enc_nm = ''
218 depfile_nm = ''
219 lyfile_nm = ''
220 outdir_prefix = '.'
221
222 for opt in options:
223     o = opt[0]
224     a = opt[1]
225     if o == '--dep' or o == '-d':
226         depfile_nm = a
227     elif o == '--outdir' or o == '-o':
228         outdir_prefix = a
229     elif o == '--lisp':
230         char_lisp_nm = a
231     elif o == '--global-lisp':
232         global_lisp_nm = a
233     elif o == '--enc':
234         enc_nm = a
235     elif o== '--help' or o == '-h':
236         help()
237     elif o == '--debug':
238         debug_b = 1
239     else:
240         print o
241         raise getopt.error
242
243 base = os.path.splitext (lyfile_nm)[0]
244
245 for filenm in files:
246     (g, m, deps) = parse_logfile (filenm)
247
248     enc_name = 'FetaEncoding'
249     if re.search ('parmesan', filenm):
250         enc_name = 'ParmesanEncoding'
251     elif re.search ('parmesan-noteheads', filenm):
252         enc_name = 'ParmesanNoteheadsEncoding'
253     elif re.search ('feta-noteheads', filenm):
254         enc_name = 'FetaNoteheadsEncoding'
255     elif re.search ('feta-flags', filenm):
256         enc_name = 'FetaFlagsEncoding'
257     elif re.search ('feta-brace', filenm):
258         enc_name = 'FetaBraceEncoding'
259     elif re.search ('feta-alphabet', filenm):
260         enc_name = 'FetaAlphabetEncoding';
261
262     open (enc_nm, 'w').write (ps_encoding (enc_name, g, m))
263     open (char_lisp_nm, 'w').write (character_lisp_table (g, m))
264     open (global_lisp_nm, 'w').write (global_lisp_table (g))
265     if depfile_nm:
266         open (depfile_nm, 'wb').write (get_deps (deps,
267                                                  [base + '.log', base + '.dvi',
268                                                   depfile_nm,
269                                                   base + '.pfb']))