#!/usr/bin/env python2.7 from twisted.internet import protocol from twisted.internet import reactor import os,re,pdb from kivy.graphics.svg import Svg from kivy.uix.widget import Widget # latex packages from pyx import * class SvgWidget(Widget): def __init__(self, filename, **kwargs): super(SvgWidget, self).__init__(**kwargs) with self.canvas: svg = Svg(filename) self.size = svg.width, svg.height class ProcessProtocolXinput(protocol.ProcessProtocol): output=[] def __init__(self,reactor,canvas,stencilview): self.reactor=reactor self.canvas=canvas self.stencilview=stencilview def connectionMade(self): pass def outReceived(self, data): if __name__ == '__main__': text_file = open("."+os.sep+"seshat_outputs"+os.sep+"out.txt", "w") else: text_file = open("."+os.sep+"canvas"+os.sep+"seshat_outputs"+os.sep+"out.txt", "w") text_file.write(data) text_file.close() self.output.append(data) def errReceived(self, data): print data def inConnectionLost(self): pass def outConnectionLost(self): pass def errConnectionLost(self): pass def processExited(self, reason): pass def processEnded(self, reason): # latex_expression stores the latex expression generated latex_expression=self.output[-1].strip('\n').split('LaTeX:\n')[1] output_path=os.path.join(".","canvas","seshat_outputs") # convert it to a pdf #latex_expression= r"\[{0}\]".format(latex_expression) latex_expression="$"+latex_expression+"$" c = canvas.canvas() c.text(0, 0, latex_expression) pdf_path=os.path.join(output_path,"temp.pdf") c.writePDFfile(pdf_path) # convert pdf to svg svg_path=os.path.join(output_path,"temp.svg") self.reactor.spawnProcess(self, "pdf2svg", ["pdf2svg",pdf_path,svg_path], env=None) # render svg on kivy canvas ### DOESNT WORK print svg_path with open(svg_path,'r') as fp: whole_file_text = fp.read() with open(svg_path,'w') as fp: text_to_write=whole_file_text.replace('pt"','"') print 'pt' in text_to_write fp.write(text_to_write) with open(svg_path,'r') as fp: whole_file_text = fp.read() print 'pt' in whole_file_text svg = SvgWidget(svg_path, size_hint=(None, None)) self.stencilview.add_widget(svg) if __name__ == '__main__': process_protocol_xinput_obj = ProcessProtocolXinput() #./seshat_proj/seshat -c ./seshat_proj/Config/CONFIG -i exp.scgink -o out.inkml -r render.pgm -d out.dot reactor.spawnProcess(process_protocol_xinput_obj, "./seshat", ["seshat","-c","./Config/CONFIG","-i", "exp.scgink","-o","out.inkml","-r","render.pgm","-d","out.dot"], env=None) reactor.run()