Skip to content

Commit 3c29748

Browse files
aliPMPAINTaliPMPAINT
authored andcommitted
Merge remote-tracking branch 'upstream/master'
2 parents 46686c6 + 2304505 commit 3c29748

File tree

3 files changed

+67
-74
lines changed

3 files changed

+67
-74
lines changed

manim/mobject/svg/tex_mobject.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ class TexSymbol(VMobjectFromSVGPathstring):
3838

3939

4040
class SingleStringMathTex(SVGMobject):
41+
"""Elementary building block for rendering text with LaTeX.
42+
43+
Tests
44+
-----
45+
Check that creating a :class:`~.SingleStringMathTex` object works::
46+
47+
>>> SingleStringMathTex('Test')
48+
SingleStringMathTex('Test')
49+
"""
50+
4151
CONFIG = {
4252
"stroke_width": 0,
4353
"fill_opacity": 1.0,
@@ -68,6 +78,9 @@ def __init__(self, tex_string, **kwargs):
6878
if self.organize_left_to_right:
6979
self.organize_submobjects_left_to_right()
7080

81+
def __repr__(self):
82+
return f"{type(self).__name__}({repr(self.tex_string)})"
83+
7184
def get_modified_expression(self, tex_string):
7285
result = self.alignment + " " + tex_string
7386
result = result.strip()
@@ -152,19 +165,25 @@ def organize_submobjects_left_to_right(self):
152165

153166

154167
class MathTex(SingleStringMathTex):
155-
"""
156-
A class for displaying mathematical formulas with Latex syntax.
157-
168+
"""A string compiled with LaTeX in math mode.
158169
159170
Examples
160171
--------
161-
.. manim:: Formula1
172+
.. manim:: Formula
162173
:save_last_frame:
163174
164-
class Formula1(Scene):
175+
class Formula(Scene):
165176
def construct(self):
166177
t = MathTex(r"\int_a^b f'(x) dx = f(b)- f(a)")
167178
self.add(t)
179+
180+
Tests
181+
-----
182+
Check that creating a :class:`~.MathTex` works::
183+
184+
>>> MathTex('a^2 + b^2 = c^2')
185+
MathTex('a^2 + b^2 = c^2')
186+
168187
"""
169188

170189
CONFIG = {
@@ -276,6 +295,18 @@ def sort_alphabetically(self):
276295

277296

278297
class Tex(MathTex):
298+
r"""A string compiled with LaTeX in normal mode.
299+
300+
Tests
301+
-----
302+
303+
Check whether writing a LaTeX string works::
304+
305+
>>> Tex('The horse does not eat cucumber salad.')
306+
Tex('The horse does not eat cucumber salad.')
307+
308+
"""
309+
279310
CONFIG = {
280311
"alignment": "\\centering",
281312
"arg_separator": "",

manim/mobject/svg/text_mobject.py

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ class CairoText(SVGMobject):
8383
`weird <https://github.com/3b1b/manim/issues/1067>`_. Consider using
8484
:meth:`remove_invisible_chars` to resolve this issue.
8585
86+
Tests
87+
-----
8688
89+
Check whether writing text works::
8790
91+
>>> Text('The horse does not eat cucumber salad.')
92+
Text('The horse does not eat cucumber salad.')
8893
8994
"""
9095

@@ -158,6 +163,9 @@ def __init__(self, text, **config):
158163
if self.height is None and self.width is None:
159164
self.scale(TEXT_MOB_SCALE_FACTOR)
160165

166+
def __repr__(self):
167+
return f"Text({repr(self.original_text)})"
168+
161169
def gen_chars(self):
162170
chars = VGroup()
163171
submobjects_char_index = 0
@@ -299,9 +307,13 @@ def text2svg(self):
299307
line_spacing = self.line_spacing * 10
300308

301309
if self.font == "":
302-
if NOT_SETTING_FONT_MSG != "":
303-
print(NOT_SETTING_FONT_MSG)
310+
if NOT_SETTING_FONT_MSG:
311+
logger.warning(NOT_SETTING_FONT_MSG)
312+
304313
dir_name = file_writer_config["text_dir"]
314+
if not os.path.exists(dir_name):
315+
os.makedirs(dir_name)
316+
305317
hash_name = self.text2hash()
306318
file_name = os.path.join(dir_name, hash_name) + ".svg"
307319
if os.path.exists(file_name):
@@ -333,71 +345,6 @@ def text2svg(self):
333345
return file_name
334346

335347

336-
# Following class is just a Little implementation of upcomming feautures. Ignore it for now.
337-
class TextWithBackground(CairoText):
338-
CONFIG = {
339-
"background_color": BLACK,
340-
}
341-
342-
def __init__(self, text, **config):
343-
Text.__init__(self, text, **config)
344-
# self.text_backgrounds = self.gen_text_backgrounds(text)
345-
346-
def gen_text_backgrounds(self, text):
347-
text_with_visible_chars = text.replace(" ", "").replace("\t", "")
348-
text_list = text_with_visible_chars.split("\n")
349-
text_backgrounds = VGroup()
350-
start_i = 0
351-
for line_no in range(text_list.__len__()):
352-
rect_counts = len(text_list[line_no])
353-
text_backgrounds.add(*self[start_i:rect_counts])
354-
start_i += 2 * rect_counts
355-
text_backgrounds.set_color(self.background_color)
356-
357-
return text_backgrounds
358-
359-
def text2svg1(self):
360-
# anti-aliasing
361-
size = self.size * 10
362-
line_spacing = self.line_spacing * 10
363-
364-
if self.font == "":
365-
if NOT_SETTING_FONT_MSG != "":
366-
logger.warning(NOT_SETTING_FONT_MSG)
367-
dir_name = consts.TEXT_DIR
368-
hash_name = self.text2hash()
369-
file_name = os.path.join(dir_name, hash_name) + ".svg"
370-
# if os.path.exists(file_name):
371-
# return file_name
372-
373-
surface = cairo.SVGSurface(file_name, 600, 400)
374-
context = cairo.Context(surface)
375-
context.set_font_size(size)
376-
context.move_to(START_X, START_Y)
377-
378-
settings = self.text2settings()
379-
offset_x = 0
380-
last_line_num = 0
381-
for setting in settings:
382-
font = setting.font
383-
slant = self.str2slant(setting.slant)
384-
weight = self.str2weight(setting.weight)
385-
text = self.text[setting.start : setting.end].replace("\n", " ")
386-
context.select_font_face(font, slant, weight)
387-
if setting.line_num != last_line_num:
388-
offset_x = 0
389-
last_line_num = setting.line_num
390-
tempx = START_X + offset_x
391-
tempy = START_Y + line_spacing * setting.line_num
392-
char_offset_x = 0
393-
char_height = tempy - size / 2 - (line_spacing - size)
394-
context.move_to(tempx, tempy)
395-
context.show_text(text)
396-
offset_x += context.text_extents(text)[4]
397-
surface.finish()
398-
return file_name
399-
400-
401348
class Paragraph(VGroup):
402349
r"""Display a paragraph of text.
403350
@@ -634,6 +581,14 @@ def construct(self):
634581
self.play(Write(morning))
635582
self.wait(2)
636583
584+
Tests
585+
-----
586+
587+
Check that the creation of :class:`~.PangoText` works::
588+
589+
>>> PangoText('The horse does not eat cucumber salad.')
590+
Text('The horse does not eat cucumber salad.')
591+
637592
.. WARNING::
638593
639594
Using a :class:`.Transform` on text with leading whitespace can look
@@ -711,6 +666,9 @@ def __init__(self, text: str, **config): # pylint: disable=redefined-outer-name
711666
if self.height is None and self.width is None:
712667
self.scale(TEXT_MOB_SCALE_FACTOR)
713668

669+
def __repr__(self):
670+
return f"Text({repr(self.original_text)})"
671+
714672
def remove_last_M(self, file_name: str): # pylint: disable=invalid-name
715673
"""Internally used. Use to format the rendered SVG files."""
716674
with open(file_name, "r") as fpr:
@@ -922,7 +880,7 @@ class Text(CairoText):
922880
Text objects behave like a :class:`.VGroup`-like iterable of all characters
923881
in the given text. In particular, slicing is possible.
924882
925-
Examples
883+
Examples
926884
--------
927885
.. manim:: Example1Text
928886
:save_last_frame:

manim/utils/tex_file_writing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ def generate_tex_file(expression, tex_template, source_type):
3737
elif source_type == "tex":
3838
output = tex_template.get_text_for_tex_mode(expression)
3939

40-
result = os.path.join(file_writer_config["tex_dir"], tex_hash(output)) + ".tex"
40+
tex_dir = file_writer_config["tex_dir"]
41+
if not os.path.exists(tex_dir):
42+
os.makedirs(tex_dir)
43+
44+
result = os.path.join(tex_dir, tex_hash(output)) + ".tex"
4145
if not os.path.exists(result):
4246
logger.info('Writing "%s" to %s' % ("".join(expression), result))
4347
with open(result, "w", encoding="utf-8") as outfile:

0 commit comments

Comments
 (0)