2016-04-17 12:59:53 -07:00
|
|
|
from vectorized_mobject import VMobject
|
2016-04-20 19:24:54 -07:00
|
|
|
from svg_mobject import SVGMobject, VMobjectFromSVGPathstring
|
2016-07-26 12:32:51 -07:00
|
|
|
from topics.geometry import BackgroundRectangle
|
2015-10-28 17:18:50 -07:00
|
|
|
from helpers import *
|
2016-07-21 15:16:49 -07:00
|
|
|
import collections
|
2015-10-28 17:18:50 -07:00
|
|
|
|
2016-08-02 15:50:32 -07:00
|
|
|
TEX_MOB_SCALE_FACTOR = 0.05
|
|
|
|
TEXT_MOB_SCALE_FACTOR = 0.05
|
2016-04-20 19:24:54 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TexSymbol(VMobjectFromSVGPathstring):
|
2016-07-19 11:08:31 -07:00
|
|
|
def pointwise_become_partial(self, mobject, a, b):
|
2016-04-20 19:24:54 -07:00
|
|
|
#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
|
2016-07-19 11:08:31 -07:00
|
|
|
VMobjectFromSVGPathstring.pointwise_become_partial(
|
2016-04-20 19:24:54 -07:00
|
|
|
self, mobject, 0, b
|
|
|
|
)
|
|
|
|
self.set_stroke(width = width)
|
|
|
|
self.set_fill(opacity = opacity)
|
|
|
|
|
2016-04-17 19:29:27 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
class TexMobject(SVGMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-28 17:18:50 -07:00
|
|
|
"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-07-21 11:00:45 -07:00
|
|
|
"separate_list_arg_with_spaces" : True,
|
2016-07-21 15:16:49 -07:00
|
|
|
"enforce_new_line_structure" : False,
|
2016-08-02 15:50:32 -07:00
|
|
|
"initial_scale_factor" : TEX_MOB_SCALE_FACTOR,
|
2016-07-12 15:16:20 -07:00
|
|
|
"organize_left_to_right" : False,
|
2016-04-23 23:36:05 -07:00
|
|
|
"propogate_style_to_family" : True,
|
2015-10-28 17:18:50 -07:00
|
|
|
}
|
|
|
|
def __init__(self, expression, **kwargs):
|
2016-04-17 00:31:38 -07:00
|
|
|
digest_config(self, kwargs, locals())
|
2016-07-21 15:16:49 -07:00
|
|
|
self.is_input_a_list = isinstance(expression, list)
|
2016-04-17 12:59:53 -07:00
|
|
|
VMobject.__init__(self, **kwargs)
|
2016-04-17 19:29:27 -07:00
|
|
|
self.move_into_position()
|
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
|
|
|
|
2016-07-21 15:16:49 -07:00
|
|
|
def handle_input_type(self):
|
|
|
|
if isinstance(self.expression, str):
|
|
|
|
self.is_input_a_list = False
|
|
|
|
elif isinstance(self.expression, collections.Iterable):
|
|
|
|
self.is_input_a_list = True
|
|
|
|
self.expression = list(self.expression)
|
|
|
|
else:
|
|
|
|
raise Exception(
|
|
|
|
"TexMobject was expecting string or list, got " + \
|
|
|
|
str(type(self.expression)) + \
|
|
|
|
" instead."
|
|
|
|
)
|
|
|
|
|
2016-04-20 19:24:54 -07:00
|
|
|
def path_string_to_mobject(self, path_string):
|
|
|
|
#Overwrite superclass default to use
|
|
|
|
#specialized path_string mobject
|
|
|
|
return TexSymbol(path_string)
|
|
|
|
|
|
|
|
|
2016-07-21 11:00:45 -07:00
|
|
|
def generate_points(self):
|
2016-07-21 15:16:49 -07:00
|
|
|
self.svg_file = tex_to_svg_file(
|
|
|
|
self.get_modified_expression(),
|
|
|
|
self.template_tex_file
|
|
|
|
)
|
2016-07-21 11:00:45 -07:00
|
|
|
SVGMobject.generate_points(self)
|
2016-07-21 15:16:49 -07:00
|
|
|
if self.is_input_a_list:
|
2016-07-21 11:00:45 -07:00
|
|
|
self.handle_list_expression(self.expression)
|
|
|
|
|
2016-07-21 15:16:49 -07:00
|
|
|
def get_modified_expression(self):
|
|
|
|
separator = ""
|
|
|
|
if self.is_input_a_list and self.separate_list_arg_with_spaces:
|
|
|
|
separator = " "
|
|
|
|
result = separator.join(self.expression)
|
|
|
|
if self.enforce_new_line_structure:
|
|
|
|
result = result.replace("\n", " \\\\ \n ")
|
|
|
|
return result
|
2016-07-21 11:00:45 -07:00
|
|
|
|
|
|
|
def handle_list_expression(self, list_expression):
|
|
|
|
new_submobjects = []
|
|
|
|
curr_index = 0
|
|
|
|
for expr in list_expression:
|
|
|
|
model = TexMobject(expr, **self.CONFIG)
|
|
|
|
new_index = curr_index + len(model.submobjects)
|
|
|
|
new_submobjects.append(VMobject(
|
|
|
|
*self.submobjects[curr_index:new_index]
|
|
|
|
))
|
|
|
|
curr_index = new_index
|
|
|
|
self.submobjects = new_submobjects
|
2016-04-17 12:59:53 -07:00
|
|
|
return self
|
|
|
|
|
2016-07-12 10:34:35 -07:00
|
|
|
def organize_submobjects_left_to_right(self):
|
2016-04-17 19:29:27 -07:00
|
|
|
self.submobjects.sort(
|
|
|
|
lambda m1, m2 : int((m1.get_left()-m2.get_left())[0])
|
|
|
|
)
|
|
|
|
|
2016-07-22 11:22:31 -07:00
|
|
|
def add_background_rectangle(self, color = BLACK, opacity = 0.75):
|
2016-08-03 16:51:16 -07:00
|
|
|
rect = BackgroundRectangle(
|
|
|
|
self, color = color,
|
|
|
|
fill_opacity = opacity
|
|
|
|
)
|
2016-07-25 16:04:54 -07:00
|
|
|
letters = VMobject(*self.submobjects)
|
2016-07-26 12:32:51 -07:00
|
|
|
self.submobjects = [rect, letters]
|
2016-07-22 11:22:31 -07:00
|
|
|
return self
|
2016-04-20 19:24:54 -07:00
|
|
|
|
2015-10-28 17:18:50 -07:00
|
|
|
class TextMobject(TexMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-28 17:18:50 -07:00
|
|
|
"template_tex_file" : TEMPLATE_TEXT_FILE,
|
2016-08-02 15:50:32 -07:00
|
|
|
"initial_scale_factor" : TEXT_MOB_SCALE_FACTOR,
|
2016-07-21 15:16:49 -07:00
|
|
|
"enforce_new_line_structure" : True,
|
2015-10-28 17:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-21 15:16:49 -07:00
|
|
|
|
2015-12-16 19:49:50 -08:00
|
|
|
class Brace(TexMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-28 17:18:50 -07:00
|
|
|
"buff" : 0.2,
|
|
|
|
}
|
2016-04-23 23:36:05 -07:00
|
|
|
TEX_STRING = "\\underbrace{%s}"%(3*"\\qquad")
|
2015-12-16 19:49:50 -08:00
|
|
|
def __init__(self, mobject, direction = DOWN, **kwargs):
|
2016-08-03 16:51:16 -07:00
|
|
|
digest_config(self, kwargs, locals())
|
2015-12-16 19:49:50 -08:00
|
|
|
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])
|
2016-04-23 23:36:05 -07:00
|
|
|
self.shift(left - self.get_corner(UP+LEFT) + self.buff*DOWN)
|
2015-12-16 19:49:50 -08:00
|
|
|
for mob in mobject, self:
|
|
|
|
mob.rotate(angle)
|
2016-07-19 11:08:31 -07:00
|
|
|
|
2016-08-03 16:51:16 -07:00
|
|
|
def put_at_tip(self, mob, **kwargs):
|
|
|
|
mob.next_to(self, self.direction, **kwargs)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_text(self, text, **kwargs):
|
|
|
|
text_mob = TextMobject(text)
|
|
|
|
self.put_at_tip(text_mob, **kwargs)
|
|
|
|
return text_mob
|
|
|
|
|
|
|
|
|
2015-10-28 17:18:50 -07:00
|
|
|
|
2016-05-03 23:14:40 -07:00
|
|
|
def tex_hash(expression, template_tex_file):
|
|
|
|
return str(hash(expression + template_tex_file))
|
2015-10-28 17:18:50 -07:00
|
|
|
|
2016-04-17 12:59:53 -07:00
|
|
|
def tex_to_svg_file(expression, template_tex_file):
|
2016-05-03 23:14:40 -07:00
|
|
|
image_dir = os.path.join(
|
|
|
|
TEX_IMAGE_DIR,
|
|
|
|
tex_hash(expression, template_tex_file)
|
|
|
|
)
|
2015-10-28 17:18:50 -07:00
|
|
|
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)
|
2015-10-28 17:18:50 -07:00
|
|
|
|
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
def generate_tex_file(expression, template_tex_file):
|
2016-05-03 23:14:40 -07:00
|
|
|
result = os.path.join(
|
|
|
|
TEX_DIR,
|
|
|
|
tex_hash(expression, template_tex_file)
|
|
|
|
)+".tex"
|
2015-10-28 17:18:50 -07:00
|
|
|
if not os.path.exists(result):
|
2016-04-17 00:31:38 -07:00
|
|
|
print "Writing \"%s\" to %s"%(
|
|
|
|
"".join(expression), result
|
2015-10-28 17:18:50 -07:00
|
|
|
)
|
|
|
|
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):
|
2015-10-28 17:18:50 -07:00
|
|
|
commands = [
|
2016-04-17 12:59:53 -07:00
|
|
|
"latex",
|
2015-10-28 17:18:50 -07:00
|
|
|
"-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):
|
2015-10-28 17:18:50 -07:00
|
|
|
"""
|
|
|
|
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):
|
2015-10-28 17:18:50 -07:00
|
|
|
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"
|
2015-10-28 17:18:50 -07:00
|
|
|
]
|
|
|
|
os.system(" ".join(commands))
|
2016-04-17 00:31:38 -07:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2015-10-28 17:18:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|