1 Star 0 Fork 0

coodder / unipdf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
model.go 2.18 KB
一键复制 编辑 原始数据 按行查看 历史
jhonm 提交于 2023-08-07 15:31 . init
package model
import (
"gitee.com/coodder/unipdf/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
}
Go
1
https://gitee.com/coodder/unipdf.git
git@gitee.com:coodder/unipdf.git
coodder
unipdf
unipdf
v1.2.0

搜索帮助