add an example for Text

This commit is contained in:
Tony031218
2021-01-15 21:11:17 +08:00
parent 1d1706039e
commit f696ba4100
2 changed files with 47 additions and 0 deletions

View File

@ -104,6 +104,45 @@ class WarpSquare(Scene):
self.wait() self.wait()
class TextExample(Scene):
def construct(self):
# To run this scene properly, you should have "Consolas" font in your computer
# for full usage, you can see https://github.com/3b1b/manim/pull/680
text = Text("Here is a text", font="Consolas", size=2)
difference = Text(
"""
The most important difference between Text and TextMobject is that\n
you can change the font more easily, but can't use the LaTeX gramma
""",
font="Arial", size=0.5,
# t2c is a dict that you can choose color for different text
t2c={"Text": BLUE, "TextMobject": BLUE, "LaTeX": ORANGE}
)
VGroup(text, difference).arrange(DOWN, buff=1)
self.play(Write(text))
self.play(FadeIn(difference, UP))
self.wait(3)
fonts = Text(
"And you can also set the font according to different words",
t2f={"font": "Consolas", "words": "Consolas"},
t2c={"font": BLUE, "words": GREEN}
)
slant = Text(
"And the same as slant and weight",
font="Consolas",
t2s={"slant": ITALIC},
t2w={"weight": BOLD},
t2c={"slant": ORANGE, "weight": RED}
)
VGroup(fonts, slant).arrange(DOWN, buff=0.8)
self.play(FadeOut(text), FadeOut(difference, shift=DOWN))
self.play(Write(fonts))
self.wait()
self.play(Write(slant))
self.wait()
class SquareToCircle(Scene): class SquareToCircle(Scene):
def construct(self): def construct(self):
circle = Circle() circle = Circle()

View File

@ -52,6 +52,7 @@ class Text(SVGMobject):
self.lsh = self.size if self.lsh == -1 else self.lsh self.lsh = self.size if self.lsh == -1 else self.lsh
file_name = self.text2svg() file_name = self.text2svg()
self.remove_last_M(file_name)
SVGMobject.__init__(self, file_name, **config) SVGMobject.__init__(self, file_name, **config)
if self.t2c: if self.t2c:
@ -65,6 +66,13 @@ class Text(SVGMobject):
if self.height is None: if self.height is None:
self.scale(TEXT_MOB_SCALE_FACTOR) self.scale(TEXT_MOB_SCALE_FACTOR)
def remove_last_M(self, file_name):
with open(file_name, 'r') as fpr:
content = fpr.read()
content = re.sub(r'Z M [^A-Za-z]*? "\/>', 'Z "/>', content)
with open(file_name, 'w') as fpw:
fpw.write(content)
def find_indexes(self, word): def find_indexes(self, word):
m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word) m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word)
if m: if m: