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.
23 # todo, port: see http://starship.skyport.net/crew/amk/regex/regex-to-re.html
28 return name + ' version ' + version;
31 sys.stdout.write (program_id () + '\n')
34 sys.stdout.write ("Usage: %s [options] [files]\n"
35 "ps-to-pfa.py -- make PostScript Type 3 font from separate ps char files\n\n"
37 + " -d, --datadir=DIR use DIR for ps header/footer\n"
38 + " -h, --help print this help\n"
39 + " -o, --output=FILE set output file to FILE.\n"
47 (options, files) = getopt.getopt (
48 sys.argv[1:], 'o:d:', ['help', 'package', 'output='])
52 if o== '--help' or o == '-h':
54 elif o == '-d' or o == '--datadir':
56 elif o == '-o' or o =='--output':
64 sys.stderr.write ('[%s' % f)
71 sys.stderr.write ('can\'t open file %s\n ' % f)
74 sys.stderr.write (']')
76 sys.stderr.write ('gulped empty file: %s\n'% f)
82 font_name = os.path.basename (os.path.splitext (mf)[0])
84 output_name = font_name + '.pfa'
87 sys.stderr.write ('Font: %s\n'% font_name)
90 f.write ('%!PS-AdobeFont-3.0: ' + font_name + '\n')
91 f.write ('%%%%Creator: %s-%s\n' % (name, version))
93 f.write ('/setgray { 1 add } bind def\n'
96 '/FontType 3 def %% Required elements of font\n'
98 '/FontMatrix [.001 0 0 .001 0 0] def\n'
99 '%%/FontMatrix [.01 0 0 .01 0 0] def\n'
100 '%%/FontMatrix [0.1 0 0 0.1 0 0] def\n'
101 '%%/FontBBox [-1000 -1000 1000 1000] def\n'
102 '/FontBBox [-3000 -3000 3000 3000] def\n'
103 '%%/FontBBox [-300 -300 300 300] def\n'
104 '%%/FontBBox [-30 -30 30 30] def\n'
106 '/Encoding 256 array def %% Trivial encoding vector\n'
107 '0 1 255 {Encoding exch /.notdef put} for\n' % (font_name))
111 '/BuildGlyph { % Stack contains: font charname\n'
113 '% -1000 -1000 1000 1000 % Bounding Box\n'
114 '% -750 -750 750 750 % Bounding Box\n'
115 '% -750 -750 750 750 % Bounding Box\n'
117 ' -3000 -3000 3000 3000 % Bounding Box\n'
119 '% -300 -300 300 300 % Bounding Box\n'
121 '% -30 -30 30 30 % Bounding Box\n'
123 ' exch /CharProcs get exch % Get CharProcs dictionary\n'
124 ' 2 copy known not {pop /.notdef} if % See if charname is known\n'
125 ' get exec % Execute character procedure\n'
128 '/BuildChar { % Level 1 compatibility\n'
129 ' 1 index /Encoding get exch get\n'
130 ' 1 index /BuildGlyph get exec\n'
134 'end % of font dictionary\n')
136 f.write ('/%s\n' % font_name)
138 'exch definefont pop % Define the font\n')
142 # chars = os.listdir ()
144 sys.stderr.write ('[')
145 pipe = os.popen ('/bin/ls -1 ' + font_name + '.[0-9] ' + font_name + '.[0-9][0-9] ' + font_name + '.[0-9][0-9][0-9] 2> /dev/null')
149 chars.append (i[0:len (i)-1])
152 '/CharProcs 3 dict def % Subsidiary dictiorary for\n'
153 'CharProcs begin % individual character definitions\n')
154 charprocs = ' /.notdef {} def\n'
158 s = regsub.gsub ('^%.*\n', '', s)
159 s = regsub.gsub ('^showpage\n', '', s)
160 s = regsub.gsub ('^', ' ', s)
161 n = atoi (regsub.gsub ('.*\.', '', i))
162 s = '\n /%s-%d{\n%s} bind def\n' % (font_name, n, s)
163 encoding = encoding + 'Encoding %d /%s-%d put\n' % (n, font_name, n)
164 charprocs = charprocs + s
167 f.write ('end % of CharProcs\n')
170 sys.stderr.write (']')
173 ps_file = open (output_name, 'w')
177 sys.stderr.write ('\n')
179 sys.stderr.write ('Wrote PostScript font: %s\n'% output_name)