-
Notifications
You must be signed in to change notification settings - Fork 0
/
shading3.go
123 lines (112 loc) · 3.24 KB
/
shading3.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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2024 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package shading
import (
"errors"
"fmt"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/function"
"seehuhn.de/go/pdf/graphics"
"seehuhn.de/go/pdf/graphics/color"
)
// Type3 represents a type 3 (radial) shading.
type Type3 struct {
ColorSpace color.Space
X1, Y1, R1 float64
X2, Y2, R2 float64
// F is either 1->n function or an array of n 1->1 functions, where n is
// the number of colour components of the ColorSpace.
F function.Func
TMin, TMax float64
ExtendStart bool
ExtendEnd bool
Background []float64
BBox *pdf.Rectangle
AntiAlias bool
}
// Embed implements the [Shading] interface.
func (s *Type3) Embed(w pdf.Putter, singleUse bool, defName pdf.Name) (*graphics.Shading, error) {
if s.ColorSpace == nil {
return nil, errors.New("missing ColorSpace")
} else if color.IsPattern(s.ColorSpace) {
return nil, errors.New("invalid ColorSpace")
}
if have := len(s.Background); have > 0 {
want := color.NumValues(s.ColorSpace)
if have != want {
err := fmt.Errorf("wrong number of background values: expected %d, got %d",
want, have)
return nil, err
}
}
if s.R1 < 0 {
return nil, fmt.Errorf("invalid radius: %f", s.R1)
}
if s.R2 < 0 {
return nil, fmt.Errorf("invalid radius: %f", s.R2)
}
if s.F == nil {
return nil, errors.New("missing function")
}
switch F := s.F.(type) {
case nil:
return nil, errors.New("missing Function")
case pdf.Array:
if len(F) != color.NumValues(s.ColorSpace) {
return nil, errors.New("invalid Function")
}
case pdf.Dict, pdf.Reference:
// pass
default:
return nil, fmt.Errorf("invalid Function: %T", s.F)
}
dict := pdf.Dict{
"ShadingType": pdf.Integer(3),
"ColorSpace": s.ColorSpace.PDFObject(),
"Coords": pdf.Array{
pdf.Number(s.X1), pdf.Number(s.Y1), pdf.Number(s.R1),
pdf.Number(s.X2), pdf.Number(s.Y2), pdf.Number(s.R2),
},
"Function": s.F,
}
if len(s.Background) > 0 {
dict["Background"] = toPDF(s.Background)
}
if s.BBox != nil {
dict["BBox"] = s.BBox
}
if s.AntiAlias {
dict["AntiAlias"] = pdf.Boolean(true)
}
if s.TMin != 0 || (s.TMax != 0 && s.TMax != 1) {
dict["Domain"] = pdf.Array{pdf.Number(s.TMin), pdf.Number(s.TMax)}
}
if s.ExtendStart || s.ExtendEnd {
dict["Extend"] = pdf.Array{pdf.Boolean(s.ExtendStart), pdf.Boolean(s.ExtendEnd)}
}
var data pdf.Object
if singleUse {
data = dict
} else {
ref := w.Alloc()
err := w.Put(ref, dict)
if err != nil {
return nil, err
}
data = ref
}
return &graphics.Shading{Res: pdf.Res{DefName: defName, Data: data}}, nil
}