1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
### MATHTEX ##########################################################################################
# Author: Tom De Smedt, Cedric Foellmi.
# Copyright (c) 2008 by Tom De Smedt, Cedric Foellmi.
# See LICENSE.txt for details.
from url import URLAccumulator
from urllib import quote
from cache import Cache
def clear_cache():
Cache("mathtex").clear()
class mathTeX(URLAccumulator):
""" The mathTeX server returns a GIF or PNG-image for a LaTeX math expression.
http://www.forkosh.com/mathtex.html
"""
def __init__(self, eq, type="png", dpi=120, color="", wait=10, asynchronous=False):
eq = "\\"+type+" "+eq
eq = "\dpi{"+str(dpi)+"} " + eq
if color:
eq = "\usepackage{color} \color{"+color+"} " + eq
print eq
url = "http://www.forkosh.dreamhost.com/mathtex.cgi?"+quote(eq)
URLAccumulator.__init__(self, url, wait, asynchronous, "mathtex", type="."+type, throttle=1)
def load(self, data):
# Provide the path to the image stored in cache.
self.image = self._cache.hash(self.url)
def gif(eq, dpi=120, color=""):
return mathTeX(eq, type="gif", dpi=dpi, color=color).image
def png(eq, dpi=120, color=""):
return mathTeX(eq, type="png", dpi=dpi, color=color).image
def png300(eq, color=""):
return png(eq, color, 300)
#eq = "E = hf = \frac{hc}{\lambda} \,\! "
#image(gif(eq), 10, 10)
|