<s> import py <EOL> from py . __ . process . cmdexec import ExecutionFailed <EOL> format_to_dotargument = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> def ps2eps ( ps ) : <EOL> if not py . path . local . sysfind ( "<STR_LIT>" ) and not py . path . local . sysfind ( "<STR_LIT>" ) : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> try : <EOL> eps = ps . new ( ext = "<STR_LIT>" ) <EOL> py . process . cmdexec ( '<STR_LIT>' % ( ps , eps ) ) <EOL> except ExecutionFailed : <EOL> py . process . cmdexec ( '<STR_LIT>' % ps ) <EOL> def ps2pdf ( ps , compat_level = "<STR_LIT>" ) : <EOL> if not py . path . local . sysfind ( "<STR_LIT>" ) : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> pdf = ps . new ( ext = "<STR_LIT>" ) <EOL> options = dict ( OPTIONS = "<STR_LIT>" % compat_level , <EOL> infile = ps , outfile = pdf ) <EOL> cmd = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % options <EOL> py . process . cmdexec ( cmd ) <EOL> return pdf <EOL> def eps2pdf ( eps ) : <EOL> if not py . path . local . sysfind ( "<STR_LIT>" ) : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> py . process . cmdexec ( '<STR_LIT>' % eps ) <EOL> def dvi2eps ( dvi , dest = None ) : <EOL> if dest is None : <EOL> dest = eps . new ( ext = "<STR_LIT>" ) <EOL> command = '<STR_LIT>' % ( dest , dvi ) <EOL> if not py . path . local . sysfind ( "<STR_LIT>" ) : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> py . process . cmdexec ( command ) <EOL> def convert_dot ( fn , new_extension ) : <EOL> if not py . path . local . sysfind ( "<STR_LIT>" ) : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> result = fn . new ( ext = new_extension ) <EOL> print result <EOL> arg = "<STR_LIT>" % ( format_to_dotargument [ new_extension ] , ) <EOL> py . std . os . system ( '<STR_LIT>' % ( arg , fn , result ) ) <EOL> if new_extension == "<STR_LIT>" : <EOL> ps = result . new ( ext = "<STR_LIT>" ) <EOL> result . move ( ps ) <EOL> ps2eps ( ps ) <EOL> ps . remove ( ) <EOL> elif new_extension == "<STR_LIT>" : <EOL> eps = result . new ( ext = "<STR_LIT>" ) <EOL> ps = result . new ( ext = "<STR_LIT>" ) <EOL> result . move ( ps ) <EOL> ps2eps ( ps ) <EOL> eps2pdf ( eps ) <EOL> ps . remove ( ) <EOL> eps . remove ( ) <EOL> return result <EOL> class latexformula2png ( object ) : <EOL> def __init__ ( self , formula , dest , temp = None ) : <EOL> self . formula = formula <EOL> try : <EOL> import Image <EOL> self . Image = Image <EOL> self . scale = <NUM_LIT:2> <EOL> self . upscale = <NUM_LIT:5> <EOL> except ImportError : <EOL> self . scale = <NUM_LIT:2> <EOL> self . upscale = <NUM_LIT:1> <EOL> self . Image = None <EOL> self . output_format = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:2> ] <EOL> if temp is None : <EOL> temp = py . test . ensuretemp ( "<STR_LIT>" ) <EOL> self . temp = temp <EOL> self . latex = self . temp . join ( '<STR_LIT>' ) <EOL> self . dvi = self . temp . join ( '<STR_LIT>' ) <EOL> self . eps = self . temp . join ( '<STR_LIT>' ) <EOL> self . png = self . temp . join ( '<STR_LIT>' ) <EOL> self . saveas ( dest ) <EOL> def saveas ( self , dest ) : <EOL> self . gen_latex ( ) <EOL> self . gen_dvi ( ) <EOL> dvi2eps ( self . dvi , self . eps ) <EOL> self . gen_png ( ) <EOL> self . scale_image ( ) <EOL> self . png . copy ( dest ) <EOL> def gen_latex ( self ) : <EOL> self . latex . write ( """<STR_LIT>""" % ( self . formula ) ) <EOL> def gen_dvi ( self ) : <EOL> origdir = py . path . local ( ) <EOL> self . temp . chdir ( ) <EOL> py . process . cmdexec ( '<STR_LIT>' % ( self . latex ) ) <EOL> origdir . chdir ( ) <EOL> def gen_png ( self ) : <EOL> tempdir = py . path . local . mkdtemp ( ) <EOL> re_bbox = py . std . re . compile ( '<STR_LIT>' ) <EOL> eps = self . eps . read ( ) <EOL> x1 , y1 , x2 , y2 = [ int ( i ) for i in re_bbox . search ( eps ) . groups ( ) ] <EOL> X = x2 - x1 + <NUM_LIT:2> <EOL> Y = y2 - y1 + <NUM_LIT:2> <EOL> mx = - x1 <EOL> my = - y1 <EOL> ps = self . temp . join ( '<STR_LIT>' ) <EOL> source = self . eps <EOL> ps . write ( """<STR_LIT>""" % locals ( ) ) <EOL> sx = int ( ( x2 - x1 ) * self . scale * self . upscale ) <EOL> sy = int ( ( y2 - y1 ) * self . scale * self . upscale ) <EOL> res = <NUM_LIT> * self . scale * self . upscale <EOL> command = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % ( <EOL> sx , sy , res , res , self . output_format , self . png , ps ) <EOL> py . process . cmdexec ( command ) <EOL> def scale_image ( self ) : <EOL> if self . Image is None : <EOL> return <EOL> image = self . Image . open ( str ( self . png ) ) <EOL> image . resize ( ( image . size [ <NUM_LIT:0> ] / self . upscale , <EOL> image . size [ <NUM_LIT:1> ] / self . upscale ) , <EOL> self . Image . ANTIALIAS ) . save ( str ( self . png ) ) </s>
