From fcbbce5ca243d2323247718b89bfc4b30ae6f6c4 Mon Sep 17 00:00:00 2001 From: msoxzw <56633971+msoxzw@users.noreply.github.com> Date: Mon, 21 Oct 2019 07:03:45 +0000 Subject: [PATCH] Add with statement context manager like ttFont --- Lib/fontTools/ttLib/ttCollection.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/fontTools/ttLib/ttCollection.py b/Lib/fontTools/ttLib/ttCollection.py index ab885b87b..eb22ba76c 100644 --- a/Lib/fontTools/ttLib/ttCollection.py +++ b/Lib/fontTools/ttLib/ttCollection.py @@ -35,6 +35,16 @@ class TTCollection(object): for i in range(header.numFonts): font = TTFont(file, fontNumber=i, _tableCache=tableCache, **kwargs) fonts.append(font) + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + def close(self): + for font in self.fonts: + font.close() def save(self, file, shareTables=True): """Save the font to disk. Similarly to the constructor,