Skip to content

Commit

Permalink
font-patcher: Improve weight checking
Browse files Browse the repository at this point in the history
[why]
When the font does not have a PSweight string the font-patcher bugs.

[how]
Rewrite the code to be more robust against unexpected weight values.
Also make detected problems non-fatal.

Reported-by: František Hanzlík <[email protected]>
Signed-off-by: Fini Jastrow <[email protected]>
  • Loading branch information
Finii committed Oct 7, 2023
1 parent 3fab661 commit 809101d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions bin/scripts/name_parser/FontnameParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,14 @@ def check_weights(self, font):
os2_weight = font.os2_weight
ps_weight = FontnameTools.weight_string_to_number(font.weight)
name_weight = FontnameTools.weight_string_to_number(weight)
weightproblem = False
if ps_weight is None:
self.logger.warn('Can not parse PS-weight: {}'.format(restored_weight_token))
weightproblem = True
if name_weight is None:
self.logger.error('Can not parse name for weight: {}'.format(restored_weight_token))
return
if abs(os2_weight - ps_weight) > 50 or abs(os2_weight - name_weight) > 50:
self.logger.warn('Can not parse name for weight: {}'.format(restored_weight_token))
weightproblem = True
if weightproblem or abs(os2_weight - ps_weight) > 50 or abs(os2_weight - name_weight) > 50:
self.logger.warning('Possible problem with the weight metadata detected, check with --debug')
self.logger.debug('Weight approximations: OS2/PS/Name: {}/{}/{} (from {}/\'{}\'/\'{}\')'.format(
os2_weight, ps_weight, name_weight,
Expand Down
2 changes: 1 addition & 1 deletion bin/scripts/name_parser/FontnameTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def postscript_char_filter(name):
@staticmethod
def weight_string_to_number(w):
""" Convert a common string approximation to a PS/2 weight value """
if not len(w):
if not isinstance(w, str) or len(w) < 1:
return 400
for num, strs in FontnameTools.equivalent_weights.items():
if w.lower() in strs:
Expand Down
2 changes: 1 addition & 1 deletion font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import absolute_import, print_function, unicode_literals

# Change the script version when you edit this script:
script_version = "4.5.2"
script_version = "4.5.3"

version = "3.0.2"
projectName = "Nerd Fonts"
Expand Down

0 comments on commit 809101d

Please sign in to comment.