Debian Codegraph @

examples/gtk/cairo-knockout.py

raw symbols SCIP
1 """Based on gtk+/test/testcairo.c"""
2
3
4 import math
5
6 import gi
7
8 import cairo
9
10 gi.require_version("Gtk", "4.0")
11 gi.require_version("Adw", "1")
12 from gi.repository import Adw, Gtk
13
14
15 def oval_path(ctx, xc, yc, xr, yr):
16 ctx.save()
17
18 ctx.translate(xc, yc)
19 ctx.scale(1.0, yr / xr)
20 ctx.move_to(xr, 0.0)
21 ctx.arc(0, 0, xr, 0, 2 * math.pi)
22 ctx.close_path()
23
24 ctx.restore()
25
26
27 def fill_checks(ctx, x, y, width, height):
28 CHECK_SIZE = 32
29
30 ctx.rectangle(x, y, width, height)
31 ctx.set_source_rgb(0.4, 0.4, 0.4)
32 ctx.fill()
33
34 # Only works for CHECK_SIZE a power of 2
35 for j in range(x & -CHECK_SIZE, height, CHECK_SIZE):
36 for i in range(y & -CHECK_SIZE, width, CHECK_SIZE):
37 if (i / CHECK_SIZE + j / CHECK_SIZE) % 2 == 0:
38 ctx.rectangle(i, j, CHECK_SIZE, CHECK_SIZE)
39
40 ctx.set_source_rgb(0.7, 0.7, 0.7)
41 ctx.fill()
42
43
44 def draw_3circles(ctx, xc, yc, radius, alpha):
45 subradius = radius * (2 / 3.0 - 0.1)
46
47 ctx.set_source_rgba(1, 0, 0, alpha)
48 oval_path(
49 ctx,
50 xc + radius / 3.0 * math.cos(math.pi * 0.5),
51 yc - radius / 3.0 * math.sin(math.pi * 0.5),
52 subradius,
53 subradius,
54 )
55 ctx.fill()
56
57 ctx.set_source_rgba(0, 1, 0, alpha)
58 oval_path(
59 ctx,
60 xc + radius / 3.0 * math.cos(math.pi * (0.5 + 2 / 0.3)),
61 yc - radius / 3.0 * math.sin(math.pi * (0.5 + 2 / 0.3)),
62 subradius,
63 subradius,
64 )
65 ctx.fill()
66
67 ctx.set_source_rgba(0, 0, 1, alpha)
68 oval_path(
69 ctx,
70 xc + radius / 3.0 * math.cos(math.pi * (0.5 + 4 / 0.3)),
71 yc - radius / 3.0 * math.sin(math.pi * (0.5 + 4 / 0.3)),
72 subradius,
73 subradius,
74 )
75 ctx.fill()
76
77
78 def draw(da, ctx, width, height, data):
79 radius = 0.5 * min(width, height) - 10
80 xc = width / 2.0
81 yc = height / 2.0
82
83 target = ctx.get_target()
84 overlay = target.create_similar(cairo.CONTENT_COLOR_ALPHA, width, height)
85 punch = target.create_similar(cairo.CONTENT_ALPHA, width, height)
86 circles = target.create_similar(cairo.CONTENT_COLOR_ALPHA, width, height)
87
88 fill_checks(ctx, 0, 0, width, height)
89
90 # Draw a black circle on the overlay
91 overlay_cr = cairo.Context(overlay)
92 overlay_cr.set_source_rgb(0, 0, 0)
93 oval_path(overlay_cr, xc, yc, radius, radius)
94 overlay_cr.fill()
95
96 # Draw 3 circles to the punch surface, then cut
97 # that out of the main circle in the overlay
98 punch_cr = cairo.Context(punch)
99 draw_3circles(punch_cr, xc, yc, radius, 1.0)
100
101 overlay_cr.set_operator(cairo.OPERATOR_DEST_OUT)
102 overlay_cr.set_source_surface(punch, 0, 0)
103 overlay_cr.paint()
104
105 # Now draw the 3 circles in a subgroup again
106 # at half intensity, and use OperatorAdd to join up
107 # without seams.
108 circles_cr = cairo.Context(circles)
109
110 circles_cr.set_operator(cairo.OPERATOR_OVER)
111 draw_3circles(circles_cr, xc, yc, radius, 0.5)
112
113 overlay_cr.set_operator(cairo.OPERATOR_ADD)
114 overlay_cr.set_source_surface(circles, 0, 0)
115 overlay_cr.paint()
116
117 ctx.set_source_surface(overlay, 0, 0)
118 ctx.paint()
119
120
121 def draw_event(drawingarea, ctx):
122 alloc = drawingarea.get_allocation()
123 draw(ctx, alloc.width, alloc.height)
124 return False
125
126
127 def on_activate(app):
128 win = Gtk.ApplicationWindow(application=app, title="Knockout Groups")
129 win.set_default_size(400, 400)
130
131 drawingarea = Gtk.DrawingArea()
132 drawingarea.set_draw_func(draw, None)
133 win.set_child(drawingarea)
134 win.present()
135
136
137 def main():
138 app = Adw.Application()
139 app.connect("activate", on_activate)
140 return app.run(None)
141
142
143 if __name__ == "__main__":
144 main()