3b1b-manim/mobject/tex_mobject.py

156 lines
4.1 KiB
Python
Raw Normal View History

2016-04-17 12:59:53 -07:00
from vectorized_mobject import VMobject
2016-04-17 00:31:38 -07:00
from svg_mobject import SVGMobject
from helpers import *
2016-04-17 19:29:27 -07:00
TEX_MOB_SCALE_VAL = 0.05
2016-04-17 00:31:38 -07:00
class TexMobject(SVGMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
"template_tex_file" : TEMPLATE_TEX_FILE,
2016-04-17 12:59:53 -07:00
"stroke_width" : 0,
2016-04-17 19:29:27 -07:00
"fill_opacity" : 1.0,
"fill_color" : WHITE,
2015-12-13 15:41:45 -08:00
"should_center" : True,
2016-04-17 12:59:53 -07:00
"next_to_direction" : RIGHT,
"next_to_buff" : 0.2,
}
def __init__(self, expression, **kwargs):
2016-04-17 00:31:38 -07:00
digest_config(self, kwargs, locals())
2016-04-17 12:59:53 -07:00
VMobject.__init__(self, **kwargs)
2016-04-17 19:29:27 -07:00
self.move_into_position()
self.organize_submobjects()
2016-04-17 12:59:53 -07:00
def generate_points(self):
if isinstance(self.expression, list):
self.handle_list_expression()
else:
self.svg_file = tex_to_svg_file(
"".join(self.expression),
self.template_tex_file
)
SVGMobject.generate_points(self)
def handle_list_expression(self):
#TODO, next_to not sufficient?
subs = [
TexMobject(expr)
for expr in self.expression
]
for sm1, sm2 in zip(subs, subs[1:]):
sm2.next_to(
sm1,
self.next_to_direction,
self.next_to_buff
)
self.submobjects = subs
return self
2016-04-17 19:29:27 -07:00
def organize_submobjects(self):
self.submobjects.sort(
lambda m1, m2 : int((m1.get_left()-m2.get_left())[0])
)
def move_into_position(self):
self.center()
self.scale(TEX_MOB_SCALE_VAL)
self.init_colors()
class TextMobject(TexMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
"template_tex_file" : TEMPLATE_TEXT_FILE,
}
class Brace(TexMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
"buff" : 0.2,
}
TEX_STRING = "\\underbrace{%s}"%(14*"\\quad")
def __init__(self, mobject, direction = DOWN, **kwargs):
TexMobject.__init__(self, self.TEX_STRING, **kwargs)
angle = -np.arctan2(*direction[:2]) + np.pi
mobject.rotate(-angle)
left = mobject.get_corner(DOWN+LEFT)
right = mobject.get_corner(DOWN+RIGHT)
self.stretch_to_fit_width(right[0]-left[0])
self.shift(left - self.points[0] + self.buff*DOWN)
for mob in mobject, self:
mob.rotate(angle)
2016-04-17 00:31:38 -07:00
def tex_hash(expression):
return str(hash(expression))
2016-04-17 12:59:53 -07:00
def tex_to_svg_file(expression, template_tex_file):
2016-04-17 00:31:38 -07:00
image_dir = os.path.join(TEX_IMAGE_DIR, tex_hash(expression))
if os.path.exists(image_dir):
return get_sorted_image_list(image_dir)
2016-04-17 00:31:38 -07:00
tex_file = generate_tex_file(expression, template_tex_file)
2016-04-17 12:59:53 -07:00
dvi_file = tex_to_dvi(tex_file)
return dvi_to_svg(dvi_file)
2016-04-17 00:31:38 -07:00
def generate_tex_file(expression, template_tex_file):
result = os.path.join(TEX_DIR, tex_hash(expression))+".tex"
if not os.path.exists(result):
2016-04-17 00:31:38 -07:00
print "Writing \"%s\" to %s"%(
"".join(expression), result
)
with open(template_tex_file, "r") as infile:
body = infile.read()
body = body.replace(TEX_TEXT_TO_REPLACE, expression)
with open(result, "w") as outfile:
outfile.write(body)
return result
2016-04-17 12:59:53 -07:00
def tex_to_dvi(tex_file):
result = tex_file.replace(".tex", ".dvi")
if not os.path.exists(result):
commands = [
2016-04-17 12:59:53 -07:00
"latex",
"-interaction=batchmode",
"-output-directory=" + TEX_DIR,
tex_file,
"> /dev/null"
]
#TODO, Error check
os.system(" ".join(commands))
return result
2016-04-17 12:59:53 -07:00
def dvi_to_svg(dvi_file, regen_if_exists = False):
"""
Converts a dvi, which potentially has multiple slides, into a
directory full of enumerated pngs corresponding with these slides.
Returns a list of PIL Image objects for these images sorted as they
where in the dvi
"""
2016-04-17 12:59:53 -07:00
result = dvi_file.replace(".dvi", ".svg")
if not os.path.exists(result):
commands = [
2016-04-17 12:59:53 -07:00
"dvisvgm",
dvi_file,
"-n",
"-v",
"0",
"-o",
2016-04-17 00:31:38 -07:00
result,
2016-04-17 12:59:53 -07:00
"> /dev/null"
]
os.system(" ".join(commands))
2016-04-17 00:31:38 -07:00
return result