]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mf-to-table.py
* Documentation/user/advanced.itely: removed.
[lilypond.git] / buildscripts / mf-to-table.py
1 #!@PYTHON@
2
3 # mf-to-table.py -- convert spacing info in  MF logs .afm and .tex
4
5 # source file of the GNU LilyPond music typesetter
6
7 # (c) 1997--2004 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
17 postfixes = ['log', 'dvi', '2602gf', 'tfm']
18
19 def read_log_file (fn):
20         str = open (fn).read ()
21         str = re.sub ('\n', '', str)    
22         str = re.sub ('[\t ]+', ' ', str) 
23
24         deps = []
25         autolines = []
26         def include_func (match, d = deps):
27                 d.append (match.group (1))
28                 return ''
29
30         def auto_func (match, a = autolines):
31                 a.append (match.group (1))
32                 return ''
33
34         str = re.sub ('\(([a-zA-Z_0-9-]+\.mf)', include_func, str)
35         str = re.sub ('@{(.*?)@}', auto_func, str)
36
37         return (autolines, deps)
38
39
40
41 class Char_metric:
42         def __init__ (self):
43                 pass
44
45
46 def tfm_checksum (fn):
47         sys.stderr.write ("Reading checksum from `%s'\n" % fn) 
48         s = open (fn).read ()
49         s = s[ 12 * 2 : ]
50         cs_bytes = s[:4]
51
52         shift = 24
53         cs = 0
54         for b in cs_bytes:
55                 cs = cs  + (long (ord (b)) << shift)
56                 shift = shift - 8
57
58         return cs
59
60 ## ugh.  What's font_family supposed to be?  It's not an afm thing.
61 font_family = 'feta'
62 def parse_logfile (fn):
63         (autolines, deps) = read_log_file (fn)
64         charmetrics = []
65         global_info = {}
66         group = ''
67
68         for l in autolines:
69                 tags = string.split(l, '@:')
70                 if tags[0] == 'group':
71                         group = tags[1]
72                 elif tags[0] == 'puorg':
73                         group = ''
74                 elif tags[0] == 'char':
75                         name = tags[9]
76                         if group:
77                                 name = group + '-' + name
78                         m = {
79                                 'description':  tags[1],
80                                 'name': name, 
81                                 'tex': tags[10],
82                                 'code': string.atoi (tags[2]),
83                                 'breapth':string.atof (tags[3]),
84                                 'width': string.atof (tags[4]),
85                                 'depth':string.atof (tags[5]),
86                                 'height':string.atof (tags[6]),
87                                 'wx': string.atof (tags[7]),
88                                 'wy':string.atof (tags[8]),
89                                 }
90                         charmetrics.append (m)
91                 elif tags[0] == 'font':
92                         global font_family
93                         font_family = (tags[3])
94                         # To omit 'GNU' (foundry) from font name proper:
95                         # name = tags[2:]
96                         #urg
97                         if 0: #testing
98                                 tags.append ('Regular')
99
100                         
101                         encoding = re.sub (' ','-', tags[5])
102                         tags = tags[:-1]
103                         name = tags[1:]
104                         global_info['DesignSize'] = string.atof (tags[4])
105                         global_info['FontName'] = string.join (name,'-')
106                         global_info['FullName'] = string.join (name,' ')
107                         global_info['FamilyName'] = string.join (name[1:-1],
108                                                                  '-')
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         return (global_info, charmetrics, deps)
120
121
122 def write_afm_char_metric(file, charmetric):
123
124         f = 1000;
125         tup = (charmetric['code'],
126                 charmetric['name'],
127                 -charmetric['breapth'] *f,
128                 -charmetric['depth']*f,
129                 charmetric['width']*f,
130                 charmetric['height']*f,
131                charmetric['wx'] * f,
132                charmetric['wy'] * f)
133         
134         file.write ('C %d ; N %s ; B %d %d %d %d ; W %d %d ;\n'% tup)
135
136 def write_afm_header (file):
137         file.write ("StartFontMetrics 2.0\n")
138         file.write ("Comment Automatically generated by mf-to-table.py\n")
139
140 def write_afm_metric (file, global_info, charmetrics):
141         for (k,v) in global_info.items():
142                 file.write ("%s %s\n" % (k,v))
143         file.write ('StartCharMetrics %d\n' % len(charmetrics ))
144         for m in charmetrics:
145                 write_afm_char_metric (file,m)
146         file.write ('EndCharMetrics\n')
147         file.write ('EndFontMetrics\n')
148
149
150 def write_tex_defs (file, global_info, charmetrics):
151         ##nm = global_info['FontFamily']
152         nm = font_family
153         for m in charmetrics:
154                 file.write (r'''\gdef\%s%s{\char%d}%%%s''' % (nm, m['tex'], m['code'],'\n'))
155         file.write ('\\endinput\n')
156
157 def write_ps_encoding (name, file, global_info, charmetrics):
158         encs = ['.notdef'] * 256
159         for m in charmetrics:
160                 encs[m['code']] = m['tex']
161
162         file.write ('/%s [\n' % name)
163         for m in range(0,256):
164                 file.write ('  /%s %% %d\n' % (encs[m], m))
165         file.write ('] def\n')
166         
167 def write_fontlist (file, global_info, charmetrics):
168         ##nm = global_info['FontFamily']
169         nm = font_family
170         per_line = 3
171         file.write (r"""
172 %% LilyPond file to list all font symbols and the corresponding names
173 %% Automatically generated by mf-to-table.py
174 \score{ \new Lyrics \lyrics { \time %d/8
175 """ % (2*per_line+1))
176
177         count = 0
178         for m in charmetrics:
179
180                 count += 1
181                 
182 ## \musicglyph and \markup require "_" to be escaped differently:
183                 
184
185                 scm_string = re.sub('_', r'_', m['name'])
186                 tex_string = re.sub ('_', r'\\_' , m['name'])
187                 
188 ## prevent TeX from interpreting "--" as long dash:
189                 tex_string=re.sub('--','-{}-', tex_string)
190
191                 file.write ('  \\markup { \\raise #0.75 \\vcenter \\musicglyph #"%s" \\typewriter " %s" } 4 \n' % (scm_string, tex_string))
192
193                 if (count % 3) ==0:
194                         file.write ('\skip 8  \\break\n')
195         file.write (r"""
196 }
197   \paper{
198     interscoreline = 1.0
199     indent = 0.0 \cm
200     \context {
201       \Lyrics
202       \override SeparationItem #'padding = #2
203       minimumVerticalExtent = ##f
204     }
205     \context {
206         \Score
207         \remove "Bar_number_engraver"
208         }
209         }
210         }
211 """)
212
213 def write_deps (file, deps, targets):
214         
215         
216         for t in targets:
217                 t = re.sub ( '^\\./', '', t)
218                 file.write ('%s '% t)
219         file.write (": ")
220         for d in deps:
221                 file.write ('%s ' % d)
222         file.write ('\n')
223
224 def help():
225     sys.stdout.write(r"""Usage: mf-to-table [OPTIONS] LOGFILEs
226 Generate feta metrics table from preparated feta log.
227
228 Options:
229   -a, --afm=FILE         specify .afm file
230   -d, --dep=FILE         print dependency info to FILE
231   -h, --help             print this help
232   -l, --ly=FILE          name output table
233   -o, --outdir=DIR       prefix for dependency info
234   -p, --package=DIR      specify package
235   -t, --tex=FILE         name output tex chardefs
236
237   """
238 )
239     sys.exit (0)
240
241
242
243 (options, files) = getopt.getopt(
244     sys.argv[1:], 'a:d:hl:o:p:t:', 
245     ['enc=', 'afm=', 'outdir=', 'dep=',  'tex=', 'ly=', 'debug', 'help', 'package='])
246
247
248 enc_nm = ''
249 texfile_nm = ''
250 depfile_nm = ''
251 afmfile_nm = ''
252 lyfile_nm = ''
253 outdir_prefix = '.'
254
255 for opt in options:
256         o = opt[0]
257         a = opt[1]
258         if o == '--dep' or o == '-d':
259                 depfile_nm = a
260         elif o == '--outdir' or o == '-o':
261                 outdir_prefix = a
262         elif o == '--tex' or o == '-t':
263                 texfile_nm = a
264         elif o == '--enc':
265                 enc_nm = a
266         elif o == '--ly' or o == '-l':
267                 lyfile_nm = a
268         elif o== '--help' or o == '-h':
269                 help()
270         elif o=='--afm' or o == '-a':
271                 afmfile_nm = a
272         elif o == '--debug':
273                 debug_b = 1
274         elif o == '-p' or o == '--package':
275                 topdir = a
276         else:
277                 print o
278                 raise getopt.error
279
280 base = re.sub ('.tex$', '', texfile_nm)
281
282 for filenm in files:
283         (g, m, deps) = parse_logfile (filenm)
284         cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
285         afm = open (afmfile_nm, 'w')
286
287         write_afm_header (afm)
288         afm.write ("Comment TfmCheckSum %d\n" % cs)
289         afm.write ("Comment DesignSize %.2f\n" % g['DesignSize'])
290
291         del g['DesignSize']
292         
293         write_afm_metric (afm, g, m)
294
295         write_tex_defs (open (texfile_nm, 'w'), g, m)
296         enc_name = 'FetaEncoding'
297         if re.search ('parmesan', filenm) :
298                 enc_name = 'ParmesanEncoding'
299         elif re.search ('feta-brace', filenm) :
300                 enc_name = 'FetaBraceEncoding'
301
302         write_ps_encoding (enc_name, open (enc_nm, 'w'), g, m)
303
304         if depfile_nm:
305                 write_deps (open (depfile_nm, 'wb'), deps,
306                             [base + '.dvi', base + '.pfa', base + '.pfb',
307                              texfile_nm, afmfile_nm])
308         if lyfile_nm:
309                 write_fontlist (open (lyfile_nm, 'w'), g, m)
310
311
312