Construct LabelledString base class for MarkupText and MTex#1772
Merged
3b1b merged 28 commits into3b1b:masterfrom Apr 4, 2022
YishiMichael:master
Merged
Construct LabelledString base class for MarkupText and MTex#17723b1b merged 28 commits into3b1b:masterfrom YishiMichael:master
3b1b merged 28 commits into3b1b:masterfrom
YishiMichael:master
Conversation
|
Huge kudos for that :) |
TonyCrane
approved these changes
Apr 1, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
In previous versions, when users want to slice a
Textmobject 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 ofMTexand refactored these classes.In this PR,
MarkupTextandMTexboth inheritLabelledStringclass, so they share many similarities.Textis an alias ofMarkupText, with the only exception that the attributeis_markupis default to beFalse.apply_space_charsanddisable_ligaturesattributes are removed.Some noticeable features about
LabelledString:isolateattribute to add substrings to break up;base_colorattribute to specify the default color (defaultWHITE);get_parts_by_string,get_part_by_string,set_color_by_string,set_color_by_string_to_color_map(inMTex, aliases likeget_parts_by_texare supported; inTextandMarkupText, aliases likeget_parts_by_textare supported);TransformMatchingStrings,AddTextWordByWordanimations.The substring selecting logic is improved so that strings can be matched more easily:
Result
By default, the string passed into
Textis chopped into substrings at the following indices:So there'll be no problem if one runs the following:
Result
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 specifyisolate=["tex"]:Result
Other examples:
Result
Result
StringTest.mp4
Result
Result
StringTest5.mp4
Proposed changes
manimlib/animation/creation.py: fixAddTextWordByWordmanimlib/animation/transform_matching_parts.py: constructTransformMatchingStrings, removeTransformMatchingMTexmanimlib/mobject/svg/labelled_string.py: constructLabelledStringbase classmanimlib/mobject/svg/mtex_mobject.py: refactorMTexbased onLabelledStringmanimlib/mobject/svg/svg_mobject.py: remove submobjects ofSVGMobjectwithout pointsmanimlib/mobject/svg/text_mobject.py: refactorTextandMarkupTextbased onLabelledStringmanimlib/utils/iterables.py: remove some unused functions