-
Notifications
You must be signed in to change notification settings - Fork 33
/
rename-font
74 lines (59 loc) · 2.64 KB
/
rename-font
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# coding=utf8
import os.path
import fontforge
import sys, logging
from argparse import ArgumentParser
sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])) + '/bin/scripts/name_parser/')
from FontnameParser import FontnameParser
from TableHEADWriter import TableHEADWriter
# Setup and parse the comand-line arguments
parser = ArgumentParser()
parser.add_argument("--input", help="input file name")
parser.add_argument("--orig", help="original font filename")
parser.add_argument("--output", help="output file name")
parser.add_argument("--name", help="preferred family name")
parser.add_argument("--version", help="text to add to the existing version")
args = parser.parse_args()
SIL_TABLE = [('cascadia ?(code|mono)( ?pl)?', args.name), ]
print("\nRenaming process\n {}\n as {}\n -> {}".format(args.input, args.orig, args.output))
fname = os.path.splitext(os.path.basename(args.orig))[0]
logger = logging.getLogger()
n = FontnameParser(fname, logger)
n.add_name_substitution_table(SIL_TABLE)
n.set_keep_regular_in_family(False)
if not n.parse_ok:
sys.exit("Something with name parsing went wrong")
# Open the file
delugia=fontforge.open(args.input)
# Rename the file
n.rename_font(delugia)
# Other information
delugia.appendSFNTName("English (US)", "UniqueID", "{};{}".format(args.version, n.psname()))
delugia.appendSFNTName("English (US)", "Trademark", "")
# Mix our version information in
delugia.sfntRevision = None # Auto-set (refreshed) by fontforge
delugia.appendSFNTName("English (US)", "Version", args.version)
delugia.version = args.version
if n.psname().lower().find("mono"):
# For MS-Windows console apps
panose = list(delugia.os2_panose)
if panose[0] < 3: # https://forum.high-logic.com/postedfiles/Panose.pdf
panose[3] = 9 # 3 (4th value) = propotion: 9 = monospaced
delugia.os2_panose = tuple(panose)
# Save
delugia.generate(args.output)
print("Generated '{}' from {} version {}".format(args.output, n.fullname(), args.version))
# Fix fontforge destroying the font flags
source_font = TableHEADWriter(args.orig)
dest_font = TableHEADWriter(args.output)
source_font.find_head_table(0)
dest_font.find_head_table(0)
print("Changing flags from 0x{:X} to 0x{:X}".format(dest_font.flags, source_font.flags))
dest_font.putshort(source_font.flags, 'flags') # clear 'ppem_to_int' etc
print("Changing lowestRecPPEM from {} to {}".format(dest_font.lowppem, source_font.lowppem))
dest_font.putshort(source_font.lowppem, 'lowestRecPPEM')
dest_font.reset_table_checksum()
dest_font.reset_full_checksum()
source_font.close()
dest_font.close()