69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
import os
|
|
import re
|
|
|
|
# Directory with the font files
|
|
directory = './'
|
|
|
|
# Get list of font files
|
|
font_files = os.listdir(directory)
|
|
|
|
# Initialize an empty list for the typefaces
|
|
typefaces = []
|
|
|
|
# Loop over the font files
|
|
for file in font_files:
|
|
# Strip the file extension
|
|
name_weight, extension = os.path.splitext(file)
|
|
|
|
# Only process .ttf, .otf, .woff and .woff2 files
|
|
if extension.lower() not in ['.ttf', '.otf', '.woff', '.woff2']:
|
|
continue
|
|
|
|
# Split the name and weight
|
|
match = re.match(r'(.*)_(\d+)(I)?', name_weight)
|
|
if match:
|
|
name, weight, is_italic = match.groups()
|
|
else:
|
|
name = name_weight
|
|
weight = "400"
|
|
is_italic = None
|
|
|
|
# Check if the typeface is already in the list
|
|
for typeface in typefaces:
|
|
if typeface['name'] == name:
|
|
break
|
|
else:
|
|
# If the typeface is not in the list, add a new one
|
|
typeface = {
|
|
"name": name,
|
|
"fonts" : [],
|
|
"tags" : ["sans-serif"],
|
|
}
|
|
typefaces.append(typeface)
|
|
|
|
# Add file to the typeface
|
|
font_descriptor = {
|
|
'weight': weight,
|
|
'style': 'italic' if is_italic else 'normal',
|
|
'file': file,
|
|
'format': 'truetype' if extension == '.ttf' else 'opentype' if extension == '.otf' else 'woff' if extension == '.woff' else 'woff2'
|
|
}
|
|
typeface['fonts'].append(font_descriptor)
|
|
|
|
# Create CSS files
|
|
for typeface in typefaces:
|
|
css_content = ''
|
|
|
|
for font in typeface['fonts']:
|
|
css_content += f"""
|
|
@font-face {{
|
|
font-family: '{typeface["name"]}';
|
|
src: url('{font["file"]}') format('{font["format"]}');
|
|
font-weight: {font["weight"]};
|
|
font-style: {font["style"]};
|
|
}}
|
|
"""
|
|
|
|
with open(f'{typeface["name"].lower()}.css', 'w') as f:
|
|
f.write(css_content)
|