13
13
# limitations under the License.
14
14
15
15
import array
16
+ import os
17
+ from typing import Union
16
18
17
19
from dehinter .bitops import is_bit_k_set , clear_bit_k
18
20
19
21
20
22
# ========================================================
21
23
# Utilities
22
24
# ========================================================
23
- def has_cvt_table (tt ):
25
+ def has_cvt_table (tt ) -> bool :
24
26
"""Tests for the presence of a cvt table in a TrueType font."""
25
27
return "cvt " in tt
26
28
27
29
28
- def has_fpgm_table (tt ):
30
+ def has_fpgm_table (tt ) -> bool :
29
31
"""Tests for the presence of a fpgm table in a TrueType font."""
30
32
return "fpgm" in tt
31
33
32
34
33
- def has_gasp_table (tt ):
35
+ def has_gasp_table (tt ) -> bool :
34
36
"""Tests for the presence of a gasp table in a TrueType font."""
35
37
return "gasp" in tt
36
38
37
39
38
- def has_hdmx_table (tt ):
40
+ def has_hdmx_table (tt ) -> bool :
39
41
"""Tests for the presence of a hdmx table in a TrueType font."""
40
42
return "hdmx" in tt
41
43
42
44
43
- def has_ltsh_table (tt ):
45
+ def has_ltsh_table (tt ) -> bool :
44
46
"""Tests for the presence of a LTSH table in a TrueType font."""
45
47
return "LTSH" in tt
46
48
47
49
48
- def has_prep_table (tt ):
50
+ def has_prep_table (tt ) -> bool :
49
51
"""Tests for the presence of a prep table in a TrueType font."""
50
52
return "prep" in tt
51
53
52
54
53
- def has_ttfa_table (tt ):
55
+ def has_ttfa_table (tt ) -> bool :
54
56
"""Tests for the presence of a TTFA table in a TrueType font."""
55
57
return "TTFA" in tt
56
58
57
59
58
- def has_vdmx_table (tt ):
60
+ def has_vdmx_table (tt ) -> bool :
59
61
"""Tests for the presence of a VDMX table in a TrueType font."""
60
62
return "VDMX" in tt
61
63
62
64
63
- def is_truetype_font (filepath ) :
65
+ def is_truetype_font (filepath : Union [ bytes , str , "os.PathLike[str]" ]) -> bool :
64
66
"""Tests that a font has the TrueType file signature of either:
65
67
1) b'\x00 \x01 \x00 \x00 '
66
68
2) b'\x74 \x72 \x75 \x65 ' == 'true'"""
67
69
with open (filepath , "rb" ) as f :
68
- file_signature = f .read (4 )
70
+ file_signature : bytes = f .read (4 )
69
71
70
72
return file_signature in (b"\x00 \x01 \x00 \x00 " , b"\x74 \x72 \x75 \x65 " )
71
73
72
74
73
75
# ========================================================
74
76
# OpenType table removal
75
77
# ========================================================
76
- def remove_cvt_table (tt ):
78
+ def remove_cvt_table (tt ) -> None :
77
79
"""Removes cvt table from a fontTools.ttLib.TTFont object"""
78
80
try :
79
81
del tt ["cvt " ]
@@ -82,7 +84,7 @@ def remove_cvt_table(tt):
82
84
pass
83
85
84
86
85
- def remove_fpgm_table (tt ):
87
+ def remove_fpgm_table (tt ) -> None :
86
88
"""Removes fpgm table from a fontTools.ttLib.TTFont object"""
87
89
try :
88
90
del tt ["fpgm" ]
@@ -91,7 +93,7 @@ def remove_fpgm_table(tt):
91
93
pass
92
94
93
95
94
- def remove_hdmx_table (tt ):
96
+ def remove_hdmx_table (tt ) -> None :
95
97
"""Removes hdmx table from a fontTools.ttLib.TTFont object"""
96
98
try :
97
99
del tt ["hdmx" ]
@@ -100,7 +102,7 @@ def remove_hdmx_table(tt):
100
102
pass
101
103
102
104
103
- def remove_ltsh_table (tt ):
105
+ def remove_ltsh_table (tt ) -> None :
104
106
"""Removes LTSH table from a fontTools.ttLib.TTFont object."""
105
107
try :
106
108
del tt ["LTSH" ]
@@ -109,7 +111,7 @@ def remove_ltsh_table(tt):
109
111
pass
110
112
111
113
112
- def remove_prep_table (tt ):
114
+ def remove_prep_table (tt ) -> None :
113
115
"""Removes prep table from a fontTools.ttLib.TTFont object"""
114
116
try :
115
117
del tt ["prep" ]
@@ -118,7 +120,7 @@ def remove_prep_table(tt):
118
120
pass
119
121
120
122
121
- def remove_ttfa_table (tt ):
123
+ def remove_ttfa_table (tt ) -> None :
122
124
"""Removes TTFA table from a fontTools.ttLib.TTFont object"""
123
125
try :
124
126
del tt ["TTFA" ]
@@ -127,7 +129,7 @@ def remove_ttfa_table(tt):
127
129
pass
128
130
129
131
130
- def remove_vdmx_table (tt ):
132
+ def remove_vdmx_table (tt ) -> None :
131
133
"""Removes TTFA table from a fontTools.ttLib.TTFont object"""
132
134
try :
133
135
del tt ["VDMX" ]
@@ -139,9 +141,9 @@ def remove_vdmx_table(tt):
139
141
# ========================================================
140
142
# glyf table instruction set bytecode removal
141
143
# ========================================================
142
- def remove_glyf_instructions (tt ):
144
+ def remove_glyf_instructions (tt ) -> int :
143
145
"""Removes instruction set bytecode from glyph definitions in the glyf table."""
144
- glyph_number = 0
146
+ glyph_number : int = 0
145
147
for glyph in tt ["glyf" ].glyphs .values ():
146
148
glyph .expand (tt ["glyf" ])
147
149
if hasattr (glyph , "program" ) and glyph .program .bytecode != array .array ("B" , []):
@@ -157,7 +159,7 @@ def remove_glyf_instructions(tt):
157
159
# ========================================================
158
160
# gasp table edit
159
161
# ========================================================
160
- def update_gasp_table (tt ):
162
+ def update_gasp_table (tt ) -> bool :
161
163
"""Modifies the following gasp table fields:
162
164
1) rangeMaxPPEM changed to 65535
163
165
2) rangeGaspBehavior changed to 0x000a (symmetric grayscale, no gridfit)"""
@@ -171,9 +173,9 @@ def update_gasp_table(tt):
171
173
# =========================================
172
174
# maxp table edits
173
175
# =========================================
174
- def update_maxp_table (tt ):
176
+ def update_maxp_table (tt ) -> bool :
175
177
"""Update the maxp table with new values based on elimination of instruction sets."""
176
- changed = False
178
+ changed : bool = False
177
179
if tt ["maxp" ].maxZones != 0 :
178
180
tt ["maxp" ].maxZones = 0
179
181
changed = True
@@ -198,7 +200,7 @@ def update_maxp_table(tt):
198
200
# =========================================
199
201
# head table edits
200
202
# =========================================
201
- def update_head_table_flags (tt ):
203
+ def update_head_table_flags (tt ) -> bool :
202
204
if is_bit_k_set (tt ["head" ].flags , 4 ):
203
205
# confirm that there is no LTSH or hdmx table
204
206
# bit 4 should be set if either of these tables are present in font
0 commit comments