Last active
August 29, 2015 14:18
-
-
Save anthrotype/7372e958a459547d7ea5 to your computer and use it in GitHub Desktop.
fontTools WOFF2 encoder roundtrip test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| if input font is SFNT: | |
| - create normalised font | |
| - compress input font to WOFF2 | |
| - decompress WOFF2 | |
| - diff normalised and decompressed font | |
| else if input font is WOFF2: | |
| - decompress input font to SFNT | |
| """ | |
| import os | |
| import sys | |
| from fontTools.ttLib import TTFont | |
| from fontTools.ttLib import woff2 | |
| from tempfile import TemporaryFile | |
| def main(): | |
| if not sys.argv[1:]: | |
| print("No input font") | |
| sys.exit(1) | |
| infile = sys.argv[1] | |
| outfile = make_output_name(infile) | |
| font = TTFont(infile, recalcTimestamp=False, checkChecksums=1) | |
| if font.flavor is None: | |
| # make temporary 'normalised' version of input font | |
| print('Normalising font %s' % infile) | |
| woff2.normaliseFont(font) | |
| normalised = TemporaryFile() | |
| font.save(normalised, reorderTables=True) | |
| # convert input sfnt to woff2 | |
| print('Compressing font %s' % infile) | |
| font.flavor = "woff2" | |
| font.save(outfile) | |
| # decompress woff2 to new temporary sfnt | |
| print('Decompressing font %s' % outfile) | |
| font1 = TTFont(outfile, recalcTimestamp=False) | |
| font1.flavor = None | |
| decompressed = TemporaryFile() | |
| font1.save(decompressed, reorderTables=True) | |
| # compare normalised and decompressed fonts | |
| if not diff(normalised, decompressed): | |
| print('Success!') | |
| else: | |
| print('Failed: normalised and decompressed fonts differ.') | |
| else: | |
| # convert input woff2 to sfnt | |
| print('Decompressing font %s' % infile) | |
| font.flavor = None | |
| font.save(outfile, reorderTables=True) | |
| def diff(a_file, another_file): | |
| """Return False if the two files' content is the same, else return True.""" | |
| result = False | |
| first_is_stream = second_is_stream = True | |
| if not hasattr(a_file, 'read'): | |
| first_is_stream = False | |
| a_file = open(a_file, 'rb') | |
| if not hasattr(another_file, 'read'): | |
| second_is_stream = False | |
| another_file = open(another_file, 'rb') | |
| a_file.seek(0) | |
| another_file.seek(0) | |
| if a_file.read() != another_file.read(): | |
| result = True | |
| if not first_is_stream: | |
| a_file.close() | |
| if not second_is_stream: | |
| another_file.close() | |
| return result | |
| def make_output_name(infile): | |
| """Replace 'woff2' file extension with 'ttf' or 'otf', and viceversa.""" | |
| basename, ext = os.path.splitext(infile) | |
| if ext in ['.otf', '.ttf']: | |
| ext = ".woff2" | |
| elif ext in ['.woff2']: | |
| with open(infile, "rb") as f: | |
| f.seek(4) | |
| sfntVersion = f.read(4) | |
| ext = '.ttf' if sfntVersion == b"\x00\x01\x00\x00" else ".otf" | |
| return basename + ext | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment