fonttools/Lib/fontTools/ttLib/ttCollection.py

60 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools.ttLib.ttFont import TTFont
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"):
closeStream = True
file = open(file, "rb")
else:
# assume "file" is a readable file object
closeStream = False
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)
if closeStream:
file.close()
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)