]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/ps-to-pfa.py
release: 1.1.45
[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.3'
13
14 datadir = ''
15
16 import os
17 import sys
18
19 import getopt
20 from string import *
21 import regsub
22
23 # todo, port: see http://starship.skyport.net/crew/amk/regex/regex-to-re.html
24 import re
25 import time
26
27 def program_id ():
28     return name + ' version ' + version;
29
30 def identify ():
31     sys.stdout.write (program_id () + '\n')
32
33 def help ():
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"
36                       + "Options:\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"
40                       % (program_name)
41                       )
42     sys.exit (0)
43
44 output_name = ''
45
46 identify ()
47 (options, files) = getopt.getopt (
48     sys.argv[1:], 'o:d:', ['help', 'package', 'output='])
49 for opt in options:
50     o = opt[0]
51     a = opt[1]
52     if o== '--help' or o == '-h':
53         help ()
54     elif o == '-d' or o == '--datadir':
55         datadir = a
56     elif o == '-o' or o =='--output':
57         output_name = a
58     else:
59         print o
60         raise getopt.error
61
62
63 def gulp_file (f):
64         sys.stderr.write ('[%s' % f)
65         try:
66                 i = open (f)
67                 i.seek (0, 2)
68                 n = i.tell ()
69                 i.seek (0,0)
70         except:
71                 sys.stderr.write ('can\'t open file %s\n ' % f)
72                 return ''
73         s = i.read (n)
74         sys.stderr.write (']')
75         if len (s) <= 0:
76                 sys.stderr.write ('gulped empty file: %s\n'% f)
77         return s
78
79 mf = files[0]
80
81 input_name = mf
82 font_name = os.path.basename (os.path.splitext (mf)[0])
83 if not output_name:
84     output_name  = font_name + '.pfa'
85
86
87 sys.stderr.write ('Font: %s\n'% font_name)
88
89 def header (f):
90         f.write ('%!PS-AdobeFont-3.0: ' + font_name + '\n')
91         f.write ('%%%%Creator: %s-%s\n' % (name, version))
92         f.write ('\n')
93         f.write ('/setgray { 1 add } bind def\n'
94                 '\n'
95 '8 dict begin\n'
96 '/FontType 3 def                             %% Required elements of font\n'
97 '/FontName /%s def\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'
105 '\n'
106 '/Encoding 256 array def                     %% Trivial encoding vector\n'
107 '0 1 255 {Encoding exch /.notdef put} for\n' % (font_name))
108
109 def footer (f):
110         f.write ('\n'
111 '/BuildGlyph {                               % Stack contains: font charname\n'
112 '%  1000 0                                   % Width\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'
116 '   3000 0                                   % Width\n'
117 '   -3000 -3000 3000 3000                    % Bounding Box\n'
118 '%  300 0                                    % Width\n'
119 '%  -300 -300 300 300                        % Bounding Box\n'
120 '%  30 0                                     % Width\n'
121 '%  -30 -30 30 30                            % Bounding Box\n'
122 '  setcachedevice\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'
126 '} bind def\n'
127 '\n'
128 '/BuildChar {                                % Level 1 compatibility\n'
129 '  1 index /Encoding get exch get\n'
130 '  1 index /BuildGlyph get exec\n'
131 '} bind def\n'
132 '\n'
133 'currentdict\n'
134 'end                                         % of font dictionary\n')
135         f.write ('\n')
136         f.write ('/%s\n' % font_name)
137         f.write (''
138 'exch definefont pop                         % Define the font\n')
139
140 def characters (f):
141         #urg
142         # chars = os.listdir ()
143         # chars.sort ()
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')
146         chars = []
147         i = pipe.readline ()
148         while i:
149                 chars.append (i[0:len (i)-1])
150                 i = pipe.readline ()
151         f.write ('\n'
152 '/CharProcs 3 dict def                       % Subsidiary dictiorary for\n'
153 'CharProcs begin                             % individual character definitions\n')
154         charprocs = '  /.notdef {} def\n'
155         encoding = ''
156         for i in chars: 
157                 s = gulp_file (i)
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
165         f.write (charprocs)
166         f.write ('\n')
167         f.write ('end                                         % of CharProcs\n')
168         f.write (encoding)
169         f.write ('\n')
170         sys.stderr.write (']')
171
172
173 ps_file = open (output_name, 'w')
174 header (ps_file)
175 characters (ps_file)
176 footer (ps_file)
177 sys.stderr.write ('\n')
178 ps_file.close ()
179 sys.stderr.write ('Wrote PostScript font: %s\n'% output_name)
180