cairo-demo.py 2.52 KB
Newer Older
1 2
#!/usr/bin/env python
"""
3 4 5
Based on cairo-demo/X11/cairo-demo.c
"""

6
import cairo
7
from gi.repository import Gtk
8 9 10

SIZE = 30

11

12 13
def triangle(ctx):
    ctx.move_to(SIZE, 0)
14 15
    ctx.rel_line_to(SIZE, 2 * SIZE)
    ctx.rel_line_to(-2 * SIZE, 0)
16 17
    ctx.close_path()

18

19 20
def square(ctx):
    ctx.move_to(0, 0)
21 22 23
    ctx.rel_line_to(2 * SIZE, 0)
    ctx.rel_line_to(0, 2 * SIZE)
    ctx.rel_line_to(-2 * SIZE, 0)
24 25
    ctx.close_path()

26

27 28
def bowtie(ctx):
    ctx.move_to(0, 0)
29 30 31
    ctx.rel_line_to(2 * SIZE, 2 * SIZE)
    ctx.rel_line_to(-2 * SIZE, 0)
    ctx.rel_line_to(2 * SIZE, -2 * SIZE)
32 33
    ctx.close_path()

34

35 36
def inf(ctx):
    ctx.move_to(0, SIZE)
37 38
    ctx.rel_curve_to(0, SIZE, SIZE, SIZE, 2 * SIZE, 0)
    ctx.rel_curve_to(SIZE, -SIZE, 2 * SIZE, -SIZE, 2 * SIZE, 0)
39 40
    ctx.rel_curve_to(0, SIZE, -SIZE, SIZE, - 2 * SIZE, 0)
    ctx.rel_curve_to(-SIZE, -SIZE, - 2 * SIZE, -SIZE, - 2 * SIZE, 0)
41 42
    ctx.close_path()

43

44 45 46 47
def draw_shapes(ctx, x, y, fill):
    ctx.save()

    ctx.new_path()
48
    ctx.translate(x + SIZE, y + SIZE)
49 50 51 52 53 54 55
    bowtie(ctx)
    if fill:
        ctx.fill()
    else:
        ctx.stroke()

    ctx.new_path()
56
    ctx.translate(3 * SIZE, 0)
57 58 59 60 61 62 63
    square(ctx)
    if fill:
        ctx.fill()
    else:
        ctx.stroke()

    ctx.new_path()
64
    ctx.translate(3 * SIZE, 0)
65 66 67 68 69 70 71
    triangle(ctx)
    if fill:
        ctx.fill()
    else:
        ctx.stroke()

    ctx.new_path()
72
    ctx.translate(3 * SIZE, 0)
73 74 75 76 77 78 79 80
    inf(ctx)
    if fill:
        ctx.fill()
    else:
        ctx.stroke()

    ctx.restore()

81

82 83 84
def fill_shapes(ctx, x, y):
    draw_shapes(ctx, x, y, True)

85

86 87 88
def stroke_shapes(ctx, x, y):
    draw_shapes(ctx, x, y, False)

89

90
def draw(da, ctx):
91 92 93 94 95 96
    ctx.set_source_rgb(0, 0, 0)

    ctx.set_line_width(SIZE / 4)
    ctx.set_tolerance(0.1)

    ctx.set_line_join(cairo.LINE_JOIN_ROUND)
97
    ctx.set_dash([SIZE / 4.0, SIZE / 4.0], 0)
98 99 100
    stroke_shapes(ctx, 0, 0)

    ctx.set_dash([], 0)
101
    stroke_shapes(ctx, 0, 3 * SIZE)
102 103

    ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
104
    stroke_shapes(ctx, 0, 6 * SIZE)
105 106

    ctx.set_line_join(cairo.LINE_JOIN_MITER)
107
    stroke_shapes(ctx, 0, 9 * SIZE)
108

109
    fill_shapes(ctx, 0, 12 * SIZE)
110 111

    ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
112 113 114 115
    fill_shapes(ctx, 0, 15 * SIZE)
    ctx.set_source_rgb(1, 0, 0)
    stroke_shapes(ctx, 0, 15 * SIZE)

116 117 118

def main():
    win = Gtk.Window()
119
    win.connect('destroy', lambda w: Gtk.main_quit())
120 121 122 123
    win.set_default_size(450, 550)

    drawingarea = Gtk.DrawingArea()
    win.add(drawingarea)
124
    drawingarea.connect('draw', draw)
125 126 127 128

    win.show_all()
    Gtk.main()

Christoph Reiter's avatar
Christoph Reiter committed
129

130 131
if __name__ == '__main__':
    main()