diff --git a/manimlib/mobject/numbers.py b/manimlib/mobject/numbers.py index 71265ff7..651b3660 100644 --- a/manimlib/mobject/numbers.py +++ b/manimlib/mobject/numbers.py @@ -1,5 +1,7 @@ from manimlib.constants import * from manimlib.mobject.svg.tex_mobject import SingleStringTexMobject +from manimlib.mobject.svg.tex_mobject import tex_string_to_mob_map +from manimlib.mobject.svg.tex_mobject import SCALE_FACTOR_PER_FONT_POINT from manimlib.mobject.types.vectorized_mobject import VMobject @@ -85,7 +87,16 @@ class DecimalNumber(VMobject): return self.data["font_size"][0] def string_to_mob(self, tex_string): - return SingleStringTexMobject(tex_string, font_size=self.get_font_size()) + # Could just call SingleStringTexMobject, and there is + # some code repetition here by looking to the same cache, + # but it means keeps things from initializing a new object + # more than is necessary + if tex_string in tex_string_to_mob_map: + result = tex_string_to_mob_map[tex_string].copy() + result.scale(self.get_font_size() * SCALE_FACTOR_PER_FONT_POINT) + return result + else: + return SingleStringTexMobject(tex_string, font_size=self.get_font_size()) def get_formatter(self, **kwargs): """ diff --git a/manimlib/mobject/svg/tex_mobject.py b/manimlib/mobject/svg/tex_mobject.py index 15240d47..e6ff74eb 100644 --- a/manimlib/mobject/svg/tex_mobject.py +++ b/manimlib/mobject/svg/tex_mobject.py @@ -18,7 +18,7 @@ from manimlib.utils.tex_file_writing import display_during_execution SCALE_FACTOR_PER_FONT_POINT = 0.001 -tex_string_to_summobject_map = {} +tex_string_to_mob_map = {} class SingleStringTexMobject(VMobject): @@ -37,7 +37,7 @@ class SingleStringTexMobject(VMobject): super().__init__(**kwargs) assert(isinstance(tex_string, str)) self.tex_string = tex_string - if tex_string not in tex_string_to_summobject_map: + if tex_string not in tex_string_to_mob_map: full_tex = self.get_tex_file_body(tex_string) filename = tex_to_svg_file(full_tex) svg_mob = SVGMobject( @@ -48,11 +48,11 @@ class SingleStringTexMobject(VMobject): "should_remove_null_curves": True, } ) - tex_string_to_summobject_map[tex_string] = svg_mob.submobjects + tex_string_to_mob_map[tex_string] = svg_mob self.add(*( sm.copy() - for sm in tex_string_to_summobject_map[tex_string] + for sm in tex_string_to_mob_map[tex_string] )) if self.height is None: