forked from fonttools/fontbakery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfontbakery-fix-nonhinting.py
More file actions
executable file
·109 lines (90 loc) · 2.89 KB
/
fontbakery-fix-nonhinting.py
File metadata and controls
executable file
·109 lines (90 loc) · 2.89 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
#
# The magic is in two places:
#
# 1. The GASP table. Vern Adams <[email protected]>
# suggests it should have value 15 for all sizes.
#
# 2. The PREP table. Raph Levien <[email protected]>
# suggests using his code to turn on 'drop out control'
#
# PUSHW_1
# 511
# SCANCTRL
# PUSHB_1
# 4
# SCANTYPE
#
# This script depends on fontTools Python library, available
# in most packaging systems and sf.net/projects/fonttools/
#
# Usage:
#
# $ ./fontbakery-fix-nonhinting.py FontIn.ttf FontOut.ttf
# Import our system library and fontTools ttLib
import argparse
import os
from fontTools import ttLib
from fontTools.ttLib.tables import ttProgram
description = 'Fixes TTF GASP table so that its program ' \
'contains the minimal recommended instructions'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('fontfile_in',
nargs=1,
help="Font in OpenType (TTF/OTF) format")
parser.add_argument('fontfile_out',
nargs=1,
help="Filename for the output")
def main():
args = parser.parse_args()
# Open the font file supplied as the first argument on the command line
fontfile_in = os.path.abspath(args.fontfile_in[0])
font = ttLib.TTFont(fontfile_in)
# Save a backup
backupfont = '{}-backup-fonttools-prep-gasp{}'.format(fontfile_in[0:-4],
fontfile_in[-4:])
# print "Saving to ", backupfont
font.save(backupfont)
print backupfont, " saved."
# Print the Gasp table
if "gasp" in font:
print ("GASP was: ", font["gasp"].gaspRange)
else:
print ("GASP wasn't there")
# Print the PREP table
if "prep" in font:
old_program = ttProgram.Program.getAssembly(font["prep"].program)
print ("PREP was:\n\t" + "\n\t".join(old_program))
else:
print ("PREP wasn't there")
# Create a new GASP table
gasp = ttLib.newTable("gasp")
# Set GASP to the magic number
gasp.gaspRange = {0xFFFF: 15}
# Create a new hinting program
program = ttProgram.Program()
assembly = ['PUSHW[]',
'511',
'SCANCTRL[]',
'PUSHB[]',
'4',
'SCANTYPE[]']
program.fromAssembly(assembly)
# Create a new PREP table
prep = ttLib.newTable("prep")
# Insert the magic program into it
prep.program = program
# Add the tables to the font, replacing existing ones
font["gasp"] = gasp
font["prep"] = prep
# Print the Gasp table
print "GASP now: ", font["gasp"].gaspRange
# Print the PREP table
current_program = ttProgram.Program.getAssembly(font["prep"].program)
print ("PREP now:\n\t" + "\n\t".join(current_program))
# Save the new file with the name of the input file
fontfile_out = os.path.abspath(args.fontfile_out[0])
font.save(fontfile_out)
print fontfile_out, " saved."
if __name__ == "__main__":
main()