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.
27 return name + ' version ' + version;
30 sys.stdout.write (program_id () + '\n')
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"
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"
46 (options, files) = getopt.getopt (
47 sys.argv[1:], 'o:d:', ['help', 'package', 'output='])
51 if o== '--help' or o == '-h':
53 elif o == '-d' or o == '--datadir':
55 elif o == '-o' or o =='--output':
63 logfile.write ('[%s' % f)
70 logfile.write ('can\'t open file %s\n ' % f)
75 logfile.write ('gulped empty file: %s\n'% f)
81 font_name = os.path.basename (os.path.splitext (mf)[0])
83 output_name = font_name + '.pfa'
86 logfile.write ('Font: %s\n'% font_name)
89 f.write ('%!PS-AdobeFont-3.0: ' + font_name + '\n')
90 f.write ('%%%%Creator: %s-%s\n' % (name, version))
94 /FontType 3 def %% Required elements of font
95 /FontName /%s def""" % font_name)
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
104 /BuildGlyph { % Stack contains: font charname
106 -1000 -1000 1000 1000 % Bounding Box
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
113 /BuildChar { % Level 1 compatibility
114 1 index /Encoding get exch get
115 1 index /BuildGlyph get exec
119 end % of font dictionary
123 f.write ('/%s\n' % font_name)
125 'exch definefont pop % Define the font\n')
127 suspect_re = re.compile ('closepath ((gsave )*fill( grestore stroke)*) 1 setgray newpath (.*?) closepath fill')
134 suffixes = [".[0-9]", ".[0-9][0-9]", ".[0-9][0-9][0-9]"]
136 files = files + find.find (font_name + suf)
139 # concat all files into charprocs.
140 charprocs = ' /.notdef {} def\n'
144 s = re.sub ('%[^\n]*\n', '', s)
146 # if you want readable stuff, look at MP output.
147 s = re.sub ('[\n\t ]+', ' ', s)
148 s = re.sub ('showpage', '', s)
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)
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)
165 s = re.sub ('eovill' , 'eofill', s)
166 s = re.sub ('0 setgray' ,'', s)
169 m = re.match ('.*\.([0-9]+)',i)
170 n = atoi (m.group (1))
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
177 '/CharProcs 3 dict def % Subsidiary dictiorary for\n'
178 'CharProcs begin % individual character definitions\n')
181 f.write ('end % of CharProcs\n')
187 ps_file = open (output_name, 'w')
193 logfile.write ('Wrote PostScript font: %s\n' % output_name)