Skip to content

Commit 5295697

Browse files
committed
name-parser: Simplify code
[why] The code is rather convoluted and one can not follow what is done. [how] Add function that abstracts some steps away. Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
1 parent d411e75 commit 5295697

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

bin/scripts/name_parser/FontnameParser.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,14 @@ def preferred_family(self, short_alternative = False):
185185
# When short_alternative we get the short named alternative needed for some Windows applications
186186
(name, rest) = self._shortened_name()
187187
if short_alternative:
188+
# Try to come up with a shortened family name
189+
# This is in principle the code from family() but without including weights
190+
# because weights shall still end up in ID 17
188191
other = self.other_token
189192
if self.use_short_families[1]:
190193
[ other ] = FontnameTools.short_styles([ other ], self.use_short_families[2])
191194
pfn = FontnameTools.concat(name, rest, other, self.short_family_suff)
195+
# Only return something if it is different to our standard behavior
192196
if pfn == self.preferred_family(False):
193197
return ''
194198
return pfn
@@ -198,7 +202,7 @@ def preferred_family(self, short_alternative = False):
198202
return ''
199203
return pfn
200204

201-
def preferred_styles(self):
205+
def preferred_styles(self, _ = False):
202206
"""Get the SFNT Preferred Styles (ID 17)"""
203207
styles = self.style_token
204208
weights = self.weight_token
@@ -305,6 +309,18 @@ def check_weights(self, font):
305309
os2_weight, ps_weight, name_weight,
306310
font.os2_weight, font.weight, weight))
307311

312+
def pfam_to_sfnt(self, name_function, entry, message):
313+
"""Prepare SFNT entries for short and long form as two different languages"""
314+
languages = [ 'English (US)', 'English (British)' ]
315+
names = [ name_function(False), name_function(True) ]
316+
ret = [ ]
317+
for i in range(2):
318+
if len(names[i]):
319+
ret += [( languages[i], entry, self.checklen(31, message, names[i]) )]
320+
else:
321+
break
322+
return ret
323+
308324
def rename_font(self, font):
309325
"""Rename the font to include all information we found (font is fontforge font object)"""
310326
font.fondname = None
@@ -358,19 +374,8 @@ def rename_font(self, font):
358374
sfnt_list += [( 'English (US)', 'UniqueID', self.fullname() + version_tag )] # 3
359375
sfnt_list += [( 'English (US)', 'Fullname', self.checklen(63, 'Fullname (ID 4)', self.fullname()) )] # 4
360376
sfnt_list += [( 'English (US)', 'PostScriptName', self.checklen(63, 'PSN (ID 6)', self.psname()) )] # 6
361-
362-
p_fam = self.preferred_family(False)
363-
if len(p_fam):
364-
sfnt_list += [( 'English (US)', 'Preferred Family', self.checklen(31, 'PrefFamily (ID 16)', p_fam) )] # 16
365-
p_fam = self.preferred_family(True)
366-
if len(p_fam):
367-
sfnt_list += [( 'English (British)', 'Preferred Family', self.checklen(31, 'PrefFamily (ID 16)', p_fam) )]
368-
p_sty = self.preferred_styles()
369-
if len(p_sty):
370-
sfnt_list += [( 'English (US)', 'Preferred Styles', self.checklen(31, 'PrefStyles (ID 17)', p_sty) )] # 17
371-
if len(p_fam):
372-
# Set only if ID16 for British has been set
373-
sfnt_list += [( 'English (British)', 'Preferred Styles', self.checklen(31, 'PrefStyles (ID 17)', p_sty) )]
377+
sfnt_list += self.pfam_to_sfnt(self.preferred_family, 'Preferred Family', 'PrefFamily (ID 16)') # 16
378+
sfnt_list += self.pfam_to_sfnt(self.preferred_styles, 'Preferred Styles', 'PrefStyles (ID 17)') # 17
374379

375380
font.sfnt_names = tuple(sfnt_list)
376381

0 commit comments

Comments
 (0)