Created
April 16, 2025 06:14
-
-
Save ikouchiha47/734870f7e95fd45506a6754542b46ed4 to your computer and use it in GitHub Desktop.
font patcher
This file contains hidden or 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
from fontTools.ttLib import TTFont | |
import os | |
def rename_font_family(input_path, output_path, old_family="M+1", new_family="M+11"): | |
font = TTFont(input_path) | |
name_table = font["name"] | |
print(name_table) | |
for record in name_table.names: | |
old_value = record.string.decode(record.getEncoding(), errors="replace") | |
if old_family in old_value: | |
new_value = old_value.replace(old_family, new_family) | |
record.string = new_value.encode(record.getEncoding()) | |
print(f"Updated: {old_value} → {new_value}") | |
# Update filename | |
os.makedirs(os.path.dirname(output_path), exist_ok=True) | |
font.save(output_path) | |
font.close() | |
print(f"Saved renamed font to: {output_path}") | |
def bulk_rename_fonts(input_dir, output_dir, old_family="M+1", new_family="M+11"): | |
for root, _, files in os.walk(input_dir): | |
for filename in files: | |
if filename.lower().endswith((".ttf", ".otf")): | |
input_path = os.path.join(root, filename) | |
rel_path = os.path.relpath(input_path, input_dir) | |
output_path = os.path.join(output_dir, rel_path) | |
rename_font_family(input_path, output_path, old_family, new_family) | |
# Example usage: | |
input_folder = "/Users/darksied/Downloads/MPlus/M+11" | |
output_folder = f"{input_folder}/renames" | |
bulk_rename_fonts(input_folder, output_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment