From e66aa71025c314bfecefec578163a84fe51eb132 Mon Sep 17 00:00:00 2001 From: Nyshadh Reddy Rachamallu Date: Mon, 31 Jul 2017 10:42:35 -0700 Subject: [PATCH] Add 2 new scripts to check subset coverage --- tools/font_weights_coverage.py | 55 ++++++++++++++++++++++++ tools/fonts_subset_support.py | 76 ++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 tools/font_weights_coverage.py create mode 100644 tools/fonts_subset_support.py diff --git a/tools/font_weights_coverage.py b/tools/font_weights_coverage.py new file mode 100644 index 000000000..5b6d9a208 --- /dev/null +++ b/tools/font_weights_coverage.py @@ -0,0 +1,55 @@ +"""Tool to check codepoint coverage in all font weights. + +Ex: If FamilyName-Regular.ttf supports codepoints A-D + FamilyName-Bold.ttf supports codepoints B-E + FamilyName-Light.ttf supports codepoints A-E + +$ python tools/font_wieghts+coverage.py ofl/familyname +FamilyName-Regular.ttf failed +0x0045 +FamilyName-Bold.ttf failed +0x0041 +FamilyName-Light.ttf passed +""" + + +import os +from os import listdir +import sys + +from google.apputils import app +from util import google_fonts as fonts + + +def main(argv): + if len(argv) != 2 or not os.path.isdir(argv[1]): + sys.exit('Must have one argument, a directory containing font files.') + + dirpath = argv[1] + cps = set() + for f in _GetFontFiles(dirpath): + cps.update(fonts.CodepointsInFont(os.path.join(dirpath, f))) + + for f in _GetFontFiles(dirpath): + diff = cps - fonts.CodepointsInFont(os.path.join(dirpath, f)) + if bool(diff): + print '%s failed' % (f) + for c in diff: + print '0x%04X' % (c) + else: + print '%s passed' % (f) + + +def _GetFontFiles(path): + """Returns list of font files in a path. + + Args: + path: directory path + Returns: + Set of font files + """ + return [f for f in listdir(path) + if os.path.splitext(f)[1] in ('.ttf', '.otf')] + +if __name__ == '__main__': + app.run() diff --git a/tools/fonts_subset_support.py b/tools/fonts_subset_support.py new file mode 100644 index 000000000..dc04c1a4c --- /dev/null +++ b/tools/fonts_subset_support.py @@ -0,0 +1,76 @@ + + +import itertools +import os +import sys + +from google.apputils import app +import gflags as flags +from util import google_fonts as fonts + +FLAGS = flags.FLAGS + +flags.DEFINE_integer('max_diff_cps', 5, + 'Maximum difference in number of codepoints allowed for' + ' a particular subset before which it is flagged.') + + +def main(argv): + if len(argv) != 2 or not os.path.isdir(argv[1]): + sys.exit('Must have one argument, a directory containing font files.') + + sys.stderr = open(os.devnull, 'w') + dirpath = argv[1] + result = True + files = [] + for font in fonts.Metadata(dirpath).fonts: + files.append(os.path.join(dirpath, font.filename)) + for subset in fonts.Metadata(dirpath).subsets: + if subset == 'menu': + continue + (file1, file2, diff_size) = _LeastSimilarCoverage(files, subset) + if diff_size > FLAGS.max_diff_cps: + print '%s coverage for %s failed' % (dirpath, subset) + print 'Difference of codepoints between %s & %s is %d' % ( + file1, file2, diff_size) + result = False + + if result: + print '%s passed subset coverage' % (dirpath) + + +def _LeastSimilarCoverage(files, subset): + """Returns pair of fonts having inconsistent coverage for a subset. + + Args: + files: List of font files + subset: Name of subset + Returns: + 3 tuple of (file1, file2, number of codepoints difference) + """ + worst = (None, None, 0) + subsetcps = fonts.CodepointsInSubset(subset, True) + for pair in itertools.combinations(files, 2): + inconsistency = _InconsistentSubsetSupport(pair[0], pair[1], subsetcps) + if inconsistency > worst[2]: + worst = (pair[0], pair[1], inconsistency) + return worst + + +def _InconsistentSubsetSupport(file1, file2, subsetcps): + """Returns difference in number of codepoints supported. + + Args: + file1: Name of font file + file2: Name of font file + subsetcps: Complete set of codepoints to be supported + Returns: + Difference in number of codepoints between file1 and file2. + """ + supportcps1 = len(subsetcps.intersection(fonts.CodepointsInFont(file1))) + supportcps2 = len(subsetcps.intersection(fonts.CodepointsInFont(file2))) + return abs(supportcps1 - supportcps2) + + +if __name__ == '__main__': + app.run()