| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from __future__ import print_function |
| 17 |
import itertools |
| 18 |
import os |
| 19 |
import sys |
| 20 |
from absl import flags, app |
| 21 |
from gftools.util import google_fonts as fonts |
| 22 |
from gfsubsets import CodepointsInFont, CodepointsInSubset |
| 23 |
|
| 24 |
FLAGS = flags.FLAGS |
| 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 |
) |
| 31 |
|
| 32 |
|
| 33 |
def main(argv): |
| 34 |
if len(argv) != 2 or not os.path.isdir(argv[1]): |
| 35 |
sys.exit("Must have one argument, a directory containing font files.") |
| 36 |
|
| 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 |
| 54 |
|
| 55 |
if result: |
| 56 |
print("%s passed subset coverage" % (dirpath)) |
| 57 |
|
| 58 |
|
| 59 |
def _LeastSimilarCoverage(files, subset): |
| 60 |
"""Returns pair of fonts having inconsistent coverage for a subset. |
| 61 |
|
| 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 |
| 75 |
|
| 76 |
|
| 77 |
def _InconsistentSubsetSupport(file1, file2, subsetcps): |
| 78 |
"""Returns difference in number of codepoints supported. |
| 79 |
|
| 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) |
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__": |
| 93 |
app.run(main) |