Skip to content

Commit 370d883

Browse files
authored
Implement caching of fonts list to improve runtime performance (#3316)
* Implement caching of fonts list to improve runtime performance * Fix small use_svg_cache kwargs error * replaced font list with LRU cache
1 parent 0cba710 commit 370d883

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

manim/mobject/text/text_mobject.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def construct(self):
4949

5050
from __future__ import annotations
5151

52+
import functools
53+
5254
__all__ = ["Text", "Paragraph", "MarkupText", "register_font"]
5355

5456

@@ -407,6 +409,11 @@ def construct(self):
407409
408410
"""
409411

412+
@staticmethod
413+
@functools.lru_cache(maxsize=None)
414+
def font_list() -> list[str]:
415+
return manimpango.list_fonts()
416+
410417
def __init__(
411418
self,
412419
text: str,
@@ -431,13 +438,12 @@ def __init__(
431438
width: float = None,
432439
should_center: bool = True,
433440
disable_ligatures: bool = False,
441+
use_svg_cache: bool = False,
434442
**kwargs,
435443
) -> None:
436444
self.line_spacing = line_spacing
437-
if font and warn_missing_font:
438-
fonts_list = manimpango.list_fonts()
439-
if font not in fonts_list:
440-
logger.warning(f"Font {font} not in {fonts_list}.")
445+
if font and warn_missing_font and font not in Text.font_list():
446+
logger.warning(f"Font {font} not in {Text.font_list()}.")
441447
self.font = font
442448
self._font_size = float(font_size)
443449
# needs to be a float or else size is inflated when font_size = 24
@@ -491,7 +497,7 @@ def __init__(
491497
height=height,
492498
width=width,
493499
should_center=should_center,
494-
use_svg_cache=False,
500+
use_svg_cache=use_svg_cache,
495501
**kwargs,
496502
)
497503
self.text = text
@@ -1133,6 +1139,11 @@ def construct(self):
11331139
11341140
"""
11351141

1142+
@staticmethod
1143+
@functools.lru_cache(maxsize=None)
1144+
def font_list() -> list[str]:
1145+
return manimpango.list_fonts()
1146+
11361147
def __init__(
11371148
self,
11381149
text: str,
@@ -1156,10 +1167,8 @@ def __init__(
11561167
) -> None:
11571168
self.text = text
11581169
self.line_spacing = line_spacing
1159-
if font and warn_missing_font:
1160-
fonts_list = manimpango.list_fonts()
1161-
if font not in fonts_list:
1162-
logger.warning(f"Font {font} not in {fonts_list}.")
1170+
if font and warn_missing_font and font not in Text.font_list():
1171+
logger.warning(f"Font {font} not in {Text.font_list()}.")
11631172
self.font = font
11641173
self._font_size = float(font_size)
11651174
self.slant = slant

0 commit comments

Comments
 (0)