Text¶
Qualified name: manim.mobject.text.text\_mobject.Text
- class Text(text, fill_opacity=1.0, stroke_width=0, *, color=ManimColor('#FFFFFF'), font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', t2c=None, t2f=None, t2g=None, t2s=None, t2w=None, gradient=None, tab_width=4, warn_missing_font=True, height=None, width=None, should_center=True, disable_ligatures=False, use_svg_cache=False, **kwargs)[source]¶
Bases:
SVGMobjectDisplay (non-LaTeX) text rendered using Pango.
Text objects behave like a
VGroup-like iterable of all characters in the given text. In particular, slicing is possible.Warning
Whitespace and newline characters are stripped internally and never become their own submobject (there is nothing to render for them), so slicing indices are interpreted differently depending on where they are used, and the two conventions disagree with each other:
Slicing the object itself (
my_text[3:5]) indexes into the rendered characters, i.e. the text with whitespace removed. ForText("Hello world"), index5refers to"w", not to the space between the two words.The slice syntax in
t2c/t2s/t2w/t2f/t2g(e.g.t2c={'[3:7]': RED}) indexes into the originaltextargument, whitespace included. ForText("Hello World"),t2c={'[3:7]': RED}colors"l","o","W"(the space at index 5 falls in range but has nothing to color), whereasmy_text[3:7]selects the 4 rendered characters"loWo".
When the substring you want to select is known in advance, prefer keying
t2c/t2s/t2w/t2f/t2gby that substring directly (e.g.t2c={"world": RED}), which matches by text search instead of by index and is unaffected by this quirk.- Parameters:
text (str) – The text that needs to be created as a mobject.
font (str) – The font family to be used to render the text. This is either a system font or one loaded with register_font(). Note that font family names may be different across operating systems.
warn_missing_font (bool) – If True (default), Manim will issue a warning if the font does not exist in the (case-sensitive) list of fonts returned from manimpango.list_fonts().
fill_opacity (float)
stroke_width (float)
color (ParsableManimColor | None)
font_size (float)
line_spacing (float)
slant (str)
weight (str)
t2c (dict[str, str] | None)
t2f (dict[str, str] | None)
t2g (dict[str, Iterable[ParsableManimColor]] | None)
t2s (dict[str, str] | None)
t2w (dict[str, str] | None)
gradient (Iterable[ParsableManimColor] | None)
tab_width (int)
height (float | None)
width (float | None)
should_center (bool)
disable_ligatures (bool)
use_svg_cache (bool)
kwargs (Any)
- Returns:
The mobject-like
VGroup.- Return type:
Examples
Example: Example1Text ¶
from manim import * class Example1Text(Scene): def construct(self): text = Text('Hello world').scale(3) self.add(text)
class Example1Text(Scene): def construct(self): text = Text('Hello world').scale(3) self.add(text)Example: TextColorExample ¶
from manim import * class TextColorExample(Scene): def construct(self): text1 = Text('Hello world', color=BLUE).scale(3) text2 = Text('Hello world', gradient=(BLUE, GREEN)).scale(3).next_to(text1, DOWN) self.add(text1, text2)
class TextColorExample(Scene): def construct(self): text1 = Text('Hello world', color=BLUE).scale(3) text2 = Text('Hello world', gradient=(BLUE, GREEN)).scale(3).next_to(text1, DOWN) self.add(text1, text2)Example: TextItalicAndBoldExample ¶
from manim import * class TextItalicAndBoldExample(Scene): def construct(self): text1 = Text("Hello world", slant=ITALIC) text2 = Text("Hello world", t2s={'world':ITALIC}) text3 = Text("Hello world", weight=BOLD) text4 = Text("Hello world", t2w={'world':BOLD}) text5 = Text("Hello world", t2c={'o':YELLOW}, disable_ligatures=True) text6 = Text( "Visit us at docs.manim.community", t2c={"docs.manim.community": YELLOW}, disable_ligatures=True, ) text6.scale(1.3).shift(DOWN) self.add(text1, text2, text3, text4, text5 , text6) Group(*self.mobjects).arrange(DOWN, buff=.8).set(height=config.frame_height-LARGE_BUFF)
class TextItalicAndBoldExample(Scene): def construct(self): text1 = Text("Hello world", slant=ITALIC) text2 = Text("Hello world", t2s={'world':ITALIC}) text3 = Text("Hello world", weight=BOLD) text4 = Text("Hello world", t2w={'world':BOLD}) text5 = Text("Hello world", t2c={'o':YELLOW}, disable_ligatures=True) text6 = Text( "Visit us at docs.manim.community", t2c={"docs.manim.community": YELLOW}, disable_ligatures=True, ) text6.scale(1.3).shift(DOWN) self.add(text1, text2, text3, text4, text5 , text6) Group(*self.mobjects).arrange(DOWN, buff=.8).set(height=config.frame_height-LARGE_BUFF)Example: TextMoreCustomization ¶
from manim import * class TextMoreCustomization(Scene): def construct(self): text1 = Text( 'Google', t2c={'[:1]': '#3174f0', '[1:2]': '#e53125', '[2:3]': '#fbb003', '[3:4]': '#3174f0', '[4:5]': '#269a43', '[5:]': '#e53125'}, font_size=58).scale(3) self.add(text1)
class TextMoreCustomization(Scene): def construct(self): text1 = Text( 'Google', t2c={'[:1]': '#3174f0', '[1:2]': '#e53125', '[2:3]': '#fbb003', '[3:4]': '#3174f0', '[4:5]': '#269a43', '[5:]': '#e53125'}, font_size=58).scale(3) self.add(text1)As
Textuses Pango to render text, rendering non-English characters is easily possible:Example: MultipleFonts ¶
from manim import * class MultipleFonts(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") japanese = Text( "日本へようこそ", t2c={"日本": BLUE} ) # works same as ``Text``. mess = Text("Multi-Language", weight=BOLD) russ = Text("Здравствуйте मस नम म ", font="sans-serif") hin = Text("नमस्ते", font="sans-serif") arb = Text( "صباح الخير \n تشرفت بمقابلتك", font="sans-serif" ) # don't mix RTL and LTR languages nothing shows up then ;-) chinese = Text("臂猿「黛比」帶著孩子", font="sans-serif") self.add(morning, japanese, mess, russ, hin, arb, chinese) for i,mobj in enumerate(self.mobjects): mobj.shift(DOWN*(i-3))
class MultipleFonts(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") japanese = Text( "日本へようこそ", t2c={"日本": BLUE} ) # works same as ``Text``. mess = Text("Multi-Language", weight=BOLD) russ = Text("Здравствуйте मस नम म ", font="sans-serif") hin = Text("नमस्ते", font="sans-serif") arb = Text( "صباح الخير \n تشرفت بمقابلتك", font="sans-serif" ) # don't mix RTL and LTR languages nothing shows up then ;-) chinese = Text("臂猿「黛比」帶著孩子", font="sans-serif") self.add(morning, japanese, mess, russ, hin, arb, chinese) for i,mobj in enumerate(self.mobjects): mobj.shift(DOWN*(i-3))Example: PangoRender ¶
from manim import * class PangoRender(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") self.play(Write(morning)) self.wait(2)
class PangoRender(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") self.play(Write(morning)) self.wait(2)Tests
Check that the creation of
Textworks:>>> Text('The horse does not eat cucumber salad.') Text('The horse does not eat cucumber salad.')
Methods
font_listInitializes the colors.
Attributes
alwaysCall a method on a mobject every frame.
animateUsed to animate the application of any method of
self.animation_overridescolordepthThe depth of the mobject.
fill_colorIf there are multiple colors (for gradient) this returns the first one
font_sizehash_seedA unique hash representing the result of the generated mobject points.
heightThe height of the mobject.
n_points_per_curvesheen_factorstroke_colorwidthThe width of the mobject.
targetoriginal_id- _find_indexes(word, text)[source]¶
Finds the indexes of
textinword.- Parameters:
word (str)
text (str)
- Return type:
list[tuple[int, int]]
- _original__init__(text, fill_opacity=1.0, stroke_width=0, color=None, font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', t2c=None, t2f=None, t2g=None, t2s=None, t2w=None, gradient=None, tab_width=4, warn_missing_font=True, height=None, width=None, should_center=True, disable_ligatures=False, use_svg_cache=False, **kwargs)¶
Initialize self. See help(type(self)) for accurate signature.
- Parameters:
text (str)
fill_opacity (float)
stroke_width (float)
color (TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor') | None)
font_size (float)
line_spacing (float)
font (str)
slant (str)
weight (str)
t2c (dict[str, str] | None)
t2f (dict[str, str] | None)
t2g (dict[str, Iterable[TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor')]] | None)
t2s (dict[str, str] | None)
t2w (dict[str, str] | None)
gradient (Iterable[TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor')] | None)
tab_width (int)
warn_missing_font (bool)
height (float | None)
width (float | None)
should_center (bool)
disable_ligatures (bool)
use_svg_cache (bool)
kwargs (Any)
- _text2hash(color)[source]¶
Generates
sha256hash for file name.- Parameters:
color (ParsableManimColor)
- Return type:
str
- _text2settings(color)[source]¶
Converts the texts and styles to a setting for parsing.
- Parameters:
color (ParsableManimColor)
- Return type:
list[TextSetting]
- _text2svg(color)[source]¶
Convert the text to SVG using Pango.
- Parameters:
color (ParsableManimColor)
- Return type:
str