标记文本¶
限定名称: manim.mobject.text.text\_mobject.MarkupText
- class MarkupText(text, fill_opacity=1, stroke_width=0, color=None, font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', justify=False, gradient=None, tab_width=4, height=None, width=None, should_center=True, disable_ligatures=False, warn_missing_font=True, **kwargs)[来源]¶
基础类:
SVGMobject显示使用Pango渲染的(非LaTeX)文本。
文本对象的行为类似于
VGroup,可以迭代给定文本中的所有字符。特别是,可以进行切片操作。什么是PangoMarkup?
PangoMarkup 是一种类似于 HTML 的小型标记语言,它可以帮助你在对一段文本进行着色或样式设置时避免使用“字符范围”。你可以使用这种语言与
MarkupText一起使用。一个简单的标记字符串示例可能是:
<span foreground="blue" size="x-large">Blue text</span> is <i>cool</i>!"
它可以与
MarkupText一起使用,如下所示:示例:MarkupExample ¶
from manim import * class MarkupExample(Scene): def construct(self): text = MarkupText('<span foreground="blue" size="x-large">Blue text</span> is <i>cool</i>!"') self.add(text)
class MarkupExample(Scene): def construct(self): text = MarkupText('Blue text is cool!"') self.add(text)一个更复杂的例子是:
示例:MarkupElaborateExample ¶
from manim import * class MarkupElaborateExample(Scene): def construct(self): text = MarkupText( '<span foreground="purple">ا</span><span foreground="red">َ</span>' 'ل<span foreground="blue">ْ</span>ع<span foreground="red">َ</span>ر' '<span foreground="red">َ</span>ب<span foreground="red">ِ</span>ي' '<span foreground="green">ّ</span><span foreground="red">َ</span>ة' '<span foreground="blue">ُ</span>' ) self.add(text)
class MarkupElaborateExample(Scene): def construct(self): text = MarkupText( 'اَ' 'لْعَر' 'َبِي' 'َّة' 'ُ' ) self.add(text)PangoMarkup 也可以包含 XML 特性,例如数字字符实体,如
©用于 © 也可以使用。最通用的标记标签是
,然后还有一些方便的标签。以下是支持的标签列表:
加粗,斜体和加粗+斜体和- 下划线
删除线through打字机 字体bigger font和smaller font上标和下标underline="double" underline_color="green">double underlineunderline="error">error underlineoverline="single" overline_color="green">上划线strikethrough="true" strikethrough_color="red">删除线font_family="sans">临时的 字体 更改foreground="red">临时的 颜色 变化fgcolor="red">临时的 颜色 变化from="YELLOW" to="RED">临时 渐变
对于
标记,颜色可以指定为十六进制三元组,如#aabbcc,或者作为命名的CSS颜色,如AliceBlue。标签由Manim而不是Pango处理,并支持十六进制三元组或Manim常量,如RED或RED_A。 如果你想使用Manim常量如RED_A与一起使用,你需要使用Python的f-String语法,如下所示:MarkupText(f'<span foreground="{RED_A}">here you go</span>')
如果你的文本包含连字,
MarkupText类在创建渐变时可能会错误地确定第一个和最后一个字母。这是因为fl是两个独立的字符,但可能被设置为一个单一的符号——连字。如果你的语言不依赖于连字,考虑将disable_ligatures设置为True。如果你必须使用连字,gradient标签支持一个可选的属性offset,可以用来补偿这个错误。例如:
以便 开始 渐变提前一个字母from="RED" to="YELLOW" offset="1">example 提前一个字母结束渐变from="RED" to="YELLOW" offset=",1">example 以开始渐变提前两个字母,并结束提前一个字母from="RED" to="YELLOW" offset="2,1">example
如果要着色的文本本身包含连字,可能需要指定第二个偏移量。使用HTML实体表示特殊字符时也可能发生同样的情况。
当使用
underline、overline或strikethrough与标签一起使用时,您还需要使用偏移量,因为 下划线是最终SVGMobject中的附加路径。 请查看以下示例。特殊字符的转义:
>应该 写成>,而<和&必须 写成<和&。您可以在相应的文档页面上找到更多关于Pango标记格式的信息: Pango Markup。 请注意,并非所有功能都受此类支持,并且Pango不支持上述提到的
标签。- Parameters:
文本 (str) – 需要创建为mobject的文本。
fill_opacity (float) – 填充不透明度,1表示不透明,0表示透明。
stroke_width (float) – 描边宽度。
font_size (float) – 字体大小。
line_spacing (int) – 行间距。
font (str) – 整个文本的全局字体设置。可以进行局部覆盖。
slant (str) – 全局倾斜设置,例如 NORMAL 或 ITALIC。局部覆盖是可能的。
weight (str) – 全局权重设置,例如 NORMAL 或 BOLD。局部覆盖是可能的。
gradient (tuple) – 全局梯度设置。可以局部覆盖。
warn_missing_font (bool) – 如果为True(默认值),当字体不存在于manimpango.list_fonts()返回的(区分大小写的)字体列表中时,Manim将发出警告。
颜色 (ParsableManimColor | 无)
justify (bool)
tab_width (int)
height (int)
width (int)
should_center (bool)
disable_ligatures (bool)
- Returns:
以
VGroup形式的mobject显示的文本。- Return type:
示例
示例:BasicMarkupExample ¶
from manim import * class BasicMarkupExample(Scene): def construct(self): text1 = MarkupText("<b>foo</b> <i>bar</i> <b><i>foobar</i></b>") text2 = MarkupText("<s>foo</s> <u>bar</u> <big>big</big> <small>small</small>") text3 = MarkupText("H<sub>2</sub>O and H<sub>3</sub>O<sup>+</sup>") text4 = MarkupText("type <tt>help</tt> for help") text5 = MarkupText( '<span underline="double">foo</span> <span underline="error">bar</span>' ) group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN) self.add(group)
class BasicMarkupExample(Scene): def construct(self): text1 = MarkupText("foo bar foobar") text2 = MarkupText("foobar big small") text3 = MarkupText("H2O and H3O+") text4 = MarkupText("type help for help") text5 = MarkupText( 'foo bar' ) group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN) self.add(group)示例:ColorExample ¶
from manim import * class ColorExample(Scene): def construct(self): text1 = MarkupText( f'all in red <span fgcolor="{YELLOW}">except this</span>', color=RED ) text2 = MarkupText("nice gradient", gradient=(BLUE, GREEN)) text3 = MarkupText( 'nice <gradient from="RED" to="YELLOW">intermediate</gradient> gradient', gradient=(BLUE, GREEN), ) text4 = MarkupText( 'fl ligature <gradient from="RED" to="YELLOW">causing trouble</gradient> here' ) text5 = MarkupText( 'fl ligature <gradient from="RED" to="YELLOW" offset="1">defeated</gradient> with offset' ) text6 = MarkupText( 'fl ligature <gradient from="RED" to="YELLOW" offset="1">floating</gradient> inside' ) text7 = MarkupText( 'fl ligature <gradient from="RED" to="YELLOW" offset="1,1">floating</gradient> inside' ) group = VGroup(text1, text2, text3, text4, text5, text6, text7).arrange(DOWN) self.add(group)
class ColorExample(Scene): def construct(self): text1 = MarkupText( f'all in red except this', color=RED ) text2 = MarkupText("nice gradient", gradient=(BLUE, GREEN)) text3 = MarkupText( 'niceintermediate gradient', gradient=(BLUE, GREEN), ) text4 = MarkupText( 'fl ligaturecausing trouble here' ) text5 = MarkupText( 'fl ligaturedefeated with offset' ) text6 = MarkupText( 'fl ligaturefloating inside' ) text7 = MarkupText( 'fl ligaturefloating inside' ) group = VGroup(text1, text2, text3, text4, text5, text6, text7).arrange(DOWN) self.add(group)示例:下划线示例 ¶
from manim import * class UnderlineExample(Scene): def construct(self): text1 = MarkupText( '<span underline="double" underline_color="green">bla</span>' ) text2 = MarkupText( '<span underline="single" underline_color="green">xxx</span><gradient from="#ffff00" to="RED">aabb</gradient>y' ) text3 = MarkupText( '<span underline="single" underline_color="green">xxx</span><gradient from="#ffff00" to="RED" offset="-1">aabb</gradient>y' ) text4 = MarkupText( '<span underline="double" underline_color="green">xxx</span><gradient from="#ffff00" to="RED">aabb</gradient>y' ) text5 = MarkupText( '<span underline="double" underline_color="green">xxx</span><gradient from="#ffff00" to="RED" offset="-2">aabb</gradient>y' ) group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN) self.add(group)
class UnderlineExample(Scene): def construct(self): text1 = MarkupText( 'bla' ) text2 = MarkupText( 'xxxaabb y' ) text3 = MarkupText( 'xxxaabb y' ) text4 = MarkupText( 'xxxaabb y' ) text5 = MarkupText( 'xxxaabb y' ) group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN) self.add(group)示例:FontExample ¶
from manim import * class FontExample(Scene): def construct(self): text1 = MarkupText( 'all in sans <span font_family="serif">except this</span>', font="sans" ) text2 = MarkupText( '<span font_family="serif">mixing</span> <span font_family="sans">fonts</span> <span font_family="monospace">is ugly</span>' ) text3 = MarkupText("special char > or >") text4 = MarkupText("special char < and &") group = VGroup(text1, text2, text3, text4).arrange(DOWN) self.add(group)
class FontExample(Scene): def construct(self): text1 = MarkupText( 'all in sans except this', font="sans" ) text2 = MarkupText( 'mixing fonts is ugly' ) text3 = MarkupText("special char > or >") text4 = MarkupText("special char < and &") group = VGroup(text1, text2, text3, text4).arrange(DOWN) self.add(group)示例:换行示例 ¶
from manim import * class NewlineExample(Scene): def construct(self): text = MarkupText('foooo<span foreground="red">oo\nbaa</span>aar') self.add(text)
class NewlineExample(Scene): def construct(self): text = MarkupText('foooooo\nbaaaar') self.add(text)示例:NoLigaturesExample ¶
from manim import * class NoLigaturesExample(Scene): def construct(self): text1 = MarkupText('fl<gradient from="RED" to="GREEN">oat</gradient>ing') text2 = MarkupText('fl<gradient from="RED" to="GREEN">oat</gradient>ing', disable_ligatures=True) group = VGroup(text1, text2).arrange(DOWN) self.add(group)
class NoLigaturesExample(Scene): def construct(self): text1 = MarkupText('float ing') text2 = MarkupText('float ing', disable_ligatures=True) group = VGroup(text1, text2).arrange(DOWN) self.add(group)由于
MarkupText使用Pango来渲染文本,渲染非英文字符变得非常容易:示例:多语言 ¶
from manim import * class MultiLanguage(Scene): def construct(self): morning = MarkupText("வணக்கம்", font="sans-serif") japanese = MarkupText( '<span fgcolor="blue">日本</span>へようこそ' ) # works as in ``Text``. mess = MarkupText("Multi-Language", weight=BOLD) russ = MarkupText("Здравствуйте मस नम म ", font="sans-serif") hin = MarkupText("नमस्ते", font="sans-serif") chinese = MarkupText("臂猿「黛比」帶著孩子", font="sans-serif") group = VGroup(morning, japanese, mess, russ, hin, chinese).arrange(DOWN) self.add(group)
class MultiLanguage(Scene): def construct(self): morning = MarkupText("வணக்கம்", font="sans-serif") japanese = MarkupText( '日本へようこそ' ) # works as in ``Text``. mess = MarkupText("Multi-Language", weight=BOLD) russ = MarkupText("Здравствуйте मस नम म ", font="sans-serif") hin = MarkupText("नमस्ते", font="sans-serif") chinese = MarkupText("臂猿「黛比」帶著孩子", font="sans-serif") group = VGroup(morning, japanese, mess, russ, hin, chinese).arrange(DOWN) self.add(group)您可以通过传递
justify参数来对齐文本。示例:JustifyText ¶
from manim import * class JustifyText(Scene): def construct(self): ipsum_text = ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit." "Praesent feugiat metus sit amet iaculis pulvinar. Nulla posuere " "quam a ex aliquam, eleifend consectetur tellus viverra. Aliquam " "fermentum interdum justo, nec rutrum elit pretium ac. Nam quis " "leo pulvinar, dignissim est at, venenatis nisi." ) justified_text = MarkupText(ipsum_text, justify=True).scale(0.4) not_justified_text = MarkupText(ipsum_text, justify=False).scale(0.4) just_title = Title("Justified") njust_title = Title("Not Justified") self.add(njust_title, not_justified_text) self.play( FadeOut(not_justified_text), FadeIn(justified_text), FadeOut(njust_title), FadeIn(just_title), ) self.wait(1)
class JustifyText(Scene): def construct(self): ipsum_text = ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit." "Praesent feugiat metus sit amet iaculis pulvinar. Nulla posuere " "quam a ex aliquam, eleifend consectetur tellus viverra. Aliquam " "fermentum interdum justo, nec rutrum elit pretium ac. Nam quis " "leo pulvinar, dignissim est at, venenatis nisi." ) justified_text = MarkupText(ipsum_text, justify=True).scale(0.4) not_justified_text = MarkupText(ipsum_text, justify=False).scale(0.4) just_title = Title("Justified") njust_title = Title("Not Justified") self.add(njust_title, not_justified_text) self.play( FadeOut(not_justified_text), FadeIn(justified_text), FadeOut(njust_title), FadeIn(just_title), ) self.wait(1)测试
检查
MarkupText的创建是否有效:>>> MarkupText('The horse does not eat cucumber salad.') MarkupText('The horse does not eat cucumber salad.')
方法
font_list属性
animate用于动画化
self的任何方法的应用。animation_overridescolordepthmobject的深度。
fill_color如果有多种颜色(用于渐变),则返回第一个颜色
font_sizehash_seed表示生成的mobject点结果的唯一哈希值。
heightmobject的高度。
n_points_per_curvesheen_factorstroke_colorwidthmobject的宽度。
- _extract_color_tags()[来源]¶
用于确定字符串的哪些部分(如果有)应使用自定义颜色进行格式化。
移除
标签,因为它不是Pango标记的一部分,会导致错误。注意:使用
标签已被弃用。一旦旧语法被移除,此函数将被删除。
- _original__init__(text, fill_opacity=1, stroke_width=0, color=None, font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', justify=False, gradient=None, tab_width=4, height=None, width=None, should_center=True, disable_ligatures=False, warn_missing_font=True, **kwargs)¶
初始化自身。有关准确的签名,请参阅 help(type(self))。
- Parameters:
文本 (字符串)
fill_opacity (float)
stroke_width (float)
颜色 (ParsableManimColor | 无)
font_size (float)
line_spacing (int)
字体 (字符串)
slant (str)
weight (str)
justify (bool)
梯度 (元组)
tab_width (int)
height (int)
width (int)
should_center (bool)
disable_ligatures (bool)
warn_missing_font (bool)
- Return type:
无
- _text2hash(color)[source]¶
为文件名生成
sha256哈希值。- Parameters:
颜色 (ParsableManimColor)
- _text2svg(color)[source]¶
使用Pango将文本转换为SVG。
- Parameters:
颜色 (ParsableManimColor | 无)