2018-01-23 15:39:38 -08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
|
|
|
from fontTools.misc.py23 import *
|
2018-01-23 16:03:45 -08:00
|
|
|
from fontTools.ttLib.ttFont import TTFont
|
2018-01-23 15:39:38 -08:00
|
|
|
from fontTools.ttLib.sfnt import readTTCHeader
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TTCollection(object):
|
|
|
|
|
|
|
|
"""Object representing a TrueType Collection / OpenType Collection.
|
|
|
|
The main API is self.fonts being a list of TTFont instances.
|
|
|
|
|
|
|
|
If shareTables is True, then different fonts in the collection
|
|
|
|
might point to the same table object if the data for the table was
|
|
|
|
the same in the font file. Note, however, that this might result
|
|
|
|
in suprises and incorrect behavior if the different fonts involved
|
|
|
|
have different GlyphOrder. Use only if you know what you are doing.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, file=None, shareTables=False, **kwargs):
|
|
|
|
fonts = self.fonts = []
|
|
|
|
if file is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
assert 'fontNumber' not in kwargs, kwargs
|
|
|
|
|
|
|
|
if not hasattr(file, "read"):
|
2018-01-24 15:41:14 -08:00
|
|
|
closeStream = True
|
2018-01-23 15:39:38 -08:00
|
|
|
file = open(file, "rb")
|
2018-01-24 15:41:14 -08:00
|
|
|
else:
|
|
|
|
# assume "file" is a readable file object
|
|
|
|
closeStream = False
|
2018-01-23 15:39:38 -08:00
|
|
|
|
|
|
|
tableCache = {} if shareTables else None
|
|
|
|
|
|
|
|
header = readTTCHeader(file)
|
|
|
|
for i in range(header.numFonts):
|
|
|
|
font = TTFont(file, fontNumber=i, _tableCache=tableCache, **kwargs)
|
|
|
|
fonts.append(font)
|
|
|
|
|
2018-01-24 15:41:14 -08:00
|
|
|
if closeStream:
|
|
|
|
file.close()
|
|
|
|
|
2018-01-23 15:39:38 -08:00
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.fonts[item]
|
|
|
|
|
|
|
|
def __setitem__(self, item, value):
|
|
|
|
self.fonts[item] = values
|
|
|
|
|
|
|
|
def __delitem__(self, item):
|
|
|
|
return self.fonts[item]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.fonts)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.fonts)
|