]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/make-font-dir.py
* scripts/ly2dvi.py: Add paper sizes.
[lilypond.git] / buildscripts / make-font-dir.py
1 #!@PYTHON
2
3
4
5 ## make a fonts.scale file.
6
7
8 import re
9 import sys
10 import string
11 import os
12
13
14 ### mftrace/afm.py
15
16 # Read some global vars 
17 class Afm_reader:
18         def __init__ (self, filename):
19                 self.filename = filename
20                 self.lines = open (self.filename).readlines ()
21
22         def get_afm (self):
23                 afm = Afm_font_metric (self.filename)
24                 for i in self.lines[:20]:
25                         m = re.match ('([^ \t\n]*)[ \t]*(.*[^ \t\n])', i)
26                         if m and m.group (1):
27                                 key = m.group (1)
28                                 value = m.group (2)
29                                 if key != 'Comment':
30                                         afm.__dict__[key] = value
31                 return afm
32
33 class Afm_font_metric:
34         def __init__ (self, filename):
35                 m = re.match ('.*/(.+)', filename)
36                 self.filename = m.group (1)
37                 m = re.match ('([-_A-Za-z]*)([0-9]*)', self.filename)
38                 self.name = m.group (1) + m.group (2)
39                 self.basename = m.group (1)
40                 self.designsize = m.group (2)
41         
42 def read_afm_file (filename):
43         reader = Afm_reader (filename)
44         return reader.get_afm ()
45
46 #if __name__ == '__main__':
47 #       i = read_afm_file  (sys.argv[1])
48 #       print i, i.FullName, i.FontName
49
50 ### mftrace
51
52 class Font_info:
53         cm = {
54                 'bx': ('bold', 'roman'),
55                 'bxti' : ('bold', 'italic'),
56                 'csc' : ('smallcaps', 'roman'),
57                 'r' : ('regular', 'roman'),
58                 'tt' : ('regular', 'typewriter'),
59                 'ti' : ('regular', 'italic'),
60                 }
61               
62         def set_defaults (self, name):
63                 self.FontName = name
64                 self.FullName = name
65                 self.EncodingScheme = 'AdobeStandard'
66                 
67                 self.foundry = 'GNU'
68                 self.family = 'LilyPond'
69                 self.weight = 'Feta'
70                 self.slant = 'r'
71                 self.setwidth = 'normal'
72                 self.style = ''
73                 self.pixelsize = '0'
74                 self.pointsize = '0'
75                 self.xresolution = '0'
76                 self.yresolution = '0'
77                 self.spacing = 'p'
78                 self.averagewidth = '0'
79                 self.registry = 'GNU'
80                 self.encoding = 'FontSpecific'
81
82                 split = string.split (name, '-')
83                 if len (split) >= 4:
84                         # Assume
85                         #   Adobe FontName = X11 foundry-family-weight-style
86                         if 1:
87                                 self.foundry, self.family = split[:2]
88                         else: # testin'
89                                 self.foundry = split[0]
90                                 self.family = string.join (split[1:-2], ' ')
91                         self.weight = string.join (split[2:-1], ' ')
92                         self.style = split[-1:][0]
93                         self.FamilyName = '%s %s' % (self.family, self.weight)
94                         self.designsize = self.style
95                 elif name[:2] == 'cm':
96                         self.foundry = 'TeX' # Knuth?
97                         self.FamilyName = 'Computer Modern'
98                         self.family = self.FamilyName
99                         m = re.match ('^cm([a-z]*)([0-9]*)', name)
100                         self.weight = string.join (self.cm[m.group (1)], ' ')
101                         self.designsize = m.group (2)
102                         self.style = self.designsize
103                 else:
104                         self.FamilyName = name
105
106         def __init__ (self, x):
107                 if type (x) == type ("hallo"):
108                         m = re.match ('([-_A-Za-z]*)([0-9]*)', x)
109                         self.name = x
110                         self.basename = m.group (1)
111                         self.designsize = m.group (2)
112                         self.set_defaults (x)
113                 elif type (x) == type ({}):
114                         self.set_defaults (x['FontName'])
115                         for k in x.keys ():
116                                 self.__dict__[k] = x[k]
117
118         def __getitem__ (self, key):
119                 return self.__dict__[key]
120
121         def get_X11 (self):
122                 return (self.foundry, self.family, self.weight,
123                         self.slant, self.setwidth, self.style,
124                         self.pixelsize, self.pointsize,
125                         self.xresolution, self.yresolution,
126                         self.spacing, self.averagewidth,
127                         self.registry, self.encoding)
128
129 fontinfo = {}
130
131 # wat een intervaas...
132 ls =  sys.stdin.readline ()
133 ls = string.split (ls)
134
135 sketch_p = 0
136 if len (ls) and ls[0] == 'sketch':
137         ls = ls[1:]
138         sketch_p = 1
139
140 if not sketch_p:
141         print len(ls)
142         
143 for filename in ls:
144         basename = re.sub ('\.pf[ab]', '',filename)     
145         fontname = re.sub ('-', ' ',basename)
146
147         m = re.search ("([0-9]+)$", fontname)
148         designsize = 'normal'
149
150         
151         if m:
152                 designsize =  m.group (1)
153                 fontbase = re.sub ("([0-9]+)$", "", fontname)
154                 
155
156         # FIXME: Font naming  -- what a mess
157         # Check sane naming with xfontsel and gtkfontsel
158
159         # Adobe's font naming scheme and X11's seem to be conflicting.
160         # Adobe's FontFamily seems to be X11's family + weight
161         # Also, text selection applets like gtkfontsel, gfontview and
162         # GNOME-applications specific ones, display X11's `family'
163         # parameter as `Font', and X11's `Weight' parameter as `Style'.
164
165         # Using X11 description/convention -- good for xfontsel:
166         #  1 foundry: GNU
167         #  2 family: LilyPond <basename>
168         #  3 weight: <designsize>
169         #  4 slant: r(oman) =upright
170         #  5 setwidth: normal
171         #  6 style:
172         #  7 pixelsize: 0
173         #  8 pointsize: 0 (20 crashes xfs, moved to style)
174         #  9 xresolution: 0
175         # 10 yresolution: 0
176         # 11 spacing: p(roportional)
177         # 12 averagewidth: 0
178         # 13 registry: GNU
179         # 14 encoding: fonstpecific
180
181         # gives:
182         # feta20.pfa -GNU-LilyPond feta-20-r-normal--0-0-0-0-p-0-gnu-fontspecific
183
184         # However, GNOME (gtkfontsel, gnome apps) seems to want:
185
186         #  1 foundry: GNU
187         #  2 family: LilyPond
188         #  3 weight:  <basename>
189         #  4 slant: r(oman) =upright
190         #  5 setwidth: normal
191         #  6 style: <designsize>
192         #  7 pixelsize: 0
193         #  8 pointsize: 0 (20 crashes xfs, moved to style)
194         #  9 xresolution: 0
195         # 10 yresolution: 0
196         # 11 spacing: p(roportional)
197         # 12 averagewidth: 0
198         # 13 registry: GNU
199         # 14 encoding: fonstpecific
200
201         # which gives:
202         # feta20.pfa -GNU-LilyPond-feta-r-normal--20-0-0-0-p-0-gnu-fontspecific
203         # foundry: GNU
204         
205         ## ouch, pointsize 20 crashes xfs
206         ## XXXfeta20.pfa -GNU-LilyPond Feta-regular-r-normal--0-20-0-0-p-0-gnu-fontspecific
207
208         ## feta20.pfa -GNU-LilyPond feta-regular-r-normal-20-0-0-0-0-p-0-gnu-fontspecific
209
210         afmfile = ''
211         if not afmfile:
212                 #afmfile = find_file (basename + '.afm')
213                 afmfile = basename + '.afm'
214                 
215         if afmfile:
216                 afmfile = os.path.abspath (afmfile)
217         if os.path.exists (afmfile):    
218                 afm = read_afm_file (afmfile)
219                 fontinfo = Font_info (afm.__dict__)
220         else:
221                 fontinfo = Font_info (basename)
222                 
223         family_name = string.join (string.split (fontinfo['FamilyName'],
224                                                         '-'), ' ')
225         if not sketch_p:
226                 print filename + ' -' + string.join (fontinfo.get_X11 (), '-')
227                 
228         else:
229                 # Sketch's lilypond.sfd map:
230                 s = string.join ([fontinfo.FontName,
231                                   fontinfo.family,
232                                   '%s %s' % (fontinfo.weight, fontinfo.style),
233                                   string.join (fontinfo.get_X11 ()[:4], '-'),
234                                   string.join (fontinfo.get_X11 ()[-2:], '-'),
235                                   fontinfo.name],
236                                  ',')
237                 print s
238
239                 s = string.join ([fontinfo.FamilyName + fontinfo.designsize,
240                                   fontinfo.family,
241                                   '%s %s' % (fontinfo.weight, fontinfo.style),
242                                   string.join (fontinfo.get_X11 ()[:4], '-'),
243                                   string.join (fontinfo.get_X11 ()[-2:], '-'),
244                                   fontinfo.name],
245                                  ',')
246                 print s