-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
Conversation
This file contains 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
Huge kudos for that :) |
TonyCrane
approved these changes
Apr 1, 2022
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
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 ofMTex
and refactored these classes.In this PR,
MarkupText
andMTex
both inheritLabelledString
class, so they share many similarities.Text
is an alias ofMarkupText
, with the only exception that the attributeis_markup
is default to beFalse
.apply_space_chars
anddisable_ligatures
attributes are removed.Some noticeable features about
LabelledString
:isolate
attribute to add substrings to break up;base_color
attribute 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_tex
are supported; inText
andMarkupText
, aliases likeget_parts_by_text
are supported);TransformMatchingStrings
,AddTextWordByWord
animations.The substring selecting logic is improved so that strings can be matched more easily:
Result
By default, the string passed into
Text
is 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
: fixAddTextWordByWord
manimlib/animation/transform_matching_parts.py
: constructTransformMatchingStrings
, removeTransformMatchingMTex
manimlib/mobject/svg/labelled_string.py
: constructLabelledString
base classmanimlib/mobject/svg/mtex_mobject.py
: refactorMTex
based onLabelledString
manimlib/mobject/svg/svg_mobject.py
: remove submobjects ofSVGMobject
without pointsmanimlib/mobject/svg/text_mobject.py
: refactorText
andMarkupText
based onLabelledString
manimlib/utils/iterables.py
: remove some unused functions