2016-04-17 00:31:38 -07:00
|
|
|
from svg_mobject import SVGMobject
|
2015-10-28 17:18:50 -07:00
|
|
|
from helpers import *
|
|
|
|
|
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,
|
2015-10-29 17:00:46 -07:00
|
|
|
"color" : WHITE,
|
2016-04-17 00:31:38 -07:00
|
|
|
"stroke_width" : 1,
|
2015-12-13 15:41:45 -08:00
|
|
|
"should_center" : 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())
|
|
|
|
image_file = tex_to_image_file(
|
|
|
|
self.expression,
|
2015-10-28 17:18:50 -07:00
|
|
|
self.template_tex_file
|
|
|
|
)
|
2016-04-17 00:31:38 -07:00
|
|
|
SVGMobject.__init__(self, image_file, **kwargs)
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2015-12-16 19:49:50 -08:00
|
|
|
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)
|
2015-10-28 17:18:50 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
def tex_hash(expression):
|
|
|
|
return str(hash(expression))
|
2015-10-28 17:18:50 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
def tex_to_image_file(expression, template_tex_file):
|
|
|
|
image_dir = os.path.join(TEX_IMAGE_DIR, tex_hash(expression))
|
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)
|
|
|
|
pdf_file = tex_to_pdf(tex_file)
|
|
|
|
return pdf_to_svg(pdf_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):
|
|
|
|
result = os.path.join(TEX_DIR, tex_hash(expression))+".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 00:31:38 -07:00
|
|
|
def tex_to_pdf(tex_file):
|
|
|
|
result = tex_file.replace(".tex", ".pdf")
|
|
|
|
if not os.path.exists(result) or True:
|
2015-10-28 17:18:50 -07:00
|
|
|
commands = [
|
2016-04-17 00:31:38 -07:00
|
|
|
"pdflatex",
|
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 00:31:38 -07:00
|
|
|
def pdf_to_svg(pdf_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 00:31:38 -07:00
|
|
|
result = pdf_file.replace(".pdf", ".svg")
|
|
|
|
if not os.path.exists(result) or True:
|
2015-10-28 17:18:50 -07:00
|
|
|
commands = [
|
2016-04-17 00:31:38 -07:00
|
|
|
"pdf2svg",
|
|
|
|
pdf_file,
|
|
|
|
result,
|
|
|
|
# "> /dev/null"
|
2015-10-28 17:18:50 -07:00
|
|
|
]
|
|
|
|
os.system(" ".join(commands))
|
2016-04-17 00:31:38 -07:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
# directory, filename = os.path.split(pdf_file)
|
|
|
|
# name = filename.replace(".dvi", "")
|
|
|
|
# images_dir = os.path.join(TEX_IMAGE_DIR, name)
|
|
|
|
# if not os.path.exists(images_dir):
|
|
|
|
# os.mkdir(images_dir)
|
|
|
|
# if os.listdir(images_dir) == [] or regen_if_exists:
|
|
|
|
# commands = [
|
|
|
|
# "convert",
|
|
|
|
# "-density",
|
|
|
|
# str(PDF_DENSITY),
|
|
|
|
# pdf_file,
|
|
|
|
# "-size",
|
|
|
|
# str(DEFAULT_WIDTH) + "x" + str(DEFAULT_HEIGHT),
|
|
|
|
# os.path.join(images_dir, name + ".png")
|
|
|
|
# ]
|
|
|
|
# os.system(" ".join(commands))
|
|
|
|
# return get_sorted_image_list(images_dir)
|
2015-10-28 17:18:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_sorted_image_list(images_dir):
|
|
|
|
return sorted([
|
|
|
|
os.path.join(images_dir, name)
|
|
|
|
for name in os.listdir(images_dir)
|
|
|
|
if name.endswith(".png")
|
|
|
|
], cmp_enumerated_files)
|
|
|
|
|
|
|
|
def cmp_enumerated_files(name1, name2):
|
2015-11-02 14:09:49 -08:00
|
|
|
name1, name2 = [
|
|
|
|
os.path.split(name)[1].replace(".png", "")
|
|
|
|
for name in name1, name2
|
|
|
|
]
|
2015-10-28 17:18:50 -07:00
|
|
|
num1, num2 = [
|
2015-11-02 14:09:49 -08:00
|
|
|
int(name.split("-")[-1])
|
2015-10-28 17:18:50 -07:00
|
|
|
for name in (name1, name2)
|
|
|
|
]
|
|
|
|
return num1 - num2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|