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