forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
224 lines (193 loc) · 7.95 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
package software
import (
"fmt"
"image"
"math"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/painter"
"github.com/goki/freetype"
"github.com/goki/freetype/truetype"
"golang.org/x/image/draw"
)
type gradient interface {
Generate(int, int) image.Image
Size() fyne.Size
}
func drawCircle(c fyne.Canvas, circle *canvas.Circle, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
pad := painter.VectorPad(circle)
scaledWidth := internal.ScaleInt(c, circle.Size().Width+pad*2)
scaledHeight := internal.ScaleInt(c, circle.Size().Height+pad*2)
scaledX, scaledY := internal.ScaleInt(c, pos.X-pad), internal.ScaleInt(c, pos.Y-pad)
bounds := clip.Intersect(image.Rect(scaledX, scaledY, scaledX+scaledWidth, scaledY+scaledHeight))
raw := painter.DrawCircle(circle, pad, func(in float32) float32 {
return float32(math.Round(float64(in) * float64(c.Scale())))
})
// the clip intersect above cannot be negative, so we may need to compensate
offX, offY := 0, 0
if scaledX < 0 {
offX = -scaledX
}
if scaledY < 0 {
offY = -scaledY
}
draw.Draw(base, bounds, raw, image.Point{offX, offY}, draw.Over)
}
func drawGradient(c fyne.Canvas, g gradient, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
bounds := g.Size()
width := internal.ScaleInt(c, bounds.Width)
height := internal.ScaleInt(c, bounds.Height)
tex := g.Generate(width, height)
drawTex(internal.ScaleInt(c, pos.X), internal.ScaleInt(c, pos.Y), width, height, base, tex, clip)
}
func drawImage(c fyne.Canvas, img *canvas.Image, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
bounds := img.Size()
if bounds.IsZero() {
return
}
width := internal.ScaleInt(c, bounds.Width)
height := internal.ScaleInt(c, bounds.Height)
scaledX, scaledY := internal.ScaleInt(c, pos.X), internal.ScaleInt(c, pos.Y)
origImg := painter.PaintImage(img, c, width, height)
if img.FillMode == canvas.ImageFillContain {
imgAspect := painter.GetAspect(img)
objAspect := float32(width) / float32(height)
if objAspect > imgAspect {
newWidth := int(float32(height) * imgAspect)
scaledX += (width - newWidth) / 2
width = newWidth
} else if objAspect < imgAspect {
newHeight := int(float32(width) / imgAspect)
scaledY += (height - newHeight) / 2
height = newHeight
}
}
drawPixels(scaledX, scaledY, width, height, img.ScaleMode, base, origImg, clip)
}
func drawPixels(x, y, width, height int, mode canvas.ImageScale, base *image.NRGBA, origImg image.Image, clip image.Rectangle) {
if origImg.Bounds().Dx() == width && origImg.Bounds().Dy() == height {
// do not scale or duplicate image since not needed, draw directly
drawTex(x, y, width, height, base, origImg, clip)
return
}
scaledBounds := image.Rect(0, 0, width, height)
scaledImg := image.NewNRGBA(scaledBounds)
switch mode {
case canvas.ImageScalePixels:
draw.NearestNeighbor.Scale(scaledImg, scaledBounds, origImg, origImg.Bounds(), draw.Over, nil)
case canvas.ImageScaleFastest:
draw.ApproxBiLinear.Scale(scaledImg, scaledBounds, origImg, origImg.Bounds(), draw.Over, nil)
default:
if mode != canvas.ImageScaleSmooth {
fyne.LogError(fmt.Sprintf("Invalid canvas.ImageScale value (%d), using canvas.ImageScaleSmooth as default value", mode), nil)
}
draw.CatmullRom.Scale(scaledImg, scaledBounds, origImg, origImg.Bounds(), draw.Over, nil)
}
drawTex(x, y, width, height, base, scaledImg, clip)
}
func drawLine(c fyne.Canvas, line *canvas.Line, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
pad := painter.VectorPad(line)
scaledWidth := internal.ScaleInt(c, line.Size().Width+pad*2)
scaledHeight := internal.ScaleInt(c, line.Size().Height+pad*2)
scaledX, scaledY := internal.ScaleInt(c, pos.X-pad), internal.ScaleInt(c, pos.Y-pad)
bounds := clip.Intersect(image.Rect(scaledX, scaledY, scaledX+scaledWidth, scaledY+scaledHeight))
raw := painter.DrawLine(line, pad, func(in float32) float32 {
return float32(math.Round(float64(in) * float64(c.Scale())))
})
// the clip intersect above cannot be negative, so we may need to compensate
offX, offY := 0, 0
if scaledX < 0 {
offX = -scaledX
}
if scaledY < 0 {
offY = -scaledY
}
draw.Draw(base, bounds, raw, image.Point{offX, offY}, draw.Over)
}
func drawTex(x, y, width, height int, base *image.NRGBA, tex image.Image, clip image.Rectangle) {
outBounds := image.Rect(x, y, x+width, y+height)
clippedBounds := clip.Intersect(outBounds)
srcPt := image.Point{X: clippedBounds.Min.X - outBounds.Min.X, Y: clippedBounds.Min.Y - outBounds.Min.Y}
draw.Draw(base, clippedBounds, tex, srcPt, draw.Over)
}
func drawText(c fyne.Canvas, text *canvas.Text, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
bounds := text.MinSize()
width := internal.ScaleInt(c, bounds.Width)
height := internal.ScaleInt(c, bounds.Height)
txtImg := image.NewRGBA(image.Rect(0, 0, width, height))
var opts truetype.Options
fontSize := text.TextSize * c.Scale()
opts.Size = float64(fontSize)
opts.DPI = painter.TextDPI
face := painter.CachedFontFace(text.TextStyle, &opts)
d := painter.FontDrawer{}
d.Dst = txtImg
d.Src = &image.Uniform{C: text.Color}
d.Face = face
d.Dot = freetype.Pt(0, height-face.Metrics().Descent.Ceil())
d.DrawString(text.Text, text.TextStyle.TabWidth())
size := text.Size()
offsetX := float32(0)
offsetY := float32(0)
switch text.Alignment {
case fyne.TextAlignTrailing:
offsetX = size.Width - bounds.Width
case fyne.TextAlignCenter:
offsetX = (size.Width - bounds.Width) / 2
}
if size.Height > bounds.Height {
offsetY = (size.Height - bounds.Height) / 2
}
scaledX := internal.ScaleInt(c, pos.X+offsetX)
scaledY := internal.ScaleInt(c, pos.Y+offsetY)
imgBounds := image.Rect(scaledX, scaledY, scaledX+width, scaledY+height)
clippedBounds := clip.Intersect(imgBounds)
srcPt := image.Point{X: clippedBounds.Min.X - imgBounds.Min.X, Y: clippedBounds.Min.Y - imgBounds.Min.Y}
draw.Draw(base, clippedBounds, txtImg, srcPt, draw.Over)
}
func drawRaster(c fyne.Canvas, rast *canvas.Raster, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
bounds := rast.Size()
if bounds.IsZero() {
return
}
width := internal.ScaleInt(c, bounds.Width)
height := internal.ScaleInt(c, bounds.Height)
scaledX, scaledY := internal.ScaleInt(c, pos.X), internal.ScaleInt(c, pos.Y)
pix := rast.Generator(width, height)
if pix.Bounds().Bounds().Dx() != width || pix.Bounds().Dy() != height {
drawPixels(scaledX, scaledY, width, height, rast.ScaleMode, base, pix, clip)
} else {
drawTex(scaledX, scaledY, width, height, base, pix, clip)
}
}
func drawRectangleStroke(c fyne.Canvas, rect *canvas.Rectangle, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
pad := painter.VectorPad(rect)
scaledWidth := internal.ScaleInt(c, rect.Size().Width+pad*2)
scaledHeight := internal.ScaleInt(c, rect.Size().Height+pad*2)
scaledX, scaledY := internal.ScaleInt(c, pos.X-pad), internal.ScaleInt(c, pos.Y-pad)
bounds := clip.Intersect(image.Rect(scaledX, scaledY, scaledX+scaledWidth, scaledY+scaledHeight))
raw := painter.DrawRectangle(rect, pad, func(in float32) float32 {
return float32(math.Round(float64(in) * float64(c.Scale())))
})
// the clip intersect above cannot be negative, so we may need to compensate
offX, offY := 0, 0
if scaledX < 0 {
offX = -scaledX
}
if scaledY < 0 {
offY = -scaledY
}
draw.Draw(base, bounds, raw, image.Point{offX, offY}, draw.Over)
}
func drawRectangle(c fyne.Canvas, rect *canvas.Rectangle, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
if rect.StrokeColor != nil && rect.StrokeWidth > 0 { // use a rasterizer if there is a stroke
drawRectangleStroke(c, rect, pos, base, clip)
return
}
scaledWidth := internal.ScaleInt(c, rect.Size().Width)
scaledHeight := internal.ScaleInt(c, rect.Size().Height)
scaledX, scaledY := internal.ScaleInt(c, pos.X), internal.ScaleInt(c, pos.Y)
bounds := clip.Intersect(image.Rect(scaledX, scaledY, scaledX+scaledWidth, scaledY+scaledHeight))
draw.Draw(base, bounds, image.NewUniform(rect.FillColor), image.Point{}, draw.Over)
}