spacepaste

  1.  
  2. #!/usr/bin/env python2.7
  3. from twisted.internet import protocol
  4. from twisted.internet import reactor
  5. import os,re,pdb
  6. from kivy.graphics.svg import Svg
  7. from kivy.uix.widget import Widget
  8. # latex packages
  9. from pyx import *
  10. class SvgWidget(Widget):
  11. def __init__(self, filename, **kwargs):
  12. super(SvgWidget, self).__init__(**kwargs)
  13. with self.canvas:
  14. svg = Svg(filename)
  15. self.size = svg.width, svg.height
  16. class ProcessProtocolXinput(protocol.ProcessProtocol):
  17. output=[]
  18. def __init__(self,reactor,canvas,stencilview):
  19. self.reactor=reactor
  20. self.canvas=canvas
  21. self.stencilview=stencilview
  22. def connectionMade(self):
  23. pass
  24. def outReceived(self, data):
  25. if __name__ == '__main__':
  26. text_file = open("."+os.sep+"seshat_outputs"+os.sep+"out.txt", "w")
  27. else:
  28. text_file = open("."+os.sep+"canvas"+os.sep+"seshat_outputs"+os.sep+"out.txt", "w")
  29. text_file.write(data)
  30. text_file.close()
  31. self.output.append(data)
  32. def errReceived(self, data):
  33. print data
  34. def inConnectionLost(self):
  35. pass
  36. def outConnectionLost(self):
  37. pass
  38. def errConnectionLost(self):
  39. pass
  40. def processExited(self, reason):
  41. pass
  42. def processEnded(self, reason):
  43. # latex_expression stores the latex expression generated
  44. latex_expression=self.output[-1].strip('\n').split('LaTeX:\n')[1]
  45. output_path=os.path.join(".","canvas","seshat_outputs")
  46. # convert it to a pdf
  47. #latex_expression= r"\[{0}\]".format(latex_expression)
  48. latex_expression="$"+latex_expression+"$"
  49. c = canvas.canvas()
  50. c.text(0, 0, latex_expression)
  51. pdf_path=os.path.join(output_path,"temp.pdf")
  52. c.writePDFfile(pdf_path)
  53. # convert pdf to svg
  54. svg_path=os.path.join(output_path,"temp.svg")
  55. self.reactor.spawnProcess(self, "pdf2svg", ["pdf2svg",pdf_path,svg_path], env=None)
  56. # render svg on kivy canvas
  57. ### DOESNT WORK
  58. print svg_path
  59. with open(svg_path,'r') as fp:
  60. whole_file_text = fp.read()
  61. with open(svg_path,'w') as fp:
  62. text_to_write=whole_file_text.replace('pt"','"')
  63. print 'pt' in text_to_write
  64. fp.write(text_to_write)
  65. with open(svg_path,'r') as fp:
  66. whole_file_text = fp.read()
  67. print 'pt' in whole_file_text
  68. svg = SvgWidget(svg_path, size_hint=(None, None))
  69. self.stencilview.add_widget(svg)
  70. if __name__ == '__main__':
  71. process_protocol_xinput_obj = ProcessProtocolXinput()
  72. #./seshat_proj/seshat -c ./seshat_proj/Config/CONFIG -i exp.scgink -o out.inkml -r render.pgm -d out.dot
  73. reactor.spawnProcess(process_protocol_xinput_obj, "./seshat", ["seshat","-c","./Config/CONFIG","-i",
  74. "exp.scgink","-o","out.inkml","-r","render.pgm","-d","out.dot"], env=None)
  75. reactor.run()
  76.