From 97c0f4857b322934c5752b8450b85ffd2626c50c Mon Sep 17 00:00:00 2001 From: Bill Xi <86190295+TurkeyBilly@users.noreply.github.com> Date: Tue, 15 Feb 2022 09:35:10 +0800 Subject: [PATCH 1/4] Update numbers.py Added config passing for text --- manimlib/mobject/numbers.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/manimlib/mobject/numbers.py b/manimlib/mobject/numbers.py index 7a8d05ba..b098f87c 100644 --- a/manimlib/mobject/numbers.py +++ b/manimlib/mobject/numbers.py @@ -20,6 +20,7 @@ class DecimalNumber(VMobject): "include_background_rectangle": False, "edge_to_fix": LEFT, "font_size": 48, + "text_config": {} # Do not pass in font_size here } def __init__(self, number=0, **kwargs): @@ -30,13 +31,13 @@ class DecimalNumber(VMobject): def set_submobjects_from_number(self, number): self.number = number self.set_submobjects([]) - + string_to_mob_ = lambda s: self.string_to_mob(s, **self.text_config) num_string = self.get_num_string(number) - self.add(*map(self.string_to_mob, num_string)) + self.add(*map(string_to_mob_, num_string)) # Add non-numerical bits if self.show_ellipsis: - dots = self.string_to_mob("...") + dots = string_to_mob_("...") dots.arrange(RIGHT, buff=2 * dots[0].get_width()) self.add(dots) if self.unit is not None: @@ -85,10 +86,15 @@ class DecimalNumber(VMobject): def get_font_size(self): return self.data["font_size"][0] - def string_to_mob(self, string, mob_class=Text): - if string not in string_to_mob_map: - string_to_mob_map[string] = mob_class(string, font_size=1) - mob = string_to_mob_map[string].copy() + def string_to_mob(self, string, mob_class=Text, **kwargs): + if not kwargs: + if string not in string_to_mob_map: + string_to_mob_map[string] = mob_class(string, font_size=1, **kwargs) + mob = string_to_mob_map[string].copy() + else: + if (string, str(kwargs)) not in string_to_mob_map: + string_to_mob_map[(string, str(kwargs))] = mob_class(string, font_size=1, **kwargs) + mob = string_to_mob_map[(string, str(kwargs))].copy() mob.scale(self.get_font_size()) return mob From 9d04e287d7590068feabc9179b25325cb72b0dc5 Mon Sep 17 00:00:00 2001 From: Bill Xi <86190295+TurkeyBilly@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:20:06 +0800 Subject: [PATCH 2/4] Removed init_colors --- manimlib/mobject/numbers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/manimlib/mobject/numbers.py b/manimlib/mobject/numbers.py index b098f87c..84bfa3e4 100644 --- a/manimlib/mobject/numbers.py +++ b/manimlib/mobject/numbers.py @@ -26,7 +26,6 @@ class DecimalNumber(VMobject): def __init__(self, number=0, **kwargs): super().__init__(**kwargs) self.set_submobjects_from_number(number) - self.init_colors() def set_submobjects_from_number(self, number): self.number = number From aef02bfcf99c5bc84271463f2c589b21e7fd6d7b Mon Sep 17 00:00:00 2001 From: Bill Xi <86190295+TurkeyBilly@users.noreply.github.com> Date: Tue, 15 Feb 2022 11:45:17 +0800 Subject: [PATCH 3/4] changed hashing --- manimlib/mobject/numbers.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/manimlib/mobject/numbers.py b/manimlib/mobject/numbers.py index 84bfa3e4..7481e0d7 100644 --- a/manimlib/mobject/numbers.py +++ b/manimlib/mobject/numbers.py @@ -86,14 +86,20 @@ class DecimalNumber(VMobject): return self.data["font_size"][0] def string_to_mob(self, string, mob_class=Text, **kwargs): - if not kwargs: - if string not in string_to_mob_map: - string_to_mob_map[string] = mob_class(string, font_size=1, **kwargs) - mob = string_to_mob_map[string].copy() - else: - if (string, str(kwargs)) not in string_to_mob_map: - string_to_mob_map[(string, str(kwargs))] = mob_class(string, font_size=1, **kwargs) - mob = string_to_mob_map[(string, str(kwargs))].copy() + def make_hash(o): + if isinstance(o, (set, tuple, list)): + return tuple([make_hash(e) for e in o]) + elif not isinstance(o, dict): + return hash(o) + from copy import deepcopy as _deepcopy + new_o = _deepcopy(o) + for k, v in new_o.items(): + new_o[k] = make_hash(v) + return hash(tuple(frozenset(sorted(new_o.items())))) + + if (string, make_hash(kwargs)) not in string_to_mob_map: + string_to_mob_map[(string, make_hash(kwargs))] = mob_class(string, font_size=1, **kwargs) + mob = string_to_mob_map[(string, make_hash(kwargs))].copy() mob.scale(self.get_font_size()) return mob From 0bb9216c14cdfa0fc6c548199aeefa51879a8bab Mon Sep 17 00:00:00 2001 From: Bill Xi <86190295+TurkeyBilly@users.noreply.github.com> Date: Tue, 15 Feb 2022 21:50:14 +0800 Subject: [PATCH 4/4] Update hash_obj method --- manimlib/mobject/numbers.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/manimlib/mobject/numbers.py b/manimlib/mobject/numbers.py index 7481e0d7..a56353ae 100644 --- a/manimlib/mobject/numbers.py +++ b/manimlib/mobject/numbers.py @@ -2,6 +2,7 @@ from manimlib.constants import * from manimlib.mobject.svg.tex_mobject import SingleStringTex from manimlib.mobject.svg.text_mobject import Text from manimlib.mobject.types.vectorized_mobject import VMobject +from manimlib.utils.iterables import hash_obj string_to_mob_map = {} @@ -86,20 +87,9 @@ class DecimalNumber(VMobject): return self.data["font_size"][0] def string_to_mob(self, string, mob_class=Text, **kwargs): - def make_hash(o): - if isinstance(o, (set, tuple, list)): - return tuple([make_hash(e) for e in o]) - elif not isinstance(o, dict): - return hash(o) - from copy import deepcopy as _deepcopy - new_o = _deepcopy(o) - for k, v in new_o.items(): - new_o[k] = make_hash(v) - return hash(tuple(frozenset(sorted(new_o.items())))) - - if (string, make_hash(kwargs)) not in string_to_mob_map: - string_to_mob_map[(string, make_hash(kwargs))] = mob_class(string, font_size=1, **kwargs) - mob = string_to_mob_map[(string, make_hash(kwargs))].copy() + if (string, hash_obj(kwargs)) not in string_to_mob_map: + string_to_mob_map[(string, hash_obj(kwargs))] = mob_class(string, font_size=1, **kwargs) + mob = string_to_mob_map[(string, hash_obj(kwargs))].copy() mob.scale(self.get_font_size()) return mob