]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/make-font-dir.py
Fix.
[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 sodipodi_p = 0
137 if len (ls) and ls[0] == 'sodipodi':
138         ls = ls[1:]
139         sodipodi_p = 1
140 elif len (ls) and ls[0] == 'sketch':
141         ls = ls[1:]
142         sketch_p = 1
143
144 if not (sketch_p or sodipodi_p):
145         print len(ls)
146         
147 for filename in ls:
148         basename = re.sub ('\.pf[ab]', '',filename)     
149         fontname = re.sub ('-', ' ',basename)
150
151         m = re.search ("([0-9]+)$", fontname)
152         designsize = 'normal'
153
154         
155         if m:
156                 designsize =  m.group (1)
157                 fontbase = re.sub ("([0-9]+)$", "", fontname)
158                 
159
160         # FIXME: Font naming  -- what a mess
161         # Check sane naming with xfontsel and gtkfontsel
162
163         # Adobe's font naming scheme and X11's seem to be conflicting.
164         # Adobe's FontFamily seems to be X11's family + weight
165         # Also, text selection applets like gtkfontsel, gfontview and
166         # GNOME-applications specific ones, display X11's `family'
167         # parameter as `Font', and X11's `Weight' parameter as `Style'.
168
169         # Using X11 description/convention -- good for xfontsel:
170         #  1 foundry: GNU
171         #  2 family: LilyPond <basename>
172         #  3 weight: <designsize>
173         #  4 slant: r(oman) =upright
174         #  5 setwidth: normal
175         #  6 style:
176         #  7 pixelsize: 0
177         #  8 pointsize: 0 (20 crashes xfs, moved to style)
178         #  9 xresolution: 0
179         # 10 yresolution: 0
180         # 11 spacing: p(roportional)
181         # 12 averagewidth: 0
182         # 13 registry: GNU
183         # 14 encoding: fonstpecific
184
185         # gives:
186         # feta20.pfa -GNU-LilyPond feta-20-r-normal--0-0-0-0-p-0-gnu-fontspecific
187
188         # However, GNOME (gtkfontsel, gnome apps) seems to want:
189
190         #  1 foundry: GNU
191         #  2 family: LilyPond
192         #  3 weight:  <basename>
193         #  4 slant: r(oman) =upright
194         #  5 setwidth: normal
195         #  6 style: <designsize>
196         #  7 pixelsize: 0
197         #  8 pointsize: 0 (20 crashes xfs, moved to style)
198         #  9 xresolution: 0
199         # 10 yresolution: 0
200         # 11 spacing: p(roportional)
201         # 12 averagewidth: 0
202         # 13 registry: GNU
203         # 14 encoding: fonstpecific
204
205         # which gives:
206         # feta20.pfa -GNU-LilyPond-feta-r-normal--20-0-0-0-p-0-gnu-fontspecific
207         # foundry: GNU
208         
209         ## ouch, pointsize 20 crashes xfs
210         ## XXXfeta20.pfa -GNU-LilyPond Feta-regular-r-normal--0-20-0-0-p-0-gnu-fontspecific
211
212         ## feta20.pfa -GNU-LilyPond feta-regular-r-normal-20-0-0-0-0-p-0-gnu-fontspecific
213
214         afmfile = ''
215         if not afmfile:
216                 #afmfile = find_file (basename + '.afm')
217                 afmfile = basename + '.afm'
218                 
219         if afmfile:
220                 afmfile = os.path.abspath (afmfile)
221         if os.path.exists (afmfile):    
222                 afm = read_afm_file (afmfile)
223                 fontinfo = Font_info (afm.__dict__)
224         else:
225                 fontinfo = Font_info (basename)
226                 
227         family_name = string.join (string.split (fontinfo['FamilyName'],
228                                                         '-'), ' ')
229
230         if sodipodi_p:
231                 print string.join ((os.path.abspath (filename),
232                                     fontinfo.FamilyName,
233                                     fontinfo.FamilyName,''
234                                     ),
235                                    
236                                    ',')
237                                    
238         elif sketch_p:
239                 # Sketch's lilypond.sfd map:
240                 s = string.join ([fontinfo.FontName,
241                                   fontinfo.family,
242                                   '%s %s' % (fontinfo.weight, fontinfo.style),
243                                   string.join (fontinfo.get_X11 ()[:4], '-'),
244                                   string.join (fontinfo.get_X11 ()[-2:], '-'),
245                                   fontinfo.name],
246                                  ',')
247                 print s
248
249                 s = string.join ([fontinfo.FamilyName + fontinfo.designsize,
250                                   fontinfo.family,
251                                   '%s %s' % (fontinfo.weight, fontinfo.style),
252                                   string.join (fontinfo.get_X11 ()[:4], '-'),
253                                   string.join (fontinfo.get_X11 ()[-2:], '-'),
254                                   fontinfo.name],
255                                  ',')
256                 print s
257         else:
258                 print filename + ' -' + string.join (fontinfo.get_X11 (), '-')