mirror of
https://github.com/3b1b/manim.git
synced 2025-11-15 05:07:46 +00:00
Add option for StringMobject to only render a single svg
Then set as the default behavior for MTex and Text
This commit is contained in:
parent
1a485ddd19
commit
9c106eb873
3 changed files with 14 additions and 14 deletions
|
|
@ -29,6 +29,7 @@ class MTex(StringMobject):
|
||||||
additional_preamble: str = "",
|
additional_preamble: str = "",
|
||||||
tex_to_color_map: dict = dict(),
|
tex_to_color_map: dict = dict(),
|
||||||
t2c: dict = dict(),
|
t2c: dict = dict(),
|
||||||
|
use_labelled_svg: bool = True,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
# Prevent from passing an empty string.
|
# Prevent from passing an empty string.
|
||||||
|
|
@ -40,7 +41,7 @@ class MTex(StringMobject):
|
||||||
self.additional_preamble = additional_preamble
|
self.additional_preamble = additional_preamble
|
||||||
self.tex_to_color_map = dict(**t2c, **tex_to_color_map)
|
self.tex_to_color_map = dict(**t2c, **tex_to_color_map)
|
||||||
|
|
||||||
super().__init__(tex_string, **kwargs)
|
super().__init__(tex_string, use_labelled_svg=use_labelled_svg, **kwargs)
|
||||||
|
|
||||||
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
||||||
self.scale(SCALE_FACTOR_PER_FONT_POINT * font_size)
|
self.scale(SCALE_FACTOR_PER_FONT_POINT * font_size)
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SVG_HASH_TO_MOB_MAP: dict[int, VMobject] = {}
|
SVG_HASH_TO_MOB_MAP: dict[int, list[VMobject]] = {}
|
||||||
|
|
||||||
|
|
||||||
def _convert_point_to_3d(x: float, y: float) -> np.ndarray:
|
def _convert_point_to_3d(x: float, y: float) -> np.ndarray:
|
||||||
|
|
@ -97,12 +97,13 @@ class SVGMobject(VMobject):
|
||||||
def init_svg_mobject(self) -> None:
|
def init_svg_mobject(self) -> None:
|
||||||
hash_val = hash_obj(self.hash_seed)
|
hash_val = hash_obj(self.hash_seed)
|
||||||
if hash_val in SVG_HASH_TO_MOB_MAP:
|
if hash_val in SVG_HASH_TO_MOB_MAP:
|
||||||
mob = SVG_HASH_TO_MOB_MAP[hash_val].copy()
|
submobs = [sm.copy() for sm in SVG_HASH_TO_MOB_MAP[hash_val]]
|
||||||
self.add(*mob)
|
else:
|
||||||
return
|
submobs = self.mobjects_from_file(self.get_file_path())
|
||||||
|
SVG_HASH_TO_MOB_MAP[hash_val] = [sm.copy() for sm in submobs]
|
||||||
|
|
||||||
self.generate_mobject()
|
self.add(*submobs)
|
||||||
SVG_HASH_TO_MOB_MAP[hash_val] = self.copy()
|
self.flip(RIGHT) # Flip y
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hash_seed(self) -> tuple:
|
def hash_seed(self) -> tuple:
|
||||||
|
|
@ -115,8 +116,7 @@ class SVGMobject(VMobject):
|
||||||
self.file_name
|
self.file_name
|
||||||
)
|
)
|
||||||
|
|
||||||
def generate_mobject(self) -> None:
|
def mobjects_from_file(self, file_path: str) -> list[VMobject]:
|
||||||
file_path = self.get_file_path()
|
|
||||||
element_tree = ET.parse(file_path)
|
element_tree = ET.parse(file_path)
|
||||||
new_tree = self.modify_xml_tree(element_tree)
|
new_tree = self.modify_xml_tree(element_tree)
|
||||||
|
|
||||||
|
|
@ -127,9 +127,7 @@ class SVGMobject(VMobject):
|
||||||
svg = se.SVG.parse(data_stream)
|
svg = se.SVG.parse(data_stream)
|
||||||
data_stream.close()
|
data_stream.close()
|
||||||
|
|
||||||
mobjects = self.get_mobjects_from(svg)
|
return self.mobjects_from_svg(svg)
|
||||||
self.add(*mobjects)
|
|
||||||
self.flip(RIGHT) # Flip y
|
|
||||||
|
|
||||||
def get_file_path(self) -> str:
|
def get_file_path(self) -> str:
|
||||||
if self.file_name is None:
|
if self.file_name is None:
|
||||||
|
|
@ -178,7 +176,7 @@ class SVGMobject(VMobject):
|
||||||
result[svg_key] = str(svg_default_dict[style_key])
|
result[svg_key] = str(svg_default_dict[style_key])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
|
def mobjects_from_svg(self, svg: se.SVG) -> list[VMobject]:
|
||||||
result = []
|
result = []
|
||||||
for shape in svg.elements():
|
for shape in svg.elements():
|
||||||
if isinstance(shape, (se.Group, se.Use)):
|
if isinstance(shape, (se.Group, se.Use)):
|
||||||
|
|
|
||||||
|
|
@ -416,9 +416,10 @@ class Text(MarkupText):
|
||||||
text: str,
|
text: str,
|
||||||
# For backward compatibility
|
# For backward compatibility
|
||||||
isolate: Selector = (re.compile(r"\w+", re.U), re.compile(r"\S+", re.U)),
|
isolate: Selector = (re.compile(r"\w+", re.U), re.compile(r"\S+", re.U)),
|
||||||
|
use_labelled_svg: bool = True,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
super().__init__(text, isolate=isolate, **kwargs)
|
super().__init__(text, isolate=isolate, use_labelled_svg=use_labelled_svg, **kwargs)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_command_matches(string: str) -> list[re.Match]:
|
def get_command_matches(string: str) -> list[re.Match]:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue