forked from cucy/unipdf1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.go
433 lines (374 loc) · 11.9 KB
/
pattern.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package model
import (
"errors"
"fmt"
"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/core"
)
// A PdfPattern can represent a Pattern, either a tiling pattern or a shading pattern.
// Note that all patterns shall be treated as colours; a Pattern colour space shall be established with the CS or cs
// operator just like other colour spaces, and a particular pattern shall be installed as the current colour with the
// SCN or scn operator.
type PdfPattern struct {
// Type: Pattern
PatternType int64
context PdfModel // The sub pattern, either PdfTilingPattern (Type 1) or PdfShadingPattern (Type 2).
container core.PdfObject
}
// GetContainingPdfObject returns the container of the pattern object (indirect object).
func (p *PdfPattern) GetContainingPdfObject() core.PdfObject {
return p.container
}
// GetContext returns a reference to the subpattern entry: either PdfTilingPattern or PdfShadingPattern.
func (p *PdfPattern) GetContext() PdfModel {
return p.context
}
// SetContext sets the sub pattern (context). Either PdfTilingPattern or PdfShadingPattern.
func (p *PdfPattern) SetContext(ctx PdfModel) {
p.context = ctx
}
// IsTiling specifies if the pattern is a tiling pattern.
func (p *PdfPattern) IsTiling() bool {
return p.PatternType == 1
}
// IsShading specifies if the pattern is a shading pattern.
func (p *PdfPattern) IsShading() bool {
return p.PatternType == 2
}
// GetAsTilingPattern returns a tiling pattern. Check with IsTiling() prior to using this.
func (p *PdfPattern) GetAsTilingPattern() *PdfTilingPattern {
return p.context.(*PdfTilingPattern)
}
// GetAsShadingPattern returns a shading pattern. Check with IsShading() prior to using this.
func (p *PdfPattern) GetAsShadingPattern() *PdfShadingPattern {
return p.context.(*PdfShadingPattern)
}
// PdfTilingPattern is a Tiling pattern that consists of repetitions of a pattern cell with defined intervals.
// It is a type 1 pattern. (PatternType = 1).
// A tiling pattern is represented by a stream object, where the stream content is
// a content stream that describes the pattern cell.
type PdfTilingPattern struct {
*PdfPattern
PaintType *core.PdfObjectInteger // Colored or uncolored tiling pattern.
TilingType *core.PdfObjectInteger // Constant spacing, no distortion or constant spacing/faster tiling.
BBox *PdfRectangle
XStep *core.PdfObjectFloat
YStep *core.PdfObjectFloat
Resources *PdfPageResources
Matrix *core.PdfObjectArray // Pattern matrix (6 numbers).
}
// IsColored specifies if the pattern is colored.
func (p *PdfTilingPattern) IsColored() bool {
if p.PaintType != nil && *p.PaintType == 1 {
return true
}
return false
}
// GetContentStream returns the pattern cell's content stream
func (p *PdfTilingPattern) GetContentStream() ([]byte, error) {
decoded, _, err := p.GetContentStreamWithEncoder()
return decoded, err
}
// GetContentStreamWithEncoder returns the pattern cell's content stream and its encoder
func (p *PdfTilingPattern) GetContentStreamWithEncoder() ([]byte, core.StreamEncoder, error) {
streamObj, ok := p.container.(*core.PdfObjectStream)
if !ok {
common.Log.Debug("Tiling pattern container not a stream (got %T)", p.container)
return nil, nil, core.ErrTypeError
}
decoded, err := core.DecodeStream(streamObj)
if err != nil {
common.Log.Debug("Failed decoding stream, err: %v", err)
return nil, nil, err
}
encoder, err := core.NewEncoderFromStream(streamObj)
if err != nil {
common.Log.Debug("Failed finding decoding encoder: %v", err)
return nil, nil, err
}
return decoded, encoder, nil
}
// SetContentStream sets the pattern cell's content stream.
func (p *PdfTilingPattern) SetContentStream(content []byte, encoder core.StreamEncoder) error {
streamObj, ok := p.container.(*core.PdfObjectStream)
if !ok {
common.Log.Debug("Tiling pattern container not a stream (got %T)", p.container)
return core.ErrTypeError
}
// If encoding is not set, use raw encoder.
if encoder == nil {
encoder = core.NewRawEncoder()
}
streamDict := streamObj.PdfObjectDictionary
// Make a new stream dict based on the encoding parameters.
encDict := encoder.MakeStreamDict()
// Merge the encoding dict into the stream dict.
streamDict.Merge(encDict)
encoded, err := encoder.EncodeBytes(content)
if err != nil {
return err
}
// Update length.
streamDict.Set("Length", core.MakeInteger(int64(len(encoded))))
streamObj.Stream = []byte(encoded)
return nil
}
// PdfShadingPattern is a Shading patterns that provide a smooth transition between colors across an area to be painted,
// i.e. color(x,y) = f(x,y) at each point.
// It is a type 2 pattern (PatternType = 2).
type PdfShadingPattern struct {
*PdfPattern
Shading *PdfShading
Matrix *core.PdfObjectArray
ExtGState core.PdfObject
}
// Load a pdf pattern from an indirect object. Used in parsing/loading PDFs.
func newPdfPatternFromPdfObject(container core.PdfObject) (*PdfPattern, error) {
pattern := &PdfPattern{}
var dict *core.PdfObjectDictionary
if indObj, is := core.GetIndirect(container); is {
pattern.container = indObj
d, ok := indObj.PdfObject.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("Pattern indirect object not containing dictionary (got %T)", indObj.PdfObject)
return nil, core.ErrTypeError
}
dict = d
} else if streamObj, is := core.GetStream(container); is {
pattern.container = streamObj
dict = streamObj.PdfObjectDictionary
} else {
common.Log.Debug("Pattern not an indirect object or stream. %T", container)
return nil, core.ErrTypeError
}
// PatternType.
obj := dict.Get("PatternType")
if obj == nil {
common.Log.Debug("Pdf Pattern not containing PatternType")
return nil, ErrRequiredAttributeMissing
}
patternType, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("Pattern type not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
if *patternType != 1 && *patternType != 2 {
common.Log.Debug("Pattern type != 1/2 (got %d)", *patternType)
return nil, core.ErrRangeError
}
pattern.PatternType = int64(*patternType)
switch *patternType {
case 1: // Tiling pattern.
ctx, err := newPdfTilingPatternFromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfPattern = pattern
pattern.context = ctx
return pattern, nil
case 2: // Shading pattern.
ctx, err := newPdfShadingPatternFromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfPattern = pattern
pattern.context = ctx
return pattern, nil
}
return nil, errors.New("unknown pattern")
}
// Load entries specific to a pdf tiling pattern from a dictionary.
// Used in parsing/loading PDFs.
func newPdfTilingPatternFromDictionary(dict *core.PdfObjectDictionary) (*PdfTilingPattern, error) {
pattern := &PdfTilingPattern{}
// PaintType (required).
obj := dict.Get("PaintType")
if obj == nil {
common.Log.Debug("PaintType missing")
return nil, ErrRequiredAttributeMissing
}
paintType, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("PaintType not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
pattern.PaintType = paintType
// TilingType (required).
obj = dict.Get("TilingType")
if obj == nil {
common.Log.Debug("TilingType missing")
return nil, ErrRequiredAttributeMissing
}
tilingType, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("TilingType not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
pattern.TilingType = tilingType
// BBox (required).
obj = dict.Get("BBox")
if obj == nil {
common.Log.Debug("BBox missing")
return nil, ErrRequiredAttributeMissing
}
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("BBox should be specified by an array (got %T)", obj)
return nil, core.ErrTypeError
}
rect, err := NewPdfRectangle(*arr)
if err != nil {
common.Log.Debug("BBox error: %v", err)
return nil, err
}
pattern.BBox = rect
// XStep (required).
obj = dict.Get("XStep")
if obj == nil {
common.Log.Debug("XStep missing")
return nil, ErrRequiredAttributeMissing
}
xStep, err := core.GetNumberAsFloat(obj)
if err != nil {
common.Log.Debug("Error getting XStep as float: %v", xStep)
return nil, err
}
pattern.XStep = core.MakeFloat(xStep)
// YStep (required).
obj = dict.Get("YStep")
if obj == nil {
common.Log.Debug("YStep missing")
return nil, ErrRequiredAttributeMissing
}
yStep, err := core.GetNumberAsFloat(obj)
if err != nil {
common.Log.Debug("Error getting YStep as float: %v", yStep)
return nil, err
}
pattern.YStep = core.MakeFloat(yStep)
// Resources (required).
obj = dict.Get("Resources")
if obj == nil {
common.Log.Debug("Resources missing")
return nil, ErrRequiredAttributeMissing
}
dict, ok = core.TraceToDirectObject(obj).(*core.PdfObjectDictionary)
if !ok {
return nil, fmt.Errorf("invalid resource dictionary (%T)", obj)
}
resources, err := NewPdfPageResourcesFromDict(dict)
if err != nil {
return nil, err
}
pattern.Resources = resources
// Matrix (optional).
if obj := dict.Get("Matrix"); obj != nil {
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Matrix not an array (got %T)", obj)
return nil, core.ErrTypeError
}
pattern.Matrix = arr
}
return pattern, nil
}
// Load entries specific to a pdf shading pattern from a dictionary.
// Used in parsing/loading PDFs.
func newPdfShadingPatternFromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingPattern, error) {
pattern := &PdfShadingPattern{}
// Shading (required).
obj := dict.Get("Shading")
if obj == nil {
common.Log.Debug("Shading missing")
return nil, ErrRequiredAttributeMissing
}
shading, err := newPdfShadingFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error loading shading: %v", err)
return nil, err
}
pattern.Shading = shading
// Matrix (optional).
if obj := dict.Get("Matrix"); obj != nil {
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Matrix not an array (got %T)", obj)
return nil, core.ErrTypeError
}
pattern.Matrix = arr
}
// ExtGState (optional).
if obj := dict.Get("ExtGState"); obj != nil {
pattern.ExtGState = obj
}
return pattern, nil
}
/* Conversions to pdf objects. */
func (p *PdfPattern) getDict() *core.PdfObjectDictionary {
if indObj, is := p.container.(*core.PdfIndirectObject); is {
dict, ok := indObj.PdfObject.(*core.PdfObjectDictionary)
if !ok {
return nil
}
return dict
} else if streamObj, is := p.container.(*core.PdfObjectStream); is {
return streamObj.PdfObjectDictionary
} else {
common.Log.Debug("Trying to access pattern dictionary of invalid object type (%T)", p.container)
return nil
}
}
// ToPdfObject returns the PDF representation of the pattern.
func (p *PdfPattern) ToPdfObject() core.PdfObject {
d := p.getDict()
d.Set("Type", core.MakeName("Pattern"))
d.Set("PatternType", core.MakeInteger(p.PatternType))
return p.container
}
// ToPdfObject returns the PDF representation of the tiling pattern.
func (p *PdfTilingPattern) ToPdfObject() core.PdfObject {
p.PdfPattern.ToPdfObject()
d := p.getDict()
if p.PaintType != nil {
d.Set("PaintType", p.PaintType)
}
if p.TilingType != nil {
d.Set("TilingType", p.TilingType)
}
if p.BBox != nil {
d.Set("BBox", p.BBox.ToPdfObject())
}
if p.XStep != nil {
d.Set("XStep", p.XStep)
}
if p.YStep != nil {
d.Set("YStep", p.YStep)
}
if p.Resources != nil {
d.Set("Resources", p.Resources.ToPdfObject())
}
if p.Matrix != nil {
d.Set("Matrix", p.Matrix)
}
return p.container
}
// ToPdfObject returns the PDF representation of the shading pattern.
func (p *PdfShadingPattern) ToPdfObject() core.PdfObject {
p.PdfPattern.ToPdfObject()
d := p.getDict()
if p.Shading != nil {
d.Set("Shading", p.Shading.ToPdfObject())
}
if p.Matrix != nil {
d.Set("Matrix", p.Matrix)
}
if p.ExtGState != nil {
d.Set("ExtGState", p.ExtGState)
}
return p.container
}