]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/ps-to-pfa.py
2bbd2813da258a7cefd8b115f9bb1f4ebc8ffeb8
[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 name = 'ps-to-pfa'
10 version = '0.2'
11
12 outdir = 'out/'
13 datadir = ''
14
15 import os
16 import sys
17
18 import getopt
19 from string import *
20 import regex
21 import regsub
22 import time
23
24 def program_id ():
25     return name + ' version ' + version;
26
27 def identify ():
28     sys.stdout.write (program_id () + '\n')
29
30 def help ():
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"
33                       + "Options:\n"
34                       + "  -d, --datadir=DIR      use DIR for ps header/footer\n"
35                       + "  -h, --help             print this help\n"
36                       % (program_name)
37                       )
38     sys.exit (0)
39
40 identify ()
41 (options, files) = getopt.getopt (
42     sys.argv[1:], 'd:', ['help', 'package'])
43 for opt in options:
44     o = opt[0]
45     a = opt[1]
46     if o== '--help' or o == '-h':
47         help ()
48     elif o == '-d' or o == '--datadir':
49         datadir = a
50     else:
51         print o
52         raise getopt.error
53
54
55 def gulp_file (f):
56         sys.stderr.write ('[%s' % f)
57         try:
58                 i = open (f)
59                 i.seek (0, 2)
60                 n = i.tell ()
61                 i.seek (0,0)
62         except:
63                 sys.stderr.write ('can\'t open file %s\n ' % f)
64                 return ''
65         s = i.read (n)
66         sys.stderr.write (']')
67         if len (s) <= 0:
68                 sys.stderr.write ('gulped empty file: %s\n'% f)
69         return s
70
71 mf = files[0]
72 # urg ?
73 font = os.path.basename (os.path.splitext (mf)[0])
74 sys.stderr.write ('Font: %s\n'% font)
75
76 def header (f):
77         f.write ('%!PS-AdobeFont-3.0: ' + font + '\n')
78         f.write ('%%%%Creator: %s-%s\n' % (name, version))
79         f.write ('\n')
80         f.write ('/setgray { 1 add } bind def\n'
81                 '\n'
82 '8 dict begin\n'
83 '/FontType 3 def                             %% Required elements of font\n'
84 '/FontName /%s def\n'
85 '/FontMatrix [.001 0 0 .001 0 0] def\n'
86 '%%/FontMatrix [.01 0 0 .01 0 0] def\n'
87 '%%/FontMatrix [0.1 0 0 0.1 0 0] def\n'
88 '%%/FontBBox [-1000 -1000 1000 1000] def\n'
89 '/FontBBox [-3000 -3000 3000 3000] def\n'
90 '%%/FontBBox [-300 -300 300 300] def\n'
91 '%%/FontBBox [-30 -30 30 30] def\n'
92 '\n'
93 '/Encoding 256 array def                     %% Trivial encoding vector\n'
94 '0 1 255 {Encoding exch /.notdef put} for\n' % (font))
95
96 def footer (f):
97         f.write ('\n'
98 '/BuildGlyph {                               % Stack contains: font charname\n'
99 '%  1000 0                                   % Width\n'
100 '%  -1000 -1000 1000 1000                    % Bounding Box\n'
101 '%  -750 -750 750 750                        % Bounding Box\n'
102 '%  -750 -750 750 750                        % Bounding Box\n'
103 '   3000 0                                   % Width\n'
104 '   -3000 -3000 3000 3000                    % Bounding Box\n'
105 '%  300 0                                    % Width\n'
106 '%  -300 -300 300 300                        % Bounding Box\n'
107 '%  30 0                                     % Width\n'
108 '%  -30 -30 30 30                            % Bounding Box\n'
109 '  setcachedevice\n'
110 '  exch /CharProcs get exch                  % Get CharProcs dictionary\n'
111 '  2 copy known not {pop /.notdef} if        % See if charname is known\n'
112 '  get exec                                  % Execute character procedure\n'
113 '} bind def\n'
114 '\n'
115 '/BuildChar {                                % Level 1 compatibility\n'
116 '  1 index /Encoding get exch get\n'
117 '  1 index /BuildGlyph get exec\n'
118 '} bind def\n'
119 '\n'
120 'currentdict\n'
121 'end                                         % of font dictionary\n')
122         f.write ('\n')
123         f.write ('/%s\n' % font)
124         f.write (''
125 'exch definefont pop                         % Define the font\n')
126
127 def characters (f):
128         #urg
129         # chars = os.listdir ()
130         # chars.sort ()
131         sys.stderr.write ('[')
132         pipe = os.popen ('/bin/ls -1 ' + font + '.[0-9] ' + font + '.[0-9][0-9] ' + font + '.[0-9][0-9][0-9] 2> /dev/null')
133         chars = []
134         i = pipe.readline ()
135         while i:
136                 chars.append (i[0:len (i)-1])
137                 i = pipe.readline ()
138         f.write ('\n'
139 '/CharProcs 3 dict def                       % Subsidiary dictiorary for\n'
140 'CharProcs begin                             % individual character definitions\n')
141         charprocs = '  /.notdef {} def\n'
142         encoding = ''
143         for i in chars: 
144                 s = gulp_file (i)
145                 s = regsub.gsub ('^%.*\n', '', s)
146                 s = regsub.gsub ('^showpage\n', '', s)
147                 s = regsub.gsub ('^', '    ', s)
148                 n = atoi (regsub.gsub ('.*\.', '', i))
149                 s = '\n  /%s-%d{\n%s} bind def\n' % (font, n, s)
150                 encoding = encoding + 'Encoding %d /%s-%d put\n' % (n, font, n)
151                 charprocs = charprocs + s
152         f.write (charprocs)
153         f.write ('\n')
154         f.write ('end                                         % of CharProcs\n')
155         f.write (encoding)
156         f.write ('\n')
157         sys.stderr.write (']')
158
159 ps = outdir + font + '.pfa'
160 ps_file = open (ps, 'w')
161 header (ps_file)
162 characters (ps_file)
163 footer (ps_file)
164 sys.stderr.write ('\n')
165 ps_file.close ()
166 sys.stderr.write ('Wrote PostScript font: %s\n'% ps)
167