From 4aeccd776909f25e026131e08d1790744cc885bd Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Thu, 17 Feb 2022 19:03:45 +0800 Subject: [PATCH 01/20] Specify UTF-8 encoding for tex files --- manimlib/utils/tex_file_writing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manimlib/utils/tex_file_writing.py b/manimlib/utils/tex_file_writing.py index cfc157f4..1ca7ed39 100644 --- a/manimlib/utils/tex_file_writing.py +++ b/manimlib/utils/tex_file_writing.py @@ -32,7 +32,7 @@ def get_tex_config(): get_manim_dir(), "manimlib", "tex_templates", SAVED_TEX_CONFIG["template_file"], ) - with open(template_filename, "r") as file: + with open(template_filename, "r", encoding="utf-8") as file: SAVED_TEX_CONFIG["tex_body"] = file.read() return SAVED_TEX_CONFIG @@ -88,7 +88,7 @@ def tex_to_dvi(tex_file): if exit_code != 0: log_file = tex_file.replace(".tex", ".log") log.error("LaTeX Error! Not a worry, it happens to the best of us.") - with open(log_file, "r") as file: + with open(log_file, "r", encoding="utf-8") as file: for line in file.readlines(): if line.startswith("!"): log.debug(f"The error could be: `{line[2:-1]}`") From e879da32d59278508136a3eb7408f5ec690cc866 Mon Sep 17 00:00:00 2001 From: YishiMichael <50232075+YishiMichael@users.noreply.github.com> Date: Thu, 17 Feb 2022 19:09:55 +0800 Subject: [PATCH 02/20] Specify UTF-8 encoding for tex files (#1748) --- manimlib/utils/tex_file_writing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manimlib/utils/tex_file_writing.py b/manimlib/utils/tex_file_writing.py index cfc157f4..1ca7ed39 100644 --- a/manimlib/utils/tex_file_writing.py +++ b/manimlib/utils/tex_file_writing.py @@ -32,7 +32,7 @@ def get_tex_config(): get_manim_dir(), "manimlib", "tex_templates", SAVED_TEX_CONFIG["template_file"], ) - with open(template_filename, "r") as file: + with open(template_filename, "r", encoding="utf-8") as file: SAVED_TEX_CONFIG["tex_body"] = file.read() return SAVED_TEX_CONFIG @@ -88,7 +88,7 @@ def tex_to_dvi(tex_file): if exit_code != 0: log_file = tex_file.replace(".tex", ".log") log.error("LaTeX Error! Not a worry, it happens to the best of us.") - with open(log_file, "r") as file: + with open(log_file, "r", encoding="utf-8") as file: for line in file.readlines(): if line.startswith("!"): log.debug(f"The error could be: `{line[2:-1]}`") From fa8962e02442320b8f69fa62ccead7166910badf Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Sun, 20 Feb 2022 23:35:48 +0800 Subject: [PATCH 03/20] Refactor Text --- manimlib/mobject/svg/svg_mobject.py | 49 ++++-- manimlib/mobject/svg/text_mobject.py | 253 ++++++++++++++------------- 2 files changed, 166 insertions(+), 136 deletions(-) diff --git a/manimlib/mobject/svg/svg_mobject.py b/manimlib/mobject/svg/svg_mobject.py index 3b8bb6ad..dd47d525 100644 --- a/manimlib/mobject/svg/svg_mobject.py +++ b/manimlib/mobject/svg/svg_mobject.py @@ -176,7 +176,7 @@ class SVGMobject(VMobject): se.Ellipse: self.ellipse_to_mobject, se.Polygon: self.polygon_to_mobject, se.Polyline: self.polyline_to_mobject, - # se.Text: self.text_to_mobject, # TODO + se.Text: self.text_to_mobject, # TODO } for shape_class, func in shape_class_to_func_map.items(): if isinstance(shape, shape_class): @@ -259,7 +259,11 @@ class SVGMobject(VMobject): return Polyline(*points) def text_to_mobject(self, text): - pass + from manimlib.mobject.svg.text_mobject import Text + mob = Text(text.text, font=text.font_family, font_size=text.font_size) + mob.scale(1 / 0.0076) # TODO + mob.flip(RIGHT) + return mob def move_into_position(self): if self.should_center: @@ -278,10 +282,21 @@ class VMobjectFromSVGPath(VMobject): } def __init__(self, path_obj, **kwargs): + self.path_obj = self.modify_path(path_obj) + super().__init__(**kwargs) + + @staticmethod + def modify_path(path_obj): # Get rid of arcs path_obj.approximate_arcs_with_quads() - self.path_obj = path_obj - super().__init__(**kwargs) + # Remove trailing "Z M" command + if len(path_obj) >= 2: + if all([ + isinstance(path_obj[-2], se.Close), + isinstance(path_obj[-1], se.Move), + ]): + del path_obj[len(path_obj._segments) - 1] + return path_obj def init_points(self): # After a given svg_path has been converted into points, the result @@ -297,17 +312,18 @@ class VMobjectFromSVGPath(VMobject): self.set_points(np.load(points_filepath)) self.triangulation = np.load(tris_filepath) self.needs_new_triangulation = False - else: - self.handle_commands() - if self.should_subdivide_sharp_curves: - # For a healthy triangulation later - self.subdivide_sharp_curves() - if self.should_remove_null_curves: - # Get rid of any null curves - self.set_points(self.get_points_without_null_curves()) - # Save to a file for future use - np.save(points_filepath, self.get_points()) - np.save(tris_filepath, self.get_triangulation()) + return + + self.handle_commands() + if self.should_subdivide_sharp_curves: + # For a healthy triangulation later + self.subdivide_sharp_curves() + if self.should_remove_null_curves: + # Get rid of any null curves + self.set_points(self.get_points_without_null_curves()) + # Save to a file for future use + np.save(points_filepath, self.get_points()) + np.save(tris_filepath, self.get_triangulation()) def handle_commands(self): segment_class_to_func_map = { @@ -325,3 +341,6 @@ class VMobjectFromSVGPath(VMobject): for attr_name in attr_names ] func(*points) + #print(self.get_num_points()) + #self.close_path() + #print(self.get_num_points()) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 6052648d..753260a6 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -17,6 +17,7 @@ from manimlib.logger import log from manimlib.constants import * from manimlib.mobject.geometry import Dot from manimlib.mobject.svg.svg_mobject import SVGMobject +from manimlib.mobject.types.vectorized_mobject import VMobject from manimlib.mobject.types.vectorized_mobject import VGroup from manimlib.utils.config_ops import digest_config from manimlib.utils.customization import get_customization @@ -34,12 +35,11 @@ class Text(SVGMobject): "height": None, "stroke_width": 0, # Text - "font": '', - "gradient": None, - "lsh": -1, + "lsh": None, "size": None, "font_size": 48, - "tab_width": 4, + "font": None, + "gradient": None, "slant": NORMAL, "weight": NORMAL, "t2c": {}, @@ -53,56 +53,81 @@ class Text(SVGMobject): def __init__(self, text, **kwargs): self.full2short(kwargs) digest_config(self, kwargs) + self.text = text if self.size: log.warning( "`self.size` has been deprecated and will " "be removed in future.", ) self.font_size = self.size - if self.lsh == -1: - self.lsh = self.font_size + self.font_size * DEFAULT_LINE_SPACING_SCALE - else: - self.lsh = self.font_size + self.font_size * self.lsh - text_without_tabs = text - if text.find('\t') != -1: - text_without_tabs = text.replace('\t', ' ' * self.tab_width) - self.text = text_without_tabs + self.lsh = self.font_size * (1 + ( + self.lsh or DEFAULT_LINE_SPACING_SCALE + )) + if self.font is None: + self.font = get_customization()["style"]["font"] file_name = self.text2svg() - PangoUtils.remove_last_M(file_name) - self.remove_empty_path(file_name) - SVGMobject.__init__(self, file_name, **kwargs) - self.text = text - if self.disable_ligatures: - self.apply_space_chars() - if self.t2c: - self.set_color_by_t2c() + #self.remove_last_M(file_name) + #self.remove_empty_path(file_name) + super().__init__(file_name, **kwargs) + self.remove_empty_submobs() # TODO: move into generate_mobject + self.apply_space_chars() # TODO: move into generate_mobject + + self.set_color_by_t2c(self.t2c) if self.gradient: self.set_color_by_gradient(*self.gradient) - if self.t2g: - self.set_color_by_t2g() + self.set_color_by_t2g(self.t2g) # anti-aliasing if self.height is None: self.scale(TEXT_MOB_SCALE_FACTOR) - def remove_empty_path(self, file_name): - with open(file_name, 'r') as fpr: - content = fpr.read() - content = re.sub(r'', '', content) - with open(file_name, 'w') as fpw: - fpw.write(content) + #def remove_empty_path(self, file_name): + # with open(file_name, 'r') as fpr: + # content = fpr.read() + # content = re.sub(r'', '', content) + # with open(file_name, 'w') as fpw: + # fpw.write(content) + + #def remove_last_M(self, file_name): + # """Remove element from the SVG file in order to allow comparison.""" + # with open(file_name, "r") as fpr: + # content = fpr.read() + # content = re.sub(r'Z M [^A-Za-z]*? "\/>', 'Z "/>', content) + # with open(file_name, "w") as fpw: + # fpw.write(content) + + def remove_empty_submobs(self): # TODO + self.set_submobjects(list(filter( + lambda submob: submob.has_points(), + self.submobjects + ))) def apply_space_chars(self): + # Align every character with a submobject submobs = self.submobjects.copy() - for char_index in range(len(self.text)): - if self.text[char_index] in [" ", "\t", "\n"]: - space = Dot(radius=0, fill_opacity=0, stroke_opacity=0) - space.move_to(submobs[max(char_index - 1, 0)].get_center()) - submobs.insert(char_index, space) + for wsp_match_obj in re.finditer(r"\s", self.text): + char_index = wsp_match_obj.start() + space = Dot(radius=0, fill_opacity=0, stroke_opacity=0) + space.move_to(submobs[max(char_index - 1, 0)].get_center()) + submobs.insert(char_index, space) self.set_submobjects(submobs) - def find_indexes(self, word): - m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word) + def set_color_by_t2c(self, t2c): + for word, color in t2c.items(): + for start, end in self.find_indexes(word): + self[start:end].set_color(color) + + def set_color_by_t2g(self, t2g): + for word, gradient in t2g.items(): + for start, end in self.find_indexes(word): + self[start:end].set_color_by_gradient(*gradient) + + def find_indexes(self, tuple_or_word): + if isinstance(tuple_or_word, tuple): + return [tuple_or_word] + + # TODO: needed? + m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', tuple_or_word) if m: start = int(m.group(1)) if m.group(1) != '' else 0 end = int(m.group(2)) if m.group(2) != '' else len(self.text) @@ -110,12 +135,10 @@ class Text(SVGMobject): end = len(self.text) + end if end < 0 else end return [(start, end)] - indexes = [] - index = self.text.find(word) - while index != -1: - indexes.append((index, index + len(word))) - index = self.text.find(word, index + len(word)) - return indexes + return [ + match_obj.span() + for match_obj in re.finditer(re.escape(tuple_or_word), self.text) + ] def get_parts_by_text(self, word): return VGroup(*( @@ -145,19 +168,7 @@ class Text(SVGMobject): if kwargs.__contains__('text2weight'): kwargs['t2w'] = kwargs.pop('text2weight') - def set_color_by_t2c(self, t2c=None): - t2c = t2c if t2c else self.t2c - for word, color in t2c.items(): - for start, end in self.find_indexes(word): - self[start:end].set_color(color) - - def set_color_by_t2g(self, t2g=None): - t2g = t2g if t2g else self.t2g - for word, gradient in t2g.items(): - for start, end in self.find_indexes(word): - self[start:end].set_color_by_gradient(*gradient) - - def text2hash(self): + def text2hash(self): # TODO settings = self.font + self.slant + self.weight settings += str(self.t2f) + str(self.t2s) + str(self.t2w) settings += str(self.lsh) + str(self.font_size) @@ -166,63 +177,65 @@ class Text(SVGMobject): hasher.update(id_str.encode()) return hasher.hexdigest()[:16] + def get_t2x_components(self, t2x_dict): + """ + Convert to non-overlapping items + """ + result = [] + for key, value in t2x_dict.items(): + for text_span in self.find_indexes(key): + if text_span[0] >= text_span[1]: + continue + new_result = [(text_span, value)] + for s, v in result: + if text_span[1] <= s[0] or s[1] <= text_span[0]: + new_result.append((s, v)) + continue + if s[0] < text_span[0]: + new_result.append(((s[0], text_span[0]), v)) + if text_span[1] < s[1]: + new_result.append(((text_span[1], s[1]), v)) + result = new_result + return sorted(result) + + def merge_t2x_items(self, *t2x_dicts): + result = [] + def append_item(t2x_items, current_index): + next_index = min(item[0][1] for item in t2x_items) + result.append(((current_index, next_index), tuple(item[1] for item in t2x_items))) + return next_index + + t2x_item_generators = [ + iter(self.get_t2x_components(t2x_dict)) + for t2x_dict in t2x_dicts + ] + t2x_items = [next(gen) for gen in t2x_item_generators] + current_index = append_item(t2x_items, 0) + text_len = len(self.text) + while current_index != text_len: + for i, gen in enumerate(t2x_item_generators): + if t2x_items[i][0][1] == current_index: + t2x_items[i] = next(gen) + current_index = append_item(t2x_items, current_index) + return result + def text2settings(self): - """ - Substrings specified in t2f, t2s, t2w can occupy each other. - For each category of style, a stack following first-in-last-out is constructed, - and the last value in each stack takes effect. - """ - settings = [] - self.line_num = 0 - def add_text_settings(start, end, style_stacks): - if start == end: - return - breakdown_indices = [start, *[ - i + start + 1 for i, char in enumerate(self.text[start:end]) if char == "\n" - ], end] - style = [stack[-1] for stack in style_stacks] - for atom_start, atom_end in zip(breakdown_indices[:-1], breakdown_indices[1:]): - if atom_start < atom_end: - settings.append(TextSetting(atom_start, atom_end, *style, self.line_num)) - self.line_num += 1 - self.line_num -= 1 - - # Set all the default and specified values. - len_text = len(self.text) - t2x_items = sorted([ - *[ - (0, len_text, t2x_index, value) - for t2x_index, value in enumerate([self.font, self.slant, self.weight]) - ], - *[ - (start, end, t2x_index, value) - for t2x_index, t2x in enumerate([self.t2f, self.t2s, self.t2w]) - for word, value in t2x.items() - for start, end in self.find_indexes(word) - ] - ], key=lambda item: item[0]) - - # Break down ranges and construct settings separately. - active_items = [] - style_stacks = [[] for _ in range(3)] - for item, next_start in zip(t2x_items, [*[item[0] for item in t2x_items[1:]], len_text]): - active_items.append(item) - start, end, t2x_index, value = item - style_stacks[t2x_index].append(value) - halting_items = sorted(filter( - lambda item: item[1] <= next_start, - active_items - ), key=lambda item: item[1]) - atom_start = start - for halting_item in halting_items: - active_items.remove(halting_item) - _, atom_end, t2x_index, _ = halting_item - add_text_settings(atom_start, atom_end, style_stacks) - style_stacks[t2x_index].pop() - atom_start = atom_end - add_text_settings(atom_start, next_start, style_stacks) - - del self.line_num + # Substrings specified in t2f, t2s, t2w may occupy each other + full_span = (0, len(self.text)) + t2f = {full_span: self.font, **self.t2f} + t2s = {full_span: self.slant, **self.t2s} + t2w = {full_span: self.weight, **self.t2w} + t2l = {full_span: self.text.count("\n"), **{ + match_obj.span(): line_num + for line_num, match_obj in enumerate(re.finditer(r".*\n", self.text)) + }} + setting_items = self.merge_t2x_items( + t2f, t2s, t2w, t2l + ) + settings = [ + TextSetting(*text_span, *values) + for text_span, values in setting_items + ] return settings def text2svg(self): @@ -230,14 +243,11 @@ class Text(SVGMobject): size = self.font_size lsh = self.lsh - if self.font == '': - self.font = get_customization()['style']['font'] - dir_name = get_text_dir() hash_name = self.text2hash() file_name = os.path.join(dir_name, hash_name) + '.svg' - if os.path.exists(file_name): - return file_name + #if os.path.exists(file_name): # TODO: recover + # return file_name settings = self.text2settings() width = DEFAULT_PIXEL_WIDTH height = DEFAULT_PIXEL_HEIGHT @@ -504,25 +514,26 @@ class Code(Text): def __init__(self, code, **kwargs): self.full2short(kwargs) digest_config(self, kwargs) - code = code.lstrip("\n") # avoid mismatches of character indices lexer = pygments.lexers.get_lexer_by_name(self.language) tokens_generator = pygments.lex(code, lexer) styles_dict = dict(pygments.styles.get_style_by_name(self.code_style)) default_color_hex = styles_dict[pygments.token.Text]["color"] if not default_color_hex: default_color_hex = self.color[1:] + code = "" start_index = 0 t2c = {} t2s = {} t2w = {} for pair in tokens_generator: ttype, token = pair + code += token end_index = start_index + len(token) - range_str = f"[{start_index}:{end_index}]" + span_tuple = (start_index, end_index) style_dict = styles_dict[ttype] - t2c[range_str] = "#" + (style_dict["color"] or default_color_hex) - t2s[range_str] = ITALIC if style_dict["italic"] else NORMAL - t2w[range_str] = BOLD if style_dict["bold"] else NORMAL + t2c[span_tuple] = "#" + (style_dict["color"] or default_color_hex) + t2s[span_tuple] = ITALIC if style_dict["italic"] else NORMAL + t2w[span_tuple] = BOLD if style_dict["bold"] else NORMAL start_index = end_index t2c.update(self.t2c) t2s.update(self.t2s) @@ -530,7 +541,7 @@ class Code(Text): kwargs["t2c"] = t2c kwargs["t2s"] = t2s kwargs["t2w"] = t2w - Text.__init__(self, code, **kwargs) + super().__init__(code, **kwargs) if self.char_width is not None: self.set_monospace(self.char_width) From b06a5d3f23b22a9a3b534193d6b3631d1e69cf3e Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Sat, 26 Feb 2022 20:31:26 +0800 Subject: [PATCH 04/20] Refactor Text --- manimlib/mobject/svg/svg_mobject.py | 4 + manimlib/mobject/svg/text_mobject.py | 664 ++++++++------------------- 2 files changed, 188 insertions(+), 480 deletions(-) diff --git a/manimlib/mobject/svg/svg_mobject.py b/manimlib/mobject/svg/svg_mobject.py index 3b8bb6ad..2de1ed6b 100644 --- a/manimlib/mobject/svg/svg_mobject.py +++ b/manimlib/mobject/svg/svg_mobject.py @@ -325,3 +325,7 @@ class VMobjectFromSVGPath(VMobject): for attr_name in attr_names ] func(*points) + + # Get rid of the side effect of trailing "Z M" commands. + if self.has_new_path_started(): + self.resize_points(self.get_num_points() - 1) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 6052648d..9ae19cc4 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -1,27 +1,26 @@ -import hashlib +import itertools as it import os import re -import io import typing -import xml.etree.ElementTree as ET -import functools -import pygments -import pygments.lexers -import pygments.styles - +import xml.sax.saxutils as saxutils from contextlib import contextmanager from pathlib import Path -import manimpango +import pygments +import pygments.formatters +import pygments.lexers + +import manimglpango from manimlib.logger import log from manimlib.constants import * from manimlib.mobject.geometry import Dot from manimlib.mobject.svg.svg_mobject import SVGMobject -from manimlib.mobject.types.vectorized_mobject import VGroup +from manimlib.utils.iterables import adjacent_pairs +from manimlib.utils.tex_file_writing import tex_hash from manimlib.utils.config_ops import digest_config -from manimlib.utils.customization import get_customization -from manimlib.utils.directories import get_downloads_dir, get_text_dir -from manimpango import PangoUtils, TextSetting, MarkupUtils +from manimlib.utils.directories import get_downloads_dir +from manimlib.utils.directories import get_text_dir + TEXT_MOB_SCALE_FACTOR = 0.0076 DEFAULT_LINE_SPACING_SCALE = 0.6 @@ -30,16 +29,21 @@ DEFAULT_LINE_SPACING_SCALE = 0.6 class Text(SVGMobject): CONFIG = { # Mobject - "color": WHITE, + "svg_default": { + "color": WHITE, + "opacity": 1.0, + "stroke_width": 0, + }, "height": None, - "stroke_width": 0, # Text - "font": '', - "gradient": None, - "lsh": -1, - "size": None, "font_size": 48, - "tab_width": 4, + "lsh": None, + "justify": False, + "indent": 0, + "alignment": "LEFT", + "line_width": -1, # No auto wrapping if set to -1 + "font": "", + "gradient": None, "slant": NORMAL, "weight": NORMAL, "t2c": {}, @@ -48,501 +52,201 @@ class Text(SVGMobject): "t2s": {}, "t2w": {}, "disable_ligatures": True, + "escape_chars": True, + "apply_space_chars": True, } def __init__(self, text, **kwargs): self.full2short(kwargs) digest_config(self, kwargs) - if self.size: - log.warning( - "`self.size` has been deprecated and will " - "be removed in future.", - ) - self.font_size = self.size - if self.lsh == -1: - self.lsh = self.font_size + self.font_size * DEFAULT_LINE_SPACING_SCALE - else: - self.lsh = self.font_size + self.font_size * self.lsh - text_without_tabs = text - if text.find('\t') != -1: - text_without_tabs = text.replace('\t', ' ' * self.tab_width) - self.text = text_without_tabs - file_name = self.text2svg() - PangoUtils.remove_last_M(file_name) - self.remove_empty_path(file_name) - SVGMobject.__init__(self, file_name, **kwargs) - self.text = text - if self.disable_ligatures: - self.apply_space_chars() - if self.t2c: - self.set_color_by_t2c() - if self.gradient: - self.set_color_by_gradient(*self.gradient) - if self.t2g: - self.set_color_by_t2g() - - # anti-aliasing - if self.height is None: - self.scale(TEXT_MOB_SCALE_FACTOR) - - def remove_empty_path(self, file_name): - with open(file_name, 'r') as fpr: - content = fpr.read() - content = re.sub(r'', '', content) - with open(file_name, 'w') as fpw: - fpw.write(content) - - def apply_space_chars(self): - submobs = self.submobjects.copy() - for char_index in range(len(self.text)): - if self.text[char_index] in [" ", "\t", "\n"]: - space = Dot(radius=0, fill_opacity=0, stroke_opacity=0) - space.move_to(submobs[max(char_index - 1, 0)].get_center()) - submobs.insert(char_index, space) - self.set_submobjects(submobs) - - def find_indexes(self, word): - m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word) - if m: - start = int(m.group(1)) if m.group(1) != '' else 0 - end = int(m.group(2)) if m.group(2) != '' else len(self.text) - start = len(self.text) + start if start < 0 else start - end = len(self.text) + end if end < 0 else end - return [(start, end)] - - indexes = [] - index = self.text.find(word) - while index != -1: - indexes.append((index, index + len(word))) - index = self.text.find(word, index + len(word)) - return indexes - - def get_parts_by_text(self, word): - return VGroup(*( - self[i:j] - for i, j in self.find_indexes(word) - )) - - def get_part_by_text(self, word): - parts = self.get_parts_by_text(word) - if len(parts) > 0: - return parts[0] - else: - return None - - 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'): - kwargs['t2f'] = kwargs.pop('text2font') - if kwargs.__contains__('text2gradient'): - kwargs['t2g'] = kwargs.pop('text2gradient') - if kwargs.__contains__('text2slant'): - kwargs['t2s'] = kwargs.pop('text2slant') - if kwargs.__contains__('text2weight'): - kwargs['t2w'] = kwargs.pop('text2weight') - - def set_color_by_t2c(self, t2c=None): - t2c = t2c if t2c else self.t2c - for word, color in t2c.items(): - for start, end in self.find_indexes(word): - self[start:end].set_color(color) - - def set_color_by_t2g(self, t2g=None): - t2g = t2g if t2g else self.t2g - for word, gradient in t2g.items(): - for start, end in self.find_indexes(word): - self[start:end].set_color_by_gradient(*gradient) - - 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.font_size) - id_str = self.text + settings - hasher = hashlib.sha256() - hasher.update(id_str.encode()) - return hasher.hexdigest()[:16] - - def text2settings(self): - """ - Substrings specified in t2f, t2s, t2w can occupy each other. - For each category of style, a stack following first-in-last-out is constructed, - and the last value in each stack takes effect. - """ - settings = [] - self.line_num = 0 - def add_text_settings(start, end, style_stacks): - if start == end: - return - breakdown_indices = [start, *[ - i + start + 1 for i, char in enumerate(self.text[start:end]) if char == "\n" - ], end] - style = [stack[-1] for stack in style_stacks] - for atom_start, atom_end in zip(breakdown_indices[:-1], breakdown_indices[1:]): - if atom_start < atom_end: - settings.append(TextSetting(atom_start, atom_end, *style, self.line_num)) - self.line_num += 1 - self.line_num -= 1 - - # Set all the default and specified values. - len_text = len(self.text) - t2x_items = sorted([ - *[ - (0, len_text, t2x_index, value) - for t2x_index, value in enumerate([self.font, self.slant, self.weight]) - ], - *[ - (start, end, t2x_index, value) - for t2x_index, t2x in enumerate([self.t2f, self.t2s, self.t2w]) - for word, value in t2x.items() - for start, end in self.find_indexes(word) - ] - ], key=lambda item: item[0]) - - # Break down ranges and construct settings separately. - active_items = [] - style_stacks = [[] for _ in range(3)] - for item, next_start in zip(t2x_items, [*[item[0] for item in t2x_items[1:]], len_text]): - active_items.append(item) - start, end, t2x_index, value = item - style_stacks[t2x_index].append(value) - halting_items = sorted(filter( - lambda item: item[1] <= next_start, - active_items - ), key=lambda item: item[1]) - atom_start = start - for halting_item in halting_items: - active_items.remove(halting_item) - _, atom_end, t2x_index, _ = halting_item - add_text_settings(atom_start, atom_end, style_stacks) - style_stacks[t2x_index].pop() - atom_start = atom_end - add_text_settings(atom_start, next_start, style_stacks) - - del self.line_num - return settings - - def text2svg(self): - # anti-aliasing - size = self.font_size - lsh = self.lsh - - if self.font == '': - self.font = get_customization()['style']['font'] - - dir_name = get_text_dir() - hash_name = self.text2hash() - file_name = os.path.join(dir_name, hash_name) + '.svg' - if os.path.exists(file_name): - return file_name - settings = self.text2settings() - width = DEFAULT_PIXEL_WIDTH - height = DEFAULT_PIXEL_HEIGHT - disable_liga = self.disable_ligatures - return manimpango.text2svg( - settings, - size, - lsh, - disable_liga, - file_name, - START_X, - START_Y, - width, - height, - self.text, - ) - - -class MarkupText(SVGMobject): - CONFIG = { - # Mobject - "color": WHITE, - "height": None, - # Text - "font": '', - "font_size": 48, - "lsh": None, - "justify": False, - "slant": NORMAL, - "weight": NORMAL, - "tab_width": 4, - "gradient": None, - "disable_ligatures": True, - } - - def __init__(self, text, **config): - digest_config(self, config) - self.text = f'{text}' - self.original_text = self.text - self.text_for_parsing = self.text - text_without_tabs = text - if "\t" in text: - text_without_tabs = text.replace("\t", " " * self.tab_width) - try: - colormap = self.extract_color_tags() - gradientmap = self.extract_gradient_tags() - except ET.ParseError: - # let pango handle that error - pass - validate_error = MarkupUtils.validate(self.text) + validate_error = manimglpango.validate(text) if validate_error: raise ValueError(validate_error) - file_name = self.text2svg() - PangoUtils.remove_last_M(file_name) - super().__init__( - file_name, - **config, - ) - self.chars = self.get_group_class()(*self.submobjects) - self.text = text_without_tabs.replace(" ", "").replace("\n", "") + self.text = text + super.__init__(**kwargs) + + self.scale(self.font_size / 48) # TODO if self.gradient: self.set_color_by_gradient(*self.gradient) - for col in colormap: - self.chars[ - col["start"] - - col["start_offset"] : col["end"] - - col["start_offset"] - - col["end_offset"] - ].set_color(self._parse_color(col["color"])) - for grad in gradientmap: - self.chars[ - grad["start"] - - grad["start_offset"] : grad["end"] - - grad["start_offset"] - - grad["end_offset"] - ].set_color_by_gradient( - *(self._parse_color(grad["from"]), self._parse_color(grad["to"])) - ) # anti-aliasing if self.height is None: self.scale(TEXT_MOB_SCALE_FACTOR) - def text2hash(self): - """Generates ``sha256`` hash for file name.""" - settings = ( - "MARKUPPANGO" + self.font + self.slant + self.weight + self.color - ) # to differentiate from classical Pango Text - settings += str(self.lsh) + str(self.font_size) - settings += str(self.disable_ligatures) - settings += str(self.justify) - id_str = self.text + settings - hasher = hashlib.sha256() - hasher.update(id_str.encode()) - return hasher.hexdigest()[:16] - - def text2svg(self): - """Convert the text to SVG using Pango.""" - size = self.font_size - dir_name = get_text_dir() - disable_liga = self.disable_ligatures - if not os.path.exists(dir_name): - os.makedirs(dir_name) - hash_name = self.text2hash() - file_name = os.path.join(dir_name, hash_name) + ".svg" - if os.path.exists(file_name): - return file_name - - extra_kwargs = {} - extra_kwargs['justify'] = self.justify - extra_kwargs['pango_width'] = DEFAULT_PIXEL_WIDTH - 100 - if self.lsh: - extra_kwargs['line_spacing']=self.lsh - return MarkupUtils.text2svg( - f'{self.text}', + @property + def hash_seed(self): + return ( + self.__class__.__name__, + self.svg_default, + self.path_string_config, + self.text, + #self.font_size, + self.lsh, + self.justify, + self.indent, + self.alignment, + self.line_width, self.font, self.slant, self.weight, - size, - 0, # empty parameter - disable_liga, - file_name, - START_X, - START_Y, - DEFAULT_PIXEL_WIDTH, # width - DEFAULT_PIXEL_HEIGHT, # height - **extra_kwargs + self.t2c, + self.t2f, + self.t2s, + self.t2w, + self.disable_ligatures, + self.escape_chars, + self.apply_space_chars ) - def _parse_color(self, col): - """Parse color given in ```` or ```` tags.""" - if re.match("#[0-9a-f]{6}", col): - return col - else: - return globals()[col.upper()] # this is hacky + def get_file_path(self): + full_markup = self.get_full_markup_str() + svg_file = os.path.join( + get_text_dir(), tex_hash(full_markup) + ".svg" + ) + if not os.path.exists(svg_file): + self.markup_to_svg(full_markup, svg_file) + return svg_file - @functools.lru_cache(10) - def get_text_from_markup(self, element=None): - if not element: - element = ET.fromstring(self.text_for_parsing) - final_text = '' - for i in element.itertext(): - final_text += i - return final_text + def get_full_markup_str(self): + if self.t2g: + log.warning( + "Manim currently cannot parse gradient from svg. " + "Please set gradient via `set_color_by_gradient`.", + ) - def extract_color_tags(self, text=None, colormap = None): - """Used to determine which parts (if any) of the string should be formatted - with a custom color. - Removes the ```` tag, as it is not part of Pango's markup and would cause an error. - Note: Using the ```` tags is deprecated. As soon as the legacy syntax is gone, this function - will be removed. - """ - if not text: - text = self.text_for_parsing - if not colormap: - colormap = list() - elements = ET.fromstring(text) - text_from_markup = self.get_text_from_markup() - final_xml = ET.fromstring(f'{elements.text if elements.text else ""}') - def get_color_map(elements): - for element in elements: - if element.tag == 'color': - element_text = self.get_text_from_markup(element) - start = text_from_markup.find(element_text) - end = start + len(element_text) - offsets = element.get('offset').split(",") if element.get('offset') else [0] - start_offset = int(offsets[0]) if offsets[0] else 0 - end_offset = int(offsets[1]) if len(offsets) == 2 and offsets[1] else 0 - colormap.append( - { - "start": start, - "end": end, - "color": element.get('col'), - "start_offset": start_offset, - "end_offset": end_offset, - } - ) - - _elements_list = list(element.iter()) - if len(_elements_list) <= 1: - final_xml.append(ET.fromstring(f'{element.text if element.text else ""}')) - else: - final_xml.append(_elements_list[-1]) - else: - if len(list(element.iter())) == 1: - final_xml.append(element) - else: - get_color_map(element) - get_color_map(elements) - with io.BytesIO() as f: - tree = ET.ElementTree() - tree._setroot(final_xml) - tree.write(f) - self.text = f.getvalue().decode() - self.text_for_parsing = self.text # gradients will use it - return colormap + global_params = {} + lsh = self.lsh or DEFAULT_LINE_SPACING_SCALE + global_params["line_height"] = 0.6 * lsh + 0.64 + if self.font: + global_params["font_family"] = self.font + #global_params["font_size"] = self.font_size * 1024 + global_params["font_style"] = self.slant + global_params["font_weight"] = self.weight + if self.disable_ligatures: + global_params["font_features"] = "liga=0,dlig=0,clig=0,hlig=0" + text_span_to_params_map = { + (0, len(self.text)): global_params + } - def extract_gradient_tags(self, text=None,gradientmap=None): - """Used to determine which parts (if any) of the string should be formatted - with a gradient. - Removes the ```` tag, as it is not part of Pango's markup and would cause an error. - """ - if not text: - text = self.text_for_parsing - if not gradientmap: - gradientmap = list() + for t2x_dict, key in ( + (self.t2c, "color"), + (self.t2f, "font_family"), + (self.t2s, "font_style"), + (self.t2w, "font_weight") + ): + for word_or_text_span, value in t2x_dict.items(): + for text_span in self.find_indexes(word_or_text_span): + if text_span not in text_span_to_params_map: + text_span_to_params_map[text_span] = {} + text_span_to_params_map[text_span][key] = value - elements = ET.fromstring(text) - text_from_markup = self.get_text_from_markup() - final_xml = ET.fromstring(f'{elements.text if elements.text else ""}') - def get_gradient_map(elements): - for element in elements: - if element.tag == 'gradient': - element_text = self.get_text_from_markup(element) - start = text_from_markup.find(element_text) - end = start + len(element_text) - offsets = element.get('offset').split(",") if element.get('offset') else [0] - start_offset = int(offsets[0]) if offsets[0] else 0 - end_offset = int(offsets[1]) if len(offsets) == 2 and offsets[1] else 0 - gradientmap.append( - { - "start": start, - "end": end, - "from": element.get('from'), - "to": element.get('to'), - "start_offset": start_offset, - "end_offset": end_offset, - } - ) - _elements_list = list(element.iter()) - if len(_elements_list) == 1: - final_xml.append(ET.fromstring(f'{element.text if element.text else ""}')) - else: - final_xml.append(_elements_list[-1]) - else: - if len(list(element.iter())) == 1: - final_xml.append(element) - else: - get_gradient_map(element) - get_gradient_map(elements) - with io.BytesIO() as f: - tree = ET.ElementTree() - tree._setroot(final_xml) - tree.write(f) - self.text = f.getvalue().decode() + indices, _, flags, param_dicts = zip(*sorted([ + (*text_span[::(1, -1)[flag]], flag, param_dict) + for text_span, param_dict in text_span_to_params_map.items() + for flag in range(2) + ])) + tag_pieces = [ + (f"", "")[flag] + for flag, param_dict in zip(flags, param_dicts) + ] + tag_pieces.insert(0, "") + string_pieces = [ + self.text[slice(*piece_span)] + for piece_span in list(adjacent_pairs(indices))[:-1] + ] + if self.escape_chars: + string_pieces = list(map(saxutils.escape, string_pieces)) + return "".join(it.chain(*zip(tag_pieces, string_pieces))) - return gradientmap + def find_indexes(self, word_or_text_span): + if isinstance(word_or_text_span, tuple): + return [word_or_text_span] - def __repr__(self): - return f"MarkupText({repr(self.original_text)})" + return [ + match_obj.span() + for match_obj in re.finditer(re.escape(word_or_text_span), self.text) + ] + + @staticmethod + def get_attr_list_str(param_dict): + return " ".join([ + f"{key}='{value}'" + for key, value in param_dict.items() + ]) + + def markup_to_svg(self, markup_str, file_name): + width = DEFAULT_PIXEL_WIDTH + height = DEFAULT_PIXEL_HEIGHT + justify = self.justify + indent = self.indent + alignment = ["LEFT", "CENTER", "RIGHT"].index(self.alignment.upper()) + line_width = self.line_width * 1024 + + return manimglpango.markup_to_svg( + markup_str, + file_name, + width, + height, + justify=justify, + indent=indent, + alignment=alignment, + line_width=line_width + ) + + def generate_mobject(self): + super().generate_mobject() + + # Remove empty paths + submobjects = list(filter(lambda submob: submob.has_points(), self)) + + # Apply space characters + if self.apply_space_chars: + for char_index, char in enumerate(self.text): + if not re.match(r"\s", char): + continue + space = Dot(radius=0, fill_opacity=0, stroke_opacity=0) + space.move_to(submobjects[max(char_index - 1, 0)].get_center()) + submobjects.insert(char_index, space) + self.set_submobjects(submobjects) + + def full2short(self, config): + conversion_dict = { + "line_spacing_height": "lsh", + "text2color": "t2c", + "text2font": "t2f", + "text2gradient": "t2g", + "text2slant": "t2s", + "text2weight": "t2w" + } + for kwargs in [config, self.CONFIG]: + for long_name, short_name in conversion_dict.items(): + if long_name in kwargs: + kwargs[short_name] = kwargs.pop(long_name) + + +class MarkupText(Text): + CONFIG = { + "escape_chars": False, + "apply_space_chars": False, + } class Code(Text): CONFIG = { "font": "Consolas", "font_size": 24, - "lsh": 1.0, + "lsh": 1.0, # TODO "language": "python", # Visit https://pygments.org/demo/ to have a preview of more styles. - "code_style": "monokai", - # If not None, then each character will cover a space of equal width. - "char_width": None + "code_style": "monokai" } def __init__(self, code, **kwargs): - self.full2short(kwargs) digest_config(self, kwargs) - code = code.lstrip("\n") # avoid mismatches of character indices + self.code = code lexer = pygments.lexers.get_lexer_by_name(self.language) - tokens_generator = pygments.lex(code, lexer) - styles_dict = dict(pygments.styles.get_style_by_name(self.code_style)) - default_color_hex = styles_dict[pygments.token.Text]["color"] - if not default_color_hex: - default_color_hex = self.color[1:] - start_index = 0 - t2c = {} - t2s = {} - t2w = {} - for pair in tokens_generator: - ttype, token = pair - end_index = start_index + len(token) - range_str = f"[{start_index}:{end_index}]" - style_dict = styles_dict[ttype] - t2c[range_str] = "#" + (style_dict["color"] or default_color_hex) - t2s[range_str] = ITALIC if style_dict["italic"] else NORMAL - t2w[range_str] = BOLD if style_dict["bold"] else NORMAL - start_index = end_index - t2c.update(self.t2c) - t2s.update(self.t2s) - t2w.update(self.t2w) - kwargs["t2c"] = t2c - kwargs["t2s"] = t2s - kwargs["t2w"] = t2w - Text.__init__(self, code, **kwargs) - if self.char_width is not None: - self.set_monospace(self.char_width) - - def set_monospace(self, char_width): - current_char_index = 0 - for i, char in enumerate(self.text): - if char == "\n": - current_char_index = 0 - continue - self[i].set_x(current_char_index * char_width) - current_char_index += 1 - self.center() + formatter = pygments.formatters.PangoMarkupFormatter(style=self.code_style) + markup_code = pygments.highlight(code, lexer, formatter) + super().__init__(markup_code, **kwargs) @contextmanager @@ -592,7 +296,7 @@ def register_font(font_file: typing.Union[str, Path]): raise FileNotFoundError(error) try: - assert manimpango.register_font(str(file_path)) + assert manimglpango.register_font(str(file_path)) yield finally: - manimpango.unregister_font(str(file_path)) + manimglpango.unregister_font(str(file_path)) From 956e3a69c75e7b17f3154d4b9f88980691147420 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Wed, 2 Mar 2022 18:38:24 +0800 Subject: [PATCH 05/20] Refactor Text --- manimlib/mobject/svg/text_mobject.py | 331 ++++++++++++++++++++------- requirements.txt | 2 +- setup.cfg | 2 +- 3 files changed, 254 insertions(+), 81 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 9ae19cc4..f1c40f9f 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -1,4 +1,3 @@ -import itertools as it import os import re import typing @@ -10,12 +9,13 @@ import pygments import pygments.formatters import pygments.lexers -import manimglpango +import manimpango +from manimpango import MarkupUtils from manimlib.logger import log from manimlib.constants import * from manimlib.mobject.geometry import Dot from manimlib.mobject.svg.svg_mobject import SVGMobject -from manimlib.utils.iterables import adjacent_pairs +from manimlib.utils.customization import get_customization from manimlib.utils.tex_file_writing import tex_hash from manimlib.utils.config_ops import digest_config from manimlib.utils.directories import get_downloads_dir @@ -26,49 +26,228 @@ TEXT_MOB_SCALE_FACTOR = 0.0076 DEFAULT_LINE_SPACING_SCALE = 0.6 +class _TextParser(object): + # See https://docs.gtk.org/Pango/pango_markup.html + # A tag containing two aliases will cause warning, + # so only use the first key of each group of aliases. + SPAN_ATTR_KEY_ALIAS_LIST = ( + ("font", "font_desc"), + ("font_family", "face"), + ("font_size", "size"), + ("font_style", "style"), + ("font_weight", "weight"), + ("font_variant", "variant"), + ("font_stretch", "stretch"), + ("font_features",), + ("foreground", "fgcolor", "color"), + ("background", "bgcolor"), + ("alpha", "fgalpha"), + ("background_alpha", "bgalpha"), + ("underline", "underline_color"), + ("overline", "overline_color"), + ("rise",), + ("baseline_shift",), + ("font_scale",), + ("strikethrough",), + ("strikethrough_color",), + ("fallback",), + ("lang",), + ("letter_spacing",), + ("gravity",), + ("gravity_hint",), + ("show",), + ("insert_hyphens",), + ("allow_breaks",), + ("line_height",), + ("text_transform",), + ("segment",), + ) + SPAN_ATTR_KEY_CONVERSION = { + key: key_alias_list[0] + for key_alias_list in SPAN_ATTR_KEY_ALIAS_LIST + for key in key_alias_list + } + SPAN_ATTR_KEY_ALIASES = tuple(SPAN_ATTR_KEY_CONVERSION.keys()) + + TAG_TO_ATTR_DICT = { + "b": {"font_weight": "bold"}, + "big": {"font_size": "larger"}, + "i": {"font_style": "italic"}, + "s": {"strikethrough": "true"}, + "sub": {"baseline_shift": "subscript", "font_scale": "subscript"}, + "sup": {"baseline_shift": "superscript", "font_scale": "superscript"}, + "small": {"font_size": "smaller"}, + "tt": {"font_family": "monospace"}, + "u": {"underline": "single"}, + } + + def __init__(self, text: str = "", is_markup: bool = True): + self.text = text + self.is_markup = is_markup + self.global_attrs = {} + self.local_attrs = {(0, len(self.text)): {}} + self.tag_strings = set() + if is_markup: + self.parse_markup() + + def parse_markup(self) -> None: + tag_pattern = r"""<(/?)(\w+)\s*((\w+\s*\=\s*('[^']*'|"[^"]*")\s*)*)>""" + attr_pattern = r"""(\w+)\s*\=\s*(?:(?:'([^']*)')|(?:"([^"]*)"))""" + start_match_obj_stack = [] + match_obj_pairs = [] + for match_obj in re.finditer(tag_pattern, self.text): + if not match_obj.group(1): + start_match_obj_stack.append(match_obj) + else: + match_obj_pairs.append((start_match_obj_stack.pop(), match_obj)) + self.tag_strings.add(match_obj.group()) + assert not start_match_obj_stack, "Unclosed tag(s) detected" + + for start_match_obj, end_match_obj in match_obj_pairs: + tag_name = start_match_obj.group(2) + assert tag_name == end_match_obj.group(2), "Unmatched tag names" + assert not end_match_obj.group(3), "Attributes shan't exist in ending tags" + if tag_name == "span": + attr_dict = { + match.group(1): match.group(2) or match.group(3) + for match in re.finditer(attr_pattern, start_match_obj.group(3)) + } + elif tag_name in _TextParser.TAG_TO_ATTR_DICT.keys(): + assert not start_match_obj.group(3), f"Attributes shan't exist in tag '{tag_name}'" + attr_dict = _TextParser.TAG_TO_ATTR_DICT[tag_name] + else: + raise AssertionError(f"Unknown tag: '{tag_name}'") + + text_span = (start_match_obj.end(), end_match_obj.start()) + self.update_local_attrs(text_span, attr_dict) + + @staticmethod + def convert_key_alias(key: str) -> str: + return _TextParser.SPAN_ATTR_KEY_CONVERSION[key] + + @staticmethod + def update_attr_dict(attr_dict: dict[str, str], key: str, value: str) -> None: + converted_key = _TextParser.convert_key_alias(key) + attr_dict[converted_key] = value + + def update_global_attr(self, key: str, value: str) -> None: + _TextParser.update_attr_dict(self.global_attrs, key, value) + + def update_global_attrs(self, attr_dict: dict[str, str]) -> None: + for key, value in attr_dict.items(): + self.update_global_attr(key, value) + + def update_local_attr(self, span: tuple[int, int], key: str, value: str) -> None: + if span[0] >= span[1]: + log.warning(f"Span {span} doesn't match any part of the string") + return + + if span in self.local_attrs.keys(): + _TextParser.update_attr_dict(self.local_attrs[span], key, value) + return + + span_triplets = [] + for sp, attr_dict in self.local_attrs.items(): + if sp[1] <= span[0] or span[1] <= sp[0]: + continue + span_to_become = (max(sp[0], span[0]), min(sp[1], span[1])) + spans_to_add = [] + if sp[0] < span[0]: + spans_to_add.append((sp[0], span[0])) + if span[1] < sp[1]: + spans_to_add.append((span[1], sp[1])) + span_triplets.append((sp, span_to_become, spans_to_add)) + for span_to_remove, span_to_become, spans_to_add in span_triplets: + attr_dict = self.local_attrs.pop(span_to_remove) + for span_to_add in spans_to_add: + self.local_attrs[span_to_add] = attr_dict.copy() + self.local_attrs[span_to_become] = attr_dict + _TextParser.update_attr_dict(self.local_attrs[span_to_become], key, value) + + def update_local_attrs(self, text_span: tuple[int, int], attr_dict: dict[str, str]) -> None: + for key, value in attr_dict.items(): + self.update_local_attr(text_span, key, value) + + def get_string_content(self, string: str) -> str: + for tag_string in self.tag_strings: + string = string.replace(tag_string, "") + if not self.is_markup: + string = saxutils.escape(string) + return string + + def get_text_pieces(self) -> list[tuple[str, dict[str, str]]]: + result = [] + for span in sorted(self.local_attrs.keys()): + text_piece = self.get_string_content(self.text[slice(*span)]) + if not text_piece: + continue + attr_dict = self.global_attrs.copy() + attr_dict.update(self.local_attrs[span]) + result.append((text_piece, attr_dict)) + return result + + def get_markup_str_with_attrs(self): + return "".join([ + f"{text_piece}" + for text_piece, attr_dict in self.get_text_pieces() + ]) + + @staticmethod + def get_attr_dict_str(attr_dict: dict[str, str]): + return " ".join([ + f"{key}='{value}'" + for key, value in attr_dict.items() + ]) + + +# Temporary handler +class _Alignment: + VAL_LIST = ["LEFT", "CENTER", "RIGHT"] + def __init__(self, s): + self.value = _Alignment.VAL_LIST.index(s.upper()) + + class Text(SVGMobject): CONFIG = { # Mobject + "stroke_width": 0, "svg_default": { "color": WHITE, - "opacity": 1.0, - "stroke_width": 0, }, "height": None, # Text + "is_markup": False, "font_size": 48, "lsh": None, "justify": False, "indent": 0, "alignment": "LEFT", - "line_width": -1, # No auto wrapping if set to -1 + "line_width_factor": None, # No auto wrapping if set to None "font": "", - "gradient": None, "slant": NORMAL, "weight": NORMAL, + "gradient": None, "t2c": {}, "t2f": {}, "t2g": {}, "t2s": {}, "t2w": {}, "disable_ligatures": True, - "escape_chars": True, "apply_space_chars": True, } def __init__(self, text, **kwargs): self.full2short(kwargs) digest_config(self, kwargs) - validate_error = manimglpango.validate(text) + validate_error = MarkupUtils.validate(text) if validate_error: raise ValueError(validate_error) self.text = text - super.__init__(**kwargs) + self.parser = _TextParser(text, is_markup=self.is_markup) + super().__init__(**kwargs) - self.scale(self.font_size / 48) # TODO if self.gradient: self.set_color_by_gradient(*self.gradient) - # anti-aliasing if self.height is None: self.scale(TEXT_MOB_SCALE_FACTOR) @@ -79,12 +258,13 @@ class Text(SVGMobject): self.svg_default, self.path_string_config, self.text, - #self.font_size, + self.is_markup, + self.font_size, self.lsh, self.justify, self.indent, self.alignment, - self.line_width, + self.line_width_factor, self.font, self.slant, self.weight, @@ -93,7 +273,6 @@ class Text(SVGMobject): self.t2s, self.t2w, self.disable_ligatures, - self.escape_chars, self.apply_space_chars ) @@ -113,49 +292,36 @@ class Text(SVGMobject): "Please set gradient via `set_color_by_gradient`.", ) - global_params = {} - lsh = self.lsh or DEFAULT_LINE_SPACING_SCALE - global_params["line_height"] = 0.6 * lsh + 0.64 - if self.font: - global_params["font_family"] = self.font - #global_params["font_size"] = self.font_size * 1024 - global_params["font_style"] = self.slant - global_params["font_weight"] = self.weight - if self.disable_ligatures: - global_params["font_features"] = "liga=0,dlig=0,clig=0,hlig=0" - text_span_to_params_map = { - (0, len(self.text)): global_params + config_style_dict = self.generate_config_style_dict() + global_attr_dict = { + "line_height": str(((self.lsh or DEFAULT_LINE_SPACING_SCALE) + 1) * 0.6), + "font_family": self.font or get_customization()["style"]["font"], + "font_size": str(self.font_size * 1024), + "font_style": self.slant, + "font_weight": self.weight, + # TODO, it seems this doesn't work + "font_features": "liga=0,dlig=0,clig=0,hlig=0" if self.disable_ligatures else None, + "foreground": config_style_dict.get("fill", None), + "alpha": config_style_dict.get("fill-opacity", None) } + global_attr_dict = { + k: v + for k, v in global_attr_dict.items() + if v is not None + } + self.parser.update_global_attrs(global_attr_dict) for t2x_dict, key in ( - (self.t2c, "color"), + (self.t2c, "foreground"), (self.t2f, "font_family"), (self.t2s, "font_style"), (self.t2w, "font_weight") ): for word_or_text_span, value in t2x_dict.items(): for text_span in self.find_indexes(word_or_text_span): - if text_span not in text_span_to_params_map: - text_span_to_params_map[text_span] = {} - text_span_to_params_map[text_span][key] = value + self.parser.update_local_attr(text_span, key, str(value)) - indices, _, flags, param_dicts = zip(*sorted([ - (*text_span[::(1, -1)[flag]], flag, param_dict) - for text_span, param_dict in text_span_to_params_map.items() - for flag in range(2) - ])) - tag_pieces = [ - (f"", "")[flag] - for flag, param_dict in zip(flags, param_dicts) - ] - tag_pieces.insert(0, "") - string_pieces = [ - self.text[slice(*piece_span)] - for piece_span in list(adjacent_pairs(indices))[:-1] - ] - if self.escape_chars: - string_pieces = list(map(saxutils.escape, string_pieces)) - return "".join(it.chain(*zip(tag_pieces, string_pieces))) + return self.parser.get_markup_str_with_attrs() def find_indexes(self, word_or_text_span): if isinstance(word_or_text_span, tuple): @@ -166,30 +332,33 @@ class Text(SVGMobject): for match_obj in re.finditer(re.escape(word_or_text_span), self.text) ] - @staticmethod - def get_attr_list_str(param_dict): - return " ".join([ - f"{key}='{value}'" - for key, value in param_dict.items() - ]) - def markup_to_svg(self, markup_str, file_name): - width = DEFAULT_PIXEL_WIDTH - height = DEFAULT_PIXEL_HEIGHT - justify = self.justify - indent = self.indent - alignment = ["LEFT", "CENTER", "RIGHT"].index(self.alignment.upper()) - line_width = self.line_width * 1024 + # `manimpango` is under construction, + # so the following code is intended to suit its interface + alignment = _Alignment(self.alignment) + if self.line_width_factor is None: + pango_width = -1 + else: + pango_width = self.line_width_factor * DEFAULT_PIXEL_WIDTH - return manimglpango.markup_to_svg( - markup_str, - file_name, - width, - height, - justify=justify, - indent=indent, + return MarkupUtils.text2svg( + text=markup_str, + font="", # Already handled + slant="NORMAL", # Already handled + weight="NORMAL", # Already handled + size=1, # Already handled + _=0, # Empty parameter + disable_liga=False, # Already handled + file_name=file_name, + START_X=0, + START_Y=0, + width=DEFAULT_PIXEL_WIDTH, + height=DEFAULT_PIXEL_HEIGHT, + justify=self.justify, + indent=self.indent, + line_spacing=None, # Already handled alignment=alignment, - line_width=line_width + pango_width=pango_width ) def generate_mobject(self): @@ -198,9 +367,10 @@ class Text(SVGMobject): # Remove empty paths submobjects = list(filter(lambda submob: submob.has_points(), self)) - # Apply space characters + # Apply space characters (may be deprecated?) if self.apply_space_chars: - for char_index, char in enumerate(self.text): + content_str = self.parser.get_string_content(self.text) + for char_index, char in enumerate(content_str): if not re.match(r"\s", char): continue space = Dot(radius=0, fill_opacity=0, stroke_opacity=0) @@ -225,19 +395,20 @@ class Text(SVGMobject): class MarkupText(Text): CONFIG = { - "escape_chars": False, + "is_markup": True, "apply_space_chars": False, } -class Code(Text): +class Code(MarkupText): CONFIG = { "font": "Consolas", "font_size": 24, - "lsh": 1.0, # TODO + "lsh": 1.0, "language": "python", # Visit https://pygments.org/demo/ to have a preview of more styles. - "code_style": "monokai" + "code_style": "monokai", + "apply_space_chars": True } def __init__(self, code, **kwargs): @@ -245,8 +416,10 @@ class Code(Text): self.code = code lexer = pygments.lexers.get_lexer_by_name(self.language) formatter = pygments.formatters.PangoMarkupFormatter(style=self.code_style) - markup_code = pygments.highlight(code, lexer, formatter) - super().__init__(markup_code, **kwargs) + markup = pygments.highlight(code, lexer, formatter) + markup = markup.replace("", f"") + markup = markup.replace("", "") + super().__init__(markup, **kwargs) @contextmanager @@ -296,7 +469,7 @@ def register_font(font_file: typing.Union[str, Path]): raise FileNotFoundError(error) try: - assert manimglpango.register_font(str(file_path)) + assert manimpango.register_font(str(file_path)) yield finally: - manimglpango.unregister_font(str(file_path)) + manimpango.unregister_font(str(file_path)) diff --git a/requirements.txt b/requirements.txt index 32857ee8..ade806c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,6 +17,6 @@ screeninfo validators ipython PyOpenGL -manimpango>=0.2.0,<0.4.0 +manimpango>=0.4.0.post0,<0.5.0 isosurfaces svgelements diff --git a/setup.cfg b/setup.cfg index 4640d91d..5c86c61a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,7 +49,7 @@ install_requires = validators ipython PyOpenGL - manimpango>=0.2.0,<0.4.0 + manimpango>=0.4.0.post0,<0.5.0 isosurfaces svgelements From 52a99a0c4962719a9c1392a8144013f7d13c9c77 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Wed, 2 Mar 2022 19:34:56 +0800 Subject: [PATCH 06/20] Add global_config, local_configs params --- manimlib/mobject/svg/text_mobject.py | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index f1c40f9f..dc425741 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -126,18 +126,18 @@ class _TextParser(object): return _TextParser.SPAN_ATTR_KEY_CONVERSION[key] @staticmethod - def update_attr_dict(attr_dict: dict[str, str], key: str, value: str) -> None: + def update_attr_dict(attr_dict: dict[str, str], key: str, value: typing.Any) -> None: converted_key = _TextParser.convert_key_alias(key) - attr_dict[converted_key] = value + attr_dict[converted_key] = str(value) - def update_global_attr(self, key: str, value: str) -> None: + def update_global_attr(self, key: str, value: typing.Any) -> None: _TextParser.update_attr_dict(self.global_attrs, key, value) - def update_global_attrs(self, attr_dict: dict[str, str]) -> None: + def update_global_attrs(self, attr_dict: dict[str, typing.Any]) -> None: for key, value in attr_dict.items(): self.update_global_attr(key, value) - def update_local_attr(self, span: tuple[int, int], key: str, value: str) -> None: + def update_local_attr(self, span: tuple[int, int], key: str, value: typing.Any) -> None: if span[0] >= span[1]: log.warning(f"Span {span} doesn't match any part of the string") return @@ -164,7 +164,7 @@ class _TextParser(object): self.local_attrs[span_to_become] = attr_dict _TextParser.update_attr_dict(self.local_attrs[span_to_become], key, value) - def update_local_attrs(self, text_span: tuple[int, int], attr_dict: dict[str, str]) -> None: + def update_local_attrs(self, text_span: tuple[int, int], attr_dict: dict[str, typing.Any]) -> None: for key, value in attr_dict.items(): self.update_local_attr(text_span, key, value) @@ -224,6 +224,8 @@ class Text(SVGMobject): "alignment": "LEFT", "line_width_factor": None, # No auto wrapping if set to None "font": "", + "disable_ligatures": True, + "apply_space_chars": True, "slant": NORMAL, "weight": NORMAL, "gradient": None, @@ -232,8 +234,8 @@ class Text(SVGMobject): "t2g": {}, "t2s": {}, "t2w": {}, - "disable_ligatures": True, - "apply_space_chars": True, + "global_config": {}, + "local_configs": {}, } def __init__(self, text, **kwargs): @@ -266,14 +268,16 @@ class Text(SVGMobject): self.alignment, self.line_width_factor, self.font, + self.disable_ligatures, + self.apply_space_chars, self.slant, self.weight, self.t2c, self.t2f, self.t2s, self.t2w, - self.disable_ligatures, - self.apply_space_chars + self.global_config, + self.local_configs ) def get_file_path(self): @@ -294,9 +298,9 @@ class Text(SVGMobject): config_style_dict = self.generate_config_style_dict() global_attr_dict = { - "line_height": str(((self.lsh or DEFAULT_LINE_SPACING_SCALE) + 1) * 0.6), + "line_height": ((self.lsh or DEFAULT_LINE_SPACING_SCALE) + 1) * 0.6, "font_family": self.font or get_customization()["style"]["font"], - "font_size": str(self.font_size * 1024), + "font_size": self.font_size * 1024, "font_style": self.slant, "font_weight": self.weight, # TODO, it seems this doesn't work @@ -310,6 +314,7 @@ class Text(SVGMobject): if v is not None } self.parser.update_global_attrs(global_attr_dict) + self.parser.update_global_attrs(self.global_config) for t2x_dict, key in ( (self.t2c, "foreground"), @@ -319,7 +324,9 @@ class Text(SVGMobject): ): for word_or_text_span, value in t2x_dict.items(): for text_span in self.find_indexes(word_or_text_span): - self.parser.update_local_attr(text_span, key, str(value)) + self.parser.update_local_attr(text_span, key, value) + for text_span, local_config in self.local_configs.items(): + self.parser.update_local_attrs(text_span, local_config) return self.parser.get_markup_str_with_attrs() @@ -396,7 +403,6 @@ class Text(SVGMobject): class MarkupText(Text): CONFIG = { "is_markup": True, - "apply_space_chars": False, } @@ -407,8 +413,7 @@ class Code(MarkupText): "lsh": 1.0, "language": "python", # Visit https://pygments.org/demo/ to have a preview of more styles. - "code_style": "monokai", - "apply_space_chars": True + "code_style": "monokai" } def __init__(self, code, **kwargs): From fce38fd8a519a135acfd95f5bf9670bc5da9d158 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Wed, 2 Mar 2022 19:52:45 +0800 Subject: [PATCH 07/20] Modify default value of apply_space_chars --- manimlib/mobject/svg/text_mobject.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index dc425741..5ccfcee9 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -43,8 +43,10 @@ class _TextParser(object): ("background", "bgcolor"), ("alpha", "fgalpha"), ("background_alpha", "bgalpha"), - ("underline", "underline_color"), - ("overline", "overline_color"), + ("underline",), + ("underline_color",), + ("overline",), + ("overline_color",), ("rise",), ("baseline_shift",), ("font_scale",), @@ -222,10 +224,10 @@ class Text(SVGMobject): "justify": False, "indent": 0, "alignment": "LEFT", - "line_width_factor": None, # No auto wrapping if set to None + "line_width_factor": None, "font": "", "disable_ligatures": True, - "apply_space_chars": True, + "apply_space_chars": False, "slant": NORMAL, "weight": NORMAL, "gradient": None, From e0b0ae280ec5e30e5d2f68fb369bb4fbf1ae7e5e Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Wed, 2 Mar 2022 19:59:14 +0800 Subject: [PATCH 08/20] Allow passing strings to local_configs --- manimlib/mobject/svg/text_mobject.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 5ccfcee9..9d385f8d 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -327,8 +327,9 @@ class Text(SVGMobject): for word_or_text_span, value in t2x_dict.items(): for text_span in self.find_indexes(word_or_text_span): self.parser.update_local_attr(text_span, key, value) - for text_span, local_config in self.local_configs.items(): - self.parser.update_local_attrs(text_span, local_config) + for word_or_text_span, local_config in self.local_configs.items(): + for text_span in self.find_indexes(word_or_text_span): + self.parser.update_local_attrs(text_span, local_config) return self.parser.get_markup_str_with_attrs() From a227ffde0534a71396b6b8dec94cec765589a5f1 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Wed, 2 Mar 2022 20:28:26 +0800 Subject: [PATCH 09/20] PEP8: reorder imports --- manimlib/mobject/svg/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 9d385f8d..f2cbdc69 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -8,9 +8,9 @@ from pathlib import Path import pygments import pygments.formatters import pygments.lexers - import manimpango from manimpango import MarkupUtils + from manimlib.logger import log from manimlib.constants import * from manimlib.mobject.geometry import Dot From 11af9508f227293e7e5d10ded11a3492b3ea5cf0 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Thu, 3 Mar 2022 20:38:15 +0800 Subject: [PATCH 10/20] add back get_parts_by_text, get_part_by_text methods --- manimlib/mobject/svg/text_mobject.py | 46 +++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index f2cbdc69..6de801e8 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -69,7 +69,6 @@ class _TextParser(object): for key_alias_list in SPAN_ATTR_KEY_ALIAS_LIST for key in key_alias_list } - SPAN_ATTR_KEY_ALIASES = tuple(SPAN_ATTR_KEY_CONVERSION.keys()) TAG_TO_ATTR_DICT = { "b": {"font_weight": "bold"}, @@ -227,7 +226,7 @@ class Text(SVGMobject): "line_width_factor": None, "font": "", "disable_ligatures": True, - "apply_space_chars": False, + "apply_space_chars": True, "slant": NORMAL, "weight": NORMAL, "gradient": None, @@ -315,19 +314,21 @@ class Text(SVGMobject): for k, v in global_attr_dict.items() if v is not None } + global_attr_dict.update(self.global_config) self.parser.update_global_attrs(global_attr_dict) - self.parser.update_global_attrs(self.global_config) - for t2x_dict, key in ( - (self.t2c, "foreground"), - (self.t2f, "font_family"), - (self.t2s, "font_style"), - (self.t2w, "font_weight") - ): - for word_or_text_span, value in t2x_dict.items(): - for text_span in self.find_indexes(word_or_text_span): - self.parser.update_local_attr(text_span, key, value) - for word_or_text_span, local_config in self.local_configs.items(): + local_attr_items = [ + (word_or_text_span, {key: value}) + for t2x_dict, key in ( + (self.t2c, "foreground"), + (self.t2f, "font_family"), + (self.t2s, "font_style"), + (self.t2w, "font_weight") + ) + for word_or_text_span, value in t2x_dict.items() + ] + local_attr_items.extend(self.local_configs.items()) + for word_or_text_span, local_config in local_attr_items: for text_span in self.find_indexes(word_or_text_span): self.parser.update_local_attrs(text_span, local_config) @@ -377,7 +378,7 @@ class Text(SVGMobject): # Remove empty paths submobjects = list(filter(lambda submob: submob.has_points(), self)) - # Apply space characters (may be deprecated?) + # Apply space characters if self.apply_space_chars: content_str = self.parser.get_string_content(self.text) for char_index, char in enumerate(content_str): @@ -402,10 +403,27 @@ class Text(SVGMobject): if long_name in kwargs: kwargs[short_name] = kwargs.pop(long_name) + def get_parts_by_text(self, word): + if not self.apply_space_chars: + log.warning( + "Slicing Text without applying spaces, " + "the result could be unexpected." + ) + return VGroup(*( + self[i:j] + for i, j in self.find_indexes(word) + )) + + def get_part_by_text(self, word): + parts = self.get_parts_by_text(word) + if len(parts) > 0: + return parts[0] + class MarkupText(Text): CONFIG = { "is_markup": True, + "apply_space_chars": False, } From d744311f156017a94f435d94c3a5eece9b5a5e08 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Thu, 3 Mar 2022 20:47:44 +0800 Subject: [PATCH 11/20] add warning for slicing methods --- manimlib/mobject/svg/text_mobject.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 6de801e8..2ed044e1 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -404,9 +404,14 @@ class Text(SVGMobject): kwargs[short_name] = kwargs.pop(long_name) def get_parts_by_text(self, word): - if not self.apply_space_chars: + if self.is_markup: log.warning( - "Slicing Text without applying spaces, " + "Slicing MarkupText via `get_parts_by_text`, " + "the result could be unexpected." + ) + elif not self.apply_space_chars: + log.warning( + "Slicing Text without applying spaces via `get_parts_by_text`, " "the result could be unexpected." ) return VGroup(*( From 2d764e12f48de022ddaf98407aad50aa3def2c8e Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Thu, 3 Mar 2022 21:09:05 +0800 Subject: [PATCH 12/20] fix char escaping bug --- manimlib/mobject/svg/text_mobject.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 2ed044e1..e9e281cf 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -169,19 +169,19 @@ class _TextParser(object): for key, value in attr_dict.items(): self.update_local_attr(text_span, key, value) - def get_string_content(self, string: str) -> str: + def remove_tags(self, string: str) -> str: for tag_string in self.tag_strings: string = string.replace(tag_string, "") - if not self.is_markup: - string = saxutils.escape(string) return string def get_text_pieces(self) -> list[tuple[str, dict[str, str]]]: result = [] for span in sorted(self.local_attrs.keys()): - text_piece = self.get_string_content(self.text[slice(*span)]) + text_piece = self.remove_tags(self.text[slice(*span)]) if not text_piece: continue + if not self.is_markup: + text_piece = saxutils.escape(text_piece) attr_dict = self.global_attrs.copy() attr_dict.update(self.local_attrs[span]) result.append((text_piece, attr_dict)) @@ -380,7 +380,9 @@ class Text(SVGMobject): # Apply space characters if self.apply_space_chars: - content_str = self.parser.get_string_content(self.text) + content_str = self.parser.remove_tags(self.text) + if self.is_markup: + content_str = saxutils.unescape(content_str) for char_index, char in enumerate(content_str): if not re.match(r"\s", char): continue @@ -439,7 +441,7 @@ class Code(MarkupText): "lsh": 1.0, "language": "python", # Visit https://pygments.org/demo/ to have a preview of more styles. - "code_style": "monokai" + "code_style": "monokai", } def __init__(self, code, **kwargs): From 0cef9a1e6197e8f5869be50bf41888fe90332079 Mon Sep 17 00:00:00 2001 From: Bill Xi <86190295+TurkeyBilly@users.noreply.github.com> Date: Sun, 6 Mar 2022 13:54:42 +0800 Subject: [PATCH 13/20] Reorganize getters for ParametricCurve --- manimlib/mobject/functions.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/manimlib/mobject/functions.py b/manimlib/mobject/functions.py index 3677a119..d6869bbf 100644 --- a/manimlib/mobject/functions.py +++ b/manimlib/mobject/functions.py @@ -48,6 +48,18 @@ class ParametricCurve(VMobject): self.set_points([self.t_func(t_min)]) return self + def get_t_func(self): + return self.t_func + + def get_function(self): + if hasattr(self, "underlying_function"): + return self.underlying_function + if hasattr(self, "function"): + return self.function + + def get_x_range(self): + if hasattr(self, "x_range"): + return self.x_range class FunctionGraph(ParametricCurve): CONFIG = { @@ -67,12 +79,6 @@ class FunctionGraph(ParametricCurve): super().__init__(parametric_function, self.x_range, **kwargs) - def get_function(self): - return self.function - - def get_point_from_function(self, x): - return self.t_func(x) - class ImplicitFunction(VMobject): CONFIG = { From de46df78dcf6ca1a6711582a512948163f65b84e Mon Sep 17 00:00:00 2001 From: YishiMichael <50232075+YishiMichael@users.noreply.github.com> Date: Thu, 17 Mar 2022 10:58:41 +0800 Subject: [PATCH 14/20] Modify warning message --- manimlib/mobject/svg/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index e9e281cf..b8d7585f 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -413,7 +413,7 @@ class Text(SVGMobject): ) elif not self.apply_space_chars: log.warning( - "Slicing Text without applying spaces via `get_parts_by_text`, " + "Slicing Text via `get_parts_by_text` without applying spaces, " "the result could be unexpected." ) return VGroup(*( From 2a0709664d17ca380883b69ec6c70f2b62146aef Mon Sep 17 00:00:00 2001 From: YishiMichael <50232075+YishiMichael@users.noreply.github.com> Date: Thu, 17 Mar 2022 11:33:53 +0800 Subject: [PATCH 15/20] Add explicit return statement --- manimlib/mobject/svg/text_mobject.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index b8d7585f..1fd2a2cf 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -423,8 +423,7 @@ class Text(SVGMobject): def get_part_by_text(self, word): parts = self.get_parts_by_text(word) - if len(parts) > 0: - return parts[0] + return parts[0] if parts else None class MarkupText(Text): From 67f8007764e56155ddb83edef2196faeb97ada31 Mon Sep 17 00:00:00 2001 From: widcardw <55699713+widcardw@users.noreply.github.com> Date: Thu, 17 Mar 2022 14:10:30 +0800 Subject: [PATCH 16/20] Fix the width of riemann rectangles --- manimlib/mobject/coordinate_systems.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/manimlib/mobject/coordinate_systems.py b/manimlib/mobject/coordinate_systems.py index 3ad01086..dbfadb61 100644 --- a/manimlib/mobject/coordinate_systems.py +++ b/manimlib/mobject/coordinate_systems.py @@ -235,6 +235,7 @@ class CoordinateSystem(): stroke_color=BLACK, fill_opacity=1, colors=(BLUE, GREEN), + stroke_background=True, show_signed_area=True): if x_range is None: x_range = self.x_range[:2] @@ -257,7 +258,8 @@ class CoordinateSystem(): height = get_norm( self.i2gp(sample, graph) - self.c2p(sample, 0) ) - rect = Rectangle(width=x1 - x0, height=height) + rect = Rectangle(width=self.x_axis.n2p(x1)[0] - self.x_axis.n2p(x0)[0], + height=height) rect.move_to(self.c2p(x0, 0), DL) rects.append(rect) result = VGroup(*rects) @@ -266,6 +268,7 @@ class CoordinateSystem(): stroke_width=stroke_width, stroke_color=stroke_color, fill_opacity=fill_opacity, + stroke_background=stroke_background ) return result From c51811d2f1bb83ab682990bd15e7be25f1ba3475 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Mon, 21 Mar 2022 22:45:06 +0800 Subject: [PATCH 17/20] Except IndexError for MTex.get_part_by_tex --- manimlib/mobject/svg/mtex_mobject.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/manimlib/mobject/svg/mtex_mobject.py b/manimlib/mobject/svg/mtex_mobject.py index f08dcee7..47a8acfc 100644 --- a/manimlib/mobject/svg/mtex_mobject.py +++ b/manimlib/mobject/svg/mtex_mobject.py @@ -557,7 +557,10 @@ class MTex(_TexSVG): def get_part_by_tex(self, tex, index=0): all_parts = self.get_parts_by_tex(tex) - return all_parts[index] + try: + return all_parts[index] + except IndexError: + return None def set_color_by_tex(self, tex, color): self.get_parts_by_tex(tex).set_color(color) From cabc1322d625b85e2a0f3cc573fdab450540d424 Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Mon, 21 Mar 2022 23:06:47 +0800 Subject: [PATCH 18/20] Clean up code --- manimlib/mobject/svg/mtex_mobject.py | 5 +---- manimlib/mobject/svg/text_mobject.py | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/manimlib/mobject/svg/mtex_mobject.py b/manimlib/mobject/svg/mtex_mobject.py index 47a8acfc..f08dcee7 100644 --- a/manimlib/mobject/svg/mtex_mobject.py +++ b/manimlib/mobject/svg/mtex_mobject.py @@ -557,10 +557,7 @@ class MTex(_TexSVG): def get_part_by_tex(self, tex, index=0): all_parts = self.get_parts_by_tex(tex) - try: - return all_parts[index] - except IndexError: - return None + return all_parts[index] def set_color_by_tex(self, tex, color): self.get_parts_by_tex(tex).set_color(color) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 1fd2a2cf..b2ff42d9 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -449,8 +449,7 @@ class Code(MarkupText): lexer = pygments.lexers.get_lexer_by_name(self.language) formatter = pygments.formatters.PangoMarkupFormatter(style=self.code_style) markup = pygments.highlight(code, lexer, formatter) - markup = markup.replace("", f"") - markup = markup.replace("", "") + markup = markup.replace("", "").replace("", "") super().__init__(markup, **kwargs) From a8c2a9fa3f11c74996d0b8f73a358a82a6bbd95e Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Mon, 21 Mar 2022 23:11:37 +0800 Subject: [PATCH 19/20] Clean up code --- manimlib/mobject/svg/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index b2ff42d9..0e74bbef 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -449,7 +449,7 @@ class Code(MarkupText): lexer = pygments.lexers.get_lexer_by_name(self.language) formatter = pygments.formatters.PangoMarkupFormatter(style=self.code_style) markup = pygments.highlight(code, lexer, formatter) - markup = markup.replace("", "").replace("", "") + markup = re.sub(r"", "", markup) super().__init__(markup, **kwargs) From e5ce0ca28627727223ef824779c9dba36319404a Mon Sep 17 00:00:00 2001 From: YishiMichael Date: Tue, 22 Mar 2022 20:46:35 +0800 Subject: [PATCH 20/20] Reorganize methods --- manimlib/mobject/svg/svg_mobject.py | 70 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/manimlib/mobject/svg/svg_mobject.py b/manimlib/mobject/svg/svg_mobject.py index 2de1ed6b..2e67388b 100644 --- a/manimlib/mobject/svg/svg_mobject.py +++ b/manimlib/mobject/svg/svg_mobject.py @@ -89,7 +89,8 @@ class SVGMobject(VMobject): element_tree = ET.parse(file_path) new_tree = self.modify_xml_tree(element_tree) # Create a temporary svg file to dump modified svg to be parsed - modified_file_path = file_path.replace(".svg", "_.svg") + root, ext = os.path.splitext(file_path) + modified_file_path = root + "_" + ext new_tree.write(modified_file_path) svg = se.SVG.parse(modified_file_path) @@ -148,47 +149,33 @@ class SVGMobject(VMobject): for shape in svg.elements(): if isinstance(shape, se.Group): continue - mob = self.get_mobject_from(shape) - if mob is None: + elif isinstance(shape, se.Path): + mob = self.path_to_mobject(shape) + elif isinstance(shape, se.SimpleLine): + mob = self.line_to_mobject(shape) + elif isinstance(shape, se.Rect): + mob = self.rect_to_mobject(shape) + elif isinstance(shape, se.Circle): + mob = self.circle_to_mobject(shape) + elif isinstance(shape, se.Ellipse): + mob = self.ellipse_to_mobject(shape) + elif isinstance(shape, se.Polygon): + mob = self.polygon_to_mobject(shape) + elif isinstance(shape, se.Polyline): + mob = self.polyline_to_mobject(shape) + # elif isinstance(shape, se.Text): + # mob = self.text_to_mobject(shape) + elif type(shape) == se.SVGElement: continue + else: + log.warning(f"Unsupported element type: {type(shape)}") + continue + self.apply_style_to_mobject(mob, shape) if isinstance(shape, se.Transformable) and shape.apply: self.handle_transform(mob, shape.transform) result.append(mob) return result - @staticmethod - def handle_transform(mob, matrix): - mat = np.array([ - [matrix.a, matrix.c], - [matrix.b, matrix.d] - ]) - vec = np.array([matrix.e, matrix.f, 0.0]) - mob.apply_matrix(mat) - mob.shift(vec) - return mob - - def get_mobject_from(self, shape): - shape_class_to_func_map = { - se.Path: self.path_to_mobject, - se.SimpleLine: self.line_to_mobject, - se.Rect: self.rect_to_mobject, - se.Circle: self.circle_to_mobject, - se.Ellipse: self.ellipse_to_mobject, - se.Polygon: self.polygon_to_mobject, - se.Polyline: self.polyline_to_mobject, - # se.Text: self.text_to_mobject, # TODO - } - for shape_class, func in shape_class_to_func_map.items(): - if isinstance(shape, shape_class): - mob = func(shape) - self.apply_style_to_mobject(mob, shape) - return mob - - shape_class_name = shape.__class__.__name__ - if shape_class_name != "SVGElement": - log.warning(f"Unsupported element type: {shape_class_name}") - return None - @staticmethod def apply_style_to_mobject(mob, shape): mob.set_style( @@ -200,6 +187,17 @@ class SVGMobject(VMobject): ) return mob + @staticmethod + def handle_transform(mob, matrix): + mat = np.array([ + [matrix.a, matrix.c], + [matrix.b, matrix.d] + ]) + vec = np.array([matrix.e, matrix.f, 0.0]) + mob.apply_matrix(mat) + mob.shift(vec) + return mob + def path_to_mobject(self, path): return VMobjectFromSVGPath(path, **self.path_string_config)