57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package model
|
|
|
|
import (
|
|
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/core"
|
|
)
|
|
|
|
// A PDFModel is a higher level PDF construct which can be collapsed into a PDF primitive.
|
|
// Each PDFModel has an underlying Primitive and vice versa.
|
|
// Copies can be made, but care must be taken to do it properly.
|
|
type PdfModel interface {
|
|
ToPdfObject() core.PdfObject
|
|
GetContainingPdfObject() core.PdfObject
|
|
}
|
|
|
|
// The model manager is used to cache Primitive <-> Model mappings where needed.
|
|
// In many cases only Model -> Primitive mapping is needed and only a reference to the Primitive
|
|
// is stored in the Model. In some cases, the Model needs to be found from the Primitive,
|
|
// and that is where the ModelManager can be used (in both directions).
|
|
//
|
|
// Note that it is not always used, the Primitive <-> 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
|
|
}
|
|
|
|
func NewModelManager() *ModelManager {
|
|
mm := ModelManager{}
|
|
mm.primitiveCache = map[PdfModel]core.PdfObject{}
|
|
mm.modelCache = map[core.PdfObject]PdfModel{}
|
|
return &mm
|
|
}
|
|
|
|
// Register (cache) a model to primitive relationship.
|
|
func (mm *ModelManager) Register(primitive core.PdfObject, model PdfModel) {
|
|
mm.primitiveCache[model] = primitive
|
|
mm.modelCache[primitive] = model
|
|
}
|
|
|
|
func (mm *ModelManager) GetPrimitiveFromModel(model PdfModel) core.PdfObject {
|
|
primitive, has := mm.primitiveCache[model]
|
|
if !has {
|
|
return nil
|
|
}
|
|
return primitive
|
|
}
|
|
|
|
func (mm *ModelManager) GetModelFromPrimitive(primitive core.PdfObject) PdfModel {
|
|
model, has := mm.modelCache[primitive]
|
|
if !has {
|
|
return nil
|
|
}
|
|
return model
|
|
}
|