1313# limitations under the License.
1414
1515import array
16+ import os
17+ from typing import Union
1618
1719from dehinter .bitops import is_bit_k_set , clear_bit_k
1820
1921
2022# ========================================================
2123# Utilities
2224# ========================================================
23- def has_cvt_table (tt ):
25+ def has_cvt_table (tt ) -> bool :
2426 """Tests for the presence of a cvt table in a TrueType font."""
2527 return "cvt " in tt
2628
2729
28- def has_fpgm_table (tt ):
30+ def has_fpgm_table (tt ) -> bool :
2931 """Tests for the presence of a fpgm table in a TrueType font."""
3032 return "fpgm" in tt
3133
3234
33- def has_gasp_table (tt ):
35+ def has_gasp_table (tt ) -> bool :
3436 """Tests for the presence of a gasp table in a TrueType font."""
3537 return "gasp" in tt
3638
3739
38- def has_hdmx_table (tt ):
40+ def has_hdmx_table (tt ) -> bool :
3941 """Tests for the presence of a hdmx table in a TrueType font."""
4042 return "hdmx" in tt
4143
4244
43- def has_ltsh_table (tt ):
45+ def has_ltsh_table (tt ) -> bool :
4446 """Tests for the presence of a LTSH table in a TrueType font."""
4547 return "LTSH" in tt
4648
4749
48- def has_prep_table (tt ):
50+ def has_prep_table (tt ) -> bool :
4951 """Tests for the presence of a prep table in a TrueType font."""
5052 return "prep" in tt
5153
5254
53- def has_ttfa_table (tt ):
55+ def has_ttfa_table (tt ) -> bool :
5456 """Tests for the presence of a TTFA table in a TrueType font."""
5557 return "TTFA" in tt
5658
5759
58- def has_vdmx_table (tt ):
60+ def has_vdmx_table (tt ) -> bool :
5961 """Tests for the presence of a VDMX table in a TrueType font."""
6062 return "VDMX" in tt
6163
6264
63- def is_truetype_font (filepath ) :
65+ def is_truetype_font (filepath : Union [ bytes , str , "os.PathLike[str]" ]) -> bool :
6466 """Tests that a font has the TrueType file signature of either:
6567 1) b'\x00 \x01 \x00 \x00 '
6668 2) b'\x74 \x72 \x75 \x65 ' == 'true'"""
6769 with open (filepath , "rb" ) as f :
68- file_signature = f .read (4 )
70+ file_signature : bytes = f .read (4 )
6971
7072 return file_signature in (b"\x00 \x01 \x00 \x00 " , b"\x74 \x72 \x75 \x65 " )
7173
7274
7375# ========================================================
7476# OpenType table removal
7577# ========================================================
76- def remove_cvt_table (tt ):
78+ def remove_cvt_table (tt ) -> None :
7779 """Removes cvt table from a fontTools.ttLib.TTFont object"""
7880 try :
7981 del tt ["cvt " ]
@@ -82,7 +84,7 @@ def remove_cvt_table(tt):
8284 pass
8385
8486
85- def remove_fpgm_table (tt ):
87+ def remove_fpgm_table (tt ) -> None :
8688 """Removes fpgm table from a fontTools.ttLib.TTFont object"""
8789 try :
8890 del tt ["fpgm" ]
@@ -91,7 +93,7 @@ def remove_fpgm_table(tt):
9193 pass
9294
9395
94- def remove_hdmx_table (tt ):
96+ def remove_hdmx_table (tt ) -> None :
9597 """Removes hdmx table from a fontTools.ttLib.TTFont object"""
9698 try :
9799 del tt ["hdmx" ]
@@ -100,7 +102,7 @@ def remove_hdmx_table(tt):
100102 pass
101103
102104
103- def remove_ltsh_table (tt ):
105+ def remove_ltsh_table (tt ) -> None :
104106 """Removes LTSH table from a fontTools.ttLib.TTFont object."""
105107 try :
106108 del tt ["LTSH" ]
@@ -109,7 +111,7 @@ def remove_ltsh_table(tt):
109111 pass
110112
111113
112- def remove_prep_table (tt ):
114+ def remove_prep_table (tt ) -> None :
113115 """Removes prep table from a fontTools.ttLib.TTFont object"""
114116 try :
115117 del tt ["prep" ]
@@ -118,7 +120,7 @@ def remove_prep_table(tt):
118120 pass
119121
120122
121- def remove_ttfa_table (tt ):
123+ def remove_ttfa_table (tt ) -> None :
122124 """Removes TTFA table from a fontTools.ttLib.TTFont object"""
123125 try :
124126 del tt ["TTFA" ]
@@ -127,7 +129,7 @@ def remove_ttfa_table(tt):
127129 pass
128130
129131
130- def remove_vdmx_table (tt ):
132+ def remove_vdmx_table (tt ) -> None :
131133 """Removes TTFA table from a fontTools.ttLib.TTFont object"""
132134 try :
133135 del tt ["VDMX" ]
@@ -139,9 +141,9 @@ def remove_vdmx_table(tt):
139141# ========================================================
140142# glyf table instruction set bytecode removal
141143# ========================================================
142- def remove_glyf_instructions (tt ):
144+ def remove_glyf_instructions (tt ) -> int :
143145 """Removes instruction set bytecode from glyph definitions in the glyf table."""
144- glyph_number = 0
146+ glyph_number : int = 0
145147 for glyph in tt ["glyf" ].glyphs .values ():
146148 glyph .expand (tt ["glyf" ])
147149 if hasattr (glyph , "program" ) and glyph .program .bytecode != array .array ("B" , []):
@@ -157,7 +159,7 @@ def remove_glyf_instructions(tt):
157159# ========================================================
158160# gasp table edit
159161# ========================================================
160- def update_gasp_table (tt ):
162+ def update_gasp_table (tt ) -> bool :
161163 """Modifies the following gasp table fields:
162164 1) rangeMaxPPEM changed to 65535
163165 2) rangeGaspBehavior changed to 0x000a (symmetric grayscale, no gridfit)"""
@@ -171,9 +173,9 @@ def update_gasp_table(tt):
171173# =========================================
172174# maxp table edits
173175# =========================================
174- def update_maxp_table (tt ):
176+ def update_maxp_table (tt ) -> bool :
175177 """Update the maxp table with new values based on elimination of instruction sets."""
176- changed = False
178+ changed : bool = False
177179 if tt ["maxp" ].maxZones != 0 :
178180 tt ["maxp" ].maxZones = 0
179181 changed = True
@@ -198,7 +200,7 @@ def update_maxp_table(tt):
198200# =========================================
199201# head table edits
200202# =========================================
201- def update_head_table_flags (tt ):
203+ def update_head_table_flags (tt ) -> bool :
202204 if is_bit_k_set (tt ["head" ].flags , 4 ):
203205 # confirm that there is no LTSH or hdmx table
204206 # bit 4 should be set if either of these tables are present in font
0 commit comments