This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
paths.extend(sel) | |
else: | |
for variant in variants: | |
sel = glob.glob(f'{root}/{fontname.lower()}/**/{fontname}-{variant}.ttf', recursive=True) | |
for path in sel: | |
paths.append(path) | |
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