From 5f6b653803e3b138a2c9337c4a22dead375299c8 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 09:35:13 +0800 Subject: [PATCH 1/8] improve Text's handling of non-display characters --- example_scenes.py | 1 + manimlib/mobject/svg/text_mobject.py | 31 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/example_scenes.py b/example_scenes.py index 999e3830..27bcaf3e 100644 --- a/example_scenes.py +++ b/example_scenes.py @@ -125,6 +125,7 @@ class TextExample(Scene): fonts = Text( "And you can also set the font according to different words", + font="Arial", t2f={"font": "Consolas", "words": "Consolas"}, t2c={"font": BLUE, "words": GREEN} ) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index b1718dae..e5a57c87 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -4,6 +4,7 @@ import copy import hashlib import cairo from manimlib.constants import * +from manimlib.mobject.geometry import Dot, Rectangle from manimlib.mobject.svg.svg_mobject import SVGMobject from manimlib.utils.config_ops import digest_config from manimlib.utils.customization import get_customization @@ -13,9 +14,6 @@ from manimlib.utils.directories import get_text_dir TEXT_MOB_SCALE_FACTOR = 0.001048 -# Warning, these classes are currently based on an old rendering mode -# not supported in this version of manim - class TextSetting(object): def __init__(self, start, end, font, slant, weight, line_num=-1): self.start = start @@ -38,6 +36,7 @@ class Text(SVGMobject): 'lsh': -1, 'size': 1, 'font_size': 48, + 'tab_width': 4, 'slant': NORMAL, 'weight': NORMAL, 't2c': {}, @@ -48,14 +47,19 @@ class Text(SVGMobject): } def __init__(self, text, **config): - self.text = text self.full2short(config) digest_config(self, config) self.lsh = self.size if self.lsh == -1 else 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() self.remove_last_M(file_name) + self.remove_empty_path(file_name) SVGMobject.__init__(self, file_name, **config) + self.text = text + self.apply_space_chars() if self.t2c: self.set_color_by_t2c() @@ -68,6 +72,16 @@ class Text(SVGMobject): if self.height is None: self.scale(TEXT_MOB_SCALE_FACTOR * self.font_size) + def apply_space_chars(self): + 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(redius=0, fill_opacity=0, stroke_opacity=0) + if char_index == 0: + space.move_to(self.submobjects[char_index].get_center()) + else: + space.move_to(self.submobjects[char_index - 1].get_center()) + self.submobjects.insert(char_index, space) + def remove_last_M(self, file_name): with open(file_name, 'r') as fpr: content = fpr.read() @@ -75,6 +89,13 @@ class Text(SVGMobject): 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 find_indexes(self, word): m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word) if m: From 7f27e926f2e45e79cc0319a760f7e585950fdb97 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 09:37:54 +0800 Subject: [PATCH 2/8] replace single quotes with double quotes --- manimlib/mobject/svg/text_mobject.py | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index e5a57c87..cac4355c 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -27,23 +27,23 @@ class TextSetting(object): class Text(SVGMobject): CONFIG = { # Mobject - 'color': WHITE, - 'height': None, - 'stroke_width': 0, + "color": WHITE, + "height": None, + "stroke_width": 0, # Text - 'font': '', - 'gradient': None, - 'lsh': -1, - 'size': 1, - 'font_size': 48, - 'tab_width': 4, - 'slant': NORMAL, - 'weight': NORMAL, - 't2c': {}, - 't2f': {}, - 't2g': {}, - 't2s': {}, - 't2w': {}, + "font": '', + "gradient": None, + "lsh": -1, + "size": 1, + "font_size": 48, + "tab_width": 4, + "slant": NORMAL, + "weight": NORMAL, + "t2c": {}, + "t2f": {}, + "t2g": {}, + "t2s": {}, + "t2w": {}, } def __init__(self, text, **config): From 314c847463aa07892d8d87d4ca095b82228292b3 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 09:40:05 +0800 Subject: [PATCH 3/8] remove unused NOT_SETTING_FONT_MSG --- manimlib/constants.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/manimlib/constants.py b/manimlib/constants.py index 1e24533f..5141194e 100644 --- a/manimlib/constants.py +++ b/manimlib/constants.py @@ -61,24 +61,12 @@ JOINT_TYPE_MAP = { } # Related to Text -NOT_SETTING_FONT_MSG = ''' -Warning: -You haven't set font. -If you are not using English, this may cause text rendering problem. -You set font like: -text = Text('your text', font='your font') -or: -class MyText(Text): - CONFIG = { - 'font': 'My Font' - } -''' START_X = 30 START_Y = 20 -NORMAL = 'NORMAL' -ITALIC = 'ITALIC' -OBLIQUE = 'OBLIQUE' -BOLD = 'BOLD' +NORMAL = "NORMAL" +ITALIC = "ITALIC" +OBLIQUE = "OBLIQUE" +BOLD = "BOLD" DEFAULT_STROKE_WIDTH = 4 From 3d118a5bb6d0dd55111dc8bc496db07d010f26d9 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 10:20:41 +0800 Subject: [PATCH 4/8] change the method of reset submobjects --- manimlib/mobject/svg/text_mobject.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index cac4355c..4cd088a4 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -73,14 +73,17 @@ class Text(SVGMobject): self.scale(TEXT_MOB_SCALE_FACTOR * self.font_size) def apply_space_chars(self): + submobs = self.submobjects.copy() 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(redius=0, fill_opacity=0, stroke_opacity=0) + space = Dot(radius=0, fill_opacity=0, stroke_opacity=0) if char_index == 0: - space.move_to(self.submobjects[char_index].get_center()) + space.move_to(submobs[char_index].get_center()) else: - space.move_to(self.submobjects[char_index - 1].get_center()) - self.submobjects.insert(char_index, space) + space.move_to(submobs[char_index - 1].get_center()) + # self.submobjects.insert(char_index, space) + submobs.insert(char_index, space) + self.set_submobjects(submobs) def remove_last_M(self, file_name): with open(file_name, 'r') as fpr: From f6b5edede29a972c2a1b08d035b37119a0a9d2c0 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 10:21:42 +0800 Subject: [PATCH 5/8] reduce annoying errors --- manimlib/scene/scene.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/manimlib/scene/scene.py b/manimlib/scene/scene.py index 3f0818e2..6a940c8c 100644 --- a/manimlib/scene/scene.py +++ b/manimlib/scene/scene.py @@ -539,9 +539,14 @@ class Scene(object): pass def on_key_press(self, symbol, modifiers): - if chr(symbol) == "r": + try: + char = chr(symbol) + except OverflowError: + print(" Warning: The value of the pressed key is too large.") + return + if char == "r": self.camera.frame.to_default_state() - elif chr(symbol) == "q": + elif char == "q": self.quit_interaction = True def on_resize(self, width: int, height: int): From 453ccc2765beb1144ad061b918b0de6db6cbde15 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 10:23:11 +0800 Subject: [PATCH 6/8] remove unnecessary comment --- manimlib/mobject/svg/text_mobject.py | 1 - 1 file changed, 1 deletion(-) diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 4cd088a4..b15604b1 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -81,7 +81,6 @@ class Text(SVGMobject): space.move_to(submobs[char_index].get_center()) else: space.move_to(submobs[char_index - 1].get_center()) - # self.submobjects.insert(char_index, space) submobs.insert(char_index, space) self.set_submobjects(submobs) From b7bd40a6f6af9ec56237529ba954935c62b51d12 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 10:46:30 +0800 Subject: [PATCH 7/8] make window_position changeable --- custom_defaults.yml | 4 ++++ manimlib/config.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/custom_defaults.yml b/custom_defaults.yml index 7c3d03f6..c2ff3a09 100644 --- a/custom_defaults.yml +++ b/custom_defaults.yml @@ -29,6 +29,10 @@ universal_import_line: "from manimlib.imports import *" style: font: "Consolas" background_color: "#333333" +# Set the position of preview window, you can use directions, e.g. UL/DR/OL/OO/... +# also, you can also specify the position(pixel) of the upper left corner of +# the window on the monitor, e.g. "960,540" +window_position: UR camera_qualities: low: resolution: "854x480" diff --git a/manimlib/config.py b/manimlib/config.py index cc5126ef..d5e74af3 100644 --- a/manimlib/config.py +++ b/manimlib/config.py @@ -203,7 +203,23 @@ def get_configuration(args): else: window_width = monitor.width / 2 window_height = window_width * 9 / 16 - window_position = (int(monitor.width - window_width), 0) + custom_position = custom_defaults["window_position"] + if "," in custom_position: + posx, posy = map(int, custom_position.split(",")) + else: + if custom_position[1] == "L": + posx = 0 + elif custom_position[1] == "O": + posx = int((monitor.width - window_width) / 2) + elif custom_position[1] == "R": + posx = int(monitor.width - window_width) + if custom_position[0] == "U": + posy = 0 + elif custom_position[0] == "O": + posy = int((monitor.height - window_height) / 2) + elif custom_position[0] == "D": + posy = int(monitor.height - window_height) + window_position = (posx, posy) config["window_config"] = { "size": (window_width, window_height), "position": window_position, From cfa8577454c5272ccad3d7103e710e9d17ae1643 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sat, 16 Jan 2021 11:22:34 +0800 Subject: [PATCH 8/8] add PyOpenGL packages in requirements.txt and environment.yml --- environment.yml | 1 + requirements.txt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 96780d9c..5facc09a 100644 --- a/environment.yml +++ b/environment.yml @@ -24,4 +24,5 @@ dependencies: - pyyaml - validators - ipython + - PyOpenGL diff --git a/requirements.txt b/requirements.txt index 1507d768..3adf46ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,4 +15,5 @@ pyyaml screeninfo pyreadline; sys_platform == 'win32' validators -ipython \ No newline at end of file +ipython +PyOpenGL \ No newline at end of file