diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index 32f5e4dc25..d394bccd23 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -444,8 +444,21 @@ def __init__( **kwargs, ) -> None: self.line_spacing = line_spacing - if font and warn_missing_font and font not in Text.font_list(): - logger.warning(f"Font {font} not in {Text.font_list()}.") + if font and warn_missing_font: + fonts_list = Text.font_list() + # handle special case of sans/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 + 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 self._font_size = float(font_size) # needs to be a float or else size is inflated when font_size = 24 @@ -1169,8 +1182,21 @@ def __init__( ) -> None: self.text = text self.line_spacing = line_spacing - if font and warn_missing_font and font not in Text.font_list(): - logger.warning(f"Font {font} not in {Text.font_list()}.") + if font and warn_missing_font: + fonts_list = Text.font_list() + # handle special case of sans/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 + 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 self._font_size = float(font_size) self.slant = slant diff --git a/tests/module/mobject/text/test_text_mobject.py b/tests/module/mobject/text/test_text_mobject.py index 048ae883a1..a6b8b3b453 100644 --- a/tests/module/mobject/text/test_text_mobject.py +++ b/tests/module/mobject/text/test_text_mobject.py @@ -1,5 +1,8 @@ from __future__ import annotations +from contextlib import redirect_stdout +from io import StringIO + from manim.mobject.text.text_mobject import MarkupText, Text @@ -11,3 +14,20 @@ 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("System-ui", 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)