#NEWLINE# def main( ) : #NEWLINE# #INDENT# global font #NEWLINE# log_format = '%(levelname)-8s %(message)s  ' #NEWLINE# logger = logging . getLogger ( ) #NEWLINE# handler = logging . StreamHandler ( ) #NEWLINE# formatter = logging . Formatter ( log_format ) #NEWLINE# handler . setFormatter ( formatter ) #NEWLINE# logger . addHandler ( handler ) #NEWLINE# parser = argparse . ArgumentParser ( description = 'Compare TTF files when upgrading families.' ) #NEWLINE# parser . add_argument ( 'arg_filepaths' , nargs = '+' , help = 'font file path(s) to check. Wildcards like *.ttf are allowed.' ) #NEWLINE# parser . add_argument ( '-v' , '--verbose' , action = 'count' , default = 0 , help = 'increase output verbosity' ) #NEWLINE# parser . add_argument ( '-b' , '--versionbump' , action = 'store_true' , help = "increment font files' minor version number" ) #NEWLINE# args = parser . parse_args ( ) #NEWLINE# if ( args . verbose == 1 ) : #NEWLINE# #INDENT# logger . setLevel ( logging . INFO ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( args . verbose >= 2 ) : #NEWLINE# #INDENT# logger . setLevel ( logging . DEBUG ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . setLevel ( logging . ERROR ) #NEWLINE# #UNINDENT# #UNINDENT# import magic #NEWLINE# logging . debug ( 'Checking each file is a ttf' ) #NEWLINE# fonts_to_check = [ ] #NEWLINE# for arg_filepath in sorted ( args . arg_filepaths ) : #NEWLINE# #INDENT# for fullpath in glob . glob ( arg_filepath ) : #NEWLINE# #INDENT# ( file_path , file_name ) = os . path . split ( fullpath ) #NEWLINE# mime = magic . Magic ( mime = True ) #NEWLINE# mimetype = mime . from_file ( fullpath ) #NEWLINE# if ( mimetype == 'application/x-font-ttf' ) : #NEWLINE# #INDENT# logging . debug ( "'{}' has a ttf mimetype" . format ( file_name ) ) #NEWLINE# fonts_to_check . append ( fullpath ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logging . warning ( "Skipping '{}' as mime was '{}', should be 'application/x-font-ttf')" . format ( filename , mimetype ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# fonts_to_check . sort ( ) #NEWLINE# for new_file in fonts_to_check : #NEWLINE# #INDENT# logging . debug ( 'Comparison of filesizes' ) #NEWLINE# old_file = ( new_file + '-old' ) #NEWLINE# new_filesize = os . path . getsize ( new_file ) #NEWLINE# old_filesize = os . path . getsize ( old_file ) #NEWLINE# delta = ( new_filesize - old_filesize ) #NEWLINE# percentage = ( float ( delta ) / old_filesize ) #NEWLINE# if ( delta > 0 ) : #NEWLINE# #INDENT# logging . warning ( "New font file '{}' is {} bytes larger" . format ( new_file , delta ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( delta < 0 ) : #NEWLINE# #INDENT# logging . warning ( "New font file '{}' is {} bytes smaller" . format ( new_file , ( - delta ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logging . info ( "New font file '{}' preserves filesize." . format ( new_file ) ) #NEWLINE# #UNINDENT# #UNINDENT# new_font = ttLib . TTFont ( new_file ) #NEWLINE# old_font = ttLib . TTFont ( old_file ) #NEWLINE# logging . debug ( 'Checking font version fields' ) #NEWLINE# old_version = parse_version_string ( str ( old_font [ 'head' ] . fontRevision ) ) #NEWLINE# if ( old_version == None ) : #NEWLINE# #INDENT# logging . error ( ( "Could not parse TTF version string on the 'head' table." + " Please fix it. Current value is '{}'" . format ( str ( old_font [ 'head' ] . fontRevision ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if args . versionbump : #NEWLINE# #INDENT# minor = old_version [ 1 ] #NEWLINE# num_digits = len ( minor ) #NEWLINE# minor = str ( ( int ( minor ) + 1 ) ) #NEWLINE# minor = ( ( '0' * ( num_digits - len ( minor ) ) ) + minor ) #NEWLINE# incremented_version = [ old_version [ 0 ] , minor , old_version [ 2 ] ] #NEWLINE# new_version = parse_version_string ( str ( new_font [ 'head' ] . fontRevision ) ) #NEWLINE# if ( new_version != incremented_version ) : #NEWLINE# #INDENT# logging . warning ( 'Auto-incrementing fontRevision from {}.{} to {}.{}' . format ( new_version [ 0 ] , new_version [ 1 ] , incremented_version [ 0 ] , incremented_version [ 1 ] ) ) #NEWLINE# new_font [ 'head' ] . fontRevision = '{}.{}' . format ( incremented_version [ 0 ] , incremented_version [ 1 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logging . info ( 'OK: fontRevision is {}.{}' . format ( new_version [ 0 ] , new_version [ 1 ] ) ) #NEWLINE# #UNINDENT# for name in new_font [ 'name' ] . names : #NEWLINE# #INDENT# if ( name . nameID == NAMEID_VERSION_STRING ) : #NEWLINE# #INDENT# encoding = name . getEncoding ( ) #NEWLINE# s = name . string . decode ( encoding ) #NEWLINE# s = 'Version {}.{};{}' . format ( incremented_version [ 0 ] , incremented_version [ 1 ] , incremented_version [ 2 ] ) #NEWLINE# new_string = s . encode ( encoding ) #NEWLINE# if ( name . string != new_string ) : #NEWLINE# #INDENT# logging . info ( 'NAMEID_VERSION_STRING from {} to {}' . format ( name . string , new_string ) ) #NEWLINE# name . string = new_string #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( 'CFF ' in new_font . keys ( ) ) : #NEWLINE# #INDENT# ( major , minor , _ ) = incremented_version #NEWLINE# if ( new_font [ 'CFF ' ] . cff . major != major ) : #NEWLINE# #INDENT# new_font [ 'CFF ' ] . cff . major = major #NEWLINE# #UNINDENT# if ( new_font [ 'CFF ' ] . cff . minor != minor ) : #NEWLINE# #INDENT# new_font [ 'CFF ' ] . cff . minor = minor #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# new_font . close ( ) #NEWLINE# old_font . close ( )