-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraphicstate.go
578 lines (514 loc) · 15.8 KB
/
graphicstate.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package model
import (
"fmt"
"strconv"
)
type DashPattern struct {
Array []Fl
Phase Fl
}
// String returns a description as a PDF array.
func (d DashPattern) String() string {
return fmt.Sprintf("[%s %s]", writeFloatArray(d.Array), FmtFloat(d.Phase))
}
// Clone returns a deep copy
func (d *DashPattern) Clone() *DashPattern {
if d == nil {
return nil
}
out := *d
out.Array = append([]Fl(nil), d.Array...)
return &out
}
type FontStyle struct {
Font *FontDict
Size Fl
}
func (f FontStyle) pdfString(pdf pdfWriter) string {
ref := pdf.addItem(f.Font)
return fmt.Sprintf("[%s %s]", ref, FmtFloat(f.Size))
}
func (f FontStyle) clone(cache cloneCache) FontStyle {
out := f
out.Font = cache.checkOrClone(f.Font).(*FontDict)
return out
}
type TransparencyGroup struct {
CS ColorSpace
I bool // optional, default value: false
K bool // optional, default value: false
}
func (tg TransparencyGroup) pdfString(pdf pdfWriter, ref Reference, withType bool) string {
gDict := "<</S/Transparency"
if withType {
gDict += " /Type/Group"
}
if tg.CS != nil {
gDict += "/CS " + tg.CS.colorSpaceWrite(pdf, ref)
}
if tg.I {
gDict += "/I true"
}
if tg.K {
gDict += "/K true"
}
gDict += ">>"
return gDict
}
func (tg TransparencyGroup) clone(cache cloneCache) TransparencyGroup {
out := tg
out.CS = cloneColorSpace(tg.CS, cache)
return out
}
var _ XObject = (*XObjectTransparencyGroup)(nil)
// XObjectTransparencyGroup is a sequence of consecutive objects in a transparency stack that shall be collected
// together and composited to produce a single colour, shape, and opacity at each point. The result shall then be
// treated as if it were a single object for subsequent compositing operations. Groups may be nested within other
// groups to form a tree-structured group hierarchy.
// See Table 147 – Additional entries specific to a transparency group attributes dictionary
type XObjectTransparencyGroup struct {
XObjectForm
Group TransparencyGroup
}
func (tg *XObjectTransparencyGroup) pdfContent(pdf pdfWriter, ref Reference) (StreamHeader, string, []byte) {
base := tg.XObjectForm.commonFields(pdf, ref)
gDict := tg.Group.pdfString(pdf, ref, true)
base.Fields["Group"] = gDict
return base, "", tg.Content
}
func (tg *XObjectTransparencyGroup) clone(cache cloneCache) Referenceable {
if tg == nil {
return tg
}
out := *tg
out.XObjectForm = *(tg.XObjectForm.clone(cache).(*XObjectForm))
out.Group = tg.Group.clone(cache)
return &out
}
// SoftMaskDict
// See Table 144 – Entries in a soft-mask dictionary
// In addition, we use the following convention:
// - S == "" means 'nil' (not specified)
// - S == /None means the name None
// - other value means normal dictionary
type SoftMaskDict struct {
S Name // required
G *XObjectTransparencyGroup // required
BC []Fl // optional, length: number of color components
// Optional. nil means the same as the /Identity name
TR *FunctionDict
}
func (s SoftMaskDict) pdfString(pdf pdfWriter) string {
if s.S == "None" {
return s.S.String()
}
out := "<</S" + s.S.String()
if s.G != nil {
ref := pdf.addItem(s.G)
out += "/G " + ref.String()
}
if len(s.BC) != 0 {
out += "/BC " + writeFloatArray(s.BC)
}
if s.TR != nil {
ref := pdf.addItem(s.TR)
out += "/TR " + ref.String()
}
out += ">>"
return out
}
func (s SoftMaskDict) clone(cache cloneCache) SoftMaskDict {
out := s
out.G = cache.checkOrClone(s.G).(*XObjectTransparencyGroup)
out.BC = append([]Fl(nil), s.BC...)
if s.TR != nil {
out.TR = cache.checkOrClone(s.TR).(*FunctionDict)
}
return out
}
// GraphicState precises parameters in the graphics state.
// See Table 58 – Entries in a Graphics State Parameter Dictionary
// TODO: The following entries are not yet supported:
// - OP, op, OPM
// - BG, BG2, UCR, UCR2, TR, TR2
// - HT
// - FL
// - TK
type GraphicState struct {
LW Fl
LC MaybeInt // optional, >= 0
LJ MaybeInt // optional, >= 0
ML Fl
D *DashPattern // optional
RI Name
Font FontStyle // font and size
SM MaybeFloat // optional
SA bool
// Blend mode
// See Table 136 – Standard separable blend modes
// and Table 137 – Standard nonseparable blend modes
BM []Name // 1-element array are written in PDF as a singlename
SMask SoftMaskDict // optional
CA MaybeFloat // stroking, optional, >= 0
Ca MaybeFloat // non-stroking, optional, >= 0
AIS bool
}
func (g *GraphicState) pdfContent(pdf pdfWriter, _ Reference) (StreamHeader, string, []byte) {
b := newBuffer()
b.WriteString("<<")
if g.LW != 0 {
b.fmt("/LW %s", FmtFloat(g.LW))
}
if g.LC != nil {
b.fmt("/LC %d", g.LC.(ObjInt))
}
if g.LJ != nil {
b.fmt("/LJ %d", g.LJ.(ObjInt))
}
if g.ML != 0 {
b.fmt("/ML %s", FmtFloat(g.ML))
}
if g.D != nil {
b.fmt("/D %s", *g.D)
}
if g.RI != "" {
b.fmt("/RI %s", g.RI)
}
if g.Font.Font != nil {
b.fmt("/Font %s", g.Font.pdfString(pdf))
}
if g.SM != nil {
b.fmt("/SM %s", FmtFloat(Fl(g.SM.(ObjFloat))))
}
if g.SA {
b.fmt("/SA %v", g.SA)
}
if len(g.BM) == 1 {
b.WriteString("/BM " + g.BM[0].String())
} else if len(g.BM) > 1 {
b.WriteString("/BM " + writeNameArray(g.BM))
}
if g.SMask.S != "" {
b.WriteString("/SMask " + g.SMask.pdfString(pdf))
}
if g.CA != nil {
b.fmt("/CA %s", FmtFloat(Fl(g.CA.(ObjFloat))))
}
if g.Ca != nil {
b.fmt("/ca %s", FmtFloat(Fl(g.Ca.(ObjFloat))))
}
if g.AIS {
b.fmt("/AIS %v", g.AIS)
}
b.WriteString(">>")
return StreamHeader{}, b.String(), nil
}
func (g *GraphicState) clone(cache cloneCache) Referenceable {
if g == nil {
return g
}
out := *g // shallow copy
out.D = g.D.Clone()
out.Font = g.Font.clone(cache)
out.BM = append([]Name(nil), g.BM...)
out.SMask = g.SMask.clone(cache)
return &out
}
// ----------------------- Patterns -----------------------
// Pattern is either a Tiling or a Shading pattern
type Pattern interface {
isPattern()
Referenceable
}
func (*PatternTiling) isPattern() {}
func (*PatternShading) isPattern() {}
// PatternTiling is a type 1 pattern
type PatternTiling struct {
ContentStream
PaintType uint8 // 1 for coloured; 2 for uncoloured
TilingType uint8 // 1, 2, 3
BBox Rectangle
XStep Fl
YStep Fl
Resources ResourcesDict
Matrix Matrix // optional, default to identity
}
func (t *PatternTiling) pdfContent(pdf pdfWriter, ref Reference) (StreamHeader, string, []byte) {
out := t.ContentStream.PDFCommonFields(true)
out.Fields["PatternType"] = "1"
out.Fields["PaintType"] = strconv.Itoa(int(t.PaintType))
out.Fields["TilingType"] = strconv.Itoa(int(t.TilingType))
out.Fields["BBox"] = t.BBox.String()
out.Fields["XStep"] = ObjFloat(t.XStep).Write(nil, 0)
out.Fields["YStep"] = ObjFloat(t.YStep).Write(nil, 0)
out.Fields["Resources"] = t.Resources.pdfString(pdf, ref)
if t.Matrix != (Matrix{}) {
out.Fields["Matrix"] = t.Matrix.String()
}
return out, "", t.Content
}
func (t *PatternTiling) clone(cache cloneCache) Referenceable {
if t == nil {
return t
}
out := *t
out.ContentStream = t.ContentStream.Clone()
out.Resources = t.Resources.clone(cache)
return &out
}
// Shading is the kind of shading and
// may be FunctionBased, Axial, Radial, FreeForm,
// Lattice, Coons, TensorProduct
type Shading interface {
// update `shadingFields` and returns the content stream
shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte)
Clone() Shading
}
type ShadingFunctionBased struct {
Domain [4]Fl // optional, default to [0 1 0 1]
Matrix Matrix // optional, default to identity
Function []FunctionDict // either one 2 -> n function, or n 2 -> 1 functions
}
func (s ShadingFunctionBased) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
fns := pdf.writeFunctions(s.Function)
shadingFields["ShadingType"] = "1"
shadingFields["Function"] = writeRefArray(fns)
if s.Domain != [4]Fl{} {
shadingFields["Domain"] = writeFloatArray(s.Domain[:])
}
if (s.Matrix != Matrix{}) {
shadingFields["Matrix"] = s.Matrix.String()
}
return StreamHeader{Fields: shadingFields}, nil
}
// Clone returns a deep copy with concrete type `FunctionBased`
func (f ShadingFunctionBased) Clone() Shading {
out := f
out.Function = append([]FunctionDict(nil), f.Function...)
return out
}
// BaseGradient stores attributes common to linear and radial gradients.
type BaseGradient struct {
Domain [2]Fl // optional, default to [0 1]
// either one 1 -> n function, or n 1->1 functions,
// where n is the number of color components
Function []FunctionDict
Extend [2]bool // optional, default to [false, false]
}
// update out
//
// `pdf` is used to write the functions
func (g BaseGradient) pdfString(pdf pdfWriter, out map[Name]string) {
fns := pdf.writeFunctions(g.Function)
// we have to differentiate one-length arrays
if len(fns) == 1 {
out["Function"] = fns[0].String()
} else {
out["Function"] = writeRefArray(fns)
}
if g.Domain != [2]Fl{} {
out["Domain"] = writeFloatArray(g.Domain[:])
}
if g.Extend != [2]bool{} {
out["Extend"] = fmt.Sprintf("[%v %v]", g.Extend[0], g.Extend[1])
}
}
// Clone returns a deep copy
func (b BaseGradient) Clone() BaseGradient {
out := b
if b.Function != nil { // to preserve reflect.DeepEqual
out.Function = make([]FunctionDict, len(b.Function))
}
for i, f := range b.Function {
out.Function[i] = f.Clone()
}
return out
}
type ShadingAxial struct {
BaseGradient
Coords [4]Fl // x0, y0, x1, y1
}
func (s ShadingAxial) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
s.BaseGradient.pdfString(pdf, shadingFields)
shadingFields["ShadingType"] = "2"
shadingFields["Coords"] = writeFloatArray(s.Coords[:])
return StreamHeader{Fields: shadingFields}, nil
}
// Clone returns a deep copy with concrete type `Axial`
func (f ShadingAxial) Clone() Shading {
out := f
out.BaseGradient = f.BaseGradient.Clone()
return out
}
type ShadingRadial struct {
BaseGradient
Coords [6]Fl // x0, y0, r0, x1, y1, r1
}
func (s ShadingRadial) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
s.BaseGradient.pdfString(pdf, shadingFields)
shadingFields["ShadingType"] = "3"
shadingFields["Coords"] = writeFloatArray(s.Coords[:])
return StreamHeader{Fields: shadingFields}, nil
}
// Clone returns a deep copy with concrete type `Radial`
func (f ShadingRadial) Clone() Shading {
out := f
out.BaseGradient = f.BaseGradient.Clone()
return out
}
// ShadingStream is the base type shared by 4 to 7 shadings type
type ShadingStream struct {
Stream
BitsPerCoordinate uint8 // 1, 2, 4, 8, 12, 16, 24, or 32
BitsPerComponent uint8 // 1, 2, 4, 8, 12, or 16
Decode [][2]Fl
Function []FunctionDict // optional, one 1->n function or n 1->1 functions (n is the number of colour components)
}
// Clone returns a deep copy
func (ss ShadingStream) Clone() ShadingStream {
out := ss
out.Stream = ss.Stream.Clone()
out.Decode = append([][2]Fl(nil), ss.Decode...)
if ss.Function != nil { // to preserve reflect.DeepEqual
out.Function = make([]FunctionDict, len(ss.Function))
}
for i, f := range ss.Function {
out.Function[i] = f.Clone()
}
return out
}
// return the shared dict attributes
func (ss ShadingStream) pdfFields(shadingFields map[Name]string, pdf pdfWriter, type_ uint8) (StreamHeader, []byte) {
args := ss.PDFCommonFields(true)
args.updateWith(shadingFields)
args.Fields["ShadingType"] = strconv.Itoa(int(type_))
args.Fields["BitsPerCoordinate"] = strconv.Itoa(int(ss.BitsPerCoordinate))
args.Fields["BitsPerComponent"] = strconv.Itoa(int(ss.BitsPerComponent))
args.Fields["Decode"] = writePointsArray(ss.Decode)
if len(ss.Function) != 0 {
fns := pdf.writeFunctions(ss.Function)
args.Fields["Function"] = writeRefArray(fns)
}
return args, ss.Content
}
type ShadingFreeForm struct {
ShadingStream
BitsPerFlag uint8 // 2, 4, or 8
}
// method shared with ShadingCoons and ShadingTensorProduct
// type_ is 4, 6 or 7
func (c ShadingFreeForm) pdfContentExt(shadingFields map[Name]string, pdf pdfWriter, type_ uint8) (StreamHeader, []byte) {
out, content := c.ShadingStream.pdfFields(shadingFields, pdf, type_)
out.Fields["BitsPerFlag"] = strconv.Itoa(int(c.BitsPerFlag))
return out, content
}
func (c ShadingFreeForm) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
return c.pdfContentExt(shadingFields, pdf, 4)
}
// Clone returns a deep copy with concrete type `ShadingFreeForm`
func (co ShadingFreeForm) Clone() Shading {
out := co
out.ShadingStream = co.ShadingStream.Clone()
return out
}
type ShadingLattice struct {
ShadingStream
VerticesPerRow int // required
}
func (c ShadingLattice) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
out, content := c.ShadingStream.pdfFields(shadingFields, pdf, 5)
out.Fields["VerticesPerRow"] = strconv.Itoa(c.VerticesPerRow)
return out, content
}
// Clone returns a deep copy with concrete type `ShadingLattice`
func (co ShadingLattice) Clone() Shading {
out := co
out.ShadingStream = co.ShadingStream.Clone()
return out
}
type ShadingCoons ShadingFreeForm
func (c ShadingCoons) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
return ShadingFreeForm(c).pdfContentExt(shadingFields, pdf, 6)
}
// Clone returns a deep copy with concrete type `ShadingCoons`
func (co ShadingCoons) Clone() Shading {
return ShadingCoons(ShadingFreeForm(co).Clone().(ShadingFreeForm))
}
type ShadingTensorProduct ShadingFreeForm
func (c ShadingTensorProduct) shadingPDFContent(shadingFields map[Name]string, pdf pdfWriter) (StreamHeader, []byte) {
return ShadingFreeForm(c).pdfContentExt(shadingFields, pdf, 7)
}
// Clone returns a deep copy with concrete type `ShadingTensorProduct`
func (co ShadingTensorProduct) Clone() Shading {
return ShadingTensorProduct(ShadingFreeForm(co).Clone().(ShadingFreeForm))
}
// ShadingDict is either a plain dict, or is a stream (+ dict)
type ShadingDict struct {
ShadingType Shading
ColorSpace ColorSpace // required
// colour components appropriate to the colour space
// only applied when part of a (shading) pattern
Background []Fl // optional
BBox *Rectangle // optional in shading’s target coordinate space
AntiAlias bool // optional, default to false
}
func (s *ShadingDict) pdfContent(pdf pdfWriter, ref Reference) (StreamHeader, string, []byte) {
b := make(map[Name]string)
if s.ColorSpace != nil {
b["ColorSpace"] = s.ColorSpace.colorSpaceWrite(pdf, ref)
}
if len(s.Background) != 0 {
b["Background"] = writeFloatArray(s.Background)
}
if s.BBox != nil {
b["BBox"] = s.BBox.String()
}
b["AntiAlias"] = strconv.FormatBool(s.AntiAlias)
out, by := s.ShadingType.shadingPDFContent(b, pdf)
return out, "", by
}
func (s *ShadingDict) clone(cache cloneCache) Referenceable {
if s == nil {
return s
}
out := *s // shallow copy
if s.ShadingType != nil {
out.ShadingType = s.ShadingType.Clone()
}
out.ColorSpace = cloneColorSpace(s.ColorSpace, cache)
out.Background = append([]Fl(nil), s.Background...)
if s.BBox != nil {
bbox := *s.BBox
out.BBox = &bbox
}
return &out
}
// PatternShading is a type2 pattern
type PatternShading struct {
Shading *ShadingDict // required
Matrix Matrix // optionnal, default to Identity
ExtGState *GraphicState // optionnal
}
func (s *PatternShading) pdfContent(pdf pdfWriter, _ Reference) (StreamHeader, string, []byte) {
b := newBuffer()
shadingRef := pdf.addItem(s.Shading)
b.fmt("<</PatternType 2/Shading %s", shadingRef)
if s.Matrix != (Matrix{}) {
b.fmt("/Matrix %s", s.Matrix)
}
if s.ExtGState != nil {
stateRef := pdf.addItem(s.ExtGState)
b.fmt("/ExtGState %s", stateRef)
}
b.fmt(">>")
return StreamHeader{}, b.String(), nil
}
func (s *PatternShading) clone(cache cloneCache) Referenceable {
if s == nil {
return s
}
out := *s
out.Shading = cache.checkOrClone(s.Shading).(*ShadingDict)
out.ExtGState = cache.checkOrClone(s.ExtGState).(*GraphicState)
return &out
}