mirror of
https://github.com/3b1b/manim.git
synced 2025-08-03 04:04:36 +08:00
Fix bug when handling multi-line tex
This commit is contained in:
@ -214,7 +214,7 @@ class AddTextWordByWord(ShowIncreasingSubsets):
|
||||
|
||||
def __init__(self, string_mobject, **kwargs):
|
||||
assert isinstance(string_mobject, LabelledString)
|
||||
grouped_mobject = string_mobject.get_submob_groups()
|
||||
grouped_mobject = string_mobject.submob_groups
|
||||
digest_config(self, kwargs)
|
||||
if self.run_time is None:
|
||||
self.run_time = self.time_per_word * len(grouped_mobject)
|
||||
|
@ -160,77 +160,67 @@ class TransformMatchingStrings(AnimationGroup):
|
||||
}
|
||||
|
||||
def __init__(self,
|
||||
source_mobject: LabelledString,
|
||||
target_mobject: LabelledString,
|
||||
source: LabelledString,
|
||||
target: LabelledString,
|
||||
**kwargs
|
||||
):
|
||||
digest_config(self, kwargs)
|
||||
assert isinstance(source_mobject, LabelledString)
|
||||
assert isinstance(target_mobject, LabelledString)
|
||||
assert isinstance(source, LabelledString)
|
||||
assert isinstance(target, LabelledString)
|
||||
anims = []
|
||||
rest_source_indices = list(range(len(source_mobject.submobjects)))
|
||||
rest_target_indices = list(range(len(target_mobject.submobjects)))
|
||||
source_indices = list(range(len(source.labelled_submobjects)))
|
||||
target_indices = list(range(len(target.labelled_submobjects)))
|
||||
|
||||
def get_indices_lists(mobject, parts):
|
||||
return [
|
||||
[
|
||||
mobject.labelled_submobjects.index(submob)
|
||||
for submob in part
|
||||
]
|
||||
for part in parts
|
||||
]
|
||||
|
||||
def add_anims_from(anim_class, func, source_args, target_args=None):
|
||||
if target_args is None:
|
||||
target_args = source_args.copy()
|
||||
for source_arg, target_arg in zip(source_args, target_args):
|
||||
source_parts = func(source_mobject, source_arg)
|
||||
target_parts = func(target_mobject, target_arg)
|
||||
source_indices_lists = source_mobject.indices_lists_of_parts(
|
||||
source_parts
|
||||
)
|
||||
target_indices_lists = target_mobject.indices_lists_of_parts(
|
||||
target_parts
|
||||
)
|
||||
filtered_source_indices_lists = list(filter(
|
||||
source_parts = func(source, source_arg)
|
||||
target_parts = func(target, target_arg)
|
||||
source_indices_lists = list(filter(
|
||||
lambda indices_list: all([
|
||||
index in rest_source_indices
|
||||
index in source_indices
|
||||
for index in indices_list
|
||||
]), source_indices_lists
|
||||
]), get_indices_lists(source, source_parts)
|
||||
))
|
||||
filtered_target_indices_lists = list(filter(
|
||||
target_indices_lists = list(filter(
|
||||
lambda indices_list: all([
|
||||
index in rest_target_indices
|
||||
index in target_indices
|
||||
for index in indices_list
|
||||
]), target_indices_lists
|
||||
]), get_indices_lists(target, target_parts)
|
||||
))
|
||||
if not all([
|
||||
filtered_source_indices_lists,
|
||||
filtered_target_indices_lists
|
||||
]):
|
||||
if not source_indices_lists or not target_indices_lists:
|
||||
continue
|
||||
anims.append(anim_class(source_parts, target_parts, **kwargs))
|
||||
for index in it.chain(*filtered_source_indices_lists):
|
||||
rest_source_indices.remove(index)
|
||||
for index in it.chain(*filtered_target_indices_lists):
|
||||
rest_target_indices.remove(index)
|
||||
for index in it.chain(*source_indices_lists):
|
||||
source_indices.remove(index)
|
||||
for index in it.chain(*target_indices_lists):
|
||||
target_indices.remove(index)
|
||||
|
||||
def get_common_substrs(func):
|
||||
def get_common_substrs(substrs_from_source, substrs_from_target):
|
||||
return sorted([
|
||||
substr for substr in func(source_mobject)
|
||||
if substr and substr in func(target_mobject)
|
||||
substr for substr in substrs_from_source
|
||||
if substr and substr in substrs_from_target
|
||||
], key=len, reverse=True)
|
||||
|
||||
def get_parts_from_keys(mobject, keys):
|
||||
if not isinstance(keys, tuple):
|
||||
keys = (keys,)
|
||||
indices = []
|
||||
if isinstance(keys, str):
|
||||
keys = [keys]
|
||||
result = VGroup()
|
||||
for key in keys:
|
||||
if isinstance(key, int):
|
||||
indices.append(key)
|
||||
elif isinstance(key, range):
|
||||
indices.extend(key)
|
||||
elif isinstance(key, str):
|
||||
all_parts = mobject.get_parts_by_string(key)
|
||||
indices.extend(it.chain(*[
|
||||
mobject.indices_of_part(part) for part in all_parts
|
||||
]))
|
||||
else:
|
||||
if not isinstance(key, str):
|
||||
raise TypeError(key)
|
||||
return VGroup(VGroup(*[
|
||||
mobject[index] for index in remove_list_redundancies(indices)
|
||||
]))
|
||||
result.add(*mobject.get_parts_by_string(key))
|
||||
return result
|
||||
|
||||
add_anims_from(
|
||||
ReplacementTransform, get_parts_from_keys,
|
||||
@ -239,38 +229,32 @@ class TransformMatchingStrings(AnimationGroup):
|
||||
add_anims_from(
|
||||
FadeTransformPieces,
|
||||
LabelledString.get_parts_by_string,
|
||||
get_common_substrs(LabelledString.get_specified_substrs)
|
||||
get_common_substrs(
|
||||
source.specified_substrs,
|
||||
target.specified_substrs
|
||||
)
|
||||
)
|
||||
add_anims_from(
|
||||
FadeTransformPieces,
|
||||
LabelledString.get_parts_by_group_substr,
|
||||
get_common_substrs(LabelledString.get_group_substrs)
|
||||
get_common_substrs(
|
||||
source.group_substrs,
|
||||
target.group_substrs
|
||||
)
|
||||
)
|
||||
|
||||
fade_source = VGroup(*[
|
||||
source_mobject[index]
|
||||
for index in rest_source_indices
|
||||
])
|
||||
fade_target = VGroup(*[
|
||||
target_mobject[index]
|
||||
for index in rest_target_indices
|
||||
])
|
||||
rest_source = VGroup(*[source[index] for index in source_indices])
|
||||
rest_target = VGroup(*[target[index] for index in target_indices])
|
||||
if self.transform_mismatches:
|
||||
anims.append(ReplacementTransform(
|
||||
fade_source,
|
||||
fade_target,
|
||||
**kwargs
|
||||
))
|
||||
anims.append(
|
||||
ReplacementTransform(rest_source, rest_target, **kwargs)
|
||||
)
|
||||
else:
|
||||
anims.append(FadeOutToPoint(
|
||||
fade_source,
|
||||
target_mobject.get_center(),
|
||||
**kwargs
|
||||
))
|
||||
anims.append(FadeInFromPoint(
|
||||
fade_target,
|
||||
source_mobject.get_center(),
|
||||
**kwargs
|
||||
))
|
||||
anims.append(
|
||||
FadeOutToPoint(rest_source, target.get_center(), **kwargs)
|
||||
)
|
||||
anims.append(
|
||||
FadeInFromPoint(rest_target, source.get_center(), **kwargs)
|
||||
)
|
||||
|
||||
super().__init__(*anims)
|
||||
|
Reference in New Issue
Block a user