#!/usr/bin/env python ## Copyright (c) Valek Filippov, 2007 ## License: GPL v3. import sys import gtk import cairo import math import array ## data = array.array('b','\x80'*8) #vertlines | ## data = array.array('b','\xff\x00\x00\x00\x00\x00\x00\x00') #horlines - ## data = array.array('b','\x80\x40\x20\x10\x08\x04\x02\x01') #diaglines / ## data = array.array('b','\x01\x02\x04\x08\x10\x20\x40\x80') #diaglines \ ## data = array.array('b','\xFF'+'\x80'*7) # pluses +++ data = array.array('b','\x81\x42\x24\x18\x18\x24\x42\x81') # crosses xxx def expose (da, event,cs): lin = cairo.LinearGradient(180,20,300,140) lin.add_color_stop_rgb(0,1,0,1) lin.add_color_stop_rgb(1,0,1,1) ctx = da.window.cairo_create() ## just circle ctx.set_source_rgb(0.4,0,0.5) ctx.arc(60,80,55,0,math.pi*2) ctx.stroke() ## circle filled with pattern from file ctx.save() ctx.arc(150,80,55,0,math.pi*2) ctx.clip() pat = cairo.SurfacePattern(cs) pat.set_extend(1) ctx.set_source(pat) ctx.paint() ctx.restore() ## circle filled with linear gradient and mask from data array ctx.save() ctx.arc(240,80,55,0,math.pi*2) ctx.clip() sur= cairo.ImageSurface.create_for_data(data,3,8,8,1) ctx.set_source(lin) pat = cairo.SurfacePattern(sur) pat.set_extend(1) ctx.mask(pat) ctx.restore() def main(): fname = sys.argv[1] input = open(fname) imagebuf = input.read() pixbufloader = gtk.gdk.PixbufLoader() pixbufloader.write(imagebuf) pixbufloader.close() pixbuf = pixbufloader.get_pixbuf() imgw=pixbuf.get_width() imgh=pixbuf.get_height() cs = cairo.ImageSurface(0,imgw,imgh) ct = cairo.Context(cs) ct2 = gtk.gdk.CairoContext(ct) ct2.set_source_pixbuf(pixbuf,0,0) ct2.paint() win = gtk.Window() win.connect('destroy', gtk.main_quit) win.set_default_size(300, 160) da = gtk.DrawingArea() win.add(da) da.connect('expose_event', expose,cs) win.show_all() gtk.main() if __name__ == '__main__': if len(sys.argv) != 2: program = sys.argv[0] print program +':', 'usage:', program, '' sys.exit(0) else: main()