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

Improve handling of specified font name #3429

Merged
Merged
Show file tree
Hide file tree
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
34 changes: 30 additions & 4 deletions manim/mobject/text/text_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/module/mobject/text/test_text_mobject.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Loading