#!/usr/bin/python2
'''fontreview: A simple python FontForge script to show almost all the font metadata to help with diagnostics
and chasing duplicates.
It traverses all the directories from where it was launched and reports on fonts hits. 
You need a recent version of FontForge with its python module.
This works on all font formats FontForge supports.'''
__copyright__ = 'Copyright (c) 2018, SIL International  (http://www.sil.org)'
__license__ = 'Released under the MIT License (http://opensource.org/licenses/MIT)'
__author__ = 'Nicolas Spalinger. Thanks to Martin Hosken for his help.'
__version__ = '0.3'

import fontforge
import os

''' version checking to make sure we have a decently recent FontForge '''
required_version = "20100212"
if fontforge.version() < required_version:
    print('Your version of FontForge is too old - %s or newer is required' % (required_version))


def main():
    for dirpath, dirnames, files in os.walk("."):
        for file in files:
            if file.endswith(".ttf") or file.endswith(".otf") or file.endswith(".ufo") or file.endswith(".pfb") or file.endswith(".woff"): 
                targets = os.path.join(dirpath, file)
                font = fontforge.open(targets)
                ''' get the names for the sfnt object '''
                names = {}
                for n in font.sfnt_names:
                    if n[0] == "English (US)":
                        names[n[1]] = n[2]
                print(" ")
                print("------------------------------------ ")
                print(" ")
                print("File: " + font.path)
                print(" ")
                print("Full name: " + font.fullname)
                print(" ")
                print("Version: ")
                print(font.version)
                print(" ")
                print("Copyright: ")
                print(font.copyright)
                print(" ")
                print("License: ")
                print names.get('License')
                print(" ")
                print("License URL: ")
                print names.get('License URL')
                print(" ")
                print("Designer: ")
                print names.get('Designer')
                print(" ")
                print("Designer URL: ")
                print names.get('Designer URL')
                print(" ")
                print("Manufacturer: ")
                print names.get('Manufacturer')
                print(" ")
                print("Vendor URL: ")
                print names.get('Vendor URL')
                print(" ")
                print("Trademark: ")
                print names.get('Trademark')
                print(" ")
                print("Embedding restrictions in fstype table:")
                print(font.os2_fstype)
                print(" ")


if __name__ == '__main__':
    main()
