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