From 2d4fb6c0011a8a63e9b75510e0b82b9a8e11c82b Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Wed, 13 Dec 2023 01:07:01 +0100 Subject: [PATCH] Update `to_static` method: set italic style flag based on coordinates values. --- fontbro/font.py | 7 +++++ tests/test_instantiation.py | 59 ++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/fontbro/font.py b/fontbro/font.py index 14185f1..65a6d35 100644 --- a/fontbro/font.py +++ b/fontbro/font.py @@ -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. diff --git a/tests/test_instantiation.py b/tests/test_instantiation.py index 193162c..ed18143 100644 --- a/tests/test_instantiation.py +++ b/tests/test_instantiation.py @@ -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) @@ -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()