1999-12-16 21:34:53 +00:00
|
|
|
"""ttLib.macUtils.py -- Various Mac-specific stuff."""
|
|
|
|
|
2014-01-14 15:07:50 +08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
2013-11-27 17:27:45 -05:00
|
|
|
from fontTools.misc.py23 import *
|
2003-08-22 18:56:01 +00:00
|
|
|
import sys
|
1999-12-16 21:34:53 +00:00
|
|
|
import os
|
2003-08-22 18:56:01 +00:00
|
|
|
if sys.platform not in ("mac", "darwin"):
|
2013-11-27 02:42:28 -05:00
|
|
|
raise ImportError("This module is Mac-only!")
|
2002-06-06 19:58:18 +00:00
|
|
|
try:
|
|
|
|
from Carbon import Res
|
|
|
|
except ImportError:
|
|
|
|
import Res
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2013-11-27 14:37:28 -05:00
|
|
|
|
2003-08-22 18:56:01 +00:00
|
|
|
def MyOpenResFile(path):
|
|
|
|
mode = 1 # read only
|
|
|
|
try:
|
2012-10-18 12:49:22 +00:00
|
|
|
resref = Res.FSOpenResFile(path, mode)
|
2003-08-22 18:56:01 +00:00
|
|
|
except Res.Error:
|
|
|
|
# try data fork
|
2013-11-27 14:37:28 -05:00
|
|
|
resref = Res.FSOpenResourceFile(path, unicode(), mode)
|
2003-08-22 18:56:01 +00:00
|
|
|
return resref
|
|
|
|
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def getSFNTResIndices(path):
|
|
|
|
"""Determine whether a file has a resource fork or not."""
|
|
|
|
try:
|
2003-08-22 18:56:01 +00:00
|
|
|
resref = MyOpenResFile(path)
|
1999-12-16 21:34:53 +00:00
|
|
|
except Res.Error:
|
|
|
|
return []
|
|
|
|
Res.UseResFile(resref)
|
|
|
|
numSFNTs = Res.Count1Resources('sfnt')
|
|
|
|
Res.CloseResFile(resref)
|
2013-11-27 03:34:48 -05:00
|
|
|
return list(range(1, numSFNTs + 1))
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-11-28 14:26:58 -05:00
|
|
|
class SFNTResourceReader(object):
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
"""Simple (Mac-only) read-only file wrapper for 'sfnt' resources."""
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def __init__(self, path, res_name_or_index):
|
2003-08-22 18:56:01 +00:00
|
|
|
resref = MyOpenResFile(path)
|
1999-12-16 21:34:53 +00:00
|
|
|
Res.UseResFile(resref)
|
2013-11-27 16:44:53 -05:00
|
|
|
if isinstance(res_name_or_index, basestring):
|
1999-12-16 21:34:53 +00:00
|
|
|
res = Res.Get1NamedResource('sfnt', res_name_or_index)
|
|
|
|
else:
|
|
|
|
res = Res.Get1IndResource('sfnt', res_name_or_index)
|
2013-11-27 05:05:46 -05:00
|
|
|
self.file = StringIO(res.data)
|
1999-12-16 21:34:53 +00:00
|
|
|
Res.CloseResFile(resref)
|
|
|
|
self.name = path
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
# cheap inheritance
|
|
|
|
return getattr(self.file, attr)
|