Skip to content

Commit

Permalink
black the bin directory too
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Jun 5, 2024
1 parent b45a0e9 commit f4577af
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 113 deletions.
106 changes: 55 additions & 51 deletions bin/fonts-subset-support.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,75 @@
import sys
from absl import flags, app
from gftools.util import google_fonts as fonts
from gfsubsets import (CodepointsInFont,
CodepointsInSubset)
from gfsubsets import CodepointsInFont, CodepointsInSubset

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.')
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.')
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
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))
if result:
print("%s passed subset coverage" % (dirpath))


def _LeastSimilarCoverage(files, subset):
"""Returns pair of fonts having inconsistent coverage for a 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 = 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
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 = 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.
"""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(CodepointsInFont(file1)))
supportcps2 = len(subsetcps.intersection(CodepointsInFont(file2)))
return abs(supportcps1 - supportcps2)
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(CodepointsInFont(file1)))
supportcps2 = len(subsetcps.intersection(CodepointsInFont(file2)))
return abs(supportcps1 - supportcps2)


if __name__ == '__main__':
app.run(main)
if __name__ == "__main__":
app.run(main)
135 changes: 73 additions & 62 deletions bin/test_args.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#!/usr/bin/env python3
import sys

target_modules = [
"gftools-build-font2ttf",
"gftools-fix-ascii-fontmetadata",
"gftools-fix-familymetadata",
"gftools-fix-fsselection",
"gftools-fix-gasp",
"gftools-fix-glyph-private-encoding",
"gftools-fix-glyphs",
"gftools-fix-nameids",
# "gftools-fix-nonhinting", #this one do not even use argparse module
"gftools-fix-ttfautohint",
"gftools-fix-vendorid",
"gftools-fix-vertical-metrics",
"gftools-list-panose",
"gftools-list-weightclass",
"gftools-list-widthclass",
"gftools-metadata-vs-api",
"gftools-update-families"
"gftools-build-font2ttf",
"gftools-fix-ascii-fontmetadata",
"gftools-fix-familymetadata",
"gftools-fix-fsselection",
"gftools-fix-gasp",
"gftools-fix-glyph-private-encoding",
"gftools-fix-glyphs",
"gftools-fix-nameids",
# "gftools-fix-nonhinting", #this one do not even use argparse module
"gftools-fix-ttfautohint",
"gftools-fix-vendorid",
"gftools-fix-vertical-metrics",
"gftools-list-panose",
"gftools-list-weightclass",
"gftools-list-widthclass",
"gftools-metadata-vs-api",
"gftools-update-families",
]


help_text = {}
for module_name in target_modules:
target = __import__(module_name)
help_text[module_name] = target.parser.format_help()
target = __import__(module_name)
help_text[module_name] = target.parser.format_help()

# We need to extend this list with our
# minimal common interface for all scripts:
Expand All @@ -35,58 +36,68 @@
# We probably want to reduce this list to the bare minimum
# and maybe make some of these mandatory.
optional_args = [
"[-v]",
"[--autofix]",
"[--csv]",
"[--verbose]",
"[-a ASCENTS]",
"[-ah ASCENTS_HHEA]",
"[-at ASCENTS_TYPO]",
"[-aw ASCENTS_WIN]",
"[-d DESCENTS]",
"[-dh DESCENTS_HHEA]",
"[-dt DESCENTS_TYPO]",
"[-dw DESCENTS_WIN]",
"[-l LINEGAPS]",
"[-lh LINEGAPS_HHEA]",
"[-lt LINEGAPS_TYPO]",
"[--api API]",
"[--cache CACHE]",
"[--set SET]",
"[--platform PLATFORM]",
"[--id ID]",
"[--ignore-copy-existing-ttf]",
"[--with-otf]",
"[-e EXISTING]"
"[-v]",
"[--autofix]",
"[--csv]",
"[--verbose]",
"[-a ASCENTS]",
"[-ah ASCENTS_HHEA]",
"[-at ASCENTS_TYPO]",
"[-aw ASCENTS_WIN]",
"[-d DESCENTS]",
"[-dh DESCENTS_HHEA]",
"[-dt DESCENTS_TYPO]",
"[-dw DESCENTS_WIN]",
"[-l LINEGAPS]",
"[-lh LINEGAPS_HHEA]",
"[-lt LINEGAPS_TYPO]",
"[--api API]",
"[--cache CACHE]",
"[--set SET]",
"[--platform PLATFORM]",
"[--id ID]",
"[--ignore-copy-existing-ttf]",
"[--with-otf]",
"[-e EXISTING]",
]

failed = False
for arg in mandatory_args:
missing = []
for module_name in help_text.keys():
if arg not in help_text[module_name]:
missing.append(module_name)
missing = []
for module_name in help_text.keys():
if arg not in help_text[module_name]:
missing.append(module_name)

if missing != []:
failed = True
print (("ERROR: These modules lack the {} command line argument:"
"\nERROR:\t{}\n").format(arg, '\nERROR:\t'.join(missing)))
if missing != []:
failed = True
print(
(
"ERROR: These modules lack the {} command line argument:"
"\nERROR:\t{}\n"
).format(arg, "\nERROR:\t".join(missing))
)

import re

for module_name in help_text.keys():
text = help_text[module_name]
args = re.findall('(\[\-[^\[]*\])', text)
# print (args)
# print ("INFO: {}: {}".format(module_name, text))
for arg in args:
if arg not in optional_args + mandatory_args:
print (("WARNING: Module {} has cmdline argument {}"
" which is not in the list of optional ones."
"").format(module_name, arg))
text = help_text[module_name]
args = re.findall("(\[\-[^\[]*\])", text)
# print (args)
# print ("INFO: {}: {}".format(module_name, text))
for arg in args:
if arg not in optional_args + mandatory_args:
print(
(
"WARNING: Module {} has cmdline argument {}"
" which is not in the list of optional ones."
""
).format(module_name, arg)
)

# TODO: we also need to verify positional attributes like font and ttfont

if failed:
sys.exit("Some errors were detected in the command-line"
" arguments of the Font Bakery scripts.")

sys.exit(
"Some errors were detected in the command-line"
" arguments of the Font Bakery scripts."
)

0 comments on commit f4577af

Please sign in to comment.