Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct LabelledString base class for MarkupText and MTex #1772

Merged
merged 28 commits into from
Apr 4, 2022
Merged

Construct LabelledString base class for MarkupText and MTex #1772

merged 28 commits into from
Apr 4, 2022

Conversation

YishiMichael
Copy link
Contributor

@YishiMichael YishiMichael commented Mar 30, 2022

Motivation

In previous versions, when users want to slice a Text mobject via a substring, the span of it is directly used to slice the mobject. The result could be unexpected due to spaces, ligatures, and fancy mobjects brought by markup tags. So I referred to the idea of MTex and refactored these classes.

In this PR, MarkupText and MTex both inherit LabelledString class, so they share many similarities. Text is an alias of MarkupText, with the only exception that the attribute is_markup is default to be False. apply_space_chars and disable_ligatures attributes are removed.

Some noticeable features about LabelledString:

  • Still maintains the original svg structure (no additional hierarchies);
  • Has an isolate attribute to add substrings to break up;
  • Has a base_color attribute to specify the default color (default WHITE);
  • Supports methods including get_parts_by_string, get_part_by_string, set_color_by_string, set_color_by_string_to_color_map (in MTex, aliases like get_parts_by_tex are supported; in Text and MarkupText, aliases like get_parts_by_text are supported);
  • Supports TransformMatchingStrings, AddTextWordByWord animations.

The substring selecting logic is improved so that strings can be matched more easily:

tex = MTex("3 \\sqrt{2}", isolate=["\\sqrt{2}"])
tex.set_color_by_tex("sqrt", ORANGE)
self.add(tex)
Result

StringTest0

By default, the string passed into Text is chopped into substrings at the following indices:

Boundaries of words (a.k.a. r"\b"),
Bounderies of continuous spaces (a.k.a. r"\s+"),
Boundaries of markup commands.

So there'll be no problem if one runs the following:

text = Text("Hello text", base_color=TEAL)
text.set_color_by_text("text", ORANGE)
self.add(text)
Result

StringTest1

But if the second line is replaced with text.set_color_by_text("tex", ORANGE), still the entire "text" will be colored. If you need to color "tex" only, you may specify isolate=["tex"]:

text = Text("Hello text", isolate=["tex"], base_color=TEAL)
text.set_color_by_text("tex", ORANGE)
self.add(text)
Result

StringTest2

Other examples:

text = MarkupText(
    "<span face='Consolas'>Hello span</span>",
    global_config={"underline": "double", "underline_color": MAROON},
    local_configs={"Hello": {"bgcolor": TEAL}}
)
text.set_color_by_text("span", ORANGE)
self.add(text)
Result

StringTest3

t0 = MarkupText("here comes <span color='red'>text</span>")
t1 = MarkupText("<span color='yellow'>text</span> is here")
self.add(t0)
self.wait()
self.play(TransformMatchingStrings(t0, t1), run_time=3)
self.wait()
Result
StringTest.mp4
tex = MTex("\\sqrt{s}", isolate=["{s}"])
tex.set_color_by_tex("s", TEAL)
self.add(tex)
Result

StringTest4

paragraph = Text("""
    Lorem ipsum dolor sit amet, consectetur 
    adipisicing elit,  sed do eiusmod tempor 
    incididunt ut labore et dolore magna aliqua. 
    Ut enim ad minim veniam, quis nostrud 
    exercitation ullamco laboris nisi ut 
    aliquip ex ea commodo consequat.
""")
self.play(AddTextWordByWord(paragraph))
Result
StringTest5.mp4

Proposed changes

  • M manimlib/animation/creation.py: fix AddTextWordByWord
  • M manimlib/animation/transform_matching_parts.py: construct TransformMatchingStrings, remove TransformMatchingMTex
  • A manimlib/mobject/svg/labelled_string.py: construct LabelledString base class
  • M manimlib/mobject/svg/mtex_mobject.py: refactor MTex based on LabelledString
  • M manimlib/mobject/svg/svg_mobject.py: remove submobjects of SVGMobject without points
  • M manimlib/mobject/svg/text_mobject.py: refactor Text and MarkupText based on LabelledString
  • M manimlib/utils/iterables.py: remove some unused functions

@Rassska
Copy link

Rassska commented Mar 30, 2022

Huge kudos for that :)
Appreciate your contribution into such a great tool!

@TonyCrane TonyCrane requested a review from 3b1b April 1, 2022 01:13
@3b1b 3b1b merged commit 648855d into 3b1b:master Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants