def set_unique_name(ttx_path, unique_name): tree = parse(ttx_path) root = tree.getroot() for child in root.find('name'): if child.attrib['nameID'] == '3': child.text = unique_name tree.write(ttx_path)
def find_and_replace_ttx(ttx_path, find_string, replace_string, tables=['name']): count = 0 # 1. modify 'name' table if 'name' in tables: tree = parse(ttx_path) root = tree.getroot() for child in root.find('name'): if child.text.find(find_string) != -1: new_text = child.text.replace(find_string, replace_string) child.text = new_text count += 1 tree.write(ttx_path) # 2. modify 'CFF ' table if 'CFF ' in tables: CFF_elements = ['version', 'Notice', 'Copyright', 'FullName', 'FamilyName', 'Weight'] tt_font = TTFont() tt_font.importXML(ttx_path) font_dict = tt_font['CFF '].cff.topDictIndex.items[0] for element in CFF_elements: text = getattr(font_dict, element) if text.find(find_string) != -1: new_text = text.replace(find_string, replace_string) setattr(font_dict, element, new_text) count += 1 tt_font.saveXML(ttx_path) # done return count
def set_version_string(ttx_path, string_text): tree = parse(ttx_path) root = tree.getroot() for child in root.find('name'): if child.attrib['nameID'] == '5': child.text = string_text tree.write(ttx_path)
def strip_names(ttx_path): """Clear several nameIDs to prevent the font from being installable on desktop OSs. ttx_path: Path of the .ttx font to be modified. """ # nameIDs which will be erased nameIDs = [1, 2, 4, 16, 17, 18] tree = parse(ttx_path) root = tree.getroot() for child in root.find('name'): if int(child.attrib['nameID']) in nameIDs: child.text = ' ' tree.write(ttx_path)