forked from cucy/unipdf1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
65 lines (57 loc) · 2.32 KB
/
model.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
/*
* 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 (
"github.com/unidoc/unipdf/v3/core"
)
// PdfModel is a higher level PDF construct which can be collapsed into a PdfObject.
// Each PdfModel has an underlying PdfObject and vice versa (one-to-one).
// Under normal circumstances there should only be one copy of each.
// Copies can be made, but care must be taken to do it properly.
type PdfModel interface {
ToPdfObject() core.PdfObject
GetContainingPdfObject() core.PdfObject
}
// modelManager is used to cache primitive PdfObject <-> Model mappings where needed (one-to-one).
// In many cases only Model -> PdfObject mapping is needed and only a reference to the PdfObject
// is stored in the Model. In some cases, the Model needs to be found from the PdfObject,
// and that is where the modelManager can be used (in both directions).
//
// Note that it is not always used, the PdfObject <-> Model mapping needs to be registered
// for each time it is used. Thus, it is only used for special cases, commonly where the same
// object is used by two higher level objects. (Example PDF Widgets owned by both Page Annotations,
// and the interactive form - AcroForm).
type modelManager struct {
primitiveCache map[PdfModel]core.PdfObject
modelCache map[core.PdfObject]PdfModel
}
// newModelManager returns a new initialized modelManager.
func newModelManager() *modelManager {
mm := modelManager{}
mm.primitiveCache = map[PdfModel]core.PdfObject{}
mm.modelCache = map[core.PdfObject]PdfModel{}
return &mm
}
// Register registers (caches) a model to primitive object relationship.
func (mm *modelManager) Register(primitive core.PdfObject, model PdfModel) {
mm.primitiveCache[model] = primitive
mm.modelCache[primitive] = model
}
// GetPrimitiveFromModel returns the primitive object corresponding to the input `model`.
func (mm *modelManager) GetPrimitiveFromModel(model PdfModel) core.PdfObject {
primitive, has := mm.primitiveCache[model]
if !has {
return nil
}
return primitive
}
// GetModelFromPrimitive returns the model corresponding to the `primitive` PdfObject.
func (mm *modelManager) GetModelFromPrimitive(primitive core.PdfObject) PdfModel {
model, has := mm.modelCache[primitive]
if !has {
return nil
}
return model
}