def handle_font(font_name): font = TTFont(font_name) orig_size = os.path.getsize(font_name) if decompress: from fontTools import subset options = subset.Options() options.desubroutinize = True subsetter = subset.Subsetter(options=options) subsetter.populate(glyphs=font.getGlyphOrder()) subsetter.subset(font) if verbose: print("Compressing font through iterative_encode:") out_name = "%s.compressed%s" % os.path.splitext(font_name) compreffor = Compreffor(font, verbose=verbose, **comp_kwargs) compreffor.compress() # save compressed font font.save(out_name) if generate_cff: # save CFF version font["CFF "].cff.compile(open("%s.cff" % os.path.splitext(out_name)[0], "w"), None) comp_size = os.path.getsize(out_name) print("Compressed to %s -- saved %s" % (os.path.basename(out_name), human_size(orig_size - comp_size))) if check: test_compression_integrity(filename, out_name) test_call_depth(out_name)
def main(filename=None, comp_fname=None, test=False, decompress=False, verbose=False, check=False, generate_cff=False, recursive=False, **comp_kwargs): if test: pass if filename and comp_fname == None: def handle_font(font_name): font = TTFont(font_name) td = font['CFF '].cff.topDictIndex[0] no_subrs = lambda fd: hasattr(fd, 'Subrs') and len(fd.Subrs) > 0 priv_subrs = (hasattr(td, 'FDArray') and any(no_subrs(fd) for fd in td.FDArray)) if len(td.GlobalSubrs) > 0 or priv_subrs: print("Warning: There are subrs in %s" % font_name) orig_size = os.path.getsize(font_name) if decompress: from fontTools import subset options = subset.Options() options.desubroutinize = True subsetter = subset.Subsetter(options=options) subsetter.populate(glyphs=font.getGlyphOrder()) subsetter.subset(font) out_name = "%s.compressed%s" % os.path.splitext(font_name) compreff(font, verbose=verbose, **comp_kwargs) # save compressed font start_time = time.time() font.save(out_name) if verbose: print("Compiled and saved (took %gs)" % (time.time() - start_time)) if generate_cff: # save CFF version font['CFF '].cff.compile(open("%s.cff" % os.path.splitext(out_name)[0], 'w'), None) comp_size = os.path.getsize(out_name) print("Compressed to %s -- saved %s" % (os.path.basename(out_name), human_size(orig_size - comp_size))) if check: test_compression_integrity(filename, out_name) test_call_depth(out_name) if recursive: for root, dirs, files in os.walk(filename): for fname in files: if os.path.splitext(fname)[1] == '.otf': handle_font(fname) else: handle_font(filename) if check and comp_fname != None: test_compression_integrity(filename, comp_fname) test_call_depth(comp_fname)
def handle_font(font_name): font = TTFont(font_name) orig_size = os.path.getsize(font_name) if decompress: from fontTools import subset options = subset.Options() options.desubroutinize = True subsetter = subset.Subsetter(options=options) subsetter.populate(glyphs=font.getGlyphOrder()) subsetter.subset(font) if verbose: print("Compressing font through iterative_encode:") out_name = "%s.compressed%s" % os.path.splitext(font_name) compreffor = Compreffor(font, verbose=verbose, **comp_kwargs) compreffor.compress() # save compressed font font.save(out_name) if generate_cff: # save CFF version font["CFF "].cff.compile( open("%s.cff" % os.path.splitext(out_name)[0], "w"), None) comp_size = os.path.getsize(out_name) print("Compressed to %s -- saved %s" % (os.path.basename(out_name), human_size(orig_size - comp_size))) if check: test_compression_integrity(filename, out_name) test_call_depth(out_name)
def main(filename=None, comp_fname=None, test=False, decompress=False, verbose=False, check=False, generate_cff=False, recursive=False, **comp_kwargs): from testPyCompressor import test_compression_integrity, test_call_depth if test: from testPyCompressor import TestCffCompressor test_suite = unittest.TestLoader().loadTestsFromTestCase(TestCffCompressor) unittest.TextTestRunner().run(test_suite) if filename and comp_fname == None: def handle_font(font_name): font = TTFont(font_name) orig_size = os.path.getsize(font_name) if decompress: from fontTools import subset options = subset.Options() options.desubroutinize = True subsetter = subset.Subsetter(options=options) subsetter.populate(glyphs=font.getGlyphOrder()) subsetter.subset(font) if verbose: print("Compressing font through iterative_encode:") out_name = "%s.compressed%s" % os.path.splitext(font_name) compreffor = Compreffor(font, verbose=verbose, **comp_kwargs) compreffor.compress() # save compressed font font.save(out_name) if generate_cff: # save CFF version font["CFF "].cff.compile(open("%s.cff" % os.path.splitext(out_name)[0], "w"), None) comp_size = os.path.getsize(out_name) print("Compressed to %s -- saved %s" % (os.path.basename(out_name), human_size(orig_size - comp_size))) if check: test_compression_integrity(filename, out_name) test_call_depth(out_name) if recursive: for root, dirs, files in os.walk(filename): for fname in files: if os.path.splitext(fname)[1] == '.otf': handle_font(fname) else: handle_font(filename) if check and comp_fname != None: test_compression_integrity(filename, comp_fname) test_call_depth(comp_fname)
def handle_font(font_name): font = TTFont(font_name) td = font['CFF '].cff.topDictIndex[0] no_subrs = lambda fd: hasattr(fd, 'Subrs') and len(fd.Subrs) > 0 priv_subrs = (hasattr(td, 'FDArray') and any(no_subrs(fd) for fd in td.FDArray)) if len(td.GlobalSubrs) > 0 or priv_subrs: print("Warning: There are subrs in %s" % font_name) orig_size = os.path.getsize(font_name) if decompress: from fontTools import subset options = subset.Options() options.desubroutinize = True subsetter = subset.Subsetter(options=options) subsetter.populate(glyphs=font.getGlyphOrder()) subsetter.subset(font) out_name = "%s.compressed%s" % os.path.splitext(font_name) compreff(font, verbose=verbose, **comp_kwargs) # save compressed font start_time = time.time() font.save(out_name) if verbose: print("Compiled and saved (took %gs)" % (time.time() - start_time)) if generate_cff: # save CFF version font['CFF '].cff.compile( open("%s.cff" % os.path.splitext(out_name)[0], 'w'), None) comp_size = os.path.getsize(out_name) print("Compressed to %s -- saved %s" % (os.path.basename(out_name), human_size(orig_size - comp_size))) if check: test_compression_integrity(filename, out_name) test_call_depth(out_name)