Skip to content

Commit

Permalink
Fix space characters problem of Text
Browse files Browse the repository at this point in the history
  • Loading branch information
xy-23 committed May 6, 2020
1 parent 67ef0ff commit 936c33f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions manimlib/mobject/svg/svg_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def get_mobjects_from(self, element):
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':
Expand Down
23 changes: 22 additions & 1 deletion manimlib/mobject/svg/text_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cairo
import manimlib.constants as consts
from manimlib.constants import *
from manimlib.mobject.geometry import Dot
from manimlib.mobject.svg.svg_mobject import SVGMobject
from manimlib.utils.config_ops import digest_config

Expand Down Expand Up @@ -44,17 +45,22 @@ class Text(SVGMobject):
't2g': {},
't2s': {},
't2w': {},
'tab_width': 4,
}

def __init__(self, text, **config):
self.text = text
self.full2short(config)
digest_config(self, config)
if text.find('\t') != -1:
text = text.replace('\t', ' '*self.tab_width)
self.text = text
self.lsh = self.size if self.lsh == -1 else self.lsh

file_name = self.text2svg()
self.remove_last_M(file_name)
SVGMobject.__init__(self, file_name, **config)
self.apply_space_chars()
self.move_into_position()

nppc = self.n_points_per_cubic_curve
for each in self:
Expand All @@ -80,6 +86,21 @@ def __init__(self, text, **config):
# anti-aliasing
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()
Expand Down

0 comments on commit 936c33f

Please sign in to comment.