Skip to content

Commit

Permalink
Fix Font Bakery error: "Verify that each group of fonts with the sa…
Browse files Browse the repository at this point in the history
…me nameID 1 has maximum of 4 fonts".
  • Loading branch information
fabiocaccamo committed May 2, 2023
1 parent 910c074 commit d516598
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
35 changes: 8 additions & 27 deletions fontbro/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,37 +931,18 @@ def rename(self, family_name="", style_name="", style_flags=True):
wws_family_name = family_name
wws_subfamily_name = style_name

# family name, subfamily name and full name
family_name_parts = [family_name]
style_name_lower = style_name.lower()
style_name_parts = style_name.split()
subfamily_name_parts = style_name_lower.split()
# family name, subfamily name and full name - fix legacy name records 1 and 2
# family name and subfamily name
subfamily_names = ["regular", "italic", "bold", "bold italic"]
subfamily_name_default = subfamily_names[0]
for name_part in style_name_parts:
if name_part.lower() not in subfamily_names:
family_name_parts.append(name_part)
subfamily_name_parts.remove(name_part.lower())
if subfamily_name_default in subfamily_name_parts:
if len(subfamily_name_parts) > 1:
subfamily_name_parts.remove(subfamily_name_default)
subfamily_name_parts = subfamily_name_parts or [subfamily_name_default]
family_name = " ".join(family_name_parts)
subfamily_name = " ".join(subfamily_name_parts).lower()
subfamily_name = style_name.lower()
if subfamily_name not in subfamily_names:
subfamily_name = subfamily_name_default
subfamily_name = subfamily_name.title()

# fix legacy name records 1 and 2
if style_name_lower not in subfamily_names:
subfamily_name = subfamily_names[style_name_lower.endswith(" italic")]
subfamily_name = subfamily_name.title()
# fix legacy name records 1 and 2
family_name_suffix = re.sub(
r"\ italic$", "", subfamily_name, flags=re.IGNORECASE
r"\ italic$", "", style_name, flags=re.IGNORECASE
)
assert family_name_suffix
family_name = f"{typographic_family_name} {family_name_suffix}"
if family_name_suffix:
family_name = f"{typographic_family_name} {family_name_suffix}"
subfamily_name = subfamily_names["italic" in subfamily_name]
subfamily_name = subfamily_name.title()

# full name
full_name = concat_names(typographic_family_name, typographic_subfamily_name)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_rename_with_family_name_only_issue_0062(self):
self.assertEqual(
font.get_names(),
{
"family_name": "Hugo Test Regular",
"family_name": "Hugo Test Thin",
"full_name": "Hugo Test Thin",
"postscript_name": "HugoTest-Thin",
"subfamily_name": "Regular",
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_rename_with_style_name_containing_characters_not_allowed_in_postscript_
font = self._get_font("/Tourney/Tourney-VariableFont_wdth,wght.ttf")
font.rename(" Tourney Custom ", " [wdth-wght] ")
names = font.get_names()
self.assertEqual(names[Font.NAME_FAMILY_NAME], "Tourney Custom Regular")
self.assertEqual(names[Font.NAME_FAMILY_NAME], "Tourney Custom [wdth-wght]")
self.assertEqual(names[Font.NAME_SUBFAMILY_NAME], "Regular")
self.assertEqual(names[Font.NAME_FULL_NAME], "Tourney Custom [wdth-wght]")
self.assertEqual(names[Font.NAME_POSTSCRIPT_NAME], "TourneyCustom-wdth-wght")
Expand Down

0 comments on commit d516598

Please sign in to comment.