]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/mf-to-table.py
44f0edc33aa3e4ae26c5da88a5a7849aa27f99e2
[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--2012 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', '', 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 l in 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             name = tags[9]
72
73             if group:
74                 name = group + '.' + name
75             m = {
76                 'description': tags[1],
77                 'name': name,
78                 'code': int (tags[2]),
79                 'breapth': float (tags[3]),
80                 'width': float (tags[4]),
81                 'depth': float (tags[5]),
82                 'height': float (tags[6]),
83                 'wx': float (tags[7]),
84                 'wy': float (tags[8]),
85             }
86             charmetrics.append (m)
87         elif tags[0] == 'font':
88             global font_family
89             font_family = (tags[3])
90             # To omit 'GNU' (foundry) from font name proper:
91             # name = tags[2:]
92             #urg
93             if 0: # testing
94                 tags.append ('Regular')
95
96             encoding = re.sub (' ','-', tags[5])
97             tags = tags[:-1]
98             name = tags[1:]
99             global_info['design_size'] = float (tags[4])
100             global_info['FontName'] = '-'.join (name)
101             global_info['FullName'] = ' '.join (name)
102             global_info['FamilyName'] = '-'.join (name[1:-1])
103             if 1:
104                 global_info['Weight'] = tags[4]
105             else: # testing
106                 global_info['Weight'] = tags[-1]
107
108             global_info['FontBBox'] = '0 0 1000 1000'
109             global_info['Ascender'] = '0'
110             global_info['Descender'] = '0'
111             global_info['EncodingScheme'] = encoding
112
113         elif tags[0] == 'parameter':
114             global_info[tags[1]] = tags[2];
115
116     return (global_info, charmetrics, deps)
117
118
119
120 def character_lisp_table (global_info, charmetrics):
121
122     def conv_char_metric (charmetric):
123         f = 1.0
124         s = """(%s .
125 ((bbox . (%f %f %f %f))
126 (subfont . "%s")
127 (subfont-index . %d)
128 (attachment . (%f . %f))))
129 """ %(charmetric['name'],
130    -charmetric['breapth'] * f,
131    -charmetric['depth'] * f,
132    charmetric['width'] * f,
133    charmetric['height'] * f,
134    global_info['filename'],
135    charmetric['code'],
136    charmetric['wx'],
137    charmetric['wy'])
138
139         return s
140
141     s = ''
142     for c in charmetrics:
143         s += conv_char_metric (c)
144
145     return s
146
147
148 def global_lisp_table (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     return str
161
162
163 def ps_encoding (name, global_info, charmetrics):
164     encs = ['.notdef'] * 256
165     for m in charmetrics:
166         encs[m['code']] = m['name']
167
168
169     s =  ('/%s [\n' % name)
170     for m in range (0, 256):
171         s += ('  /%s %% %d\n' % (encs[m], m))
172     s +=  ('] def\n')
173     return s
174
175 def get_deps (deps, targets):
176     s = ''
177     for t in targets:
178         t = re.sub ( '^\\./', '', t)
179         s += ('%s '% t)
180     s += (": ")
181     for d in deps:
182         s += ('%s ' % d)
183     s += ('\n')
184     return s
185
186 def help ():
187     sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
188
189 Generate feta metrics table from preparated feta log.
190
191 Options:
192  -d, --dep=FILE         print dependency info to FILE
193  -h, --help             print this help
194  -l, --ly=FILE          name output table
195  -o, --outdir=DIR       prefix for dependency info
196  -p, --package=DIR      specify package
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          'debug', 'help', 'package='])
208
209 global_lisp_nm = ''
210 char_lisp_nm = ''
211 enc_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 == '--lisp':
224         char_lisp_nm = a
225     elif o == '--global-lisp':
226         global_lisp_nm = a
227     elif o == '--enc':
228         enc_nm = a
229     elif o== '--help' or o == '-h':
230         help()
231     elif o == '--debug':
232         debug_b = 1
233     else:
234         print o
235         raise getopt.error
236
237 base = os.path.splitext (lyfile_nm)[0]
238
239 for filenm in files:
240     (g, m, deps) = parse_logfile (filenm)
241
242     enc_name = 'FetaEncoding'
243     if re.search ('parmesan', filenm):
244         enc_name = 'ParmesanEncoding'
245     elif re.search ('parmesan-noteheads', filenm):
246         enc_name = 'ParmesanNoteheadsEncoding'
247     elif re.search ('feta-noteheads', filenm):
248         enc_name = 'FetaNoteheadsEncoding'
249     elif re.search ('feta-flags', filenm):
250         enc_name = 'FetaFlagsEncoding'
251     elif re.search ('feta-brace', filenm):
252         enc_name = 'FetaBraceEncoding'
253     elif re.search ('feta-alphabet', filenm):
254         enc_name = 'FetaAlphabetEncoding';
255
256     open (enc_nm, 'w').write (ps_encoding (enc_name, g, m))
257     open (char_lisp_nm, 'w').write (character_lisp_table (g, m))
258     open (global_lisp_nm, 'w').write (global_lisp_table (g))
259     if depfile_nm:
260         open (depfile_nm, 'wb').write (get_deps (deps,
261                                                  [base + '.log', base + '.dvi', base + '.pfa',
262                                                   depfile_nm,
263                                                   base + '.pfb']))