• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""ttLib.macUtils.py -- Various Mac-specific stuff."""
2from __future__ import print_function, division, absolute_import
3from fontTools.misc.py23 import *
4from fontTools.misc.macRes import ResourceReader, ResourceError
5
6
7def getSFNTResIndices(path):
8	"""Determine whether a file has a 'sfnt' resource fork or not."""
9	try:
10		reader = ResourceReader(path)
11		indices = reader.getIndices('sfnt')
12		reader.close()
13		return indices
14	except ResourceError:
15		return []
16
17
18def openTTFonts(path):
19	"""Given a pathname, return a list of TTFont objects. In the case
20	of a flat TTF/OTF file, the list will contain just one font object;
21	but in the case of a Mac font suitcase it will contain as many
22	font objects as there are sfnt resources in the file.
23	"""
24	from fontTools import ttLib
25	fonts = []
26	sfnts = getSFNTResIndices(path)
27	if not sfnts:
28		fonts.append(ttLib.TTFont(path))
29	else:
30		for index in sfnts:
31			fonts.append(ttLib.TTFont(path, index))
32		if not fonts:
33			raise ttLib.TTLibError("no fonts found in file '%s'" % path)
34	return fonts
35
36
37class SFNTResourceReader(BytesIO):
38
39	"""Simple read-only file wrapper for 'sfnt' resources."""
40
41	def __init__(self, path, res_name_or_index):
42		from fontTools import ttLib
43		reader = ResourceReader(path)
44		if isinstance(res_name_or_index, basestring):
45			rsrc = reader.getNamedResource('sfnt', res_name_or_index)
46		else:
47			rsrc = reader.getIndResource('sfnt', res_name_or_index)
48		if rsrc is None:
49			raise ttLib.TTLibError("sfnt resource not found: %s" % res_name_or_index)
50		reader.close()
51		self.rsrc = rsrc
52		super(SFNTResourceReader, self).__init__(rsrc.data)
53		self.name = path
54