]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/ps-to-pfa.py
release: 1.3.70
[lilypond.git] / buildscripts / ps-to-pfa.py
1 #!@PYTHON@
2
3 # ps-to-pfa.py -- make PostScript Type 3 font from separate ps char files
4
5 # source file of the GNU LilyPond music typesetter
6
7 # (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
8
9 #TODO.  This could be more efficient.
10
11 name = 'ps-to-pfa'
12 version = '0.4'
13
14 datadir = ''
15
16 import find
17 import os
18 import sys
19 import getopt
20 from string import *
21 import re
22 import time
23
24 logfile = sys.stdout
25
26 def program_id ():
27     return name + ' version ' + version;
28
29 def identify ():
30     sys.stdout.write (program_id () + '\n')
31
32 def help ():
33     sys.stdout.write ("Usage: %s [options] [files]\n"
34                       "ps-to-pfa.py -- make PostScript Type 3 font from separate ps char files\n\n"
35                       + "Options:\n"
36                       + "  -d, --datadir=DIR      use DIR for ps header/footer\n"
37                       + "  -h, --help             print this help\n"
38                       + "  -o, --output=FILE      set output file to FILE.\n"
39                       % (program_name)
40                       )
41     sys.exit (0)
42
43 output_name = ''
44
45 identify ()
46 (options, files) = getopt.getopt (
47     sys.argv[1:], 'o:d:', ['help', 'package', 'output='])
48 for opt in options:
49     o = opt[0]
50     a = opt[1]
51     if o== '--help' or o == '-h':
52         help ()
53     elif o == '-d' or o == '--datadir':
54         datadir = a
55     elif o == '-o' or o =='--output':
56         output_name = a
57     else:
58         print o
59         raise getopt.error
60
61
62 def gulp_file (f):
63         logfile.write ('[%s' % f)
64         try:
65                 i = open (f)
66                 i.seek (0, 2)
67                 n = i.tell ()
68                 i.seek (0,0)
69         except:
70                 logfile.write ('can\'t open file %s\n ' % f)
71                 return ''
72         s = i.read (n)
73         logfile.write (']')
74         if len (s) <= 0:
75                 logfile.write ('gulped empty file: %s\n'% f)
76         return s
77
78 mf = files[0]
79
80 input_name = mf
81 font_name = os.path.basename (os.path.splitext (mf)[0])
82 if not output_name:
83     output_name  = font_name + '.pfa'
84
85
86 logfile.write ('Font: %s\n'% font_name)
87
88 def header (f):
89         f.write ('%!PS-AdobeFont-3.0: ' + font_name + '\n')
90         f.write ('%%%%Creator: %s-%s\n' % (name, version))
91         f.write ('\n')
92         f.write (r"""
93 8 dict begin
94 /FontType 3 def                             %% Required elements of font
95 /FontName /%s def""" % font_name)
96         f.write (r"""
97 /FontMatrix [.083 0 0 .083 0 0] def       %% why .83?
98 /FontBBox [-1000 -1000 1000 1000] def     %% does not seem to matter.
99 /Encoding 256 array def                     %% Trivial encoding vector
100 0 1 255 {Encoding exch /.notdef put} for
101 """)
102 def footer (f):
103         f.write (r"""
104 /BuildGlyph {                               % Stack contains: font charname
105   1000 0                                   % Width
106   -1000 -1000 1000 1000                    % Bounding Box
107   setcachedevice
108   exch /CharProcs get exch                  % Get CharProcs dictionary
109   2 copy known not {pop /.notdef} if        % See if charname is known
110   get exec                                  % Execute character procedure
111 } bind def
112
113 /BuildChar {                                % Level 1 compatibility
114   1 index /Encoding get exch get
115   1 index /BuildGlyph get exec
116 } bind def
117
118 currentdict
119 end                                         % of font dictionary
120 """
121 )
122
123         f.write ('/%s\n' % font_name)
124         f.write (''
125 'exch definefont pop                         % Define the font\n')
126
127 suspect_re = re.compile ('closepath ((gsave )*fill( grestore stroke)*) 1 setgray newpath (.*?) closepath fill')
128
129 def characters (f):
130         logfile.write ('[')
131         
132         files = []
133         import find                     # q
134         suffixes = [".[0-9]", ".[0-9][0-9]",  ".[0-9][0-9][0-9]"]
135         for suf in suffixes:
136                 files = files + find.find (font_name + suf)
137
138
139         # concat all files into charprocs.
140         charprocs = '  /.notdef {} def\n'
141         encoding = ''
142         for i in files: 
143                 s = gulp_file (i)
144                 s = re.sub ('%[^\n]*\n', '', s)
145                 
146                 # if you want readable stuff, look at MP output.
147                 s = re.sub ('[\n\t ]+', ' ', s)
148                 s = re.sub ('showpage', '', s)
149
150                 # MP's implementation of unfill confuses GS.
151                 # Look for "Metapost & setgray" on deja.com
152                 # we do some twiddling to use eofill i.s.o. fill
153                 if re.search ('setgray',s ):
154                         m = suspect_re.search (s)
155                         while m:
156                                 fill = m.group (1)
157                                 path = m.group (4)
158
159                                 # be complicated, in case of gsave/grestore.
160                                 # vill as quick hack to avoid duple substitutions.
161                                 fill = re.sub ('fill', 'eovill', fill, count = 1)
162                                 s = re.sub (m.group (0), ' %s %s ' % (path, fill), s)
163                                 m = suspect_re.search (s)
164
165                         s = re.sub ('eovill' , 'eofill', s)
166                         s = re.sub ('0 setgray' ,'', s)
167
168                         
169                 m = re.match ('.*\.([0-9]+)',i)
170                 n = atoi (m.group (1))
171
172                 s = '\n  /%s-%d{\n%s} bind def\n' % (font_name, n, s)
173                 encoding = encoding + 'Encoding %d /%s-%d put\n' % (n, font_name, n)
174                 charprocs = charprocs + s
175
176         f.write ('\n'
177 '/CharProcs 3 dict def                       % Subsidiary dictiorary for\n'
178 'CharProcs begin                             % individual character definitions\n')
179         f.write (charprocs)
180         f.write ('\n')
181         f.write ('end                                         % of CharProcs\n')
182         f.write (encoding)
183         f.write ('\n')
184         logfile.write (']')
185
186
187 ps_file = open (output_name, 'w')
188 header (ps_file)
189 characters (ps_file)
190 footer (ps_file)
191 logfile.write ('\n')
192 ps_file.close ()
193 logfile.write ('Wrote PostScript font: %s\n' % output_name)
194