]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mf-to-table.py
release: 1.3.114
[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         s = open (fn).read ()
48         s = s[ 12 * 2 : ]
49         cs_bytes = s[:4]
50
51         shift = 24
52         cs = 0
53         for b in cs_bytes:
54                 cs = cs  + (ord (b) << shift)
55                 shift = shift - 8
56
57         return cs
58   
59 def parse_logfile (fn):
60         (autolines, deps) = read_log_file (fn)
61         charmetrics = []
62         global_info = {}
63         group = ''
64
65         for l in autolines:
66                 tags = string.split(l, '@:')
67                 if tags[0] == 'group':
68                         group = tags[1]
69                 elif tags[0] == 'char':
70                         m = {
71                                 'description':  tags[1],
72                                 'name': group + '-' + tags[7],
73                                 'tex': tags[8],
74                                 'code': string.atoi (tags[2]),
75                                 'breapth':string.atof (tags[3]),
76                                 'width': string.atof (tags[4]),
77                                 'depth':string.atof (tags[5]),
78                                 'height':string.atof (tags[6])
79                                 }
80                         charmetrics.append (m)
81                 elif tags[0] == 'font':
82                         global_info['FontName'] = string.join (tags[1:])
83                         global_info['FontFamily']=tags[1]
84         
85         return (global_info, charmetrics, deps)
86
87
88 def write_afm_char_metric(file, charmetric):
89
90         f = 1000;
91         tup = (charmetric['code'],
92                 charmetric['width'] + charmetric['breapth'],
93                 charmetric['name'],
94                 -charmetric['breapth'] *f,
95                 -charmetric['depth']*f,
96                 charmetric['width']*f,
97                 charmetric['height']*f)
98         
99         
100         file.write ('C %d ; WX %d ; N  %s ;  B %d %d %d %d ;\n'% tup)
101         
102 def write_afm_metric (file, global_info, charmetrics):
103         file.write (r"""
104 StartFontMetrics 2.0
105 Comment Automatically generated by mf-to-table.py
106 """)
107         for (k,v) in global_info.items():
108                 file.write ("%s %s\n" % (k,v))
109         file.write ('StartCharMetrics %d\n' % len(charmetrics ))
110         for m in charmetrics:
111                 write_afm_char_metric (file,m)
112         file.write ('EndCharMetrics\n')
113         file.write ('EndFontMetrics %d\n')
114
115
116 def write_tex_defs (file, global_info, charmetrics):
117         nm = global_info['FontFamily']
118         for m in charmetrics:
119                 file.write (r'''\def\%s%s{\char%d}%s''' % (nm, m['tex'], m['code'],'\n'))
120
121
122 def write_deps (file, deps, targets):
123         for t in targets:
124                 file.write ('%s '% t)
125         file.write (": ")
126         for d in deps:
127                 file.write ('%s ' % d)
128         file.write ('\n')
129
130 def help():
131     sys.stdout.write(r"""Usage: mf-to-table [options] LOGFILEs
132 Generate feta metrics table from preparated feta log\n
133 Options:
134   -a, --afm=FILE         .afm file
135   -d, --dep=FILE         print dependency info to FILE
136   -h, --help             print this help
137   -l, --ly=FILE          name output table
138   -o, --outdir=DIR       prefix for dependency info
139   -p, --package=DIR      specify package
140   -t, --tex=FILE         name output tex chardefs"""
141 )
142     sys.exit (0)
143
144
145
146 (options, files) = getopt.getopt(
147     sys.argv[1:], 'a:d:hl:o:p:t:', 
148     ['afm=', 'outdir=', 'dep=',  'tex=', 'debug', 'help', 'package='])
149
150
151 texfile_nm = '';
152 depfile_nm = ''
153 afmfile_nm = ''
154 outdir_prefix = '.'
155
156 for opt in options:
157         o = opt[0]
158         a = opt[1]
159         if o == '--dep' or o == '-d':
160                 depfile_nm = a
161         elif o == '--outdir' or o == '-o':
162                 outdir_prefix = a
163         elif o == '--tex' or o == '-t':
164                 texfile_nm = a
165         elif o== '--help' or o == '-h':
166                 help()
167         elif o=='--afm' or o == '-a':
168                 afmfile_nm = a
169         elif o == '--debug':
170                 debug_b = 1
171         elif o == '-p' or o == '--package':
172                 topdir = a
173         else:
174                 print o
175                 raise getopt.error
176
177 for filenm in files:
178         (g,m, deps) =  parse_logfile (filenm)
179         cs = tfm_checksum (re.sub ('.log$', '.tfm', filenm))
180         afm = open (afmfile_nm, 'w')
181
182         afm.write ("TfmCheckSum %u\n" % cs) 
183         
184         write_afm_metric (afm, g,m)
185         write_tex_defs (open (texfile_nm, 'w'), g, m)
186         write_deps (open (depfile_nm, 'wb'), deps, [texfile_nm, afmfile_nm])
187
188