|
19 | 19 | import sys
|
20 | 20 | from absl import flags, app
|
21 | 21 | from gftools.util import google_fonts as fonts
|
22 |
| -from gfsubsets import (CodepointsInFont, |
23 |
| - CodepointsInSubset) |
| 22 | +from gfsubsets import CodepointsInFont, CodepointsInSubset |
24 | 23 |
|
25 | 24 | FLAGS = flags.FLAGS
|
26 |
| -flags.DEFINE_integer('max_diff_cps', 5, |
27 |
| - 'Maximum difference in number of codepoints allowed for' |
28 |
| - ' a particular subset before which it is flagged.') |
| 25 | +flags.DEFINE_integer( |
| 26 | + "max_diff_cps", |
| 27 | + 5, |
| 28 | + "Maximum difference in number of codepoints allowed for" |
| 29 | + " a particular subset before which it is flagged.", |
| 30 | +) |
29 | 31 |
|
30 | 32 |
|
31 | 33 | def main(argv):
|
32 |
| - if len(argv) != 2 or not os.path.isdir(argv[1]): |
33 |
| - sys.exit('Must have one argument, a directory containing font files.') |
| 34 | + if len(argv) != 2 or not os.path.isdir(argv[1]): |
| 35 | + sys.exit("Must have one argument, a directory containing font files.") |
34 | 36 |
|
35 |
| - sys.stderr = open(os.devnull, 'w') |
36 |
| - dirpath = argv[1] |
37 |
| - result = True |
38 |
| - files = [] |
39 |
| - for font in fonts.Metadata(dirpath).fonts: |
40 |
| - files.append(os.path.join(dirpath, font.filename)) |
41 |
| - for subset in fonts.Metadata(dirpath).subsets: |
42 |
| - if subset == 'menu': |
43 |
| - continue |
44 |
| - (file1, file2, diff_size) = _LeastSimilarCoverage(files, subset) |
45 |
| - if diff_size > FLAGS.max_diff_cps: |
46 |
| - print('%s coverage for %s failed' % (dirpath, subset)) |
47 |
| - print('Difference of codepoints between %s & %s is %d' % ( |
48 |
| - file1, file2, diff_size)) |
49 |
| - result = False |
| 37 | + sys.stderr = open(os.devnull, "w") |
| 38 | + dirpath = argv[1] |
| 39 | + result = True |
| 40 | + files = [] |
| 41 | + for font in fonts.Metadata(dirpath).fonts: |
| 42 | + files.append(os.path.join(dirpath, font.filename)) |
| 43 | + for subset in fonts.Metadata(dirpath).subsets: |
| 44 | + if subset == "menu": |
| 45 | + continue |
| 46 | + (file1, file2, diff_size) = _LeastSimilarCoverage(files, subset) |
| 47 | + if diff_size > FLAGS.max_diff_cps: |
| 48 | + print("%s coverage for %s failed" % (dirpath, subset)) |
| 49 | + print( |
| 50 | + "Difference of codepoints between %s & %s is %d" |
| 51 | + % (file1, file2, diff_size) |
| 52 | + ) |
| 53 | + result = False |
50 | 54 |
|
51 |
| - if result: |
52 |
| - print('%s passed subset coverage' % (dirpath)) |
| 55 | + if result: |
| 56 | + print("%s passed subset coverage" % (dirpath)) |
53 | 57 |
|
54 | 58 |
|
55 | 59 | def _LeastSimilarCoverage(files, subset):
|
56 |
| - """Returns pair of fonts having inconsistent coverage for a subset. |
| 60 | + """Returns pair of fonts having inconsistent coverage for a subset. |
57 | 61 |
|
58 |
| - Args: |
59 |
| - files: List of font files |
60 |
| - subset: Name of subset |
61 |
| - Returns: |
62 |
| - 3 tuple of (file1, file2, number of codepoints difference) |
63 |
| - """ |
64 |
| - worst = (None, None, 0) |
65 |
| - subsetcps = CodepointsInSubset(subset, True) |
66 |
| - for pair in itertools.combinations(files, 2): |
67 |
| - inconsistency = _InconsistentSubsetSupport(pair[0], pair[1], subsetcps) |
68 |
| - if inconsistency > worst[2]: |
69 |
| - worst = (pair[0], pair[1], inconsistency) |
70 |
| - return worst |
| 62 | + Args: |
| 63 | + files: List of font files |
| 64 | + subset: Name of subset |
| 65 | + Returns: |
| 66 | + 3 tuple of (file1, file2, number of codepoints difference) |
| 67 | + """ |
| 68 | + worst = (None, None, 0) |
| 69 | + subsetcps = CodepointsInSubset(subset, True) |
| 70 | + for pair in itertools.combinations(files, 2): |
| 71 | + inconsistency = _InconsistentSubsetSupport(pair[0], pair[1], subsetcps) |
| 72 | + if inconsistency > worst[2]: |
| 73 | + worst = (pair[0], pair[1], inconsistency) |
| 74 | + return worst |
71 | 75 |
|
72 | 76 |
|
73 | 77 | def _InconsistentSubsetSupport(file1, file2, subsetcps):
|
74 |
| - """Returns difference in number of codepoints supported. |
| 78 | + """Returns difference in number of codepoints supported. |
75 | 79 |
|
76 |
| - Args: |
77 |
| - file1: Name of font file |
78 |
| - file2: Name of font file |
79 |
| - subsetcps: Complete set of codepoints to be supported |
80 |
| - Returns: |
81 |
| - Difference in number of codepoints between file1 and file2. |
82 |
| - """ |
83 |
| - supportcps1 = len(subsetcps.intersection(CodepointsInFont(file1))) |
84 |
| - supportcps2 = len(subsetcps.intersection(CodepointsInFont(file2))) |
85 |
| - return abs(supportcps1 - supportcps2) |
| 80 | + Args: |
| 81 | + file1: Name of font file |
| 82 | + file2: Name of font file |
| 83 | + subsetcps: Complete set of codepoints to be supported |
| 84 | + Returns: |
| 85 | + Difference in number of codepoints between file1 and file2. |
| 86 | + """ |
| 87 | + supportcps1 = len(subsetcps.intersection(CodepointsInFont(file1))) |
| 88 | + supportcps2 = len(subsetcps.intersection(CodepointsInFont(file2))) |
| 89 | + return abs(supportcps1 - supportcps2) |
86 | 90 |
|
87 | 91 |
|
88 |
| -if __name__ == '__main__': |
89 |
| - app.run(main) |
| 92 | +if __name__ == "__main__": |
| 93 | + app.run(main) |
0 commit comments