Skip to content

Instantly share code, notes, and snippets.

Last active November 1, 2020 00:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
def filter_fonts_get_paths(df, root='./', variants=['_'], subsets=['_'], category=''):
# exceptions
if not variants or variants == [''] or variants == '': variants = ['_']
if not subsets or subsets == [''] or subsets == '': subsets = ['_']
# apply filters
regex_filters = variants + subsets + ['_'+category]
df_new = pd.concat([df.filter(regex=re.compile(regex, re.IGNORECASE), axis=1).sum(axis=1).astype(bool) for regex in regex_filters], axis=1)
mask = df_new.all(axis=1)
filtered_fontnames = list(df.loc[mask].family)
# construct file paths
paths = []
for fontname in filtered_fontnames:
if variants == ['_']: # select all variants
sel = glob.glob(f'{root}/{fontname.lower()}/**/*.ttf', recursive=True)
for variant in variants:
sel = glob.glob(f'{root}/{fontname.lower()}/**/{fontname}-{variant}.ttf', recursive=True)
for path in sel:
print(f'Found {len(paths)} font files.')
return paths
# let's try the function
paths = filter_fonts_get_paths(df, root=ROOT, subsets=['latin'], variants=['thinitalic'], category='sans-serif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment