3b1b-manim/manimlib/mobject/svg/tex_mobject.py

352 lines
11 KiB
Python
Raw Normal View History

from functools import reduce
import operator as op
import re
import itertools as it
2018-08-09 17:56:05 -07:00
from manimlib.constants import *
from manimlib.mobject.geometry import Line
from manimlib.mobject.svg.svg_mobject import SVGMobject
2021-01-13 00:11:27 -10:00
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.tex_file_writing import tex_to_svg_file
from manimlib.utils.tex_file_writing import get_tex_config
from manimlib.utils.tex_file_writing import display_during_execution
SCALE_FACTOR_PER_FONT_POINT = 0.001
tex_string_to_mob_map = {}
2016-04-20 19:24:54 -07:00
class SingleStringTex(VMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
"fill_opacity": 1.0,
"stroke_width": 0,
"should_center": True,
"font_size": 48,
"height": None,
"organize_left_to_right": False,
"alignment": "\\centering",
"math_mode": True,
}
def __init__(self, tex_string, **kwargs):
2021-01-13 00:11:27 -10:00
super().__init__(**kwargs)
assert(isinstance(tex_string, str))
2018-05-05 19:49:25 -07:00
self.tex_string = tex_string
if tex_string not in tex_string_to_mob_map:
2021-01-13 00:11:27 -10:00
full_tex = self.get_tex_file_body(tex_string)
filename = tex_to_svg_file(full_tex)
svg_mob = SVGMobject(
filename,
height=None,
path_string_config={
"should_subdivide_sharp_curves": True,
"should_remove_null_curves": True,
}
)
tex_string_to_mob_map[tex_string] = svg_mob
2021-01-13 00:11:27 -10:00
self.add(*(
sm.copy()
for sm in tex_string_to_mob_map[tex_string]
2021-01-13 00:11:27 -10:00
))
self.init_colors()
2021-01-13 00:11:27 -10:00
if self.height is None:
self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size)
2016-07-12 10:34:35 -07:00
if self.organize_left_to_right:
self.organize_submobjects_left_to_right()
2016-04-17 12:59:53 -07:00
def get_tex_file_body(self, tex_string):
new_tex = self.get_modified_expression(tex_string)
if self.math_mode:
new_tex = "\\begin{align*}\n" + new_tex + "\n\\end{align*}"
tex_config = get_tex_config()
return tex_config["tex_body"].replace(
tex_config["text_to_replace"],
new_tex
)
def get_modified_expression(self, tex_string):
result = self.alignment + " " + tex_string
2016-09-10 17:35:15 -07:00
result = result.strip()
2017-04-20 13:30:51 -07:00
result = self.modify_special_strings(result)
2016-07-21 15:16:49 -07:00
return result
2017-04-20 13:30:51 -07:00
def modify_special_strings(self, tex):
tex = tex.strip()
2018-05-08 16:15:28 -07:00
should_add_filler = reduce(op.or_, [
# Fraction line needs something to be over
tex == "\\over",
tex == "\\overline",
# Makesure sqrt has overbar
tex == "\\sqrt",
tex == "\\sqrt{",
2018-05-08 16:15:28 -07:00
# Need to add blank subscript or superscript
tex.endswith("_"),
tex.endswith("^"),
tex.endswith("dot"),
2018-05-08 16:15:28 -07:00
])
if should_add_filler:
filler = "{\\quad}"
tex += filler
if tex == "\\substack":
tex = "\\quad"
if tex == "":
tex = "\\quad"
# To keep files from starting with a line break
if tex.startswith("\\\\"):
tex = tex.replace("\\\\", "\\quad\\\\")
tex = self.balance_braces(tex)
# Handle imbalanced \left and \right
num_lefts, num_rights = [
2018-08-26 13:17:34 -07:00
len([
s for s in tex.split(substr)[1:]
if s and s[0] in "(){}[]|.\\"
])
for substr in ("\\left", "\\right")
]
if num_lefts != num_rights:
tex = tex.replace("\\left", "\\big")
tex = tex.replace("\\right", "\\big")
for context in ["array"]:
begin_in = ("\\begin{%s}" % context) in tex
end_in = ("\\end{%s}" % context) in tex
if begin_in ^ end_in:
# Just turn this into a blank string,
# which means caller should leave a
# stray \\begin{...} with other symbols
tex = ""
2017-04-20 13:30:51 -07:00
return tex
def balance_braces(self, tex):
"""
Makes Tex resiliant to unmatched { at start
"""
num_lefts, num_rights = [tex.count(char) for char in "{}"]
while num_rights > num_lefts:
2018-05-08 16:15:28 -07:00
tex = "{" + tex
num_lefts += 1
while num_lefts > num_rights:
2018-05-08 16:15:28 -07:00
tex = tex + "}"
num_rights += 1
return tex
2021-01-07 12:14:51 -08:00
def get_tex(self):
2016-08-23 13:38:33 -07:00
return self.tex_string
def organize_submobjects_left_to_right(self):
self.sort(lambda p: p[0])
return self
class Tex(SingleStringTex):
CONFIG = {
"arg_separator": " ",
# Note, use of isolate is largely rendered
# moot by the fact that you can surround such strings in
# {{ and }} as needed.
"isolate": [],
"tex_to_color_map": {},
}
def __init__(self, *tex_strings, **kwargs):
digest_config(self, kwargs)
self.tex_strings = self.break_up_tex_strings(tex_strings)
full_string = self.arg_separator.join(self.tex_strings)
with display_during_execution(f" Writing \"{full_string}\""):
super().__init__(full_string, **kwargs)
self.break_up_by_substrings()
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
if self.organize_left_to_right:
self.organize_submobjects_left_to_right()
def break_up_tex_strings(self, tex_strings):
# Separate out anything surrounded in double braces
patterns = ["{{", "}}"]
# Separate out any strings specified in the isolate
# or tex_to_color_map lists.
patterns.extend([
"({})".format(re.escape(ss))
for ss in it.chain(self.isolate, self.tex_to_color_map.keys())
])
pattern = "|".join(patterns)
pieces = []
for s in tex_strings:
pieces.extend(re.split(pattern, s))
return list(filter(lambda s: s, pieces))
def break_up_by_substrings(self):
"""
Reorganize existing submojects one layer
deeper based on the structure of tex_strings (as a list
of tex_strings)
"""
if len(self.tex_strings) == 1:
submob = self.copy()
self.set_submobjects([submob])
return self
new_submobjects = []
curr_index = 0
config = dict(self.CONFIG)
config["alignment"] = ""
2018-05-05 19:49:25 -07:00
for tex_string in self.tex_strings:
tex_string = tex_string.strip()
if len(tex_string) == 0:
continue
sub_tex_mob = SingleStringTex(tex_string, **config)
num_submobs = len(sub_tex_mob)
if num_submobs == 0:
continue
new_index = curr_index + num_submobs
sub_tex_mob.set_submobjects(self[curr_index:new_index])
new_submobjects.append(sub_tex_mob)
curr_index = new_index
self.set_submobjects(new_submobjects)
2016-04-17 12:59:53 -07:00
return self
def get_parts_by_tex(self, tex, substring=True, case_sensitive=True):
2017-03-10 16:55:23 -08:00
def test(tex1, tex2):
if not case_sensitive:
tex1 = tex1.lower()
tex2 = tex2.lower()
if substring:
return tex1 in tex2
else:
return tex1 == tex2
2017-03-22 15:06:17 -07:00
2021-01-04 16:01:04 -08:00
return VGroup(*filter(
lambda m: isinstance(m, SingleStringTex) and test(tex, m.get_tex()),
2021-01-04 16:01:04 -08:00
self.submobjects
))
2017-03-22 15:06:17 -07:00
def get_part_by_tex(self, tex, **kwargs):
all_parts = self.get_parts_by_tex(tex, **kwargs)
return all_parts[0] if all_parts else None
2017-03-10 16:55:23 -08:00
2018-03-30 11:51:31 -07:00
def set_color_by_tex(self, tex, color, **kwargs):
self.get_parts_by_tex(tex, **kwargs).set_color(color)
2016-08-18 12:54:04 -07:00
return self
def set_color_by_tex_to_color_map(self, tex_to_color_map, **kwargs):
for tex, color in list(tex_to_color_map.items()):
self.set_color_by_tex(tex, color, **kwargs)
return self
2021-01-04 16:01:04 -08:00
def index_of_part(self, part, start=0):
return self.submobjects.index(part, start)
2017-05-05 11:19:10 -07:00
2021-01-04 16:01:04 -08:00
def index_of_part_by_tex(self, tex, start=0, **kwargs):
2017-05-05 11:19:10 -07:00
part = self.get_part_by_tex(tex, **kwargs)
2021-01-04 16:01:04 -08:00
return self.index_of_part(part, start)
def slice_by_tex(self, start_tex=None, stop_tex=None, **kwargs):
if start_tex is None:
start_index = 0
else:
start_index = self.index_of_part_by_tex(start_tex, **kwargs)
if stop_tex is None:
return self[start_index:]
else:
stop_index = self.index_of_part_by_tex(stop_tex, start=start_index, **kwargs)
return self[start_index:stop_index]
2017-05-05 11:19:10 -07:00
def sort_alphabetically(self):
2021-01-07 12:14:51 -08:00
self.submobjects.sort(key=lambda m: m.get_tex())
2021-01-04 16:01:04 -08:00
def set_bstroke(self, color=BLACK, width=4):
self.set_stroke(color, width, background=True)
return self
class TexText(Tex):
2016-02-27 16:32:53 -08:00
CONFIG = {
"math_mode": False,
"arg_separator": "",
}
class BulletedList(TexText):
2017-10-19 14:31:55 -07:00
CONFIG = {
"buff": MED_LARGE_BUFF,
"dot_scale_factor": 2,
"alignment": "",
2017-10-19 14:31:55 -07:00
}
2017-10-19 14:31:55 -07:00
def __init__(self, *items, **kwargs):
line_separated_items = [s + "\\\\" for s in items]
TexText.__init__(self, *line_separated_items, **kwargs)
2017-10-19 14:31:55 -07:00
for part in self:
dot = Tex("\\cdot").scale(self.dot_scale_factor)
2017-10-19 14:31:55 -07:00
dot.next_to(part[0], LEFT, SMALL_BUFF)
part.add_to_back(dot)
2019-02-04 14:54:25 -08:00
self.arrange(
DOWN,
aligned_edge=LEFT,
buff=self.buff
2017-10-19 14:31:55 -07:00
)
def fade_all_but(self, index_or_string, opacity=0.5):
2017-10-19 14:31:55 -07:00
arg = index_or_string
if isinstance(arg, str):
part = self.get_part_by_tex(arg)
elif isinstance(arg, int):
part = self.submobjects[arg]
else:
raise Exception("Expected int or string, got {0}".format(arg))
for other_part in self.submobjects:
if other_part is part:
other_part.set_fill(opacity=1)
2017-10-19 14:31:55 -07:00
else:
other_part.set_fill(opacity=opacity)
2017-10-19 14:31:55 -07:00
2018-05-02 17:17:34 +02:00
class TexFromPresetString(Tex):
2018-05-08 16:15:28 -07:00
CONFIG = {
# To be filled by subclasses
"tex": None,
"color": None,
}
2018-05-02 17:17:34 +02:00
def __init__(self, **kwargs):
digest_config(self, kwargs)
Tex.__init__(self, self.tex, **kwargs)
2018-05-02 17:17:34 +02:00
self.set_color(self.color)
2018-05-09 14:05:32 -07:00
class Title(TexText):
2018-05-09 14:05:32 -07:00
CONFIG = {
"scale_factor": 1,
"include_underline": True,
"underline_width": FRAME_WIDTH - 2,
# This will override underline_width
"match_underline_width_to_text": False,
"underline_buff": MED_SMALL_BUFF,
}
2018-09-20 10:39:19 -07:00
def __init__(self, *text_parts, **kwargs):
TexText.__init__(self, *text_parts, **kwargs)
2018-05-09 14:05:32 -07:00
self.scale(self.scale_factor)
self.to_edge(UP)
if self.include_underline:
underline = Line(LEFT, RIGHT)
underline.next_to(self, DOWN, buff=self.underline_buff)
if self.match_underline_width_to_text:
underline.match_width(self)
else:
underline.set_width(self.underline_width)
2018-05-09 14:05:32 -07:00
self.add(underline)
self.underline = underline