/
draw.go
233 lines (196 loc) · 7.47 KB
/
draw.go
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
package gl
import (
"image/color"
"math"
"github.com/Jaeel/fyne"
"github.com/Jaeel/fyne/canvas"
"github.com/Jaeel/fyne/internal/painter"
)
func (p *glPainter) drawTextureWithDetails(o fyne.CanvasObject, creator func(canvasObject fyne.CanvasObject) Texture,
pos fyne.Position, size, frame fyne.Size, fill canvas.ImageFill, alpha float32, pad float32) {
texture := p.getTexture(o, creator)
if texture == NoTexture {
return
}
aspect := float32(0)
if img, ok := o.(*canvas.Image); ok {
aspect = painter.GetAspect(img)
if aspect == 0 {
aspect = 1 // fallback, should not occur - normally an image load error
}
}
points := p.rectCoords(size, pos, frame, fill, aspect, pad)
vbo := p.glCreateBuffer(points)
p.glDrawTexture(texture, alpha)
p.glFreeBuffer(vbo)
}
func (p *glPainter) drawCircle(circle *canvas.Circle, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(circle, p.newGlCircleTexture, pos, circle.Size(), frame, canvas.ImageFillStretch,
1.0, painter.VectorPad(circle))
}
func (p *glPainter) drawLine(line *canvas.Line, pos fyne.Position, frame fyne.Size) {
if line.StrokeColor == color.Transparent || line.StrokeColor == nil || line.StrokeWidth == 0 {
return
}
points, halfWidth, feather := p.lineCoords(pos, line.Position1, line.Position2, line.StrokeWidth, 0.5, frame)
vbo := p.glCreateLineBuffer(points)
p.glDrawLine(halfWidth, line.StrokeColor, feather)
p.glFreeBuffer(vbo)
}
func (p *glPainter) drawImage(img *canvas.Image, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(img, p.newGlImageTexture, pos, img.Size(), frame, img.FillMode, float32(img.Alpha()), 0)
}
func (p *glPainter) drawRaster(img *canvas.Raster, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(img, p.newGlRasterTexture, pos, img.Size(), frame, canvas.ImageFillStretch, float32(img.Alpha()), 0)
}
func (p *glPainter) drawGradient(o fyne.CanvasObject, texCreator func(fyne.CanvasObject) Texture, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(o, texCreator, pos, o.Size(), frame, canvas.ImageFillStretch, 1.0, 0)
}
func (p *glPainter) drawRectangle(rect *canvas.Rectangle, pos fyne.Position, frame fyne.Size) {
if (rect.FillColor == color.Transparent || rect.FillColor == nil) && (rect.StrokeColor == color.Transparent || rect.StrokeColor == nil || rect.StrokeWidth == 0) {
return
}
p.drawTextureWithDetails(rect, p.newGlRectTexture, pos, rect.Size(), frame, canvas.ImageFillStretch,
1.0, painter.VectorPad(rect))
}
func (p *glPainter) drawText(text *canvas.Text, pos fyne.Position, frame fyne.Size) {
if text.Text == "" || text.Text == " " {
return
}
size := text.MinSize()
containerSize := text.Size()
switch text.Alignment {
case fyne.TextAlignTrailing:
pos = fyne.NewPos(pos.X+containerSize.Width-size.Width, pos.Y)
case fyne.TextAlignCenter:
pos = fyne.NewPos(pos.X+(containerSize.Width-size.Width)/2, pos.Y)
}
if containerSize.Height > size.Height {
pos = fyne.NewPos(pos.X, pos.Y+(containerSize.Height-size.Height)/2)
}
p.drawTextureWithDetails(text, p.newGlTextTexture, pos, size, frame, canvas.ImageFillStretch, 1.0, 0)
}
func (p *glPainter) drawObject(o fyne.CanvasObject, pos fyne.Position, frame fyne.Size) {
if !o.Visible() {
return
}
switch obj := o.(type) {
case *canvas.Circle:
p.drawCircle(obj, pos, frame)
case *canvas.Line:
p.drawLine(obj, pos, frame)
case *canvas.Image:
p.drawImage(obj, pos, frame)
case *canvas.Raster:
p.drawRaster(obj, pos, frame)
case *canvas.Rectangle:
p.drawRectangle(obj, pos, frame)
case *canvas.Text:
p.drawText(obj, pos, frame)
case *canvas.LinearGradient:
p.drawGradient(obj, p.newGlLinearGradientTexture, pos, frame)
case *canvas.RadialGradient:
p.drawGradient(obj, p.newGlRadialGradientTexture, pos, frame)
}
}
func (p *glPainter) lineCoords(pos, pos1, pos2 fyne.Position, lineWidth, feather float32, frame fyne.Size) ([]float32, float32, float32) {
// Shift line coordinates so that they match the target position.
xPosDiff := pos.X - fyne.Min(pos1.X, pos2.X)
yPosDiff := pos.Y - fyne.Min(pos1.Y, pos2.Y)
pos1.X = roundToPixel(pos1.X+xPosDiff, p.pixScale)
pos1.Y = roundToPixel(pos1.Y+yPosDiff, p.pixScale)
pos2.X = roundToPixel(pos2.X+xPosDiff, p.pixScale)
pos2.Y = roundToPixel(pos2.Y+yPosDiff, p.pixScale)
if lineWidth <= 1 {
offset := float32(0.5) // adjust location for lines < 1pt on regular display
if lineWidth <= 0.5 && p.pixScale > 1 { // and for 1px drawing on HiDPI (width 0.5)
offset = 0.25
}
if pos1.X == pos2.X {
pos1.X -= offset
pos2.X -= offset
}
if pos1.Y == pos2.Y {
pos1.Y -= offset
pos2.Y -= offset
}
}
x1Pos := pos1.X / frame.Width
x1 := -1 + x1Pos*2
y1Pos := pos1.Y / frame.Height
y1 := 1 - y1Pos*2
x2Pos := pos2.X / frame.Width
x2 := -1 + x2Pos*2
y2Pos := pos2.Y / frame.Height
y2 := 1 - y2Pos*2
normalX := (pos2.Y - pos1.Y) / frame.Width
normalY := (pos2.X - pos1.X) / frame.Height
dirLength := float32(math.Sqrt(float64(normalX*normalX + normalY*normalY)))
normalX /= dirLength
normalY /= dirLength
normalObjX := normalX * 0.5 * frame.Width
normalObjY := normalY * 0.5 * frame.Height
widthMultiplier := float32(math.Sqrt(float64(normalObjX*normalObjX + normalObjY*normalObjY)))
halfWidth := (roundToPixel(lineWidth+feather, p.pixScale) * 0.5) / widthMultiplier
featherWidth := feather / widthMultiplier
return []float32{
// coord x, y normal x, y
x1, y1, normalX, normalY,
x2, y2, normalX, normalY,
x2, y2, -normalX, -normalY,
x2, y2, -normalX, -normalY,
x1, y1, normalX, normalY,
x1, y1, -normalX, -normalY,
}, halfWidth, featherWidth
}
// rectCoords calculates the openGL coordinate space of a rectangle
func (p *glPainter) rectCoords(size fyne.Size, pos fyne.Position, frame fyne.Size,
fill canvas.ImageFill, aspect float32, pad float32) []float32 {
size, pos = rectInnerCoords(size, pos, fill, aspect)
size, pos = roundToPixelCoords(size, pos, p.pixScale)
xPos := (pos.X - pad) / frame.Width
x1 := -1 + xPos*2
x2Pos := (pos.X + size.Width + pad) / frame.Width
x2 := -1 + x2Pos*2
yPos := (pos.Y - pad) / frame.Height
y1 := 1 - yPos*2
y2Pos := (pos.Y + size.Height + pad) / frame.Height
y2 := 1 - y2Pos*2
return []float32{
// coord x, y, z texture x, y
x1, y2, 0, 0.0, 1.0, // top left
x1, y1, 0, 0.0, 0.0, // bottom left
x2, y2, 0, 1.0, 1.0, // top right
x2, y1, 0, 1.0, 0.0, // bottom right
}
}
func rectInnerCoords(size fyne.Size, pos fyne.Position, fill canvas.ImageFill, aspect float32) (fyne.Size, fyne.Position) {
if fill == canvas.ImageFillContain || fill == canvas.ImageFillOriginal {
// change pos and size accordingly
viewAspect := size.Width / size.Height
newWidth, newHeight := size.Width, size.Height
widthPad, heightPad := float32(0), float32(0)
if viewAspect > aspect {
newWidth = size.Height * aspect
widthPad = (size.Width - newWidth) / 2
} else if viewAspect < aspect {
newHeight = size.Width / aspect
heightPad = (size.Height - newHeight) / 2
}
return fyne.NewSize(newWidth, newHeight), fyne.NewPos(pos.X+widthPad, pos.Y+heightPad)
}
return size, pos
}
func roundToPixel(v float32, pixScale float32) float32 {
if pixScale == 1.0 {
return float32(math.Round(float64(v)))
}
return float32(math.Round(float64(v*pixScale))) / pixScale
}
func roundToPixelCoords(size fyne.Size, pos fyne.Position, pixScale float32) (fyne.Size, fyne.Position) {
size.Width = roundToPixel(size.Width, pixScale)
size.Height = roundToPixel(size.Height, pixScale)
pos.X = roundToPixel(pos.X, pixScale)
pos.Y = roundToPixel(pos.Y, pixScale)
return size, pos
}