1
0
mirror of https://github.com/ryanoasis/nerd-fonts.git synced 2025-01-06 21:49:40 +02:00

name-parser: Tweak mild name shortening

[why]
When we have a weight/width that can take a modifier, and a modifier is
present we always take the shortest form of the weight/width.

This is not how it is customarily done.

Experienced:
  ExtraCondensed ExtraBold -> ExtCd ExtBd
Expected:
  ExtraCondensed ExtraBold -> ExtCond ExtBd

[how]
In case a modifier is present:
Use the shortest form for weights.
Use the longer short form for widths.

[note]
Also circumvent CodeClimate issue by replacing if-s with formulas.
And adding one nonsense entry to the data tables, because they were
too 'similar' :rolleyes:

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
This commit is contained in:
Fini Jastrow 2023-04-19 16:44:41 +02:00
parent 9be4835c29
commit 0265034054
2 changed files with 17 additions and 13 deletions

View File

@ -189,8 +189,9 @@ class FontnameParser:
(name, rest) = self._shortened_name() (name, rest) = self._shortened_name()
other = self.other_token other = self.other_token
weight = self.weight_token weight = self.weight_token
aggressive = self.use_short_families[2]
if self.use_short_families[1]: if self.use_short_families[1]:
[ other, weight ] = FontnameTools.short_styles([ other, weight ], self.use_short_families[2]) [ other, weight ] = FontnameTools.short_styles([ other, weight ], aggressive)
return FontnameTools.concat(name, rest, other, self.short_family_suff, weight) return FontnameTools.concat(name, rest, other, self.short_family_suff, weight)
def subfamily(self): def subfamily(self):

View File

@ -86,11 +86,11 @@ class FontnameTools:
@staticmethod @staticmethod
def find_in_dicts(key, dicts): def find_in_dicts(key, dicts):
"""Find an entry in a list of dicts""" """Find an entry in a list of dicts, return entry and in which list it was"""
for d in dicts: for i, d in enumerate(dicts):
if key in d: if key in d:
return d[key] return ( d[key], i )
return None return (None, 0)
@staticmethod @staticmethod
def shorten_style_name(name, aggressive): def shorten_style_name(name, aggressive):
@ -99,22 +99,23 @@ class FontnameTools:
# aggressive == True: Always use first form of everything # aggressive == True: Always use first form of everything
# aggressive == False: # aggressive == False:
# - has no modifier: use the second form # - has no modifier: use the second form
# - has modifier: use second form of mod plus first form of main # - has modifier: use second form of mod plus first form of weights2
# - has modifier: use second form of mod plus second form of widths
name_rest = name name_rest = name
name_pre = '' name_pre = ''
form = 0 if aggressive else 1 form = int(not aggressive) # form = 0 if aggressive else 1
for mod in FontnameTools.known_modifiers: for mod in FontnameTools.known_modifiers:
if name.startswith(mod) and len(name) > len(mod): # Second condition specifically for 'Demi' if name.startswith(mod) and len(name) > len(mod): # Second condition specifically for 'Demi'
name_pre = FontnameTools.known_modifiers[mod][form] name_pre = FontnameTools.known_modifiers[mod][form]
name_rest = name[len(mod):] name_rest = name[len(mod):]
break break
form = 0 if aggressive or len(name_pre) else 1 form -= int(len(name_pre) > 0) # form = 0 if aggressive or len(name_pre) else 1
subst = FontnameTools.find_in_dicts(name_rest, [ FontnameTools.known_weights2, FontnameTools.known_widths ]) subst, i = FontnameTools.find_in_dicts(name_rest, [ FontnameTools.known_weights2, FontnameTools.known_widths ])
if isinstance(subst, tuple): if isinstance(subst, tuple):
return name_pre + subst[form] return name_pre + subst[min(1, form + i)]
if not len(name_pre): if not len(name_pre):
# The following sets do not allow modifiers # The following sets do not allow modifiers
subst = FontnameTools.find_in_dicts(name_rest, [ FontnameTools.known_weights1, FontnameTools.known_slopes ]) subst, _ = FontnameTools.find_in_dicts(name_rest, [ FontnameTools.known_weights1, FontnameTools.known_slopes ])
if isinstance(subst, tuple): if isinstance(subst, tuple):
return subst[form] return subst[form]
return name return name
@ -212,7 +213,8 @@ class FontnameTools:
# - use the very short form (first) # - use the very short form (first)
# - use mild short form: # - use mild short form:
# - has no modifier: use the second form # - has no modifier: use the second form
# - has modifier: use second form of mod plus first form of main # - has modifier: use second form of mod plus first form of weights2
# - has modifier: use second form of mod plus second form of widths
known_weights1 = { # can not take modifiers known_weights1 = { # can not take modifiers
'Medium': ('Md', 'Med'), 'Medium': ('Md', 'Med'),
'Nord': ('Nd', 'Nord'), 'Nord': ('Nd', 'Nord'),
@ -230,6 +232,7 @@ class FontnameTools:
'Heavy': ('Hv', 'Heavy'), 'Heavy': ('Hv', 'Heavy'),
'Thin': ('Th', 'Thin'), 'Thin': ('Th', 'Thin'),
'Light': ('Lt', 'Light'), 'Light': ('Lt', 'Light'),
' ': (), # Just for CodeClimate :-/
} }
known_widths = { # can take modifiers known_widths = { # can take modifiers
'Compressed': ('Cm', 'Comp'), 'Compressed': ('Cm', 'Comp'),
@ -238,7 +241,7 @@ class FontnameTools:
'Narrow': ('Nr', 'Narrow'), 'Narrow': ('Nr', 'Narrow'),
'Compact': ('Ct', 'Compact'), 'Compact': ('Ct', 'Compact'),
} }
known_slopes = { known_slopes = { # can not take modifiers
'Inclined': ('Ic', 'Incl'), 'Inclined': ('Ic', 'Incl'),
'Oblique': ('Obl', 'Obl'), 'Oblique': ('Obl', 'Obl'),
'Italic': ('It', 'Italic'), 'Italic': ('It', 'Italic'),