1999-12-16 21:34:53 +00:00
|
|
|
"""ttLib.macUtils.py -- Various Mac-specific stuff."""
|
2024-02-06 15:47:35 +02:00
|
|
|
|
2021-03-29 11:45:58 +02:00
|
|
|
from io import BytesIO
|
2015-10-25 20:22:43 +00:00
|
|
|
from fontTools.misc.macRes import ResourceReader, ResourceError
|
2003-08-22 18:56:01 +00:00
|
|
|
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def getSFNTResIndices(path):
|
2015-10-25 20:22:43 +00:00
|
|
|
"""Determine whether a file has a 'sfnt' resource fork or not."""
|
1999-12-16 21:34:53 +00:00
|
|
|
try:
|
2015-10-25 20:22:43 +00:00
|
|
|
reader = ResourceReader(path)
|
|
|
|
indices = reader.getIndices("sfnt")
|
|
|
|
reader.close()
|
|
|
|
return indices
|
|
|
|
except ResourceError:
|
1999-12-16 21:34:53 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def openTTFonts(path):
|
2015-04-26 02:01:01 -04:00
|
|
|
"""Given a pathname, return a list of TTFont objects. In the case
|
1999-12-16 21:34:53 +00:00
|
|
|
of a flat TTF/OTF file, the list will contain just one font object;
|
|
|
|
but in the case of a Mac font suitcase it will contain as many
|
|
|
|
font objects as there are sfnt resources in the file.
|
|
|
|
"""
|
|
|
|
from fontTools import ttLib
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
fonts = []
|
|
|
|
sfnts = getSFNTResIndices(path)
|
|
|
|
if not sfnts:
|
|
|
|
fonts.append(ttLib.TTFont(path))
|
|
|
|
else:
|
|
|
|
for index in sfnts:
|
|
|
|
fonts.append(ttLib.TTFont(path, index))
|
|
|
|
if not fonts:
|
2013-11-27 02:42:28 -05:00
|
|
|
raise ttLib.TTLibError("no fonts found in file '%s'" % path)
|
1999-12-16 21:34:53 +00:00
|
|
|
return fonts
|
|
|
|
|
|
|
|
|
2015-10-25 20:22:43 +00:00
|
|
|
class SFNTResourceReader(BytesIO):
|
|
|
|
"""Simple read-only file wrapper for 'sfnt' resources."""
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def __init__(self, path, res_name_or_index):
|
2017-03-14 12:00:13 +01:00
|
|
|
from fontTools import ttLib
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2015-10-25 20:22:43 +00:00
|
|
|
reader = ResourceReader(path)
|
2021-03-27 10:23:29 -04:00
|
|
|
if isinstance(res_name_or_index, str):
|
2015-10-25 20:22:43 +00:00
|
|
|
rsrc = reader.getNamedResource("sfnt", res_name_or_index)
|
1999-12-16 21:34:53 +00:00
|
|
|
else:
|
2015-10-25 20:22:43 +00:00
|
|
|
rsrc = reader.getIndResource("sfnt", res_name_or_index)
|
|
|
|
if rsrc is None:
|
2016-04-10 23:10:37 +01:00
|
|
|
raise ttLib.TTLibError("sfnt resource not found: %s" % res_name_or_index)
|
2015-10-25 20:22:43 +00:00
|
|
|
reader.close()
|
|
|
|
self.rsrc = rsrc
|
|
|
|
super(SFNTResourceReader, self).__init__(rsrc.data)
|
1999-12-16 21:34:53 +00:00
|
|
|
self.name = path
|