Skip to content

Commit fcd81b2

Browse files
authored
Improve handling of specified font name (#3429)
Co-authored-by: Jason Grace <[email protected]> Co-authored-by: JasonGrace2282 <[email protected]> The proposed fix does two things : * If the specified font is 'sans-serif' : change it to 'sans' as this is the name used in the list of fonts * if the font name is not in the list of fonts, automatically check if the capitalized version of the font exists in the list of fonts. If not, print a warning to the user.
1 parent 011c36a commit fcd81b2

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

manim/mobject/text/text_mobject.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,21 @@ def __init__(
444444
**kwargs,
445445
) -> None:
446446
self.line_spacing = line_spacing
447-
if font and warn_missing_font and font not in Text.font_list():
448-
logger.warning(f"Font {font} not in {Text.font_list()}.")
447+
if font and warn_missing_font:
448+
fonts_list = Text.font_list()
449+
# handle special case of sans/sans-serif
450+
if font.lower() == "sans-serif":
451+
font = "sans"
452+
if font not in fonts_list:
453+
# check if the capitalized version is in the supported fonts
454+
if font.capitalize() in fonts_list:
455+
font = font.capitalize()
456+
elif font.lower() in fonts_list:
457+
font = font.lower()
458+
elif font.title() in fonts_list:
459+
font = font.title()
460+
else:
461+
logger.warning(f"Font {font} not in {fonts_list}.")
449462
self.font = font
450463
self._font_size = float(font_size)
451464
# needs to be a float or else size is inflated when font_size = 24
@@ -1169,8 +1182,21 @@ def __init__(
11691182
) -> None:
11701183
self.text = text
11711184
self.line_spacing = line_spacing
1172-
if font and warn_missing_font and font not in Text.font_list():
1173-
logger.warning(f"Font {font} not in {Text.font_list()}.")
1185+
if font and warn_missing_font:
1186+
fonts_list = Text.font_list()
1187+
# handle special case of sans/sans-serif
1188+
if font.lower() == "sans-serif":
1189+
font = "sans"
1190+
if font not in fonts_list:
1191+
# check if the capitalized version is in the supported fonts
1192+
if font.capitalize() in fonts_list:
1193+
font = font.capitalize()
1194+
elif font.lower() in fonts_list:
1195+
font = font.lower()
1196+
elif font.title() in fonts_list:
1197+
font = font.title()
1198+
else:
1199+
logger.warning(f"Font {font} not in {fonts_list}.")
11741200
self.font = font
11751201
self._font_size = float(font_size)
11761202
self.slant = slant

tests/module/mobject/text/test_text_mobject.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from __future__ import annotations
22

3+
from contextlib import redirect_stdout
4+
from io import StringIO
5+
36
from manim.mobject.text.text_mobject import MarkupText, Text
47

58

@@ -11,3 +14,20 @@ def test_font_size():
1114

1215
assert round(text_string.font_size, 5) == 14.4
1316
assert round(markuptext_string.font_size, 5) == 14.4
17+
18+
19+
def test_font_warnings():
20+
def warning_printed(font: str, **kwargs) -> bool:
21+
io = StringIO()
22+
with redirect_stdout(io):
23+
Text("hi!", font=font, **kwargs)
24+
txt = io.getvalue()
25+
return "Font" in txt and "not in" in txt
26+
27+
# check for normal fonts (no warning)
28+
assert not warning_printed("System-ui", warn_missing_font=True)
29+
# should be converted to sans before checking
30+
assert not warning_printed("Sans-serif", warn_missing_font=True)
31+
32+
# check random string (should be warning)
33+
assert warning_printed("Manim!" * 3, warn_missing_font=True)

0 commit comments

Comments
 (0)