Last active
November 15, 2016 18:42
-
-
Save m4rc1e/9591a3d7820ee6d1674b8d33615f3b99 to your computer and use it in GitHub Desktop.
Append '_new' to font names
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
import os | |
import sys | |
from glob import glob | |
from fontTools.ttLib import TTFont | |
import re | |
NAME_SUFFIX = '_new' | |
NAME_ENTRIES = [ | |
(1, 0, 0), # Mac | |
(3, 1, 1033) | |
] | |
def main(folder): | |
fonts_paths = [] | |
for file in os.listdir(folder): | |
if 'ttf' in file: | |
fonts_paths.append(file) | |
for font_path in fonts_paths: | |
font = TTFont(os.path.join(folder,font_path)) | |
new_filename = font_path[:-4] + NAME_SUFFIX + font_path[-4:] | |
old_fam_name = str(font['name'].getName(1, 1, 0, 0)).decode(font['name'].getName(1, 1, 0, 0).getEncoding()) | |
old_ps_name = str(font['name'].getName(6, 1, 0, 0)).decode(font['name'].getName(6, 1, 0, 0).getEncoding()) | |
for field in font['name'].names: | |
text = str(field).decode(field.getEncoding()) | |
# form = old_fam_name.encode(field.getEncoding()) | |
if old_fam_name in text: | |
new_fam_name = re.sub(old_fam_name, old_fam_name + NAME_SUFFIX, text) | |
font['name'].setName(new_fam_name, field.nameID, field.platformID, field.platEncID, field.langID) | |
elif old_ps_name in text: | |
new_ps_name = re.sub(old_ps_name, old_ps_name.split('-')[0] + NAME_SUFFIX + '-' + ''.join(old_ps_name.split('-')[1:]), text) | |
font['name'].setName(new_ps_name, field.nameID, field.platformID, field.platEncID, field.langID) | |
for field in font['name'].names: | |
print field | |
font.save(os.path.join(folder, new_filename)) | |
print 'saved %s' % os.path.join(folder, new_filename) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print '\nERROR: please include a path to the folder containing the fonts' | |
else: | |
folder = sys.argv[-1] | |
main(folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment