-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpaint.go
229 lines (189 loc) · 6.78 KB
/
paint.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
// Copyright (c) 2021-2025 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"runtime"
"github.com/richardwilkes/unison/enums/blendmode"
"github.com/richardwilkes/unison/enums/paintstyle"
"github.com/richardwilkes/unison/enums/strokecap"
"github.com/richardwilkes/unison/enums/strokejoin"
"github.com/richardwilkes/unison/internal/skia"
)
// Paint controls options applied when drawing.
type Paint struct {
paint skia.Paint
}
func newPaint(paint skia.Paint) *Paint {
p := &Paint{paint: paint}
runtime.SetFinalizer(p, func(obj *Paint) {
ReleaseOnUIThread(func() {
skia.PaintDelete(obj.paint)
})
})
return p
}
// NewPaint creates a new Paint.
func NewPaint() *Paint {
p := newPaint(skia.PaintNew())
p.SetAntialias(true)
return p
}
func (p *Paint) paintOrNil() skia.Paint {
if p == nil {
return nil
}
return p.paint
}
// Clone the Paint.
func (p *Paint) Clone() *Paint {
return newPaint(skia.PaintClone(p.paint))
}
// Equivalent returns true if these Paint objects are equivalent.
func (p *Paint) Equivalent(other *Paint) bool {
if p == nil {
return other == nil
}
if other == nil {
return false
}
return skia.PaintEquivalent(p.paint, other.paint)
}
// Reset the Paint back to its default state.
func (p *Paint) Reset() {
skia.PaintReset(p.paint)
}
// Antialias returns true if pixels on the active edges of a path may be drawn with partial transparency.
func (p *Paint) Antialias() bool {
return skia.PaintIsAntialias(p.paint)
}
// SetAntialias sets whether pixels on the active edges of a path may be drawn with partial transparency.
func (p *Paint) SetAntialias(enabled bool) {
skia.PaintSetAntialias(p.paint, enabled)
}
// Dither returns true if color error may be distributed to smooth color transition.
func (p *Paint) Dither() bool {
return skia.PaintIsDither(p.paint)
}
// SetDither sets whether color error may be distributed to smooth color transition.
func (p *Paint) SetDither(enabled bool) {
skia.PaintSetDither(p.paint, enabled)
}
// Color returns the current color.
func (p *Paint) Color() Color {
return Color(skia.PaintGetColor(p.paint))
}
// SetColor sets the color.
func (p *Paint) SetColor(color Color) {
skia.PaintSetColor(p.paint, skia.Color(color))
}
// Style returns the current PaintStyle.
func (p *Paint) Style() paintstyle.Enum {
return paintstyle.Enum(skia.PaintGetStyle(p.paint))
}
// SetStyle sets the PaintStyle.
func (p *Paint) SetStyle(style paintstyle.Enum) {
skia.PaintSetStyle(p.paint, skia.PaintStyle(style))
}
// StrokeWidth returns the current stroke width.
func (p *Paint) StrokeWidth() float32 {
return skia.PaintGetStrokeWidth(p.paint)
}
// SetStrokeWidth sets the stroke width.
func (p *Paint) SetStrokeWidth(width float32) {
skia.PaintSetStrokeWidth(p.paint, width)
}
// StrokeMiter returns the current stroke miter limit for sharp corners.
func (p *Paint) StrokeMiter() float32 {
return skia.PaintGetStrokeMiter(p.paint)
}
// SetStrokeMiter sets the miter limit for sharp corners.
func (p *Paint) SetStrokeMiter(miter float32) {
skia.PaintSetStrokeMiter(p.paint, miter)
}
// StrokeCap returns the current StrokeCap.
func (p *Paint) StrokeCap() strokecap.Enum {
return strokecap.Enum(skia.PaintGetStrokeCap(p.paint))
}
// SetStrokeCap sets the StrokeCap.
func (p *Paint) SetStrokeCap(strokeCap strokecap.Enum) {
skia.PaintSetStrokeCap(p.paint, skia.StrokeCap(strokeCap))
}
// StrokeJoin returns the current StrokeJoin.
func (p *Paint) StrokeJoin() strokejoin.Enum {
return strokejoin.Enum(skia.PaintGetStrokeJoin(p.paint))
}
// SetStrokeJoin sets the StrokeJoin.
func (p *Paint) SetStrokeJoin(strokeJoin strokejoin.Enum) {
skia.PaintSetStrokeJoin(p.paint, skia.StrokeJoin(strokeJoin))
}
// BlendMode returns the current BlendMode.
func (p *Paint) BlendMode() blendmode.Enum {
return blendmode.Enum(skia.PaintGetBlendMode(p.paint))
}
// SetBlendMode sets the BlendMode.
func (p *Paint) SetBlendMode(blendMode blendmode.Enum) {
skia.PaintSetBlendMode(p.paint, skia.BlendMode(blendMode))
}
// Shader returns the current Shader.
func (p *Paint) Shader() *Shader {
return newShader(skia.PaintGetShader(p.paint))
}
// SetShader sets the Shader.
func (p *Paint) SetShader(shader *Shader) {
skia.PaintSetShader(p.paint, shader.shaderOrNil())
}
// ColorFilter returns the current ColorFilter.
func (p *Paint) ColorFilter() *ColorFilter {
return newColorFilter(skia.PaintGetColorFilter(p.paint))
}
// SetColorFilter sets the ColorFilter.
func (p *Paint) SetColorFilter(filter *ColorFilter) {
skia.PaintSetColorFilter(p.paint, filter.filterOrNil())
}
// MaskFilter returns the current MaskFilter.
func (p *Paint) MaskFilter() *MaskFilter {
return newMaskFilter(skia.PaintGetMaskFilter(p.paint))
}
// SetMaskFilter sets the MaskFilter.
func (p *Paint) SetMaskFilter(filter *MaskFilter) {
skia.PaintSetMaskFilter(p.paint, filter.filterOrNil())
}
// ImageFilter returns the current ImageFilter.
func (p *Paint) ImageFilter() *ImageFilter {
return newImageFilter(skia.PaintGetImageFilter(p.paint))
}
// SetImageFilter sets the ImageFilter.
func (p *Paint) SetImageFilter(filter *ImageFilter) {
skia.PaintSetImageFilter(p.paint, filter.filterOrNil())
}
// PathEffect returns the current PathEffect.
func (p *Paint) PathEffect() *PathEffect {
return newPathEffect(skia.PaintGetPathEffect(p.paint))
}
// SetPathEffect sets the PathEffect.
func (p *Paint) SetPathEffect(effect *PathEffect) {
skia.PaintSetPathEffect(p.paint, effect.effectOrNil())
}
// FillPath returns a path representing the path if it was stroked. resScale determines the precision used. Values >1
// increase precision, while those <1 reduce precision to favor speed and size. If hairline returns true, the path
// represents a hairline, otherwise it represents a fill.
func (p *Paint) FillPath(path *Path, resScale float32) (result *Path, hairline bool) {
result = NewPath()
isFill := skia.PaintGetFillPath(p.paint, path.path, result.path, nil, resScale)
return result, !isFill
}
// FillPathWithCull returns a path representing the path if it was stroked. cullRect, if not nil, will prune any parts
// outside of the rect. resScale determines the precision used. Values >1 increase precision, while those <1 reduce
// precision to favor speed and size. If hairline returns true, the path represents a hairline, otherwise it represents
// a fill.
func (p *Paint) FillPathWithCull(path *Path, cullRect *Rect, resScale float32) (result *Path, hairline bool) {
result = NewPath()
isFill := skia.PaintGetFillPath(p.paint, path.path, result.path, cullRect, resScale)
return result, !isFill
}