mirror of
https://github.com/google/fonts.git
synced 2024-11-24 01:37:48 +03:00
Add 2 new scripts to check subset coverage
This commit is contained in:
parent
196332b202
commit
e66aa71025
55
tools/font_weights_coverage.py
Normal file
55
tools/font_weights_coverage.py
Normal file
@ -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()
|
76
tools/fonts_subset_support.py
Normal file
76
tools/fonts_subset_support.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user