A community-maintained Python framework for creating mathematical animations.
Add warning if font is not found for Text, Code, and MarkupText (#3046)
Kevin Lubick1b4049a01a89
diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.pyindex 41742a18e..9da5624d5 100644--- a/manim/mobject/text/code_mobject.py+++ b/manim/mobject/text/code_mobject.py@@ -17,6 +17,7 @@ from pygments.formatters.html import HtmlFormatterfrom pygments.lexers import get_lexer_by_name, guess_lexer_for_filenamefrom pygments.styles import get_all_styles+from manim import loggerfrom manim.constants import *from manim.mobject.geometry.arc import Dotfrom manim.mobject.geometry.polygram import RoundedRectangle@@ -101,7 +102,9 @@ class Code(VGroup):font_sizeA number which scales displayed code. Defaults to 24.font- The name of the text font to be used. Defaults to ``"Monospac821 BT"``.+ The name of the text font to be used. Defaults to ``"Monospace"``.+ This is either a system font or one loaded with `text.register_font()`. Note+ that font family names may be different across operating systems.stroke_widthStroke width for text. 0 is recommended, and the default.margin@@ -131,6 +134,9 @@ class Code(VGroup):'aliases or short names'.generate_html_fileDefines whether to generate highlighted html code to the folder `assets/codes/generated_html_files`. Defaults to `False`.+ warn_missing_font+ If True (default), Manim will issue a warning if the font does not exist in the+ (case-sensitive) list of fonts returned from `manimpango.list_fonts()`.Attributes----------@@ -159,7 +165,7 @@ class Code(VGroup):tab_width: int = 3,line_spacing: float = 0.3,font_size: float = 24,- font: str = "Monospac821 BT",+ font: str = "Monospace", # This should be in the font list on all platforms.stroke_width: float = 0,margin: float = 0.3,indentation_chars: str = " ",@@ -173,6 +179,7 @@ class Code(VGroup):style: str = "vim",language: str | None = None,generate_html_file: bool = False,+ warn_missing_font: bool = True,**kwargs,):super().__init__(@@ -183,6 +190,7 @@ class Code(VGroup):self.background_stroke_width = background_stroke_widthself.tab_width = tab_widthself.line_spacing = line_spacing+ self.warn_missing_font = warn_missing_fontself.font = fontself.font_size = font_sizeself.margin = margin@@ -317,6 +325,7 @@ class Code(VGroup):font=self.font,disable_ligatures=True,stroke_width=self.stroke_width,+ warn_missing_font=self.warn_missing_font,)for i in line_numbers:i.set_color(self.default_color)@@ -344,6 +353,7 @@ class Code(VGroup):font=self.font,disable_ligatures=True,stroke_width=self.stroke_width,+ warn_missing_font=self.warn_missing_font,)for line_no in range(code.__len__()):line = code.chars[line_no]diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.pyindex 709a2fb27..084b9ec7a 100644--- a/manim/mobject/text/text_mobject.py+++ b/manim/mobject/text/text_mobject.py@@ -6,7 +6,7 @@.. important::- See the corresponding tutorial :ref:`rendering-with-latex`+ See the corresponding tutorial :ref:`using-text-objects`, especially for information about fonts.The simplest way to add text to your animations is to use the :class:`~.Text` class. It uses the Pango library to render text.@@ -301,6 +301,13 @@ class Text(SVGMobject):----------textThe text that needs to be created as a mobject.+ font+ The font family to be used to render the text. This is either a system font or+ one loaded with `register_font()`. Note that font family names may be different+ across operating systems.+ warn_missing_font+ If True (default), Manim will issue a warning if the font does not exist in the+ (case-sensitive) list of fonts returned from `manimpango.list_fonts()`.Returns-------@@ -419,6 +426,7 @@ class Text(SVGMobject):t2w: dict[str, str] = None,gradient: tuple = None,tab_width: int = 4,+ warn_missing_font: bool = True,# Mobjectheight: float = None,width: float = None,@@ -427,6 +435,10 @@ class Text(SVGMobject):**kwargs,) -> None:self.line_spacing = line_spacing+ if font and warn_missing_font:+ fonts_list = manimpango.list_fonts()+ if font not in fonts_list:+ logger.warning(f"Font {font} not in {fonts_list}.")self.font = fontself._font_size = float(font_size)# needs to be a float or else size is inflated when font_size = 24@@ -910,7 +922,9 @@ class MarkupText(SVGMobject):Global weight setting, e.g. `NORMAL` or `BOLD`. Local overrides are possible.gradientGlobal gradient setting. Local overrides are possible.-+ warn_missing_font+ If True (default), Manim will issue a warning if the font does not exist in the+ (case-sensitive) list of fonts returned from `manimpango.list_fonts()`.Returns-------@@ -1094,10 +1108,15 @@ class MarkupText(SVGMobject):width: int = None,should_center: bool = True,disable_ligatures: bool = False,+ warn_missing_font: bool = True,**kwargs,) -> None:self.text = textself.line_spacing = line_spacing+ if font and warn_missing_font:+ fonts_list = manimpango.list_fonts()+ if font not in fonts_list:+ logger.warning(f"Font {font} not in {fonts_list}.")self.font = fontself._font_size = float(font_size)self.slant = slantdiff --git a/tests/test_graphical_units/test_text.py b/tests/test_graphical_units/test_text.pyindex 606ff4251..d7939fc0f 100644--- a/tests/test_graphical_units/test_text.py+++ b/tests/test_graphical_units/test_text.py@@ -24,8 +24,8 @@ def test_text_color_inheritance():their parent class."""VMobject.set_default(color=RED)# set both to a singular font so that the tests agree.- text = Text("test_color_inheritance", font="Dejavu Sans")- markup_text = MarkupText("test_color_inheritance", font="Dejavu Sans")+ text = Text("test_color_inheritance", font="Sans")+ markup_text = MarkupText("test_color_inheritance", font="Sans")assert all(char.fill_color == Color(RED) for char in text)assert all(char.fill_color == Color(RED) for char in markup_text)