A community-maintained Python framework for creating mathematical animations.
Fix space characters problem of Text (#1035)
XiaoYoung522a5b3c5f18
diff --git a/manimlib/mobject/svg/svg_mobject.py b/manimlib/mobject/svg/svg_mobject.pyindex 3759182f8..a183588f5 100644--- a/manimlib/mobject/svg/svg_mobject.py+++ b/manimlib/mobject/svg/svg_mobject.py@@ -87,9 +87,9 @@ class SVGMobject(VMobject):for child in element.childNodes])elif element.tagName == 'path':- result.append(self.path_string_to_mobject(- element.getAttribute('d')- ))+ temp = element.getAttribute('d')+ if temp != '':+ result.append(self.path_string_to_mobject(temp))elif element.tagName == 'use':result += self.use_to_mobjects(element)elif element.tagName == 'rect':diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.pyindex 456e7e469..5b260c10a 100644--- a/manimlib/mobject/svg/text_mobject.py+++ b/manimlib/mobject/svg/text_mobject.py@@ -5,6 +5,7 @@ import hashlibimport cairoimport manimlib.constants as constsfrom manimlib.constants import *+from manimlib.mobject.geometry import Dotfrom manimlib.mobject.svg.svg_mobject import SVGMobjectfrom manimlib.utils.config_ops import digest_config@@ -44,17 +45,21 @@ class Text(SVGMobject):'t2g': {},'t2s': {},'t2w': {},+ 'tab_width': 4,}def __init__(self, text, **config):- self.text = textself.full2short(config)digest_config(self, config)+ if text.find('\t') != -1:+ text = text.replace('\t', ' '*self.tab_width)+ self.text = textself.lsh = self.size if self.lsh == -1 else self.lshfile_name = self.text2svg()self.remove_last_M(file_name)SVGMobject.__init__(self, file_name, **config)+ self.apply_space_chars()nppc = self.n_points_per_cubic_curvefor each in self:@@ -78,12 +83,28 @@ class Text(SVGMobject):self.set_color_by_t2g()# anti-aliasing- self.scale(TEXT_MOB_SCALE_FACTOR)+ if self.height is None and self.width is None:+ self.scale(TEXT_MOB_SCALE_FACTOR)++ def apply_space_chars(self):+ indexes = self.find_indexes(' ') + self.find_indexes('\n')+ indexes = sorted(indexes, key=lambda i: i[0])+ if len(self.text) == len(indexes):+ space = Dot(fill_opacity=0, stroke_opacity=0)+ self.submobjects = [space.copy() for _ in range(len(indexes))]+ return+ for start, _ in indexes:+ space = Dot(fill_opacity=0, stroke_opacity=0)+ if start == 0:+ space.move_to(self.submobjects[0].get_center())+ else:+ space.move_to(self.submobjects[start-1].get_center())+ self.submobjects.insert(start, space)def remove_last_M(self, file_name):with open(file_name, 'r') as fpr:content = fpr.read()- content = re.sub(r'Z M [^[A-Za-z]*? "\/>', 'Z "/>', content)+ content = re.sub(r'Z M [^A-Za-z]*? "\/>', 'Z "/>', content)with open(file_name, 'w') as fpw:fpw.write(content)