Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Some bugs of code_mobject.py and Text_mobject.py #1071

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
df6969e
Update text_mobject.py
NavpreetDevpuri May 14, 2020
4c00d99
Update code_mobject.py
NavpreetDevpuri May 14, 2020
2b7572b
Update text_mobject.py
NavpreetDevpuri May 14, 2020
58521da
Update text_mobject.py
NavpreetDevpuri May 14, 2020
7ec79df
Update text_mobject.py
NavpreetDevpuri May 15, 2020
7e5e7fe
Update text_mobject.py
NavpreetDevpuri May 15, 2020
2d004f9
Update code_mobject.py
NavpreetDevpuri May 15, 2020
a133d61
Update code_mobject.py
NavpreetDevpuri May 15, 2020
cd65895
Update text_mobject.py
NavpreetDevpuri May 15, 2020
ef8a681
Update text_mobject.py
NavpreetDevpuri May 15, 2020
7847b78
Update text_mobject.py
NavpreetDevpuri May 15, 2020
68719d5
Update text_mobject.py
NavpreetDevpuri May 15, 2020
ee0d8d9
Update code_mobject.py
NavpreetDevpuri May 15, 2020
16229e5
Update text_mobject.py
NavpreetDevpuri May 15, 2020
b4381d2
Update text_mobject.py
NavpreetDevpuri May 15, 2020
c7e922d
Update text_mobject.py
NavpreetDevpuri May 15, 2020
b268b6a
Fixed find_indexes, t2c and t2g
NavpreetDevpuri May 16, 2020
092e64f
Fixed line_spacing
NavpreetDevpuri May 18, 2020
4838bd7
commented code_languages_list and code_styles_list
NavpreetDevpuri May 26, 2020
fafd82a
Auto detect language name
NavpreetDevpuri May 26, 2020
0b3ed98
removed code_languages_list and code_styles_list
NavpreetDevpuri May 26, 2020
023910f
Corrected indexes
NavpreetDevpuri May 26, 2020
7d67d78
Update code_mobject.py
NavpreetDevpuri May 26, 2020
d74f61d
Update code_mobject.py
NavpreetDevpuri May 26, 2020
fc2d9e4
Fixed remove_invisible_chars for Code
NavpreetDevpuri May 26, 2020
6b76435
Fixed Code[2] to self.code
NavpreetDevpuri May 26, 2020
30c7a5d
added pygments
NavpreetDevpuri Jun 16, 2020
ef54471
Update code_mobject.py
NavpreetDevpuri Jun 21, 2020
84e37dc
added documention
NavpreetDevpuri Jun 21, 2020
b0b19f8
improved documention
NavpreetDevpuri Jun 21, 2020
183990e
Update text_mobject.py
NavpreetDevpuri Jun 23, 2020
86af268
Update code_mobject.py
NavpreetDevpuri Jun 23, 2020
38ca38e
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
b346c1d
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
47d8293
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
5b3bc9a
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
f61a35f
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
81b864b
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
4320015
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
681ba59
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
2bb77b9
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
dbe2522
Update manimlib/mobject/svg/code_mobject.py
NavpreetDevpuri Jun 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions manimlib/mobject/svg/code_mobject.py
Expand Up @@ -33,14 +33,16 @@
class Code(VGroup):
CONFIG = {
"tab_width": 3,
"line_spacing": 0.1,
"line_spacing": -1,
"scale_factor": 0.5,
"run_time": 1,
"font": 'Monospac821 BT',
'stroke_width': 0,
'margin': 0.3,
'indentation_char': " ",
"background": "rectangle", # or window
"background_stroke_width": 1,
"background_stroke_color": WHITE,
"corner_radius": 0.2,
'insert_line_no': True,
'line_no_from': 1,
Expand Down Expand Up @@ -73,7 +75,8 @@ def __init__(self, file_name=None, **kwargs):
self.background_mobject = SurroundingRectangle(forground, buff=self.margin,
color=self.background_color,
fill_color=self.background_color,
stroke_width=0,
stroke_width=self.background_stroke_width,
stroke_color=self.background_stroke_color,
fill_opacity=1, )
self.background_mobject.round_corners(self.corner_radius)
else:
Expand All @@ -86,7 +89,8 @@ def __init__(self, file_name=None, **kwargs):
width = forground.get_width() + 0.1 * 3 + 2 * self.margin

rrect = RoundedRectangle(corner_radius=self.corner_radius, height=height, width=width,
stroke_width=0,
stroke_width=self.background_stroke_width,
stroke_color=self.background_stroke_color,
color=self.background_color, fill_opacity=1)
red_button = Dot(radius=0.1, stroke_width=0, color='#ff5f56')
red_button.shift(LEFT * 0.1 * 3)
Expand Down Expand Up @@ -140,7 +144,10 @@ def gen_line_numbers(self):
number = str(self.line_no_from + line_no)
line_numbers_array.append(number)
line_numbers = Paragraph(*[i for i in line_numbers_array], line_spacing=self.line_spacing,
alignment="right", font=self.font, stroke_width=self.stroke_width).scale(self.scale_factor)
alignment="right", font=self.font, stroke_width=self.stroke_width).scale(
self.scale_factor)
for i in line_numbers:
i.set_color(self.default_color)
return line_numbers

def gen_colored_lines(self):
Expand All @@ -151,7 +158,7 @@ def gen_colored_lines(self):
line_str = line_str + self.code_json[line_no][word_index][0]
lines_text.append(self.tab_spaces[line_no] * "\t" + line_str)
code = Paragraph(*[i for i in lines_text], line_spacing=self.line_spacing, tab_width=self.tab_width,
alignment="left", font=self.font, stroke_width=self.stroke_width).scale(self.scale_factor)
font=self.font, stroke_width=self.stroke_width).scale(self.scale_factor)
for line_no in range(code.__len__()):
line = code[line_no]
line_char_index = self.tab_spaces[line_no]
Expand Down Expand Up @@ -181,6 +188,7 @@ def gen_code_json(self):
self.default_color = "#ffffff"
else:
self.default_color = "#000000"
# print(self.default_color,self.background_color)
for i in range(3, -1, -1):
self.html_string = self.html_string.replace("</" + " " * i, "</")
for i in range(10, -1, -1):
Expand Down
187 changes: 54 additions & 133 deletions manimlib/mobject/svg/text_mobject.py
Expand Up @@ -11,7 +11,6 @@
from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.utils.config_ops import digest_config


TEXT_MOB_SCALE_FACTOR = 0.05


Expand All @@ -38,7 +37,7 @@ class Text(SVGMobject):
# Text
'font': '',
'gradient': None,
'lsh': -1,
'line_spacing': -1,
'size': 1,
'slant': NORMAL,
'weight': NORMAL,
Expand All @@ -55,14 +54,12 @@ def __init__(self, text, **config):
digest_config(self, config)
text_without_tabs = text
if text.find('\t') != -1:
text_without_tabs = text.replace('\t', ' '*self.tab_width)
text_without_tabs = text.replace('\t', ' ' * self.tab_width)
self.text = text_without_tabs
self.lsh = self.size if self.lsh == -1 else self.lsh

self.line_spacing = self.size + self.size * 0.2 if self.line_spacing == -1 else self.line_spacing
file_name = self.text2svg()
self.remove_last_M(file_name)
SVGMobject.__init__(self, file_name, **config)
self.apply_front_and_end_spaces()
self.text = text
self.apply_space_chars()

Expand All @@ -75,7 +72,7 @@ def __init__(self, text, **config):
each.clear_points()
for index, point in enumerate(points):
each.append_points([point])
if index != len(points) - 1 and (index + 1) % nppc == 0 and any(point != points[index+1]):
if index != len(points) - 1 and (index + 1) % nppc == 0 and any(point != points[index + 1]):
each.add_line_to(last)
last = points[index + 1]
each.add_line_to(last)
Expand All @@ -91,75 +88,14 @@ def __init__(self, text, **config):
if self.height is None and self.width is None:
self.scale(TEXT_MOB_SCALE_FACTOR)

def get_space_width(self):
size = self.size * 10

dir_name = consts.TEXT_DIR
file_name = os.path.join(dir_name, "space") + '.svg'

surface = cairo.SVGSurface(file_name, 600, 400)
context = cairo.Context(surface)
context.set_font_size(size)
context.move_to(START_X, START_Y)
context.select_font_face(self.font, self.str2slant(self.slant), self.str2weight(self.weight))
context.move_to(START_X, START_Y)
context.show_text("_")
surface.finish()
svg_with_space = SVGMobject(file_name, height=self.height,
width=self.width,
stroke_width=self.stroke_width,
should_center=self.should_center,
unpack_groups=self.unpack_groups, )
space_width = svg_with_space.get_width()
return space_width

def apply_front_and_end_spaces(self):
space_width = self.get_space_width()
max_height = self.get_height()
front_spaces_count = 0
i = -1
for i in range(self.text.__len__()):
if self.text[i] == " ":
front_spaces_count += 1
continue
else:
break
first_visible_char_index = i
if first_visible_char_index != 0:
space = Rectangle(width=space_width * front_spaces_count, height=max_height, fill_opacity=0,
stroke_opacity=0,
stroke_width=0)
text_width = self.get_width()
space.move_to(np.array([-text_width / 2, max_height / 2, 0]))
self.next_to(space, direction=RIGHT, buff=0)
self.submobjects.insert(0, space)

i = -1
last_spaces_count = 0
for i in range(self.text.__len__() - 1, -1, -1):
if self.text[i] == " ":
last_spaces_count += 1
continue
else:
break
last_visible_char_index = i
if last_visible_char_index != self.text.__len__() - 1:
space = Rectangle(width=space_width * last_spaces_count, height=max_height, fill_opacity=0,
stroke_opacity=0,
stroke_width=0)
text_width = self.get_width()
space.move_to(np.array([-text_width / 2, max_height / 2, 0]))
self.next_to(space, direction=LEFT, buff=0)
self.submobjects.append(space)
self.move_to(np.array([0,0,0]))

def apply_space_chars(self):
char_index = 0
while char_index < self.text.__len__() - 1:
char_index += 1
for char_index in range(self.text.__len__()):
if self.text[char_index] == " " or self.text[char_index] == "\t" or self.text[char_index] == "\n":
space = Dot(fill_opacity=0, stroke_opacity=0)
space.move_to(self.submobjects[char_index - 1].get_center())
space = Dot(redius=0, fill_opacity=0, stroke_opacity=0)
if char_index == 0:
space.move_to(self.submobjects[char_index - 1].get_center())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

focus on here.
You put all non-display characters in the position of the previous character.
But if char_index==0, the previous character is the last display character in the entire Text.

It's better to put them in the position of the first display character instead of the last one.

else:
space.move_to(self.submobjects[char_index - 1].get_center())
self.submobjects.insert(char_index, space)

def remove_last_M(self, file_name):
Expand Down Expand Up @@ -187,8 +123,6 @@ def find_indexes(self, word):

def full2short(self, config):
for kwargs in [config, self.CONFIG]:
if kwargs.__contains__('line_spacing_height'):
kwargs['lsh'] = kwargs.pop('line_spacing_height')
if kwargs.__contains__('text2color'):
kwargs['t2c'] = kwargs.pop('text2color')
if kwargs.__contains__('text2font'):
Expand Down Expand Up @@ -229,8 +163,8 @@ def str2weight(self, string):
def text2hash(self):
settings = self.font + self.slant + self.weight
settings += str(self.t2f) + str(self.t2s) + str(self.t2w)
settings += str(self.lsh) + str(self.size)
id_str = self.text+settings
settings += str(self.line_spacing) + str(self.size)
id_str = self.text + settings
hasher = hashlib.sha256()
hasher.update(id_str.encode())
return hasher.hexdigest()[:16]
Expand Down Expand Up @@ -284,15 +218,15 @@ def text2settings(self):
def text2svg(self):
# anti-aliasing
size = self.size * 10
lsh = self.lsh * 10
line_spacing = self.line_spacing * 10

if self.font == '':
if NOT_SETTING_FONT_MSG != '':
print(NOT_SETTING_FONT_MSG)

dir_name = consts.TEXT_DIR
hash_name = self.text2hash()
file_name = os.path.join(dir_name, hash_name)+'.svg'
file_name = os.path.join(dir_name, hash_name) + '.svg'
if os.path.exists(file_name):
return file_name

Expand All @@ -314,86 +248,73 @@ def text2svg(self):
if setting.line_num != last_line_num:
offset_x = 0
last_line_num = setting.line_num
context.move_to(START_X + offset_x, START_Y + lsh*setting.line_num)
context.move_to(START_X + offset_x, START_Y + line_spacing * setting.line_num)
context.show_text(text)
offset_x += context.text_extents(text)[4]

return file_name

class TextWithFixHeight(Text):
def __init__(self, text, **kwargs):
Text.__init__(self, text, **kwargs)
max_height = Text("(gyt{[/QW", **kwargs).get_height()
rectangle = Rectangle(width=0, height=max_height, fill_opacity=0,
stroke_opacity=0,
stroke_width=0)
self.submobjects.append(rectangle)

class Paragraph(VGroup):
CONFIG = {
"line_spacing": 0.1,
"alignment": "center",
"line_spacing": -1,
"alignment": None,
}

def __init__(self, *text, **config):
Container.__init__(self, **config)
self.lines_list = list(text)

lines_str = "\n".join(list(text))
lines_str_list = lines_str.split("\n")
lines_text = Text(lines_str, **config)
lines_text_list = VGroup()
char_index_counter = 0
for line_index in range(lines_str_list.__len__()):
lines_text_list.add(
lines_text[char_index_counter:char_index_counter + lines_str_list[line_index].__len__() + 1])
char_index_counter += lines_str_list[line_index].__len__() + 1

self.lines = []
self.lines.append([])
for line_no in range(self.lines_list.__len__()):
if "\n" in self.lines_list[line_no]:
self.lines_list[line_no:line_no + 1] = self.lines_list[line_no].split("\n")
for line_no in range(self.lines_list.__len__()):
self.lines[0].append(TextWithFixHeight(self.lines_list[line_no], **config))
self.char_height = TextWithFixHeight("(", **config).get_height()
for line_no in range(lines_text_list.__len__()):
self.lines[0].append(lines_text_list[line_no])
self.lines_initial_positions = []
for line_no in range(self.lines[0].__len__()):
self.lines_initial_positions.append(self.lines[0][line_no].get_center())
self.lines.append([])
self.lines[1].extend([self.alignment for _ in range(self.lines_list.__len__())])
self.lines[0][0].move_to(np.array([0, 0, 0]))
self.align_lines()
self.lines[1].extend([self.alignment for _ in range(lines_text_list.__len__())])
VGroup.__init__(self, *[self.lines[0][i] for i in range(self.lines[0].__len__())], **config)
self.move_to(np.array([0, 0, 0]))
if self.alignment:
self.set_all_lines_alignments(self.alignment)

def set_all_lines_alignment(self, alignment):
self.lines[1] = [alignment for _ in range(self.lines_list.__len__())]
def set_all_lines_alignments(self, alignment):
for line_no in range(0, self.lines[0].__len__()):
self.change_alignment_for_a_line(alignment, line_no)
return self

def set_alignment(self, alignment, line_no):
def set_line_alignment(self, alignment, line_no):
self.change_alignment_for_a_line(alignment, line_no)
return self

def set_all_lines_to_initial_positions(self):
self.lines[1] = [None for _ in range(self.lines[0].__len__())]
for line_no in range(0, self.lines[0].__len__()):
self[line_no].move_to(self.get_center() + self.lines_initial_positions[line_no])
return self

def set_line_to_initial_position(self, line_no):
self.lines[1][line_no] = None
self[line_no].move_to(self.get_center() + self.lines_initial_positions[line_no])
return self

def change_alignment_for_a_line(self, alignment, line_no):
self.lines[1][line_no] = alignment
if self.lines[1][line_no] == "center":
self[line_no].move_to(self.get_top() +
np.array([0, -self.char_height / 2, 0]) +
np.array([0, - line_no * (self.char_height + self.line_spacing), 0]))
self[line_no].move_to(np.array([self.get_center()[0], self[line_no].get_center()[1], 0]))
elif self.lines[1][line_no] == "right":
self[line_no].move_to(self.get_top() +
np.array([0, -self.char_height / 2, 0]) +
np.array([self.get_width() / 2 - self.lines[0][line_no].get_width() / 2,
- line_no * (self.char_height + self.line_spacing), 0])
)
self[line_no].move_to(
np.array([self.get_right()[0] - self[line_no].get_width() / 2, self[line_no].get_center()[1], 0]))
elif self.lines[1][line_no] == "left":
self[line_no].move_to(self.get_top() +
np.array([0, -self.char_height / 2, 0]) +
np.array([- self.get_width() / 2 + self.lines[0][line_no].get_width() / 2,
- line_no * (self.char_height + self.line_spacing), 0])
)

def align_lines(self):
for line_no in range(0, self.lines[0].__len__()):
if self.lines[1][line_no] == "center":
self.lines[0][line_no].move_to(
np.array([0, 0, 0]) + np.array([0, - line_no * (self.char_height + self.line_spacing), 0]))
elif self.lines[1][line_no] == "left":
self.lines[0][line_no].move_to(np.array([0, 0, 0]) +
np.array([self.lines[0][line_no].get_width() / 2,
- line_no * (self.char_height + self.line_spacing), 0])
)
elif self.lines[1][line_no] == "right":
self.lines[0][line_no].move_to(np.array([0, 0, 0]) +
np.array([- self.lines[0][line_no].get_width() / 2,
- line_no * (self.char_height + self.line_spacing), 0])
)
self[line_no].move_to(
np.array([self.get_left()[0] + self[line_no].get_width() / 2, self[line_no].get_center()[1], 0]))