-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgradients.go
309 lines (273 loc) · 8.34 KB
/
gradients.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
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
package contentstream
import (
"fmt"
"github.com/benoitkugler/pdf/model"
)
type RGB [3]uint8
func (rgb RGB) toArray() []Fl {
return []Fl{Fl(rgb[0]) / 255, Fl(rgb[1]) / 255, Fl(rgb[2]) / 255}
}
// GradientPointRGB defines the position and the (RGB) color
// of a gradient step.
type GradientPointRGB struct {
X, Y Fl
RGB
}
// GradientPointGray defines the position and the gray color
// of a gradient step.
type GradientPointGray struct {
X, Y Fl
G uint8
}
func newBaseGradientRGB(from, to RGB) model.BaseGradient {
return model.BaseGradient{
Function: []model.FunctionDict{
{
FunctionType: model.FunctionExpInterpolation{
C0: from.toArray(),
C1: to.toArray(),
N: 1, // linear
},
Domain: []model.Range{{0, 1}},
},
},
Extend: [2]bool{true, true},
}
}
func newBaseGradientGray(from, to GradientPointGray) model.BaseGradient {
return model.BaseGradient{
Function: []model.FunctionDict{
{
FunctionType: model.FunctionExpInterpolation{
C0: []Fl{Fl(from.G) / 255},
C1: []Fl{Fl(to.G) / 255},
N: 1, // linear
},
Domain: []model.Range{{0, 1}},
},
},
Extend: [2]bool{true, true},
}
}
// AddShading checks is the shading is in the resources map
// or generates a new name and adds the shading.
func (ap GraphicStream) AddShading(newShading *model.ShadingDict) model.ObjName {
for name, f := range ap.resources.Shading {
if f == newShading {
return name
}
}
// this convention must be respected so that the names are distincts
name := model.ObjName(fmt.Sprintf("SH%d", len(ap.resources.Shading)))
ap.resources.Shading[name] = newShading
return name
}
// AddLinearGradientRGB builds a linear gradient shading dictionnary,
// and use it to fill the current path.
//
// The vector's origin and destination are specified by
// the points `from` and `to`, expressed in user space units.
// In a linear gradient, blending occurs
// perpendicularly to this vector. Color 1 is used up to the origin of the
// vector and color 2 is used beyond the vector's end point. Between the points
// the colors are gradually blended.
func (ap *GraphicStream) FillLinearGradientRGB(from, to GradientPointRGB) {
sh := &model.ShadingDict{
ColorSpace: model.ColorSpaceRGB,
ShadingType: model.ShadingAxial{
BaseGradient: newBaseGradientRGB(from.RGB, to.RGB),
Coords: [4]Fl{from.X, from.Y, to.X, to.Y},
},
}
ap.Shading(sh)
}
// AddRadialGradientRGB builds a radial gradient shading dictionnary,
// and use it to fill the current path.
//
// Color 1 begins at the origin point specified by `from`. Color 2 begins at the
// circle specified by the center point `to` and `radius`. Colors are
// gradually blended from the origin to the circle. The origin and the circle's
// center do not necessarily have to coincide, but the origin must be within
// the circle to avoid rendering problems.
func (ap *GraphicStream) FillRadialGradientRGB(from, to GradientPointRGB, radius Fl) {
sh := &model.ShadingDict{
ColorSpace: model.ColorSpaceRGB,
ShadingType: model.ShadingRadial{
BaseGradient: newBaseGradientRGB(from.RGB, to.RGB),
Coords: [6]Fl{from.X, from.Y, 0, to.X, to.Y, radius},
},
}
ap.Shading(sh)
}
// AddExtGState checks if the graphic state is in the resources map or
// generate a new name and adds the graphic state to the resources.
func (ap GraphicStream) AddExtGState(newExtGState *model.GraphicState) model.ObjName {
for name, f := range ap.resources.ExtGState {
if f == newExtGState {
return name
}
}
// this convention must be respected so that the names are distincts
name := model.ObjName(fmt.Sprintf("GS%d", len(ap.resources.ExtGState)))
ap.resources.ExtGState[name] = newExtGState
return name
}
// AddPattern checks if the pattern is in the resources map or
// generate a new name and adds the pattern.
func (ap *GraphicStream) AddPattern(newPattern model.Pattern) model.ObjName {
for name, f := range ap.resources.Pattern {
if f == newPattern {
return name
}
}
// this convention must be respected so that the names are distincts
name := model.ObjName(fmt.Sprintf("PA%d", len(ap.resources.Pattern)))
ap.resources.Pattern[name] = newPattern
return name
}
// GradientComplex supports multiple stops and opacities.
type GradientComplex struct {
Direction GradientDirection // required
Offsets []Fl // between 0 and 1, should contain at least 2 elements
Colors [][4]Fl // RGBA values, between 0 and 1
Reapeating bool
}
// needAlpha is false if we should avoid including an alpha stream
func (gr GradientComplex) buildBaseGradients() (color, alpha model.BaseGradient, needOpacity bool) {
alphas := make([]Fl, len(gr.Colors))
for i, c := range gr.Colors {
alphas[i] = c[3]
needOpacity = needOpacity || c[3] != 1
}
alphaCouples := make([][2]Fl, len(alphas)-1)
colorCouples := make([][2][3]Fl, len(alphas)-1)
exponents := make([]int, len(alphas)-1)
for i := range alphaCouples {
alphaCouples[i] = [2]Fl{alphas[i], alphas[i+1]}
colorCouples[i] = [2][3]Fl{
{gr.Colors[i][0], gr.Colors[i][1], gr.Colors[i][2]},
{gr.Colors[i+1][0], gr.Colors[i+1][1], gr.Colors[i+1][2]},
}
exponents[i] = 1
}
// Premultiply colors
for i, alpha := range alphas {
if alpha == 0 {
if i > 0 {
colorCouples[i-1][1] = colorCouples[i-1][0]
}
if i < len(gr.Colors)-1 {
colorCouples[i][0] = colorCouples[i][1]
}
}
}
for i, v := range alphaCouples {
a0, a1 := v[0], v[1]
if a0 != 0 && a1 != 0 && v != ([2]Fl{1, 1}) {
exponents[i] = int(a0 / a1)
}
}
var functions, alphaFunctions []model.FunctionDict
for i, v := range colorCouples {
c0, c1 := v[0], v[1]
n := exponents[i]
fn := model.FunctionDict{
Domain: []model.Range{{0, 1}},
FunctionType: model.FunctionExpInterpolation{
C0: c0[:],
C1: c1[:],
N: n,
},
}
functions = append(functions, fn)
alphaFn := fn
a0, a1 := alphaCouples[i][0], alphaCouples[i][1]
alphaFn.FunctionType = model.FunctionExpInterpolation{
C0: []model.Fl{a0},
C1: []model.Fl{a1},
N: 1,
}
alphaFunctions = append(alphaFunctions, alphaFn)
}
stitching := model.FunctionStitching{
Functions: functions,
Bounds: gr.Offsets[1 : len(gr.Offsets)-1],
Encode: model.FunctionEncodeRepeat(len(gr.Colors) - 1),
}
stitchingAlpha := stitching
stitchingAlpha.Functions = alphaFunctions
bg := model.BaseGradient{
Domain: [2]Fl{gr.Offsets[0], gr.Offsets[len(gr.Offsets)-1]},
Function: []model.FunctionDict{{
Domain: []model.Range{{gr.Offsets[0], gr.Offsets[len(gr.Offsets)-1]}},
FunctionType: stitching,
}},
}
if !gr.Reapeating {
bg.Extend = [2]bool{true, true}
}
// alpha stream is similar
alphaBg := bg.Clone()
alphaBg.Function[0].FunctionType = stitchingAlpha
return bg, alphaBg, needOpacity
}
// GradientDirection is either GradientRadial or GradientLinear
type GradientDirection interface {
isRadial() bool
}
// x1, y1, x2, y2
type GradientLinear [4]Fl
func (GradientLinear) isRadial() bool { return false }
// cx, cy, fx, fy, r, fr
type GradientRadial [6]Fl
func (GradientRadial) isRadial() bool { return true }
// BuildShadings returns the shadings objects to use in a stream
// `alpha` may be nil if no opacity channel is needed.
func (gr GradientComplex) BuildShadings() (color, alpha *model.ShadingDict) {
colorBase, alphaBase, needOpacity := gr.buildBaseGradients()
var type_, alphaType model.Shading
switch dir := gr.Direction.(type) {
case GradientLinear:
type_ = model.ShadingAxial{
BaseGradient: colorBase,
Coords: dir,
}
alphaType = model.ShadingAxial{
BaseGradient: alphaBase,
Coords: dir,
}
case GradientRadial:
type_ = model.ShadingRadial{
BaseGradient: colorBase,
Coords: dir,
}
alphaType = model.ShadingRadial{
BaseGradient: alphaBase,
Coords: dir,
}
}
color = &model.ShadingDict{
ColorSpace: model.ColorSpaceRGB,
ShadingType: type_,
}
if needOpacity {
alpha = &model.ShadingDict{
ColorSpace: model.ColorSpaceGray,
ShadingType: alphaType,
}
}
return color, alpha
}
// SetAlphaMask adds the given `transparency` content as an alpha mask in
// the graphic state.
func (ap *GraphicStream) SetAlphaMask(transparency *model.XObjectForm) {
alphaState := model.GraphicState{
SMask: model.SoftMaskDict{
S: model.ObjName("Luminosity"),
G: &model.XObjectTransparencyGroup{XObjectForm: *transparency},
},
Ca: model.ObjFloat(1),
AIS: false,
}
ap.SetGraphicState(&alphaState)
}