Have DecimalMobject look to the same cache that TexMobjects do

This commit is contained in:
Grant Sanderson 2021-01-13 00:24:40 -10:00
parent 20593d8fa8
commit bf3571c103
2 changed files with 16 additions and 5 deletions

View file

@ -1,5 +1,7 @@
from manimlib.constants import * from manimlib.constants import *
from manimlib.mobject.svg.tex_mobject import SingleStringTexMobject 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 from manimlib.mobject.types.vectorized_mobject import VMobject
@ -85,7 +87,16 @@ class DecimalNumber(VMobject):
return self.data["font_size"][0] return self.data["font_size"][0]
def string_to_mob(self, tex_string): 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): def get_formatter(self, **kwargs):
""" """

View file

@ -18,7 +18,7 @@ from manimlib.utils.tex_file_writing import display_during_execution
SCALE_FACTOR_PER_FONT_POINT = 0.001 SCALE_FACTOR_PER_FONT_POINT = 0.001
tex_string_to_summobject_map = {} tex_string_to_mob_map = {}
class SingleStringTexMobject(VMobject): class SingleStringTexMobject(VMobject):
@ -37,7 +37,7 @@ class SingleStringTexMobject(VMobject):
super().__init__(**kwargs) super().__init__(**kwargs)
assert(isinstance(tex_string, str)) assert(isinstance(tex_string, str))
self.tex_string = tex_string 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) full_tex = self.get_tex_file_body(tex_string)
filename = tex_to_svg_file(full_tex) filename = tex_to_svg_file(full_tex)
svg_mob = SVGMobject( svg_mob = SVGMobject(
@ -48,11 +48,11 @@ class SingleStringTexMobject(VMobject):
"should_remove_null_curves": True, "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(*( self.add(*(
sm.copy() 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: if self.height is None: