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