github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/model.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package model 7 8 import ( 9 . "github.com/unidoc/unidoc/pdf/core" 10 ) 11 12 // A PDFModel is a higher level PDF construct which can be collapsed into a PDF primitive. 13 // Each PDFModel has an underlying Primitive and vice versa. 14 // Copies can be made, but care must be taken to do it properly. 15 type PdfModel interface { 16 ToPdfObject() PdfObject 17 GetContainingPdfObject() PdfObject 18 } 19 20 // The model manager is used to cache Primitive <-> Model mappings where needed. 21 // In many cases only Model -> Primitive mapping is needed and only a reference to the Primitive 22 // is stored in the Model. In some cases, the Model needs to be found from the Primitive, 23 // and that is where the ModelManager can be used (in both directions). 24 // 25 // Note that it is not always used, the Primitive <-> Model mapping needs to be registered 26 // for each time it is used. Thus, it is only used for special cases, commonly where the same 27 // object is used by two higher level objects. (Example PDF Widgets owned by both Page Annotations, 28 // and the interactive form - AcroForm). 29 type ModelManager struct { 30 primitiveCache map[PdfModel]PdfObject 31 modelCache map[PdfObject]PdfModel 32 } 33 34 func NewModelManager() *ModelManager { 35 mm := ModelManager{} 36 mm.primitiveCache = map[PdfModel]PdfObject{} 37 mm.modelCache = map[PdfObject]PdfModel{} 38 return &mm 39 } 40 41 // Register (cache) a model to primitive relationship. 42 func (this *ModelManager) Register(primitive PdfObject, model PdfModel) { 43 this.primitiveCache[model] = primitive 44 this.modelCache[primitive] = model 45 } 46 47 func (this *ModelManager) GetPrimitiveFromModel(model PdfModel) PdfObject { 48 primitive, has := this.primitiveCache[model] 49 if !has { 50 return nil 51 } 52 return primitive 53 } 54 55 func (this *ModelManager) GetModelFromPrimitive(primitive PdfObject) PdfModel { 56 model, has := this.modelCache[primitive] 57 if !has { 58 return nil 59 } 60 return model 61 }