Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 CompareDirs(font1, font2):
"""Compares fonts by assuming font1/2 are dirs containing METADATA.pb."""
m1 = fonts.Metadata(font1)
m2 = fonts.Metadata(font2)
subsets_to_compare = fonts.UniqueSort(m1.subsets, m2.subsets)
subsets_to_compare.remove('menu')
subsets_to_compare.append('all')
font_filename1 = os.path.join(font1, fonts.RegularWeight(m1))
font_filename2 = os.path.join(font2, fonts.RegularWeight(m2))
if FLAGS.diff_coverage:
print('Subset Coverage Change (codepoints)')
for subset in subsets_to_compare:
DiffCoverage(font_filename1, font_filename2, subset)
print(CompareSize(font_filename1, font_filename2))
def _SanityCheck(path):
"""Runs various sanity checks on the font family under path.
Args:
path: A directory containing a METADATA.pb file.
Returns:
A list of ResultMessageTuple's.
"""
try:
fonts.Metadata(path)
except ValueError as e:
return [_SadResult('Bad METADATA.pb: ' + e.message, path)]
results = []
if FLAGS.check_metadata:
results.extend(_CheckLicense(path))
results.extend(_CheckNameMatching(path))
if FLAGS.check_font:
results.extend(_CheckFontInternalValues(path))
return results
def _CheckLicense(path):
"""Verifies that METADATA.pb license is correct under path.
Assumes path is of the form ...///METADATA.pb.
Args:
path: A directory containing a METADATA.pb file.
Returns:
A list with one ResultMessageTuple. If happy, license is good.
"""
metadata = fonts.Metadata(path)
lic = metadata.license
lic_dir = os.path.basename(os.path.dirname(path))
# We use /apache for the license Apache2
if lic_dir == 'apache':
lic_dir += '2'
result = _HappyResult('License consistantly %s' % lic, path)
# if we were Python 3 we'd use casefold(); this will suffice
if lic_dir.lower() != lic.lower():
result = _SadResult('Dir license != METADATA license: %s != %s' %
(lic_dir, lic), path)
return [result]
def _CheckNameMatching(path):
"""Verifies the various name fields in the METADATA.pb file are sane.
Args:
path: A directory containing a METADATA.pb file.
Returns:
A list of ResultMessageTuple, one per validation performed.
"""
results = []
metadata = fonts.Metadata(path)
name = metadata.name
for font in metadata.fonts:
# We assume style/weight is correct in METADATA
style = font.style
weight = font.weight
values = [('name', name, font.name), ('filename', fonts.FilenameFor(
name, style, weight, '.ttf'), font.filename),
('postScriptName', fonts.FilenameFor(name, style, weight),
font.post_script_name), ('fullName', fonts.FullnameFor(
name, style, weight), font.full_name)]
for (key, expected, actual) in values:
if expected != actual:
results.append(
_SadResult('%s METADATA %s/%d %s expected %s, got %s' %
def _CheckFontInternalValues(path):
"""Validates fonts internal metadata matches METADATA.pb values.
In particular, checks 'OS/2' {usWeightClass, fsSelection, fsType} and 'name'
{fullName, postScriptName} values.
Args:
path: A directory containing a METADATA.pb file.
Returns:
A list of ResultMessageTuple, one per validation performed.
"""
results = []
metadata = fonts.Metadata(path)
name = metadata.name
for font in metadata.fonts:
font_file = font.filename
with contextlib.closing(ttLib.TTFont(os.path.join(path, font_file))) as ttf:
results.extend(_CheckFontOS2Values(path, font, ttf))
results.extend(_CheckFontNameValues(path, name, font, ttf))
results.extend(_CheckLSB0ForEmptyGlyphs(path, font, ttf))
return results
def CompareDirs(font1, font2):
"""Compares fonts by assuming font1/2 are dirs containing METADATA.pb."""
m1 = fonts.Metadata(font1)
m2 = fonts.Metadata(font2)
subsets_to_compare = fonts.UniqueSort(m1.subsets, m2.subsets)
subsets_to_compare.remove('menu')
subsets_to_compare.append('all')
font_filename1 = os.path.join(font1, fonts.RegularWeight(m1))
font_filename2 = os.path.join(font2, fonts.RegularWeight(m2))
if FLAGS.diff_coverage:
print('Subset Coverage Change (codepoints)')
for subset in subsets_to_compare:
DiffCoverage(font_filename1, font_filename2, subset)
print(CompareSize(font_filename1, font_filename2))