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