This file is indexed.

/usr/share/pyshared/FontTools/fontTools/fondLib.py is in fonttools 2.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import os
import struct, sstruct
import string
try:
	from Carbon import Res
except ImportError:
	import Res


error = "fondLib.error"

DEBUG = 0

headerformat = """
	>
	ffFlags:	h
	ffFamID:	h
	ffFirstChar:	h
	ffLastChar:	h
	ffAscent:	h
	ffDescent:	h
	ffLeading:	h
	ffWidMax:	h
	ffWTabOff:	l
	ffKernOff:	l
	ffStylOff:	l
"""

FONDheadersize = 52

class FontFamily:
	
	def __init__(self, theRes, mode = 'r'):
		self.ID, type, self.name = theRes.GetResInfo()
		if type <> 'FOND':
			raise ValueError, "FOND resource required"
		self.FOND = theRes
		self.mode = mode
		self.changed = 0
		
		if DEBUG:
			self.parsedthings = []
	
	def parse(self):
		self._getheader()
		self._getfontassociationtable()
		self._getoffsettable()
		self._getboundingboxtable()
		self._getglyphwidthtable()
		self._getstylemappingtable()
		self._getglyphencodingsubtable()
		self._getkerningtables()
	
	def minimalparse(self):
		self._getheader()
		self._getglyphwidthtable()
		self._getstylemappingtable()
	
	def __repr__(self):
		return "<FontFamily instance of %s>" % self.name
	
	def getflags(self):
		return self.fondClass
	
	def setflags(self, flags):
		self.changed = 1
		self.fondClass = flags
	
	def save(self, destresfile = None):
		if self.mode <> 'w':
			raise error, "can't save font: no write permission"
		self._buildfontassociationtable()
		self._buildoffsettable()
		self._buildboundingboxtable()
		self._buildglyphwidthtable()
		self._buildkerningtables()
		self._buildstylemappingtable()
		self._buildglyphencodingsubtable()
		rawnames = [	"_rawheader", 
					"_rawfontassociationtable", 
					"_rawoffsettable", 
					"_rawglyphwidthtable", 
					"_rawstylemappingtable", 
					"_rawglyphencodingsubtable",
					"_rawkerningtables"
				]
		for name in rawnames[1:]:	# skip header
			data = getattr(self, name)
			if len(data) & 1:
				setattr(self, name, data + '\0')
		
		self.ffWTabOff = FONDheadersize + len(self._rawfontassociationtable) + len(self._rawoffsettable)
		self.ffStylOff = self.ffWTabOff + len(self._rawglyphwidthtable)
		self.ffKernOff = self.ffStylOff + len(self._rawstylemappingtable) + len(self._rawglyphencodingsubtable)
		self.glyphTableOffset = len(self._rawstylemappingtable)
		
		if not self._rawglyphwidthtable:
			self.ffWTabOff = 0
		if not self._rawstylemappingtable:
			self.ffStylOff = 0
		if not self._rawglyphencodingsubtable:
			self.glyphTableOffset = 0
		if not self._rawkerningtables:
			self.ffKernOff = 0
		
		self._buildheader()
		
		# glyphTableOffset has only just been calculated
		self._updatestylemappingtable()
		
		newdata = ""
		for name in rawnames:
			newdata = newdata + getattr(self, name)
		if destresfile is None:
			self.FOND.data = newdata
			self.FOND.ChangedResource()
			self.FOND.WriteResource()
		else:
			ID, type, name = self.FOND.GetResInfo()
			self.FOND.DetachResource()
			self.FOND.data = newdata
			saveref = Res.CurResFile()
			Res.UseResFile(destresfile)
			self.FOND.AddResource(type, ID, name)
			Res.UseResFile(saveref)
		self.changed = 0
	
	def _getheader(self):
		data = self.FOND.data
		sstruct.unpack(headerformat, data[:28], self)
		self.ffProperty = struct.unpack(">9h", data[28:46])
		self.ffIntl = struct.unpack(">hh", data[46:50])
		self.ffVersion, = struct.unpack(">h", data[50:FONDheadersize])
		
		if DEBUG:
			self._rawheader = data[:FONDheadersize]
			self.parsedthings.append((0, FONDheadersize, 'header'))
	
	def _buildheader(self):
		header = sstruct.pack(headerformat, self)
		header = header + apply(struct.pack, (">9h",) + self.ffProperty)
		header = header + apply(struct.pack, (">hh",) + self.ffIntl)
		header = header + struct.pack(">h", self.ffVersion)
		if DEBUG:
			print "header is the same?", self._rawheader == header and 'yes.' or 'no.'
			if self._rawheader <> header:
				print len(self._rawheader), len(header)
		self._rawheader = header
	
	def _getfontassociationtable(self):
		data = self.FOND.data
		offset = FONDheadersize
		numberofentries, = struct.unpack(">h", data[offset:offset+2])
		numberofentries = numberofentries + 1
		size = numberofentries * 6
		self.fontAssoc = []
		for i in range(offset + 2, offset + size, 6):
			self.fontAssoc.append(struct.unpack(">3h", data[i:i+6]))
		
		self._endoffontassociationtable = offset + size + 2
		if DEBUG:
			self._rawfontassociationtable = data[offset:self._endoffontassociationtable]
			self.parsedthings.append((offset, self._endoffontassociationtable, 'fontassociationtable'))
	
	def _buildfontassociationtable(self):
		data = struct.pack(">h", len(self.fontAssoc) - 1)
		for size, stype, ID in self.fontAssoc:
			data = data + struct.pack(">3h", size, stype, ID)
		
		if DEBUG:
			print "font association table is the same?", self._rawfontassociationtable == data and 'yes.' or 'no.'
			if self._rawfontassociationtable <> data:
				print len(self._rawfontassociationtable), len(data)
		self._rawfontassociationtable = data
	
	def _getoffsettable(self):
		if self.ffWTabOff == 0:
			self._rawoffsettable = ""
			return
		data = self.FOND.data
		# Quick'n'Dirty. What's the spec anyway? Can't find it...
		offset = self._endoffontassociationtable
		count = self.ffWTabOff
		self._rawoffsettable = data[offset:count]
		if DEBUG:
			self.parsedthings.append((offset, count, 'offsettable&bbtable'))
	
	def _buildoffsettable(self):
		if not hasattr(self, "_rawoffsettable"):
			self._rawoffsettable = ""
	
	def _getboundingboxtable(self):
		self.boundingBoxes = None
		if self._rawoffsettable[:6] <> '\0\0\0\0\0\6':  # XXX ????
			return
		boxes = {}
		data = self._rawoffsettable[6:]
		numstyles = struct.unpack(">h", data[:2])[0] + 1
		data = data[2:]
		for i in range(numstyles):
			style, l, b, r, t = struct.unpack(">hhhhh", data[:10])
			boxes[style] = (l, b, r, t)
			data = data[10:]
		self.boundingBoxes = boxes
	
	def _buildboundingboxtable(self):
		if self.boundingBoxes and self._rawoffsettable[:6] == '\0\0\0\0\0\6':
			boxes = self.boundingBoxes.items()
			boxes.sort()
			data = '\0\0\0\0\0\6' + struct.pack(">h", len(boxes) - 1)
			for style, (l, b, r, t) in boxes:
				data = data + struct.pack(">hhhhh", style, l, b, r, t)
			self._rawoffsettable = data
	
	def _getglyphwidthtable(self):
		self.widthTables = {}
		if self.ffWTabOff == 0:
			return
		data = self.FOND.data
		offset = self.ffWTabOff
		numberofentries, = struct.unpack(">h", data[offset:offset+2])
		numberofentries = numberofentries + 1
		count = offset + 2
		for i in range(numberofentries):
			stylecode, = struct.unpack(">h", data[count:count+2])
			widthtable = self.widthTables[stylecode] = []
			count = count + 2
			for j in range(3 + self.ffLastChar - self.ffFirstChar):
				width, = struct.unpack(">h", data[count:count+2])
				widthtable.append(width)
				count = count + 2
		
		if DEBUG:
			self._rawglyphwidthtable = data[offset:count]
			self.parsedthings.append((offset, count, 'glyphwidthtable'))
	
	def _buildglyphwidthtable(self):
		if not self.widthTables:
			self._rawglyphwidthtable = ""
			return
		numberofentries = len(self.widthTables)
		data = struct.pack('>h', numberofentries - 1)
		tables = self.widthTables.items()
		tables.sort()
		for stylecode, table in tables:
			data = data + struct.pack('>h', stylecode)
			if len(table) <> (3 + self.ffLastChar - self.ffFirstChar):
				raise error, "width table has wrong length"
			for width in table:
				data = data + struct.pack('>h', width)
		if DEBUG:
			print "glyph width table is the same?", self._rawglyphwidthtable == data and 'yes.' or 'no.'
		self._rawglyphwidthtable = data
	
	def _getkerningtables(self):
		self.kernTables = {}
		if self.ffKernOff == 0:
			return
		data = self.FOND.data
		offset = self.ffKernOff
		numberofentries, = struct.unpack(">h", data[offset:offset+2])
		numberofentries = numberofentries + 1
		count = offset + 2
		for i in range(numberofentries):
			stylecode, = struct.unpack(">h", data[count:count+2])
			count = count + 2
			numberofpairs, = struct.unpack(">h", data[count:count+2])
			count = count + 2
			kerntable = self.kernTables[stylecode] = []
			for j in range(numberofpairs):
				firstchar, secondchar, kerndistance = struct.unpack(">cch", data[count:count+4])
				kerntable.append((ord(firstchar), ord(secondchar), kerndistance))
				count = count + 4
		
		if DEBUG:
			self._rawkerningtables = data[offset:count]
			self.parsedthings.append((offset, count, 'kerningtables'))
	
	def _buildkerningtables(self):
		if self.kernTables == {}:
			self._rawkerningtables = ""
			self.ffKernOff = 0
			return
		numberofentries = len(self.kernTables)
		data = [struct.pack('>h', numberofentries - 1)]
		tables = self.kernTables.items()
		tables.sort()
		for stylecode, table in tables:
			data.append(struct.pack('>h', stylecode))
			data.append(struct.pack('>h', len(table)))  # numberofpairs
			for firstchar, secondchar, kerndistance in table:
				data.append(struct.pack(">cch", chr(firstchar), chr(secondchar), kerndistance))
		
		data = string.join(data, '')
		
		if DEBUG:
			print "kerning table is the same?", self._rawkerningtables == data and 'yes.' or 'no.'
			if self._rawkerningtables <> data:
				print len(self._rawkerningtables), len(data)
		self._rawkerningtables = data
	
	def _getstylemappingtable(self):
		offset = self.ffStylOff
		self.styleStrings = []
		self.styleIndices = ()
		self.glyphTableOffset = 0
		self.fondClass = 0
		if offset == 0:
			return
		data = self.FOND.data
		self.fondClass, self.glyphTableOffset, self.styleMappingReserved, = \
				struct.unpack(">hll", data[offset:offset+10])
		self.styleIndices = struct.unpack('>48b', data[offset + 10:offset + 58])
		stringcount, = struct.unpack('>h', data[offset+58:offset+60])
		
		count = offset + 60
		for i in range(stringcount):
			str_len = ord(data[count])
			self.styleStrings.append(data[count + 1:count + 1 + str_len])
			count = count + 1 + str_len
		
		self._unpackstylestrings()
		
		data = data[offset:count]
		if len(data) % 2:
			data = data + '\0'
		if DEBUG:
			self._rawstylemappingtable = data
			self.parsedthings.append((offset, count, 'stylemappingtable'))
	
	def _buildstylemappingtable(self):
		if not self.styleIndices:
			self._rawstylemappingtable = ""
			return
		data = struct.pack(">hll", self.fondClass, self.glyphTableOffset, 
					self.styleMappingReserved)
		
		self._packstylestrings()
		data = data + apply(struct.pack, (">48b",) + self.styleIndices)
		
		stringcount = len(self.styleStrings)
		data = data + struct.pack(">h", stringcount)
		for string in self.styleStrings:
			data = data + chr(len(string)) + string
		
		if len(data) % 2:
			data = data + '\0'
		
		if DEBUG:
			print "style mapping table is the same?", self._rawstylemappingtable == data and 'yes.' or 'no.'
		self._rawstylemappingtable = data
	
	def _unpackstylestrings(self):
		psNames = {}
		self.ffFamilyName = self.styleStrings[0]
		for i in self.widthTables.keys():
			index = self.styleIndices[i]
			if index == 1:
				psNames[i] = self.styleStrings[0]
			else:
				style = self.styleStrings[0]
				codes = map(ord, self.styleStrings[index - 1])
				for code in codes:
					style = style + self.styleStrings[code - 1]
				psNames[i] = style
		self.psNames = psNames
	
	def _packstylestrings(self):
		nameparts = {}
		splitnames = {}
		for style, name in self.psNames.items():
			split = splitname(name, self.ffFamilyName)
			splitnames[style] = split
			for part in split:
				nameparts[part] = None
		del nameparts[self.ffFamilyName]
		nameparts = nameparts.keys()
		nameparts.sort()
		items = splitnames.items()
		items.sort()
		numindices = 0
		for style, split in items:
			if len(split) > 1:
				numindices = numindices + 1
		numindices = max(numindices, max(self.styleIndices) - 1)
		styleStrings = [self.ffFamilyName] + numindices * [""] + nameparts
		# XXX the next bit goes wrong for MM fonts.
		for style, split in items:
			if len(split) == 1:
				continue
			indices = ""
			for part in split[1:]:
				indices = indices + chr(nameparts.index(part) + numindices + 2)
			styleStrings[self.styleIndices[style] - 1] = indices
		self.styleStrings = styleStrings
	
	def _updatestylemappingtable(self):
		# Update the glyphTableOffset field.
		# This is necessary since we have to build this table to 
		# know what the glyphTableOffset will be.
		# And we don't want to build it twice, do we?
		data = self._rawstylemappingtable
		if not data:
			return
		data = data[:2] + struct.pack(">l", self.glyphTableOffset) + data[6:]
		self._rawstylemappingtable = data
	
	def _getglyphencodingsubtable(self):
		glyphEncoding = self.glyphEncoding = {}
		if not self.glyphTableOffset:
			return
		offset = self.ffStylOff + self.glyphTableOffset
		data = self.FOND.data
		numberofentries, = struct.unpack(">h", data[offset:offset+2])
		count = offset + 2
		for i in range(numberofentries):
			glyphcode = ord(data[count])
			count = count + 1
			strlen = ord(data[count])
			count = count + 1
			glyphname = data[count:count+strlen]
			glyphEncoding[glyphcode] = glyphname
			count = count + strlen
		
		if DEBUG:
			self._rawglyphencodingsubtable = data[offset:count]
			self.parsedthings.append((offset, count, 'glyphencodingsubtable'))
	
	def _buildglyphencodingsubtable(self):
		if not self.glyphEncoding:
			self._rawglyphencodingsubtable = ""
			return
		numberofentries = len(self.glyphEncoding)
		data = struct.pack(">h", numberofentries)
		items = self.glyphEncoding.items()
		items.sort()
		for glyphcode, glyphname in items:
			data = data + chr(glyphcode) + chr(len(glyphname)) + glyphname
		self._rawglyphencodingsubtable = data
	

uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def splitname(name, famname = None):
	# XXX this goofs up MM font names: but how should it be done??
	if famname:
		if name[:len(famname)] <> famname:
			raise error, "first part of name should be same as family name"
		name = name[len(famname):]
		split = [famname]
	else:
		split = []
	current = ""
	for c in name:
		if c == '-' or c in uppercase:
			if current:
				split.append(current)
				current = ""
		current = current + c
	if current:
		split.append(current)
	return split

def makeLWFNfilename(name):
	split = splitname(name)
	lwfnname = split[0][:5]
	for part in split[1:]:
		if part <> '-':
			lwfnname = lwfnname + part[:3]
	return lwfnname

class BitmapFontFile:
	
	def __init__(self, path, mode='r'):
		if mode == 'r':
			permission = 1	# read only
		elif mode == 'w':
			permission = 3	# exclusive r/w
		else:
			raise error, 'mode should be either "r" or "w"'
		self.mode = mode
		self.resref = Res.FSOpenResFile(path, permission)
		Res.UseResFile(self.resref)
		self.path = path
		self.fonds = []
		self.getFONDs()
	
	def getFONDs(self):
		FONDcount = Res.Count1Resources('FOND')
		for i in range(FONDcount):
			fond = FontFamily(Res.Get1IndResource('FOND', i + 1), self.mode)
			self.fonds.append(fond)
	
	def parse(self):
		self.fondsbyname = {}
		for fond in self.fonds:
			fond.parse()
			if hasattr(fond, "psNames") and fond.psNames:
				psNames = fond.psNames.values()
				psNames.sort()
				self.fondsbyname[psNames[0]] = fond
	
	def minimalparse(self):
		for fond in self.fonds:
			fond.minimalparse()
	
	def close(self):
		if self.resref <> None:
			try:
				Res.CloseResFile(self.resref)
			except Res.Error:
				pass
			self.resref = None


class FondSelector:
	
	def __init__(self, fondlist):
		import W
		if not fondlist:
			raise ValueError, "expected at least one FOND entry"
		if len(fondlist) == 1:
			self.choice = 0
			return
		fonds = []
		for fond in fondlist:
			fonds.append(fond.name)
		self.w = W.ModalDialog((200, 200), "aaa")
		self.w.donebutton = W.Button((-70, -26, 60, 16), "Done", self.close)
		self.w.l = W.List((10, 10, -10, -36), fonds, self.listhit)
		self.w.setdefaultbutton(self.w.donebutton)
		self.w.l.setselection([0])
		self.w.open()
	
	def close(self):
		self.checksel()
		sel = self.w.l.getselection()
		self.choice = sel[0]
		self.w.close()
	
	def listhit(self, isDbl):
		if isDbl:
			self.w.donebutton.push()
		else:
			self.checksel()
	
	def checksel(self):
		sel = self.w.l.getselection()
		if not sel:
			self.w.l.setselection([0])
		elif len(sel) <> 1:
			self.w.l.setselection([sel[0]])