]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/make-font-dir.py
be2be389d502ea627b01a9d77387748dccdba2f2
[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                         self.foundry, self.family = split[:2]
87                         self.weight = string.join (split[2:-1], ' ')
88                         self.style = split[-1:][0]
89                         self.FamilyName = '%s %s' % (self.family, self.weight)
90                         self.designsize = self.style
91                 elif name[:2] == 'cm':
92                         self.foundry = 'TeX' # Knuth?
93                         self.FamilyName = 'Computer Modern'
94                         self.family = self.FamilyName
95                         m = re.match ('^cm([a-z]*)([0-9]*)', name)
96                         self.weight = string.join (self.cm[m.group (1)], ' ')
97                         self.designsize = m.group (2)
98                         self.style = self.designsize
99                 else:
100                         self.FamilyName = name
101
102         def __init__ (self, x):
103                 if type (x) == type ("hallo"):
104                         m = re.match ('([-_A-Za-z]*)([0-9]*)', x)
105                         self.name = x
106                         self.basename = m.group (1)
107                         self.designsize = m.group (2)
108                         self.set_defaults (x)
109                 elif type (x) == type ({}):
110                         self.set_defaults (x['FontName'])
111                         for k in x.keys ():
112                                 self.__dict__[k] = x[k]
113
114         def __getitem__ (self, key):
115                 return self.__dict__[key]
116
117         def get_X11 (self):
118                 return (self.foundry, self.family, self.weight,
119                         self.slant, self.setwidth, self.style,
120                         self.pixelsize, self.pointsize,
121                         self.xresolution, self.yresolution,
122                         self.spacing, self.averagewidth,
123                         self.registry, self.encoding)
124
125 fontinfo = {}
126
127 # wat een intervaas...
128 ls =  sys.stdin.readline ()
129 ls = string.split (ls)
130
131 sketch_p = 0
132 if len (ls) and ls[0] == 'sketch':
133         ls = ls[1:]
134         sketch_p = 1
135
136 if not sketch_p:
137         print len(ls)
138         
139 for filename in ls:
140         basename = re.sub ('\.pf[ab]', '',filename)     
141         fontname = re.sub ('-', ' ',basename)
142
143         m = re.search ("([0-9]+)$", fontname)
144         designsize = 'normal'
145
146         
147         if m:
148                 designsize =  m.group (1)
149                 fontbase = re.sub ("([0-9]+)$", "", fontname)
150                 
151
152         # FIXME: Font naming  -- what a mess
153         # Check sane naming with xfontsel and gtkfontsel
154
155         # Adobe's font naming scheme and X11's seem to be conflicting.
156         # Adobe's FontFamily seems to be X11's family + weight
157         # Also, text selection applets like gtkfontsel, gfontview and
158         # GNOME-applications specific ones, display X11's `family'
159         # parameter as `Font', and X11's `Weight' parameter as `Style'.
160
161         # Using X11 description/convention -- good for xfontsel:
162         #  1 foundry: GNU
163         #  2 family: LilyPond <basename>
164         #  3 weight: <designsize>
165         #  4 slant: r(oman) =upright
166         #  5 setwidth: normal
167         #  6 style:
168         #  7 pixelsize: 0
169         #  8 pointsize: 0 (20 crashes xfs, moved to style)
170         #  9 xresolution: 0
171         # 10 yresolution: 0
172         # 11 spacing: p(roportional)
173         # 12 averagewidth: 0
174         # 13 registry: GNU
175         # 14 encoding: fonstpecific
176
177         # gives:
178         # feta20.pfa -GNU-LilyPond feta-20-r-normal--0-0-0-0-p-0-gnu-fontspecific
179
180         # However, GNOME (gtkfontsel, gnome apps) seems to want:
181
182         #  1 foundry: GNU
183         #  2 family: LilyPond
184         #  3 weight:  <basename>
185         #  4 slant: r(oman) =upright
186         #  5 setwidth: normal
187         #  6 style: <designsize>
188         #  7 pixelsize: 0
189         #  8 pointsize: 0 (20 crashes xfs, moved to style)
190         #  9 xresolution: 0
191         # 10 yresolution: 0
192         # 11 spacing: p(roportional)
193         # 12 averagewidth: 0
194         # 13 registry: GNU
195         # 14 encoding: fonstpecific
196
197         # which gives:
198         # feta20.pfa -GNU-LilyPond-feta-r-normal--20-0-0-0-p-0-gnu-fontspecific
199         # foundry: GNU
200         
201         ## ouch, pointsize 20 crashes xfs
202         ## XXXfeta20.pfa -GNU-LilyPond Feta-regular-r-normal--0-20-0-0-p-0-gnu-fontspecific
203
204         ## feta20.pfa -GNU-LilyPond feta-regular-r-normal-20-0-0-0-0-p-0-gnu-fontspecific
205
206         afmfile = ''
207         if not afmfile:
208                 #afmfile = find_file (basename + '.afm')
209                 afmfile = basename + '.afm'
210                 
211         if afmfile:
212                 afmfile = os.path.abspath (afmfile)
213         if os.path.exists (afmfile):    
214                 afm = read_afm_file (afmfile)
215                 fontinfo = Font_info (afm.__dict__)
216         else:
217                 fontinfo = Font_info (basename)
218                 
219         family_name = string.join (string.split (fontinfo['FamilyName'],
220                                                         '-'), ' ')
221         if not sketch_p:
222                 print filename + ' -' + string.join (fontinfo.get_X11 (), '-')
223                 
224         else:
225                 # Sketch's lilypond.sfd map:
226                 s = string.join ([fontinfo.FontName,
227                                   fontinfo.family,
228                                   '%s %s' % (fontinfo.weight, fontinfo.style),
229                                   string.join (fontinfo.get_X11 ()[:4], '-'),
230                                   string.join (fontinfo.get_X11 ()[-2:], '-'),
231                                   fontinfo.name],
232                                  ',')
233                 print s
234
235                 s = string.join ([fontinfo.FamilyName + fontinfo.designsize,
236                                   fontinfo.family,
237                                   '%s %s' % (fontinfo.weight, fontinfo.style),
238                                   string.join (fontinfo.get_X11 ()[:4], '-'),
239                                   string.join (fontinfo.get_X11 ()[-2:], '-'),
240                                   fontinfo.name],
241                                  ',')
242                 print s