3b1b-manim/mobject/tex_mobject.py
2016-07-19 14:37:18 -07:00

190 lines
5.1 KiB
Python

from vectorized_mobject import VMobject
from svg_mobject import SVGMobject, VMobjectFromSVGPathstring
from helpers import *
TEX_MOB_SCALE_VAL = 0.05
TEXT_MOB_SCALE_VAL = 0.05
class TexSymbol(VMobjectFromSVGPathstring):
def pointwise_become_partial(self, mobject, a, b):
#TODO, this assumes a = 0
if b < 0.5:
b = 2*b
width = 1
opacity = 0
else:
width = 2 - 2*b
opacity = 2*b - 1
b = 1
VMobjectFromSVGPathstring.pointwise_become_partial(
self, mobject, 0, b
)
self.set_stroke(width = width)
self.set_fill(opacity = opacity)
class TexMobject(SVGMobject):
CONFIG = {
"template_tex_file" : TEMPLATE_TEX_FILE,
"stroke_width" : 0,
"fill_opacity" : 1.0,
"fill_color" : WHITE,
"should_center" : True,
"next_to_direction" : RIGHT,
"next_to_buff" : 0.25,
"initial_scale_val" : TEX_MOB_SCALE_VAL,
"organize_left_to_right" : False,
"propogate_style_to_family" : True,
}
def __init__(self, expression, **kwargs):
digest_config(self, kwargs, locals())
VMobject.__init__(self, **kwargs)
self.move_into_position()
if self.organize_left_to_right:
self.organize_submobjects_left_to_right()
def path_string_to_mobject(self, path_string):
#Overwrite superclass default to use
#specialized path_string mobject
return TexSymbol(path_string)
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
]
self.initial_scale_val = 1
for sm1, sm2 in zip(subs, subs[1:]):
sm2.next_to(
sm1,
self.next_to_direction,
buff = self.next_to_buff
)
self.submobjects = subs
return self
def organize_submobjects_left_to_right(self):
self.submobjects.sort(
lambda m1, m2 : int((m1.get_left()-m2.get_left())[0])
)
class TextMobject(TexMobject):
CONFIG = {
"template_tex_file" : TEMPLATE_TEXT_FILE,
"initial_scale_val" : TEXT_MOB_SCALE_VAL
}
class Brace(TexMobject):
CONFIG = {
"buff" : 0.2,
}
TEX_STRING = "\\underbrace{%s}"%(3*"\\qquad")
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.get_corner(UP+LEFT) + self.buff*DOWN)
for mob in mobject, self:
mob.rotate(angle)
def tex_hash(expression, template_tex_file):
return str(hash(expression + template_tex_file))
def tex_to_svg_file(expression, template_tex_file):
image_dir = os.path.join(
TEX_IMAGE_DIR,
tex_hash(expression, template_tex_file)
)
if os.path.exists(image_dir):
return get_sorted_image_list(image_dir)
tex_file = generate_tex_file(expression, template_tex_file)
dvi_file = tex_to_dvi(tex_file)
return dvi_to_svg(dvi_file)
def generate_tex_file(expression, template_tex_file):
result = os.path.join(
TEX_DIR,
tex_hash(expression, template_tex_file)
)+".tex"
if not os.path.exists(result):
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
def tex_to_dvi(tex_file):
result = tex_file.replace(".tex", ".dvi")
if not os.path.exists(result):
commands = [
"latex",
"-interaction=batchmode",
"-output-directory=" + TEX_DIR,
tex_file,
"> /dev/null"
]
#TODO, Error check
os.system(" ".join(commands))
return result
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
"""
result = dvi_file.replace(".dvi", ".svg")
if not os.path.exists(result):
commands = [
"dvisvgm",
dvi_file,
"-n",
"-v",
"0",
"-o",
result,
"> /dev/null"
]
os.system(" ".join(commands))
return result