1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
// Network-friendly drawing application based on XRender.
//
// TODO
// - use double buffering to remove flicker
// (more pronounced over X11 forwarding)
// - maybe keep the pixmap as large as the window
package main
import (
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/render"
"github.com/BurntSushi/xgb/xproto"
"log"
)
func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }
func findPictureFormat(formats []render.Pictforminfo,
depth byte, direct render.Directformat) render.Pictformat {
for _, pf := range formats {
if pf.Depth == depth && pf.Direct == direct {
return pf.Id
}
}
return 0
}
func main() {
X, err := xgb.NewConn()
if err != nil {
log.Fatalln(err)
}
if err := render.Init(X); err != nil {
log.Fatalln(err)
}
setup := xproto.Setup(X)
screen := setup.DefaultScreen(X)
visual, depth := screen.RootVisual, screen.RootDepth
if depth < 24 {
log.Fatalln("need more colors")
}
wid, err := xproto.NewWindowId(X)
if err != nil {
log.Fatalln(err)
}
_ = xproto.CreateWindow(X, depth, wid, screen.Root,
0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
visual, xproto.CwBackPixel|xproto.CwEventMask,
[]uint32{0xffffffff, xproto.EventMaskButtonPress |
xproto.EventMaskButtonMotion | xproto.EventMaskButtonRelease |
xproto.EventMaskStructureNotify | xproto.EventMaskExposure})
title := []byte("Draw")
_ = xproto.ChangeProperty(X, xproto.PropModeReplace, wid, xproto.AtomWmName,
xproto.AtomString, 8, uint32(len(title)), title)
_ = xproto.MapWindow(X, wid)
pformats, err := render.QueryPictFormats(X).Reply()
if err != nil {
log.Fatalln(err)
}
// Find appropriate picture formats.
var pformat, pformatAlpha, pformatRGB render.Pictformat
for _, pd := range pformats.Screens[X.DefaultScreen].Depths {
for _, pv := range pd.Visuals {
if pv.Visual == visual {
pformat = pv.Format
}
}
}
if pformatAlpha = findPictureFormat(pformats.Formats, 8,
render.Directformat{
AlphaShift: 0,
AlphaMask: 0xff,
}); pformat == 0 {
log.Fatalln("required picture format not found")
}
if pformatRGB = findPictureFormat(pformats.Formats, 24,
render.Directformat{
RedShift: 16,
RedMask: 0xff,
GreenShift: 8,
GreenMask: 0xff,
BlueShift: 0,
BlueMask: 0xff,
}); pformatRGB == 0 {
log.Fatalln("required picture format not found")
}
// Picture for the window.
pid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
render.CreatePicture(X, pid, xproto.Drawable(wid), pformat, 0, []uint32{})
// Brush shape.
const brushRadius = 5
brushid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
cFull := render.Color{0xffff, 0xffff, 0xffff, 0xffff}
cTrans := render.Color{0xffff, 0xffff, 0xffff, 0}
_ = render.CreateRadialGradient(X, brushid,
render.Pointfix{F64ToFixed(brushRadius), F64ToFixed(brushRadius)},
render.Pointfix{F64ToFixed(brushRadius), F64ToFixed(brushRadius)},
F64ToFixed(0),
F64ToFixed(brushRadius),
3, []render.Fixed{F64ToFixed(0), F64ToFixed(0.1), F64ToFixed(1)},
[]render.Color{cFull, cFull, cTrans})
// Brush color.
colorid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
_ = render.CreateSolidFill(X, colorid, render.Color{
Red: 0xeeee,
Green: 0x8888,
Blue: 0x4444,
Alpha: 0xffff,
})
// Pixmaps.
const (
pixWidth = 1000
pixHeight = 1000
)
pixid, err := xproto.NewPixmapId(X)
if err != nil {
log.Fatalln(err)
}
_ = xproto.CreatePixmap(X, 24, pixid, xproto.Drawable(screen.Root),
pixWidth, pixHeight)
pixpictid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
render.CreatePicture(X, pixpictid, xproto.Drawable(pixid),
pformatRGB, 0, []uint32{})
pixmaskid, err := xproto.NewPixmapId(X)
if err != nil {
log.Fatalln(err)
}
_ = xproto.CreatePixmap(X, 8, pixmaskid, xproto.Drawable(screen.Root),
pixWidth, pixHeight)
pixmaskpictid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
render.CreatePicture(X, pixmaskpictid, xproto.Drawable(pixmaskid),
pformatAlpha, 0, []uint32{})
// Pixmaps come uninitialized.
render.FillRectangles(X,
render.PictOpSrc, pixpictid, render.Color{
Red: 0xffff, Green: 0xffff, Blue: 0xffff, Alpha: 0xffff,
}, []xproto.Rectangle{{Width: pixWidth, Height: pixHeight}})
// This is the only method we can use to render proper brush strokes.
//
// ConjointOver is defined as: A = Aa * 1 + Ab * max(1-Aa/Ab,0)
// which basically resolves to: A = max(Aa, Ab)
// which equals "lighten" with one channel only.
//
// Resources:
// - https://www.cairographics.org/operators/
// - http://ssp.impulsetrain.com/porterduff.html
// - https://keithp.com/~keithp/talks/renderproblems/renderproblems/render-title.html
// - https://keithp.com/~keithp/talks/cairo2003.pdf
drawPointAt := func(x, y int16) {
_ = render.Composite(X, render.PictOpConjointOver,
brushid, render.PictureNone, pixmaskpictid,
0, 0, 0, 0, x-brushRadius, y-brushRadius,
brushRadius*2, brushRadius*2)
_ = render.SetPictureClipRectangles(X, pid,
x-brushRadius, y-brushRadius, []xproto.Rectangle{
{Width: brushRadius * 2, Height: brushRadius * 2}})
_ = render.Composite(X, render.PictOpSrc,
pixpictid, render.PictureNone, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight)
_ = render.Composite(X, render.PictOpOver,
colorid, pixmaskpictid, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight)
}
// Integer version of Bresenham's line drawing algorithm
drawLine := func(x0, y0, x1, y1 int16) {
dx, dy := x1-x0, y1-y0
if dx < 0 {
dx = -dx
}
if dy < 0 {
dy = -dy
}
steep := dx < dy
if steep {
// Flip the coordinate system on input
x0, y0 = y0, x0
x1, y1 = y1, x1
dx, dy = dy, dx
}
var stepX, stepY int16 = 1, 1
if x0 > x1 {
stepX = -1
}
if y0 > y1 {
stepY = -1
}
dpr := dy * 2
delta := dpr - dx
dpru := delta - dx
for ; dx > 0; dx-- {
// Unflip the coordinate system on output
if steep {
drawPointAt(y0, x0)
} else {
drawPointAt(x0, y0)
}
x0 += stepX
if delta > 0 {
y0 += stepY
delta += dpru
} else {
delta += dpr
}
}
}
var startX, startY int16 = 0, 0
drawing := false
for {
ev, xerr := X.WaitForEvent()
if xerr != nil {
log.Printf("Error: %s\n", xerr)
return
}
if ev == nil {
return
}
log.Printf("Event: %s\n", ev)
switch e := ev.(type) {
case xproto.UnmapNotifyEvent:
return
case xproto.ExposeEvent:
_ = render.SetPictureClipRectangles(X, pid, int16(e.X), int16(e.Y),
[]xproto.Rectangle{{Width: e.Width, Height: e.Height}})
_ = render.Composite(X, render.PictOpSrc,
pixpictid, render.PictureNone, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight)
if drawing {
_ = render.Composite(X, render.PictOpOver,
colorid, pixmaskpictid, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight)
}
case xproto.ButtonPressEvent:
if e.Detail == xproto.ButtonIndex1 {
render.FillRectangles(X,
render.PictOpSrc, pixmaskpictid, render.Color{},
[]xproto.Rectangle{{Width: pixWidth, Height: pixHeight}})
drawing = true
drawPointAt(e.EventX, e.EventY)
startX, startY = e.EventX, e.EventY
}
case xproto.MotionNotifyEvent:
if drawing {
drawLine(startX, startY, e.EventX, e.EventY)
startX, startY = e.EventX, e.EventY
}
case xproto.ButtonReleaseEvent:
if e.Detail == xproto.ButtonIndex1 {
_ = render.Composite(X, render.PictOpOver,
colorid, pixmaskpictid, pixpictid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight)
drawing = false
}
}
}
}
|