Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Implement caching of fonts list to improve runtime performance (#3316)

Tristan Schulz370d88337449
diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py
index 3a2b0bd3d..1110258c4 100644
--- a/manim/mobject/text/text_mobject.py
+++ b/manim/mobject/text/text_mobject.py
@@ -49,6 +49,8 @@ Examples
from __future__ import annotations
+import functools
+
__all__ = ["Text", "Paragraph", "MarkupText", "register_font"]
@@ -407,6 +409,11 @@ class Text(SVGMobject):
"""
+ @staticmethod
+ @functools.lru_cache(maxsize=None)
+ def font_list() -> list[str]:
+ return manimpango.list_fonts()
+
def __init__(
self,
text: str,
@@ -431,13 +438,12 @@ class Text(SVGMobject):
width: float = None,
should_center: bool = True,
disable_ligatures: bool = False,
+ use_svg_cache: bool = False,
**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}.")
+ if font and warn_missing_font and font not in Text.font_list():
+ logger.warning(f"Font {font} not in {Text.font_list()}.")
self.font = font
self._font_size = float(font_size)
# needs to be a float or else size is inflated when font_size = 24
@@ -491,7 +497,7 @@ class Text(SVGMobject):
height=height,
width=width,
should_center=should_center,
- use_svg_cache=False,
+ use_svg_cache=use_svg_cache,
**kwargs,
)
self.text = text
@@ -1133,6 +1139,11 @@ class MarkupText(SVGMobject):
"""
+ @staticmethod
+ @functools.lru_cache(maxsize=None)
+ def font_list() -> list[str]:
+ return manimpango.list_fonts()
+
def __init__(
self,
text: str,
@@ -1156,10 +1167,8 @@ class MarkupText(SVGMobject):
) -> None:
self.text = text
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}.")
+ if font and warn_missing_font and font not in Text.font_list():
+ logger.warning(f"Font {font} not in {Text.font_list()}.")
self.font = font
self._font_size = float(font_size)
self.slant = slant