forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_g_state_obj.go
140 lines (109 loc) · 2.78 KB
/
ext_g_state_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
137
138
139
140
package gopdf
import (
"fmt"
"io"
"sync"
"github.com/pkg/errors"
)
// TODO: add all fields https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf 8.4.5 page 128
type ExtGState struct {
Index int
ca *float64
CA *float64
BM *BlendModeType
SMaskIndex *int
}
type ExtGStateOptions struct {
StrokingCA *float64
NonStrokingCa *float64
BlendMode *BlendModeType
SMaskIndex *int
}
func (extOpt ExtGStateOptions) GetId() string {
id := ""
if extOpt.StrokingCA != nil {
id += fmt.Sprintf("CA_%.3f;", *extOpt.StrokingCA)
}
if extOpt.NonStrokingCa != nil {
id += fmt.Sprintf("ca_%.3f;", *extOpt.NonStrokingCa)
}
if extOpt.BlendMode != nil {
id += fmt.Sprintf("BM_%s;", *extOpt.BlendMode)
}
if extOpt.SMaskIndex != nil {
id += fmt.Sprintf("SMask_%d_0_R;", *extOpt.SMaskIndex)
}
return id
}
func GetCachedExtGState(opts ExtGStateOptions, gp *GoPdf) (ExtGState, error) {
extGState, ok := gp.curr.extGStatesMap.Find(opts)
if !ok {
extGState = ExtGState{
BM: opts.BlendMode,
CA: opts.StrokingCA,
ca: opts.NonStrokingCa,
SMaskIndex: opts.SMaskIndex,
}
extGState.Index = gp.addObj(extGState)
pdfObj := gp.pdfObjs[gp.indexOfProcSet]
procset, ok := pdfObj.(*ProcSetObj)
if !ok {
return ExtGState{}, errors.New("can't convert pdfobject to procsetobj")
}
procset.ExtGStates = append(procset.ExtGStates, ExtGS{Index: extGState.Index})
gp.curr.extGStatesMap.Save(opts.GetId(), extGState)
//extGState = extGState
}
return extGState, nil
}
func (egs ExtGState) init(func() *GoPdf) {}
func (egs ExtGState) getType() string {
return "ExtGState"
}
func (egs ExtGState) write(w io.Writer, objID int) error {
content := "<<\n"
content += "\t/Type /ExtGState\n"
if egs.ca != nil {
content += fmt.Sprintf("\t/ca %.3F\n", *egs.ca)
}
if egs.CA != nil {
content += fmt.Sprintf("\t/CA %.3F\n", *egs.CA)
}
if egs.BM != nil {
content += fmt.Sprintf("\t/BM %s\n", *egs.BM)
}
if egs.SMaskIndex != nil {
content += fmt.Sprintf("\t/SMask %d 0 R\n", *egs.SMaskIndex+1)
}
content += ">>\n"
if _, err := io.WriteString(w, content); err != nil {
return err
}
return nil
}
type ExtGStatesMap struct {
syncer sync.Mutex
table map[string]ExtGState
}
func NewExtGStatesMap() ExtGStatesMap {
return ExtGStatesMap{
syncer: sync.Mutex{},
table: make(map[string]ExtGState),
}
}
func (extm *ExtGStatesMap) Find(extGState ExtGStateOptions) (ExtGState, bool) {
key := extGState.GetId()
extm.syncer.Lock()
defer extm.syncer.Unlock()
t, ok := extm.table[key]
if !ok {
return ExtGState{}, false
}
return t, ok
}
func (tm *ExtGStatesMap) Save(id string, extGState ExtGState) ExtGState {
tm.syncer.Lock()
defer tm.syncer.Unlock()
tm.table[id] = extGState
return extGState
}