Skip to content

Commit

Permalink
Improve handling of specified font name (ManimCommunity#3429)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
staghado authored Feb 13, 2024
1 parent 011c36a commit fcd81b2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
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)

0 comments on commit fcd81b2

Please sign in to comment.