forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smask_obj.go
136 lines (108 loc) · 2.56 KB
/
smask_obj.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
package gopdf
import (
"fmt"
"io"
"sync"
)
type SMaskSubtypes string
const (
SMaskAlphaSubtype = "/Alpha"
SMaskLuminositySubtype = "/Luminosity"
)
//SMask smask
type SMask struct {
imgInfo
data []byte
//getRoot func() *GoPdf
pdfProtection *PDFProtection
Index int
TransparencyXObjectGroupIndex int
S string
}
type SMaskOptions struct {
TransparencyXObjectGroupIndex int
Subtype SMaskSubtypes
}
func (smask SMaskOptions) GetId() string {
id := fmt.Sprintf("S_%s;G_%d_0_R", smask.Subtype, smask.TransparencyXObjectGroupIndex)
return id
}
func GetCachedMask(opts SMaskOptions, gp *GoPdf) SMask {
smask, ok := gp.curr.sMasksMap.Find(opts)
if !ok {
smask = SMask{
S: string(opts.Subtype),
TransparencyXObjectGroupIndex: opts.TransparencyXObjectGroupIndex,
}
smask.Index = gp.addObj(smask)
gp.curr.sMasksMap.Save(opts.GetId(), smask)
}
return smask
}
func (s SMask) init(func() *GoPdf) {}
func (s *SMask) setProtection(p *PDFProtection) {
s.pdfProtection = p
}
func (s SMask) protection() *PDFProtection {
return s.pdfProtection
}
func (s SMask) getType() string {
return "Mask"
}
func (s SMask) write(w io.Writer, objID int) error {
if s.TransparencyXObjectGroupIndex != 0 {
content := "<<\n"
content += "\t/Type /Mask\n"
content += fmt.Sprintf("\t/S %s\n", s.S)
content += fmt.Sprintf("\t/G %d 0 R\n", s.TransparencyXObjectGroupIndex+1)
content += ">>\n"
if _, err := io.WriteString(w, content); err != nil {
return err
}
} else {
err := writeImgProps(w, s.imgInfo, false)
if err != nil {
return err
}
fmt.Fprintf(w, "/Length %d\n>>\n", len(s.data)) // /Length 62303>>\n
io.WriteString(w, "stream\n")
if s.protection() != nil {
tmp, err := rc4Cip(s.protection().objectkey(objID), s.data)
if err != nil {
return err
}
w.Write(tmp)
io.WriteString(w, "\n")
} else {
w.Write(s.data)
}
io.WriteString(w, "\nendstream\n")
}
return nil
}
type SMaskMap struct {
syncer sync.Mutex
table map[string]SMask
}
func NewSMaskMap() SMaskMap {
return SMaskMap{
syncer: sync.Mutex{},
table: make(map[string]SMask),
}
}
func (smask *SMaskMap) Find(sMask SMaskOptions) (SMask, bool) {
key := sMask.GetId()
smask.syncer.Lock()
defer smask.syncer.Unlock()
t, ok := smask.table[key]
if !ok {
return SMask{}, false
}
return t, ok
}
func (smask *SMaskMap) Save(id string, sMask SMask) SMask {
smask.syncer.Lock()
defer smask.syncer.Unlock()
smask.table[id] = sMask
return sMask
}