From b276618981d2aaf0077001db087ec23947560092 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Wed, 30 Sep 2020 00:06:23 +0200 Subject: [PATCH 01/10] remove TextWithBackground --- manim/mobject/svg/text_mobject.py | 65 ------------------------------- 1 file changed, 65 deletions(-) diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index 8e69b37f63..6efa2da54a 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -324,71 +324,6 @@ def text2svg(self): return file_name -# Following class is just a Little implementation of upcomming feautures. Ignore it for now. -class TextWithBackground(Text): - CONFIG = { - "background_color": BLACK, - } - - def __init__(self, text, **config): - Text.__init__(self, text, **config) - # self.text_backgrounds = self.gen_text_backgrounds(text) - - def gen_text_backgrounds(self, text): - text_with_visible_chars = text.replace(" ", "").replace("\t", "") - text_list = text_with_visible_chars.split("\n") - text_backgrounds = VGroup() - start_i = 0 - for line_no in range(text_list.__len__()): - rect_counts = len(text_list[line_no]) - text_backgrounds.add(*self[start_i:rect_counts]) - start_i += 2 * rect_counts - text_backgrounds.set_color(self.background_color) - - return text_backgrounds - - def text2svg1(self): - # anti-aliasing - size = self.size * 10 - line_spacing = self.line_spacing * 10 - - if self.font == "": - if NOT_SETTING_FONT_MSG != "": - logger.warning(NOT_SETTING_FONT_MSG) - dir_name = consts.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 - - surface = cairo.SVGSurface(file_name, 600, 400) - context = cairo.Context(surface) - context.set_font_size(size) - context.move_to(START_X, START_Y) - - settings = self.text2settings() - offset_x = 0 - last_line_num = 0 - for setting in settings: - font = setting.font - slant = self.str2slant(setting.slant) - weight = self.str2weight(setting.weight) - text = self.text[setting.start : setting.end].replace("\n", " ") - context.select_font_face(font, slant, weight) - if setting.line_num != last_line_num: - offset_x = 0 - last_line_num = setting.line_num - tempx = START_X + offset_x - tempy = START_Y + line_spacing * setting.line_num - char_offset_x = 0 - char_height = tempy - size / 2 - (line_spacing - size) - context.move_to(tempx, tempy) - context.show_text(text) - offset_x += context.text_extents(text)[4] - surface.finish() - return file_name - - class Paragraph(VGroup): r"""Display a paragraph of text. From 83500e3cda9f3ad3f61eb7c12e311c24b15cebc7 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Wed, 30 Sep 2020 00:08:15 +0200 Subject: [PATCH 02/10] let Tex and Text create dirs if they don't exist --- manim/mobject/svg/text_mobject.py | 4 ++++ manim/utils/tex_file_writing.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index 6efa2da54a..562d3e064d 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -292,7 +292,11 @@ def text2svg(self): if self.font == "": if NOT_SETTING_FONT_MSG != "": print(NOT_SETTING_FONT_MSG) + dir_name = file_writer_config["text_dir"] + 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): diff --git a/manim/utils/tex_file_writing.py b/manim/utils/tex_file_writing.py index e1b527eb64..ae1641b7b8 100644 --- a/manim/utils/tex_file_writing.py +++ b/manim/utils/tex_file_writing.py @@ -34,6 +34,10 @@ def generate_tex_file(expression, tex_template, source_type): elif source_type == "tex": output = tex_template.get_text_for_tex_mode(expression) + tex_dir = file_writer_config["tex_dir"] + if not os.path.exists(tex_dir): + os.makedirs(tex_dir) + result = os.path.join(file_writer_config["tex_dir"], tex_hash(output)) + ".tex" if not os.path.exists(result): logger.info('Writing "%s" to %s' % ("".join(expression), result)) From bd33b042b33e61513be4ef823840cdef2ede8ea5 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Wed, 30 Sep 2020 00:42:52 +0200 Subject: [PATCH 03/10] add two doctests --- manim/mobject/svg/tex_mobject.py | 11 +++++++++++ manim/mobject/svg/text_mobject.py | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py index 9cb359740f..c4b99c45c7 100644 --- a/manim/mobject/svg/tex_mobject.py +++ b/manim/mobject/svg/tex_mobject.py @@ -254,6 +254,17 @@ def sort_alphabetically(self): class Tex(MathTex): + r"""A string compiled with LaTeX in normal mode. + + TESTS + ----- + + Check whether writing a LaTeX string works:: + + >>> Tex("The horse does not eat cucumber salad.") + + + """ CONFIG = { "alignment": "\\centering", "arg_separator": "", diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index 562d3e064d..77d9b28eeb 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -77,6 +77,14 @@ class Text(SVGMobject): `weird `_. Consider using :meth:`remove_invisible_chars` to resolve this issue. + TESTS + ----- + + Check whether writing text works:: + + >>> Text("The horse does not eat cucumber salad.") + + """ CONFIG = { From 7732d26774948cf6358800954de555166a9c49f0 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Wed, 30 Sep 2020 00:43:07 +0200 Subject: [PATCH 04/10] change print to logger.warning --- manim/mobject/svg/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index 77d9b28eeb..f8a712af79 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -299,7 +299,7 @@ def text2svg(self): if self.font == "": if NOT_SETTING_FONT_MSG != "": - print(NOT_SETTING_FONT_MSG) + logger.warning(NOT_SETTING_FONT_MSG) dir_name = file_writer_config["text_dir"] if not os.path.exists(dir_name): From 58a403206059475b0ab597cc7fe2a3db9a65bcb7 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Wed, 30 Sep 2020 08:48:45 +0200 Subject: [PATCH 05/10] Update manim/utils/tex_file_writing.py Co-authored-by: Naveen M K --- manim/utils/tex_file_writing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/utils/tex_file_writing.py b/manim/utils/tex_file_writing.py index ae1641b7b8..772ef488ab 100644 --- a/manim/utils/tex_file_writing.py +++ b/manim/utils/tex_file_writing.py @@ -38,7 +38,7 @@ def generate_tex_file(expression, tex_template, source_type): if not os.path.exists(tex_dir): os.makedirs(tex_dir) - result = os.path.join(file_writer_config["tex_dir"], tex_hash(output)) + ".tex" + result = os.path.join(tex_dir, tex_hash(output)) + ".tex" if not os.path.exists(result): logger.info('Writing "%s" to %s' % ("".join(expression), result)) with open(result, "w", encoding="utf-8") as outfile: From f6a51c522e50c660701695a28de2fec6ae83b502 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Thu, 8 Oct 2020 01:13:13 +0200 Subject: [PATCH 06/10] add lightweight __repr__ and some doctests --- manim/mobject/svg/tex_mobject.py | 30 +++++++++++++++++++++++++++--- manim/mobject/svg/text_mobject.py | 9 ++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py index c4b99c45c7..91647435fb 100644 --- a/manim/mobject/svg/tex_mobject.py +++ b/manim/mobject/svg/tex_mobject.py @@ -38,6 +38,16 @@ class TexSymbol(VMobjectFromSVGPathstring): class SingleStringMathTex(SVGMobject): + """Elementary building block for rendering text with LaTeX. + + Tests + ----- + Check that creating a :class:`~.SingleStringMathTex` object works:: + + >>> SingleStringMathTex('Test') + SingleStringMathTex('Test') + """ + CONFIG = { "stroke_width": 0, "fill_opacity": 1.0, @@ -61,6 +71,9 @@ def __init__(self, tex_string, **kwargs): if self.organize_left_to_right: self.organize_submobjects_left_to_right() + def __repr__(self): + return f"{type(self).__name__}({repr(self.tex_string)})" + def get_modified_expression(self, tex_string): result = self.alignment + " " + tex_string result = result.strip() @@ -145,6 +158,16 @@ def organize_submobjects_left_to_right(self): class MathTex(SingleStringMathTex): + """A string compiled with LaTeX in math mode. + + Tests + ----- + Check that creating a :cls:`~.MathTex` works:: + + >>> MathTex('a^2 + b^2 = c^2') + MathTex('a^2 + b^2 = c^2') + """ + CONFIG = { "arg_separator": " ", "substrings_to_isolate": [], @@ -256,15 +279,16 @@ def sort_alphabetically(self): class Tex(MathTex): r"""A string compiled with LaTeX in normal mode. - TESTS + Tests ----- Check whether writing a LaTeX string works:: - >>> Tex("The horse does not eat cucumber salad.") - + >>> Tex('The horse does not eat cucumber salad.') + Tex('The horse does not eat cucumber salad.') """ + CONFIG = { "alignment": "\\centering", "arg_separator": "", diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index f8a712af79..44f847db17 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -77,13 +77,13 @@ class Text(SVGMobject): `weird `_. Consider using :meth:`remove_invisible_chars` to resolve this issue. - TESTS + Tests ----- Check whether writing text works:: - >>> Text("The horse does not eat cucumber salad.") - + >>> Text('The horse does not eat cucumber salad.') + Text('The horse does not eat cucumber salad.') """ @@ -157,6 +157,9 @@ def __init__(self, text, **config): if self.height is None and self.width is None: self.scale(TEXT_MOB_SCALE_FACTOR) + def __repr__(self): + return f"Text({repr(self.original_text)})" + def gen_chars(self): chars = VGroup() submobjects_char_index = 0 From 3dcd125b547b641ecc9d9f0e58c8198d5b99bc1d Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Thu, 8 Oct 2020 01:18:59 +0200 Subject: [PATCH 07/10] fix a reference in docstring --- manim/mobject/svg/tex_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py index 91647435fb..27c523937b 100644 --- a/manim/mobject/svg/tex_mobject.py +++ b/manim/mobject/svg/tex_mobject.py @@ -162,7 +162,7 @@ class MathTex(SingleStringMathTex): Tests ----- - Check that creating a :cls:`~.MathTex` works:: + Check that creating a :class:`~.MathTex` works:: >>> MathTex('a^2 + b^2 = c^2') MathTex('a^2 + b^2 = c^2') From 804a565bb2a812791f24ce11271441b8fb6cdd70 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Thu, 8 Oct 2020 08:33:34 +0200 Subject: [PATCH 08/10] Update manim/mobject/svg/text_mobject.py Co-authored-by: Leo Torres --- manim/mobject/svg/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index 44f847db17..b6208dfa84 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -301,7 +301,7 @@ def text2svg(self): line_spacing = self.line_spacing * 10 if self.font == "": - if NOT_SETTING_FONT_MSG != "": + if NOT_SETTING_FONT_MSG: logger.warning(NOT_SETTING_FONT_MSG) dir_name = file_writer_config["text_dir"] From 6e50d94357169ef9e3897aa6e4a725dc0fbb8c56 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 12 Oct 2020 12:30:57 +0200 Subject: [PATCH 09/10] add repr and a doctest to PangoText --- manim/mobject/svg/text_mobject.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index fc172d31a3..1e6516ff2f 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -558,6 +558,14 @@ def construct(self): self.play(Write(morning)) self.wait(2) + Tests + ----- + + Check that the creation of :class:`~.PangoText` works:: + + >>> PangoText('The horse does not eat cucumber salad.') + Text('The horse does not eat cucumber salad.') + .. WARNING:: Using a :class:`.Transform` on text with leading whitespace can look @@ -635,6 +643,9 @@ def __init__(self, text: str, **config): # pylint: disable=redefined-outer-name if self.height is None and self.width is None: self.scale(TEXT_MOB_SCALE_FACTOR) + def __repr__(self): + return f"Text({repr(self.original_text)})" + def remove_last_M(self, file_name: str): # pylint: disable=invalid-name """Internally used. Use to format the rendered SVG files.""" with open(file_name, "r") as fpr: From 076526196aace2fc5a143da42d2398575f585780 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 12 Oct 2020 20:04:06 +0200 Subject: [PATCH 10/10] fix sphinx warning --- manim/mobject/svg/text_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py index 41acc8798b..906be3d176 100644 --- a/manim/mobject/svg/text_mobject.py +++ b/manim/mobject/svg/text_mobject.py @@ -880,7 +880,7 @@ class Text(CairoText): Text objects behave like a :class:`.VGroup`-like iterable of all characters in the given text. In particular, slicing is possible. - Examples + Examples -------- .. manim:: Example1Text :save_last_frame: