Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement caching of fonts list to improve runtime performance #3316

Merged
merged 5 commits into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions manim/mobject/text/text_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def construct(self):

from __future__ import annotations

import functools

__all__ = ["Text", "Paragraph", "MarkupText", "register_font"]


Expand Down Expand Up @@ -407,6 +409,11 @@ def construct(self):

"""

@staticmethod
@functools.lru_cache(maxsize=None)
def font_list() -> list[str]:
return manimpango.list_fonts()

def __init__(
self,
text: str,
Expand All @@ -431,13 +438,12 @@ def __init__(
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
Expand Down Expand Up @@ -491,7 +497,7 @@ def __init__(
height=height,
width=width,
should_center=should_center,
use_svg_cache=False,
use_svg_cache=use_svg_cache,
**kwargs,
)
self.text = text
Expand Down Expand Up @@ -1133,6 +1139,11 @@ def construct(self):

"""

@staticmethod
@functools.lru_cache(maxsize=None)
def font_list() -> list[str]:
return manimpango.list_fonts()

def __init__(
self,
text: str,
Expand All @@ -1156,10 +1167,8 @@ def __init__(
) -> 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
Expand Down
Loading