Skip to content

Commit

Permalink
Update to_static method: set italic style flag based on coordinates…
Browse files Browse the repository at this point in the history
… values.
  • Loading branch information
fabiocaccamo committed Dec 13, 2023
1 parent c549466 commit 2d4fb6c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
7 changes: 7 additions & 0 deletions fontbro/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,13 @@ def to_static(
update_style_flags=update_style_flags,
)

# update style flags based on coordinates values
if update_style_flags:
has_italic = coordinates.get("ital", 0) == 1
has_slant = coordinates.get("slnt", 0) < 0
if has_italic or has_slant:
self.set_style_flags(regular=False, italic=True)

def __str__(self):
"""
Returns a string representation of the object.
Expand Down
59 changes: 55 additions & 4 deletions tests/test_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def test_to_static_with_style_name_invalid(self):
with self.assertRaises(ValueError):
font.to_static(style_name="ExtraBlack")

def test_to_static_with_update_names_default(self):
font = self._get_variable_font()
font.to_static(coordinates={"wdth": 100.0, "wght": 900.0})
self.assertEqual(font.get_style_name(), "Black")

def test_to_static_with_update_names_and_exact_coordinates(self):
font = self._get_variable_font()
font.to_static(coordinates={"wdth": 100.0, "wght": 900.0}, update_names=True)
Expand All @@ -75,10 +80,56 @@ def test_to_static_without_update_names(self):
font.to_static(coordinates={"wdth": 100.0, "wght": 900.0}, update_names=False)
self.assertEqual(font.get_style_name(), "Thin")

def test_to_static_with_update_names_default(self):
font = self._get_variable_font()
font.to_static(coordinates={"wdth": 100.0, "wght": 900.0})
self.assertEqual(font.get_style_name(), "Black")
def test_to_static_with_update_names_default_with_update_style_flags_default_and_italic_instance(
self,
):
font = self._get_font("/Inter/Inter-VariableFont_slnt,wght.ttf")
font.to_static(coordinates={"slnt": -10.0, "wght": 900.0})
self.assertTrue(font.get_style_flag("italic"))

def test_to_static_with_update_names_with_update_style_flags_and_italic_instance(
self,
):
font = self._get_font("/Inter/Inter-VariableFont_slnt,wght.ttf")
font.to_static(
coordinates={"slnt": -10.0, "wght": 900.0},
update_names=True,
update_style_flags=True,
)
self.assertTrue(font.get_style_flag("italic"))

def test_to_static_with_update_names_without_update_style_flags_and_italic_instance(
self,
):
font = self._get_font("/Inter/Inter-VariableFont_slnt,wght.ttf")
font.to_static(
coordinates={"slnt": -10.0, "wght": 900.0},
update_names=True,
update_style_flags=False,
)
self.assertFalse(font.get_style_flag("italic"))

def test_to_static_without_update_names_with_update_style_flags_and_italic_instance(
self,
):
font = self._get_font("/Inter/Inter-VariableFont_slnt,wght.ttf")
font.to_static(
coordinates={"slnt": -10.0, "wght": 900.0},
update_names=False,
update_style_flags=True,
)
self.assertTrue(font.get_style_flag("italic"))

def test_to_static_without_update_names_without_update_style_flags_and_italic_instance(
self,
):
font = self._get_font("/Inter/Inter-VariableFont_slnt,wght.ttf")
font.to_static(
coordinates={"slnt": -10.0, "wght": 900.0},
update_names=False,
update_style_flags=False,
)
self.assertFalse(font.get_style_flag("italic"))

def test_to_sliced_variable_with_static_font(self):
font = self._get_static_font()
Expand Down

0 comments on commit 2d4fb6c

Please sign in to comment.