]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mf-to-table.py
release: 1.4.1
[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         
86         return (global_info, charmetrics, deps)
87
88
89 def write_afm_char_metric(file, charmetric):
90
91         f = 1000;
92         tup = (charmetric['code'],
93                (charmetric['width'] + charmetric['breapth'])*f,
94                 charmetric['name'],
95                 -charmetric['breapth'] *f,
96                 -charmetric['depth']*f,
97                 charmetric['width']*f,
98                 charmetric['height']*f)
99         
100         
101         file.write ('C %d ; WX %d ; N  %s ;  B %d %d %d %d ;\n'% tup)
102         
103 def write_afm_metric (file, global_info, charmetrics):
104         file.write (r"""
105 StartFontMetrics 2.0
106 Comment Automatically generated by mf-to-table.py
107 """)
108         for (k,v) in global_info.items():
109                 file.write ("%s %s\n" % (k,v))
110         file.write ('StartCharMetrics %d\n' % len(charmetrics ))
111         for m in charmetrics:
112                 write_afm_char_metric (file,m)
113         file.write ('EndCharMetrics\n')
114         file.write ('EndFontMetrics %d\n')
115
116
117 def write_tex_defs (file, global_info, charmetrics):
118         nm = global_info['FontFamily']
119         for m in charmetrics:
120                 file.write (r'''\def\%s%s{\char%d}%s''' % (nm, m['tex'], m['code'],'\n'))
121
122
123 def write_deps (file, deps, targets):
124         for t in targets:
125                 file.write ('%s '% t)
126         file.write (": ")
127         for d in deps:
128                 file.write ('%s ' % d)
129         file.write ('\n')
130
131 def help():
132     sys.stdout.write(r"""Usage: mf-to-table [options] LOGFILEs
133 Generate feta metrics table from preparated feta log\n
134 Options:
135   -a, --afm=FILE         .afm file
136   -d, --dep=FILE         print dependency info to FILE
137   -h, --help             print this help
138   -l, --ly=FILE          name output table
139   -o, --outdir=DIR       prefix for dependency info
140   -p, --package=DIR      specify package
141   -t, --tex=FILE         name output tex chardefs"""
142 )
143     sys.exit (0)
144
145
146
147 (options, files) = getopt.getopt(
148     sys.argv[1:], 'a:d:hl:o:p:t:', 
149     ['afm=', 'outdir=', 'dep=',  'tex=', 'debug', 'help', 'package='])
150
151
152 texfile_nm = '';
153 depfile_nm = ''
154 afmfile_nm = ''
155 outdir_prefix = '.'
156
157 for opt in options:
158         o = opt[0]
159         a = opt[1]
160         if o == '--dep' or o == '-d':
161                 depfile_nm = a
162         elif o == '--outdir' or o == '-o':
163                 outdir_prefix = a
164         elif o == '--tex' or o == '-t':
165                 texfile_nm = a
166         elif o== '--help' or o == '-h':
167                 help()
168         elif o=='--afm' or o == '-a':
169                 afmfile_nm = a
170         elif o == '--debug':
171                 debug_b = 1
172         elif o == '-p' or o == '--package':
173                 topdir = a
174         else:
175                 print o
176                 raise getopt.error
177
178 for filenm in files:
179         (g,m, deps) =  parse_logfile (filenm)
180         cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
181         afm = open (afmfile_nm, 'w')
182
183         afm.write ("TfmCheckSum %u\n" % cs) 
184         
185         write_afm_metric (afm, g,m)
186         write_tex_defs (open (texfile_nm, 'w'), g, m)
187         write_deps (open (depfile_nm, 'wb'), deps, [texfile_nm, afmfile_nm])
188
189