3 # ps-to-pfa.py -- make PostScript Type 3 font from separate ps char files
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
9 #TODO. This could be more efficient.
25 return name + ' version ' + version;
28 sys.stdout.write (program_id () + '\n')
31 sys.stdout.write ("Usage: %s [options] [files]\n"
32 "ps-to-pfa.py -- make PostScript Type 3 font from separate ps char files\n\n"
34 + " -d, --datadir=DIR use DIR for ps header/footer\n"
35 + " -h, --help print this help\n"
36 + " -o, --output=FILE set output file to FILE.\n"
44 (options, files) = getopt.getopt (
45 sys.argv[1:], 'o:d:', ['help', 'package', 'output='])
49 if o== '--help' or o == '-h':
51 elif o == '-d' or o == '--datadir':
53 elif o == '-o' or o =='--output':
61 sys.stderr.write ('[%s' % f)
68 sys.stderr.write ('can\'t open file %s\n ' % f)
71 sys.stderr.write (']')
73 sys.stderr.write ('gulped empty file: %s\n'% f)
79 font_name = os.path.basename (os.path.splitext (mf)[0])
81 output_name = font_name + '.pfa'
84 sys.stderr.write ('Font: %s\n'% font_name)
87 f.write ('%!PS-AdobeFont-3.0: ' + font_name + '\n')
88 f.write ('%%%%Creator: %s-%s\n' % (name, version))
92 /FontType 3 def %% Required elements of font
93 /FontName /%s def""" % font_name)
95 /FontMatrix [.083 0 0 .083 0 0] def %% why .83?
96 /FontBBox [-1000 -1000 1000 1000] def %% does not seem to matter.
97 /Encoding 256 array def %% Trivial encoding vector
98 0 1 255 {Encoding exch /.notdef put} for
102 /BuildGlyph { % Stack contains: font charname
104 -1000 -1000 1000 1000 % Bounding Box
106 exch /CharProcs get exch % Get CharProcs dictionary
107 2 copy known not {pop /.notdef} if % See if charname is known
108 get exec % Execute character procedure
111 /BuildChar { % Level 1 compatibility
112 1 index /Encoding get exch get
113 1 index /BuildGlyph get exec
117 end % of font dictionary
121 f.write ('/%s\n' % font_name)
123 'exch definefont pop % Define the font\n')
126 suspect_re = re.compile ('closepath (.*?) 1 setgray newpath (.*?) closepath fill')
129 sys.stderr.write ('[')
133 suffixes = [".[0-9]", ".[0-9][0-9]", ".[0-9][0-9][0-9]"]
135 files = files + find.find (font_name + suf)
138 # concat all files into charprocs.
139 charprocs = ' /.notdef {} def\n'
143 s = re.sub ('%[^\n]*\n', '', s)
145 # if you want readable stuff, look at MP output.
146 s = re.sub ('[\n\t ]+', ' ', s)
147 s = re.sub ('showpage', '', s)
149 # MP's implementation of unfill confuses GS.
150 # Look for "Metapost & setgray" on deja.com
151 # we do some twiddling to use eofill i.s.o. fill
152 if re.search ('setgray',s ):
153 m = suspect_re.search (s)
158 # be complicated, in case of gsave/grestore.
159 # vill as quick hack to avoid duple substitutions.
160 fill = re.sub ('fill', 'eovill', fill, count = 1)
161 s = re.sub (m.group (0), ' %s %s ' % (path, fill), s)
162 m = suspect_re.search (s)
164 s = re.sub ('eovill' , 'eofill', s)
165 s = re.sub ('0 setgray' ,'', s)
168 m = re.match ('.*\.([0-9]+)',i)
169 n = atoi (m.group (1))
171 s = '\n /%s-%d{\n%s} bind def\n' % (font_name, n, s)
172 encoding = encoding + 'Encoding %d /%s-%d put\n' % (n, font_name, n)
173 charprocs = charprocs + s
176 '/CharProcs 3 dict def % Subsidiary dictiorary for\n'
177 'CharProcs begin % individual character definitions\n')
180 f.write ('end % of CharProcs\n')
183 sys.stderr.write (']')
186 ps_file = open (output_name, 'w')
190 sys.stderr.write ('\n')
192 sys.stderr.write ('Wrote PostScript font: %s\n' % output_name)