#!/usr/bin/env python
# beancurd: an experiment in text rendering.
# http://homepage.mac.com/asagoo/tofu/index.html was the inspiration for this.

import gtk, pango, sys

text = """
I suppose that I could claim that I had always suspected that the world was a cheap and shoddy sham, a bad cover for something deeper and weirder and infinitely more strange, and that, in some way, I already knew the truth. But I think that's just how the world has always been. And even now that I know the truth, as you will, my love, if you're reading this, the world still seems cheap and shoddy. Different world, different shoddy, but that's how it feels.

They say, here's the truth, and I say, is that all there is? And they say, kind of. Pretty much. As far as we know.

So. It was 1977, and the nearest I had come to computers was I'd recently bought a big, expensive calculator, and then I'd lost the manual that came with it, so I didn't know what it did any more. I'd add, subtract, multiply and divide, and was grateful I had no need to cos, sine or find tangents or graph functions or whatever else the gizmo did, because, having been turned down by the RAF, I was working as a bookkeeper for a small discount carpet warehouse in Edgware, in North London, near the top of the Northern Line, and I was sitting at the table at the back of the warehouse that served me as a desk when the world began to melt and drip away.

Honest. It was like the walls and the ceiling and the rolls of carpet and the News of the World Topless Calendar were all made of wax, and they started to ooze and run, to flow together and to drip. I could see the houses and the sky and the clouds and the road behind them, and then that dripped and flowed away, and behind that was blackness.

I was standing in the puddle of the world, a weird, brightly coloured thing that oozed and brimmed and didn't cover the tops of my brown leather shoes (I have feet like shoeboxes. Boots have to be specially made for me. Costs me a fortune). The puddle cast a weird light upwards.
"""

class MainWindow:
	def __init__(self, text):
		self.text = text
		self.margin = 10
		self.column_width = 300
		self.par_indent = 20
		self.width = 640
		self.height = 480

		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_title("Beancurd")
		self.window.connect("delete_event", self.handle_delete)
		self.window.connect("destroy", self.handle_destroy)

		self.area = gtk.DrawingArea()
		self.area.set_size_request(self.width, self.height)
		self.area.connect("expose-event", self.handle_expose)
		self.window.add(self.area)
		self.area.show()

		self.window.show()

		self.colormap = self.area.get_colormap()
		self.drawable = self.area.window
		white = self.colormap.alloc_color(65535, 65535, 65535)
		black = self.colormap.alloc_color(0, 0, 0)
		self.fg_gc = self.drawable.new_gc(foreground = black, background = white)
		self.bg_gc = self.drawable.new_gc(foreground = white, background = black)
		self.layout = self.area.create_pango_layout("")

	def handle_expose(self, widget, event):
		self.drawable.draw_rectangle(self.bg_gc, gtk.TRUE, 0, 0, self.width, self.height)
		self.layout.set_text(self.text)
		self.layout.set_width(self.column_width)
		self.layout.set_indent(self.par_indent)
		self.layout.set_wrap(pango.WRAP_WORD_CHAR)
		self.layout.set_justify(gtk.TRUE)
		self.drawable.draw_layout(self.fg_gc, self.margin, self.margin, self.layout)

	def handle_delete(self, widget, event, data = None):
		return gtk.FALSE

	def handle_destroy(self, widget, data = None):
		gtk.main_quit()

	def main(self, argv):
		gtk.main()

if __name__ == "__main__":
	MainWindow(text).main(sys.argv)
