From b34ca73fd9f3b06fa6a4680962967b6c561da805 Mon Sep 17 00:00:00 2001 From: staghado Date: Mon, 30 Oct 2023 17:53:58 +0100 Subject: [PATCH 1/7] improve handling of specified font name --- manim/mobject/text/text_mobject.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index 497f03e560..30cd4f7899 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -436,8 +436,15 @@ def __init__( self.line_spacing = line_spacing if font and warn_missing_font: fonts_list = manimpango.list_fonts() + # handle special case of sans/sans-serif + if font == "sans-serif": + font = "sans" if font not in fonts_list: - logger.warning(f"Font {font} not in {fonts_list}.") + # check if the capitalized version is in the supported fonts + if font.capitalize() in fonts_list: + font = font.capitalize() + else: + logger.warning(f"Font {font} not in {fonts_list}.") self.font = font self._font_size = float(font_size) # needs to be a float or else size is inflated when font_size = 24 @@ -1158,8 +1165,15 @@ def __init__( self.line_spacing = line_spacing if font and warn_missing_font: fonts_list = manimpango.list_fonts() + # handle special case of sans/sans-serif + if font == "sans-serif": + font = "sans" if font not in fonts_list: - logger.warning(f"Font {font} not in {fonts_list}.") + # check if the capitalized version is in the supported fonts + if font.capitalize() in fonts_list: + font = font.capitalize() + else: + logger.warning(f"Font {font} not in {fonts_list}.") self.font = font self._font_size = float(font_size) self.slant = slant From 548c239f30f397bb5a3341a1742e870512ea032c Mon Sep 17 00:00:00 2001 From: staghado Date: Mon, 30 Oct 2023 18:10:10 +0100 Subject: [PATCH 2/7] use font.lower() to check if the font is sans-serif --- manim/mobject/text/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index 30cd4f7899..44e1b30ae6 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -437,7 +437,7 @@ def __init__( if font and warn_missing_font: fonts_list = manimpango.list_fonts() # handle special case of sans/sans-serif - if font == "sans-serif": + if font.lower() == "sans-serif": font = "sans" if font not in fonts_list: # check if the capitalized version is in the supported fonts From 1ea4ef96d0df1170b9f995e405b9aaf187cb0d78 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 1 Feb 2024 18:38:39 -0500 Subject: [PATCH 3/7] Add comparison with str.lower() and str.title() --- manim/mobject/text/text_mobject.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index 58a63c3378..0f685bca25 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -453,6 +453,10 @@ def __init__( # check if the capitalized version is in the supported fonts if font.capitalize() in fonts_list: font = font.capitalize() + elif font.lower() in fonts_list: + font = font.lower() + elif font.title() in fonts_list: + font = font.title() else: logger.warning(f"Font {font} not in {fonts_list}.") self.font = font @@ -1187,6 +1191,10 @@ def __init__( # check if the capitalized version is in the supported fonts if font.capitalize() in fonts_list: font = font.capitalize() + elif font.lower() in fonts_list: + font = font.lower() + elif font.title() in fonts_list: + font = font.title() else: logger.warning(f"Font {font} not in {fonts_list}.") self.font = font From e6ce8250fc080d68408f1b26ad3e958ecddfa15b Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 1 Feb 2024 21:51:34 -0500 Subject: [PATCH 4/7] Add test --- tests/module/mobject/text/test_text_mobject.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/module/mobject/text/test_text_mobject.py b/tests/module/mobject/text/test_text_mobject.py index 048ae883a1..aa6a5bd32f 100644 --- a/tests/module/mobject/text/test_text_mobject.py +++ b/tests/module/mobject/text/test_text_mobject.py @@ -1,6 +1,8 @@ from __future__ import annotations from manim.mobject.text.text_mobject import MarkupText, Text +from contextlib import redirect_stdout +from io import StringIO def test_font_size(): @@ -11,3 +13,19 @@ def test_font_size(): assert round(text_string.font_size, 5) == 14.4 assert round(markuptext_string.font_size, 5) == 14.4 + +def test_font_warnings(): + def warning_printed(font: str, **kwargs) -> bool: + io = StringIO() + with redirect_stdout(io): + Text("hi!", font=font, **kwargs) + txt = io.getvalue() + return "Font" in txt and "not in" in txt + + # check for normal fonts (no warning) + assert not warning_printed("Arial", warn_missing_font=True) + # should be converted to sans before checking + assert not warning_printed("Sans-serif", warn_missing_font=True) + + # check random string (should be warning) + assert warning_printed("Manim!"*3, warn_missing_font=True) From 4ccc1bf217f031fbc427b92195d978891e4aee10 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Feb 2024 02:52:34 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/module/mobject/text/test_text_mobject.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/module/mobject/text/test_text_mobject.py b/tests/module/mobject/text/test_text_mobject.py index aa6a5bd32f..c362077df8 100644 --- a/tests/module/mobject/text/test_text_mobject.py +++ b/tests/module/mobject/text/test_text_mobject.py @@ -1,9 +1,10 @@ from __future__ import annotations -from manim.mobject.text.text_mobject import MarkupText, Text from contextlib import redirect_stdout from io import StringIO +from manim.mobject.text.text_mobject import MarkupText, Text + def test_font_size(): """Test that Text and MarkupText return the @@ -14,6 +15,7 @@ def test_font_size(): assert round(text_string.font_size, 5) == 14.4 assert round(markuptext_string.font_size, 5) == 14.4 + def test_font_warnings(): def warning_printed(font: str, **kwargs) -> bool: io = StringIO() @@ -21,11 +23,11 @@ def warning_printed(font: str, **kwargs) -> bool: Text("hi!", font=font, **kwargs) txt = io.getvalue() return "Font" in txt and "not in" in txt - + # check for normal fonts (no warning) assert not warning_printed("Arial", warn_missing_font=True) # should be converted to sans before checking assert not warning_printed("Sans-serif", warn_missing_font=True) - + # check random string (should be warning) - assert warning_printed("Manim!"*3, warn_missing_font=True) + assert warning_printed("Manim!" * 3, warn_missing_font=True) From 7139564d3f9a871513b896caff40ea91ccee3a89 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 1 Feb 2024 22:09:24 -0500 Subject: [PATCH 6/7] Change font to one available on linux --- tests/module/mobject/text/test_text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/module/mobject/text/test_text_mobject.py b/tests/module/mobject/text/test_text_mobject.py index c362077df8..a6b8b3b453 100644 --- a/tests/module/mobject/text/test_text_mobject.py +++ b/tests/module/mobject/text/test_text_mobject.py @@ -25,7 +25,7 @@ def warning_printed(font: str, **kwargs) -> bool: return "Font" in txt and "not in" in txt # check for normal fonts (no warning) - assert not warning_printed("Arial", warn_missing_font=True) + assert not warning_printed("System-ui", warn_missing_font=True) # should be converted to sans before checking assert not warning_printed("Sans-serif", warn_missing_font=True) From 40d357a75d95a5c78e29890948fdea7b6ccb1f94 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Fri, 2 Feb 2024 07:56:49 -0500 Subject: [PATCH 7/7] Fix sans-serif check condition --- manim/mobject/text/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index 0f685bca25..d394bccd23 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -1185,7 +1185,7 @@ def __init__( if font and warn_missing_font: fonts_list = Text.font_list() # handle special case of sans/sans-serif - if font == "sans-serif": + if font.lower() == "sans-serif": font = "sans" if font not in fonts_list: # check if the capitalized version is in the supported fonts