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:
Grant Sanderson 2022-12-20 12:22:25 -08:00
parent 1a485ddd19
commit 9c106eb873
3 changed files with 14 additions and 14 deletions

View file

@ -29,6 +29,7 @@ class MTex(StringMobject):
additional_preamble: str = "",
tex_to_color_map: dict = dict(),
t2c: dict = dict(),
use_labelled_svg: bool = True,
**kwargs
):
# Prevent from passing an empty string.
@ -40,7 +41,7 @@ class MTex(StringMobject):
self.additional_preamble = additional_preamble
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.scale(SCALE_FACTOR_PER_FONT_POINT * font_size)

View file

@ -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:
@ -97,12 +97,13 @@ class SVGMobject(VMobject):
def init_svg_mobject(self) -> None:
hash_val = hash_obj(self.hash_seed)
if hash_val in SVG_HASH_TO_MOB_MAP:
mob = SVG_HASH_TO_MOB_MAP[hash_val].copy()
self.add(*mob)
return
submobs = [sm.copy() for sm in SVG_HASH_TO_MOB_MAP[hash_val]]
else:
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()
SVG_HASH_TO_MOB_MAP[hash_val] = self.copy()
self.add(*submobs)
self.flip(RIGHT) # Flip y
@property
def hash_seed(self) -> tuple:
@ -115,8 +116,7 @@ class SVGMobject(VMobject):
self.file_name
)
def generate_mobject(self) -> None:
file_path = self.get_file_path()
def mobjects_from_file(self, file_path: str) -> list[VMobject]:
element_tree = ET.parse(file_path)
new_tree = self.modify_xml_tree(element_tree)
@ -127,9 +127,7 @@ class SVGMobject(VMobject):
svg = se.SVG.parse(data_stream)
data_stream.close()
mobjects = self.get_mobjects_from(svg)
self.add(*mobjects)
self.flip(RIGHT) # Flip y
return self.mobjects_from_svg(svg)
def get_file_path(self) -> str:
if self.file_name is None:
@ -178,7 +176,7 @@ class SVGMobject(VMobject):
result[svg_key] = str(svg_default_dict[style_key])
return result
def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
def mobjects_from_svg(self, svg: se.SVG) -> list[VMobject]:
result = []
for shape in svg.elements():
if isinstance(shape, (se.Group, se.Use)):

View file

@ -416,9 +416,10 @@ class Text(MarkupText):
text: str,
# For backward compatibility
isolate: Selector = (re.compile(r"\w+", re.U), re.compile(r"\S+", re.U)),
use_labelled_svg: bool = True,
**kwargs
):
super().__init__(text, isolate=isolate, **kwargs)
super().__init__(text, isolate=isolate, use_labelled_svg=use_labelled_svg, **kwargs)
@staticmethod
def get_command_matches(string: str) -> list[re.Match]: