def get_font_size(self, font_id): """The size of the font compressed as a woff2.""" if font_id in SIZE_CACHE: return SIZE_CACHE[font_id] ttf_bytes = self.font_loader.load_font(font_id) woff2_bytes = woff2.ttf_to_woff2(ttf_bytes) SIZE_CACHE[font_id] = len(woff2_bytes) return SIZE_CACHE[font_id]
def test_encode(self): with open("./patch_subset/testdata/Roboto-Regular.ttf", "rb") as roboto: roboto_bytes = roboto.read() roboto_woff2_bytes = woff2.ttf_to_woff2(roboto_bytes) self.assertIsNot(roboto_woff2_bytes, None) self.assertGreater(len(roboto_woff2_bytes), 0) self.assertLess(len(roboto_woff2_bytes), len(roboto_bytes)) self.assertEqual(roboto_woff2_bytes[0:4], b'wOF2')
def subset_size(self, cache_key, codepoints, font_bytes): """Returns the size of subset (a set of codepoints) of font_bytes after woff2 encoding.""" if cache_key in self.size_cache: return self.size_cache[cache_key] subset_bytes = self.subset(font_bytes, codepoints) woff2_bytes = woff2.ttf_to_woff2(subset_bytes) final_size = len(woff2_bytes) self.size_cache[cache_key] = final_size return final_size
def test_encode_failure(self): invalid_bytes = b'aaaaaaaaa' with self.assertRaises(woff2.Woff2EncodeError): woff2.ttf_to_woff2(invalid_bytes)