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