]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mf-to-table.py
Reduced line spacing in the font list
[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 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  + (ord (b) << shift)
56                 shift = shift - 8
57
58         return cs
59   
60 def parse_logfile (fn):
61         (autolines, deps) = read_log_file (fn)
62         charmetrics = []
63         global_info = {}
64         group = ''
65
66         for l in autolines:
67                 tags = string.split(l, '@:')
68                 if tags[0] == 'group':
69                         group = tags[1]
70                 elif tags[0] == 'char':
71                         m = {
72                                 'description':  tags[1],
73                                 'name': group + '-' + tags[7],
74                                 'tex': tags[8],
75                                 'code': string.atoi (tags[2]),
76                                 'breapth':string.atof (tags[3]),
77                                 'width': string.atof (tags[4]),
78                                 'depth':string.atof (tags[5]),
79                                 'height':string.atof (tags[6])
80                                 }
81                         charmetrics.append (m)
82                 elif tags[0] == 'font':
83                         global_info['FontName'] = string.join (tags[1:])
84                         global_info['FontFamily']=tags[1]
85                         global_info['FontBBox'] = '0 0 1000 1000'
86                         global_info['Ascender'] = '0'
87                         global_info['Descender'] = '0'
88         
89         return (global_info, charmetrics, deps)
90
91
92 def write_afm_char_metric(file, charmetric):
93
94         f = 1000;
95         tup = (charmetric['code'],
96                (charmetric['width'] + charmetric['breapth'])*f,
97                 charmetric['name'],
98                 -charmetric['breapth'] *f,
99                 -charmetric['depth']*f,
100                 charmetric['width']*f,
101                 charmetric['height']*f)
102         
103         
104         file.write ('C %d ; WX %d ; N  %s ;  B %d %d %d %d ;\n'% tup)
105         
106 def write_afm_metric (file, global_info, charmetrics):
107         file.write (r"""
108 StartFontMetrics 2.0
109 Comment Automatically generated by mf-to-table.py
110 """)
111         for (k,v) in global_info.items():
112                 file.write ("%s %s\n" % (k,v))
113         file.write ('StartCharMetrics %d\n' % len(charmetrics ))
114         for m in charmetrics:
115                 write_afm_char_metric (file,m)
116         file.write ('EndCharMetrics\n')
117         file.write ('EndFontMetrics %d\n')
118
119
120 def write_tex_defs (file, global_info, charmetrics):
121         nm = global_info['FontFamily']
122         for m in charmetrics:
123                 file.write (r'''\def\%s%s{\char%d}%s''' % (nm, m['tex'], m['code'],'\n'))
124
125 def write_ps_encoding (file, global_info, charmetrics):
126         encs = ['.notdef'] * 256
127         for m in charmetrics:
128                 encs[m['code']] = m['tex']
129                 
130         file.write ('/FetaEncoding [\n')
131         for m in range(0,256):
132                 file.write ('  /%s %% %d\n' % (encs[m], m))
133         file.write ('] def\n')
134         
135 def write_fontlist (file, global_info, charmetrics):
136         nm = global_info['FontFamily']
137         file.write (r"""
138 % Lilypond file to list all font symbols and the corresponding names
139 % Automatically generated by mf-to-table.py
140 \score{\notes{\fatText
141 """)
142         for m in charmetrics:
143                 escapedname=re.sub('_','\\\\\\\\_', m['name'])
144                 file.write ('s^#\'(lines (music \"%s\") \"%s\")\n' % (m['name'], escapedname))
145         file.write (r"""
146 }
147   \paper{
148     interscoreline=1
149     \translator{
150       \ScoreContext
151       \remove "Bar_number_engraver"
152       TextScript \override #'extra-extent-X = #'(-1 . 1)
153     }
154     \translator{
155       \StaffContext
156       \remove "Clef_engraver"
157       \remove "Key_engraver"
158       \remove "Time_signature_engraver"
159       \remove "Staff_symbol_engraver"
160       minimumVerticalExtent = ##f
161     }
162   }
163 }
164 """)
165
166 def write_deps (file, deps, targets):
167         for t in targets:
168                 file.write ('%s '% t)
169         file.write (": ")
170         for d in deps:
171                 file.write ('%s ' % d)
172         file.write ('\n')
173
174 def help():
175     sys.stdout.write(r"""Usage: mf-to-table [options] LOGFILEs
176 Generate feta metrics table from preparated feta log\n
177 Options:
178   -a, --afm=FILE         .afm file
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   -t, --tex=FILE         name output tex chardefs"""
185 )
186     sys.exit (0)
187
188
189
190 (options, files) = getopt.getopt(
191     sys.argv[1:], 'a:d:hl:o:p:t:', 
192     ['enc=', 'afm=', 'outdir=', 'dep=',  'tex=', 'ly=', 'debug', 'help', 'package='])
193
194
195 enc_nm = ''
196 texfile_nm = ''
197 depfile_nm = ''
198 afmfile_nm = ''
199 lyfile_nm = ''
200 outdir_prefix = '.'
201
202 for opt in options:
203         o = opt[0]
204         a = opt[1]
205         if o == '--dep' or o == '-d':
206                 depfile_nm = a
207         elif o == '--outdir' or o == '-o':
208                 outdir_prefix = a
209         elif o == '--tex' or o == '-t':
210                 texfile_nm = a
211         elif o == '--enc':
212                 enc_nm = a
213         elif o == '--ly' or o == '-':
214                 lyfile_nm = a
215         elif o== '--help' or o == '-h':
216                 help()
217         elif o=='--afm' or o == '-a':
218                 afmfile_nm = a
219         elif o == '--debug':
220                 debug_b = 1
221         elif o == '-p' or o == '--package':
222                 topdir = a
223         else:
224                 print o
225                 raise getopt.error
226
227 base = re.sub ('.tex$', '', texfile_nm)
228
229 for filenm in files:
230         (g,m, deps) =  parse_logfile (filenm)
231         cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
232         afm = open (afmfile_nm, 'w')
233
234         afm.write ("TfmCheckSum %u\n" % cs) 
235         
236         write_afm_metric (afm, g,m)
237         write_tex_defs (open (texfile_nm, 'w'), g, m)
238         write_ps_encoding (open (enc_nm, 'w'), g, m)
239         
240         write_deps (open (depfile_nm, 'wb'), deps, [base + '.dvi', texfile_nm, afmfile_nm])
241         if lyfile_nm != '':
242                 write_fontlist(open (lyfile_nm, 'w'), g, m)
243
244
245