]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/ps-to-pfa.py
33ee75ddec096632e521420a952811918e1ec256
[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 '%  -750 -750 750 750                        % Bounding Box\n'
101 '  3000 0                                    % Width\n'
102 '  -3000 -3000 3000 3000                     % Bounding Box\n'
103 '%  300 0                                    % Width\n'
104 '%  -300 -300 300 300                        % Bounding Box\n'
105 '%  30 0                                     % Width\n'
106 '%  -30 -30 30 30                            % Bounding Box\n'
107 '  setcachedevice\n'
108 '  exch /CharProcs get exch                  % Get CharProcs dictionary\n'
109 '  2 copy known not {pop /.notdef} if        % See if charname is known\n'
110 '  get exec                                  % Execute character procedure\n'
111 '} bind def\n'
112 '\n'
113 '/BuildChar {                                % Level 1 compatibility\n'
114 '  1 index /Encoding get exch get\n'
115 '  1 index /BuildGlyph get exec\n'
116 '} bind def\n'
117 '\n'
118 'currentdict\n'
119 'end                                         % of font dictionary\n')
120         f.write ('\n')
121         f.write ('/%s\n' % font)
122         f.write (''
123 'exch definefont pop                         % Define the font\n')
124
125 def characters (f):
126         #urg
127         # chars = os.listdir ()
128         # chars.sort ()
129         sys.stderr.write ('[')
130         pipe = os.popen ('/bin/ls -1 ' + font + '.[0-9] ' + font + '.[0-9][0-9] ' + font + '.[0-9][0-9][0-9] 2> /dev/null')
131         chars = []
132         i = pipe.readline ()
133         while i:
134                 chars.append (i[0:len (i)-1])
135                 i = pipe.readline ()
136         f.write ('\n'
137 '/CharProcs 3 dict def                       % Subsidiary dictiorary for\n'
138 'CharProcs begin                             % individual character definitions\n')
139         charprocs = '  /.notdef {} def\n'
140         encoding = ''
141         for i in chars: 
142                 s = gulp_file (i)
143                 s = regsub.gsub ('^%.*\n', '', s)
144                 s = regsub.gsub ('^showpage\n', '', s)
145                 s = regsub.gsub ('^', '    ', s)
146                 n = atoi (regsub.gsub ('.*\.', '', i))
147                 s = '\n  /%s-%d{\n%s} bind def\n' % (font, n, s)
148                 encoding = encoding + 'Encoding %d /%s-%d put\n' % (n, font, n)
149                 charprocs = charprocs + s
150         f.write (charprocs)
151         f.write ('\n')
152         f.write ('end                                         % of CharProcs\n')
153         f.write (encoding)
154         f.write ('\n')
155         sys.stderr.write (']')
156
157 ps = outdir + font + '.pfa'
158 ps_file = open (ps, 'w')
159 header (ps_file)
160 characters (ps_file)
161 footer (ps_file)
162 sys.stderr.write ('\n')
163 ps_file.close ()
164 sys.stderr.write ('Wrote PostScript font: %s\n'% ps)
165