@@ -170,7 +170,8 @@ def subroutinize(
170170 if not inplace :
171171 otf = copy .deepcopy (otf )
172172
173- glyphOrder = otf .getGlyphOrder ()
173+ # ensure the glyph order is decompiled before CFF table is replaced
174+ _ = otf .getGlyphOrder ()
174175
175176 buf = io .BytesIO ()
176177 otf .save (buf )
@@ -190,16 +191,36 @@ def subroutinize(
190191 and keep_glyph_names
191192 ):
192193 # set 'post' to format 2 to keep the glyph names dropped from CFF2
193- post_table = otf .get ("post" )
194- if post_table and post_table .formatType != 2.0 :
195- post_table .formatType = 2.0
196- post_table .extraNames = []
197- post_table .mapping = {}
198- post_table .glyphOrder = glyphOrder
194+ set_post_table_format (otf , 2.0 )
195+ elif (
196+ input_format == CFFTableTag .CFF2
197+ and output_format == CFFTableTag .CFF
198+ ):
199+ # set 'post' to format 3 so CFF glyph names are not stored twice
200+ # TODO convert to CID when keep_glyph_names=False?
201+ set_post_table_format (otf , 3.0 )
199202
200203 return otf
201204
202205
206+ def set_post_table_format (otf , formatType ):
207+ if formatType not in (2.0 , 3.0 ):
208+ raise NotImplementedError (formatType )
209+
210+ post = otf .get ("post" )
211+ if post and post .formatType != formatType :
212+ post .formatType = formatType
213+ if formatType == 2.0 :
214+ post .extraNames = []
215+ post .mapping = {}
216+ post .glyphOrder = otf .getGlyphOrder ()
217+ else :
218+ for attr in ("extraNames" , "mapping" ):
219+ if hasattr (post , attr ):
220+ delattr (post , attr )
221+ post .glyphOrder = None
222+
223+
203224def has_subroutines (otf : ttLib .TTFont ) -> bool :
204225 """Return True if the font's CFF or CFF2 table contains any subroutines."""
205226 table_tag = _sniff_cff_table_format (otf )
0 commit comments