Ai
1 Star 0 Fork 0

coorrer/unipdf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
shading.go 29.03 KB
一键复制 编辑 原始数据 按行查看 历史
xushuai 提交于 2025-09-24 14:26 +08:00 . fix
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
package model
import (
"errors"
"gitee.com/coorrer/unipdf/common"
"gitee.com/coorrer/unipdf/core"
)
// PdfShading represents a shading dictionary. There are 7 types of shading,
// indicatedby the shading type variable:
// 1: Function-based shading.
// 2: Axial shading.
// 3: Radial shading.
// 4: Free-form Gouraud-shaded triangle mesh.
// 5: Lattice-form Gouraud-shaded triangle mesh.
// 6: Coons patch mesh.
// 7: Tensor-product patch mesh.
// types 4-7 are contained in a stream object, where the dictionary is given by the stream dictionary.
type PdfShading struct {
ShadingType *core.PdfObjectInteger
ColorSpace PdfColorspace
Background *core.PdfObjectArray
BBox *PdfRectangle
AntiAlias *core.PdfObjectBool
context PdfModel // The sub shading type entry (types 1-7). Represented by PdfShadingType1-7.
container core.PdfObject // The container. Can be stream, indirect object, or dictionary.
}
// GetContainingPdfObject returns the container of the shading object (indirect object).
func (s *PdfShading) GetContainingPdfObject() core.PdfObject {
return s.container
}
// GetContext returns a reference to the subshading entry as represented by PdfShadingType1-7.
func (s *PdfShading) GetContext() PdfModel {
return s.context
}
// SetContext set the sub annotation (context).
func (s *PdfShading) SetContext(ctx PdfModel) {
s.context = ctx
}
func (s *PdfShading) getShadingDict() (*core.PdfObjectDictionary, error) {
obj := s.container
if indObj, isInd := obj.(*core.PdfIndirectObject); isInd {
d, ok := indObj.PdfObject.(*core.PdfObjectDictionary)
if !ok {
return nil, core.ErrTypeError
}
return d, nil
} else if streamObj, isStream := obj.(*core.PdfObjectStream); isStream {
return streamObj.PdfObjectDictionary, nil
} else if d, isDict := obj.(*core.PdfObjectDictionary); isDict {
return d, nil
} else {
common.Log.Debug("Unable to access shading dictionary")
return nil, core.ErrTypeError
}
}
// PdfShadingType1 is a Function-based shading.
type PdfShadingType1 struct {
*PdfShading
Domain *core.PdfObjectArray
Matrix *core.PdfObjectArray
Function []PdfFunction
}
// PdfShadingType2 is an Axial shading.
type PdfShadingType2 struct {
*PdfShading
Coords *core.PdfObjectArray
Domain *core.PdfObjectArray
Function []PdfFunction
Extend *core.PdfObjectArray
}
// PdfShadingType3 is a Radial shading.
type PdfShadingType3 struct {
*PdfShading
Coords *core.PdfObjectArray
Domain *core.PdfObjectArray
Function []PdfFunction
Extend *core.PdfObjectArray
}
// PdfShadingType4 is a Free-form Gouraud-shaded triangle mesh.
type PdfShadingType4 struct {
*PdfShading
BitsPerCoordinate *core.PdfObjectInteger
BitsPerComponent *core.PdfObjectInteger
BitsPerFlag *core.PdfObjectInteger
Decode *core.PdfObjectArray
Function []PdfFunction
}
// PdfShadingType5 is a Lattice-form Gouraud-shaded triangle mesh.
type PdfShadingType5 struct {
*PdfShading
BitsPerCoordinate *core.PdfObjectInteger
BitsPerComponent *core.PdfObjectInteger
VerticesPerRow *core.PdfObjectInteger
Decode *core.PdfObjectArray
Function []PdfFunction
}
// PdfShadingType6 is a Coons patch mesh.
type PdfShadingType6 struct {
*PdfShading
BitsPerCoordinate *core.PdfObjectInteger
BitsPerComponent *core.PdfObjectInteger
BitsPerFlag *core.PdfObjectInteger
Decode *core.PdfObjectArray
Function []PdfFunction
}
// PdfShadingType7 is a Tensor-product patch mesh.
type PdfShadingType7 struct {
*PdfShading
BitsPerCoordinate *core.PdfObjectInteger
BitsPerComponent *core.PdfObjectInteger
BitsPerFlag *core.PdfObjectInteger
Decode *core.PdfObjectArray
Function []PdfFunction
}
// Used for PDF parsing. Loads the PDF shading from a PDF object.
// Can be either an indirect object (types 1-3) containing the dictionary, or
// a stream object with the stream dictionary containing the shading dictionary (types 4-7).
func newPdfShadingFromPdfObject(obj core.PdfObject) (*PdfShading, error) {
shading := &PdfShading{}
var dict *core.PdfObjectDictionary
if indObj, isInd := core.GetIndirect(obj); isInd {
shading.container = indObj
d, ok := indObj.PdfObject.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("Object not a dictionary type")
return nil, core.ErrTypeError
}
dict = d
} else if streamObj, isStream := core.GetStream(obj); isStream {
shading.container = streamObj
dict = streamObj.PdfObjectDictionary
} else if d, isDict := core.GetDict(obj); isDict {
shading.container = d
dict = d
} else {
common.Log.Debug("Object type unexpected (%T)", obj)
return nil, core.ErrTypeError
}
if dict == nil {
common.Log.Debug("Dictionary missing")
return nil, errors.New("dict missing")
}
// Shading type (required).
obj = dict.Get("ShadingType")
if obj == nil {
common.Log.Debug("Required shading type missing")
return nil, ErrRequiredAttributeMissing
}
obj = core.TraceToDirectObject(obj)
shadingType, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("Invalid type for shading type (%T)", obj)
return nil, core.ErrTypeError
}
if *shadingType < 1 || *shadingType > 7 {
common.Log.Debug("Invalid shading type, not 1-7 (got %d)", *shadingType)
return nil, core.ErrTypeError
}
shading.ShadingType = shadingType
// Color space (required).
obj = dict.Get("ColorSpace")
if obj == nil {
common.Log.Debug("Required ColorSpace entry missing")
return nil, ErrRequiredAttributeMissing
}
cs, err := NewPdfColorspaceFromPdfObject(obj)
if err != nil {
common.Log.Debug("Failed loading colorspace: %v", err)
return nil, err
}
shading.ColorSpace = cs
// Background (optional). Array of color components.
obj = dict.Get("Background")
if obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Background should be specified by an array (got %T)", obj)
return nil, core.ErrTypeError
}
shading.Background = arr
}
// BBox.
obj = dict.Get("BBox")
if obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Background 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
}
shading.BBox = rect
}
// AntiAlias.
obj = dict.Get("AntiAlias")
if obj != nil {
obj = core.TraceToDirectObject(obj)
val, ok := obj.(*core.PdfObjectBool)
if !ok {
common.Log.Debug("AntiAlias invalid type, should be bool (got %T)", obj)
return nil, core.ErrTypeError
}
shading.AntiAlias = val
}
// Load specific shading type specific entries.
switch *shadingType {
case 1:
ctx, err := newPdfShadingType1FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
case 2:
ctx, err := newPdfShadingType2FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
case 3:
ctx, err := newPdfShadingType3FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
case 4:
ctx, err := newPdfShadingType4FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
case 5:
ctx, err := newPdfShadingType5FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
case 6:
ctx, err := newPdfShadingType6FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
case 7:
ctx, err := newPdfShadingType7FromDictionary(dict)
if err != nil {
return nil, err
}
ctx.PdfShading = shading
shading.context = ctx
return shading, nil
}
return nil, errors.New("unknown shading type")
}
// Load shading type 1 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType1FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType1, error) {
shading := PdfShadingType1{}
// Domain (optional).
if obj := dict.Get("Domain"); obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Domain not an array (got %T)", obj)
return nil, errors.New("type check error")
}
shading.Domain = arr
}
// Matrix (optional).
if obj := dict.Get("Matrix"); obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Matrix not an array (got %T)", obj)
return nil, errors.New("type check error")
}
shading.Matrix = arr
}
// Function (required).
obj := dict.Get("Function")
if obj == nil {
common.Log.Debug("Required attribute missing: Function")
return nil, ErrRequiredAttributeMissing
}
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
return &shading, nil
}
// Load shading type 2 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType2FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType2, error) {
shading := PdfShadingType2{}
// Coords (required).
obj := dict.Get("Coords")
if obj == nil {
common.Log.Debug("Required attribute missing: Coords")
return nil, ErrRequiredAttributeMissing
}
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Coords not an array (got %T)", obj)
return nil, errors.New("type check error")
}
if arr.Len() != 4 {
common.Log.Debug("Coords length not 4 (got %d)", arr.Len())
return nil, errors.New("invalid attribute")
}
shading.Coords = arr
// Domain (optional).
if obj := dict.Get("Domain"); obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Domain not an array (got %T)", obj)
return nil, errors.New("type check error")
}
shading.Domain = arr
}
// Function (required).
obj = dict.Get("Function")
if obj == nil {
common.Log.Debug("Required attribute missing: Function")
return nil, ErrRequiredAttributeMissing
}
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
// Extend (optional).
if obj := dict.Get("Extend"); obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Matrix not an array (got %T)", obj)
return nil, core.ErrTypeError
}
if arr.Len() != 2 {
common.Log.Debug("Extend length not 2 (got %d)", arr.Len())
return nil, ErrInvalidAttribute
}
shading.Extend = arr
}
return &shading, nil
}
// Load shading type 3 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType3FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType3, error) {
shading := PdfShadingType3{}
// Coords (required).
obj := dict.Get("Coords")
if obj == nil {
common.Log.Debug("Required attribute missing: Coords")
return nil, ErrRequiredAttributeMissing
}
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Coords not an array (got %T)", obj)
return nil, core.ErrTypeError
}
if arr.Len() != 6 {
common.Log.Debug("Coords length not 6 (got %d)", arr.Len())
return nil, ErrInvalidAttribute
}
shading.Coords = arr
// Domain (optional).
if obj := dict.Get("Domain"); obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Domain not an array (got %T)", obj)
return nil, core.ErrTypeError
}
shading.Domain = arr
}
// Function (required).
obj = dict.Get("Function")
if obj == nil {
common.Log.Debug("Required attribute missing: Function")
return nil, ErrRequiredAttributeMissing
}
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
// Extend (optional).
if obj := dict.Get("Extend"); obj != nil {
obj = core.TraceToDirectObject(obj)
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Matrix not an array (got %T)", obj)
return nil, core.ErrTypeError
}
if arr.Len() != 2 {
common.Log.Debug("Extend length not 2 (got %d)", arr.Len())
return nil, ErrInvalidAttribute
}
shading.Extend = arr
}
return &shading, nil
}
// Load shading type 4 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType4FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType4, error) {
shading := PdfShadingType4{}
// BitsPerCoordinate (required).
obj := dict.Get("BitsPerCoordinate")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerCoordinate")
return nil, ErrRequiredAttributeMissing
}
integer, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerCoordinate not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerCoordinate = integer
// BitsPerComponent (required).
obj = dict.Get("BitsPerComponent")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerComponent")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerComponent not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// BitsPerFlag (required).
obj = dict.Get("BitsPerFlag")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerFlag")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerFlag not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// Decode (required).
obj = dict.Get("Decode")
if obj == nil {
common.Log.Debug("Required attribute missing: Decode")
return nil, ErrRequiredAttributeMissing
}
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Decode not an array (got %T)", obj)
return nil, core.ErrTypeError
}
shading.Decode = arr
// Function (required).
obj = dict.Get("Function")
if obj == nil {
common.Log.Debug("Required attribute missing: Function")
return nil, ErrRequiredAttributeMissing
}
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
return &shading, nil
}
// Load shading type 5 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType5FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType5, error) {
shading := PdfShadingType5{}
// BitsPerCoordinate (required).
obj := dict.Get("BitsPerCoordinate")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerCoordinate")
return nil, ErrRequiredAttributeMissing
}
integer, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerCoordinate not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerCoordinate = integer
// BitsPerComponent (required).
obj = dict.Get("BitsPerComponent")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerComponent")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerComponent not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// VerticesPerRow (required).
obj = dict.Get("VerticesPerRow")
if obj == nil {
common.Log.Debug("Required attribute missing: VerticesPerRow")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("VerticesPerRow not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.VerticesPerRow = integer
// Decode (required).
obj = dict.Get("Decode")
if obj == nil {
common.Log.Debug("Required attribute missing: Decode")
return nil, ErrRequiredAttributeMissing
}
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Decode not an array (got %T)", obj)
return nil, core.ErrTypeError
}
shading.Decode = arr
// Function (optional).
if obj := dict.Get("Function"); obj != nil {
// Function (required).
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
}
return &shading, nil
}
// Load shading type 6 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType6FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType6, error) {
shading := PdfShadingType6{}
// BitsPerCoordinate (required).
obj := dict.Get("BitsPerCoordinate")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerCoordinate")
return nil, ErrRequiredAttributeMissing
}
integer, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerCoordinate not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerCoordinate = integer
// BitsPerComponent (required).
obj = dict.Get("BitsPerComponent")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerComponent")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerComponent not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// BitsPerFlag (required).
obj = dict.Get("BitsPerFlag")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerFlag")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerFlag not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// Decode (required).
obj = dict.Get("Decode")
if obj == nil {
common.Log.Debug("Required attribute missing: Decode")
return nil, ErrRequiredAttributeMissing
}
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Decode not an array (got %T)", obj)
return nil, core.ErrTypeError
}
shading.Decode = arr
// Function (optional).
if obj := dict.Get("Function"); obj != nil {
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
}
return &shading, nil
}
// Load shading type 7 specific attributes from pdf object.
// Used in parsing/loading PDFs.
func newPdfShadingType7FromDictionary(dict *core.PdfObjectDictionary) (*PdfShadingType7, error) {
shading := PdfShadingType7{}
// BitsPerCoordinate (required).
obj := dict.Get("BitsPerCoordinate")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerCoordinate")
return nil, ErrRequiredAttributeMissing
}
integer, ok := obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerCoordinate not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerCoordinate = integer
// BitsPerComponent (required).
obj = dict.Get("BitsPerComponent")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerComponent")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerComponent not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// BitsPerFlag (required).
obj = dict.Get("BitsPerFlag")
if obj == nil {
common.Log.Debug("Required attribute missing: BitsPerFlag")
return nil, ErrRequiredAttributeMissing
}
integer, ok = obj.(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("BitsPerFlag not an integer (got %T)", obj)
return nil, core.ErrTypeError
}
shading.BitsPerComponent = integer
// Decode (required).
obj = dict.Get("Decode")
if obj == nil {
common.Log.Debug("Required attribute missing: Decode")
return nil, ErrRequiredAttributeMissing
}
arr, ok := obj.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Decode not an array (got %T)", obj)
return nil, core.ErrTypeError
}
shading.Decode = arr
// Function (optional).
if obj := dict.Get("Function"); obj != nil {
shading.Function = []PdfFunction{}
if array, is := obj.(*core.PdfObjectArray); is {
for _, obj := range array.Elements() {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
} else {
function, err := newPdfFunctionFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error parsing function: %v", err)
return nil, err
}
shading.Function = append(shading.Function, function)
}
}
return &shading, nil
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShading) ToPdfObject() core.PdfObject {
container := s.container
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.ShadingType != nil {
d.Set("ShadingType", s.ShadingType)
}
if s.ColorSpace != nil {
d.Set("ColorSpace", s.ColorSpace.ToPdfObject())
}
if s.Background != nil {
d.Set("Background", s.Background)
}
if s.BBox != nil {
d.Set("BBox", s.BBox.ToPdfObject())
}
if s.AntiAlias != nil {
d.Set("AntiAlias", s.AntiAlias)
}
return container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType1) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.Domain != nil {
d.Set("Domain", s.Domain)
}
if s.Matrix != nil {
d.Set("Matrix", s.Matrix)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
return s.container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType2) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if d == nil {
common.Log.Error("Shading dict is nil")
return nil
}
if s.Coords != nil {
d.Set("Coords", s.Coords)
}
if s.Domain != nil {
d.Set("Domain", s.Domain)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
if s.Extend != nil {
d.Set("Extend", s.Extend)
}
return s.container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType3) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.Coords != nil {
d.Set("Coords", s.Coords)
}
if s.Domain != nil {
d.Set("Domain", s.Domain)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
if s.Extend != nil {
d.Set("Extend", s.Extend)
}
return s.container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType4) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.BitsPerCoordinate != nil {
d.Set("BitsPerCoordinate", s.BitsPerCoordinate)
}
if s.BitsPerComponent != nil {
d.Set("BitsPerComponent", s.BitsPerComponent)
}
if s.BitsPerFlag != nil {
d.Set("BitsPerFlag", s.BitsPerFlag)
}
if s.Decode != nil {
d.Set("Decode", s.Decode)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
return s.container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType5) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.BitsPerCoordinate != nil {
d.Set("BitsPerCoordinate", s.BitsPerCoordinate)
}
if s.BitsPerComponent != nil {
d.Set("BitsPerComponent", s.BitsPerComponent)
}
if s.VerticesPerRow != nil {
d.Set("VerticesPerRow", s.VerticesPerRow)
}
if s.Decode != nil {
d.Set("Decode", s.Decode)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
return s.container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType6) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.BitsPerCoordinate != nil {
d.Set("BitsPerCoordinate", s.BitsPerCoordinate)
}
if s.BitsPerComponent != nil {
d.Set("BitsPerComponent", s.BitsPerComponent)
}
if s.BitsPerFlag != nil {
d.Set("BitsPerFlag", s.BitsPerFlag)
}
if s.Decode != nil {
d.Set("Decode", s.Decode)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
return s.container
}
// ToPdfObject returns the PDF representation of the shading dictionary.
func (s *PdfShadingType7) ToPdfObject() core.PdfObject {
s.PdfShading.ToPdfObject()
d, err := s.getShadingDict()
if err != nil {
common.Log.Error("Unable to access shading dict")
return nil
}
if s.BitsPerCoordinate != nil {
d.Set("BitsPerCoordinate", s.BitsPerCoordinate)
}
if s.BitsPerComponent != nil {
d.Set("BitsPerComponent", s.BitsPerComponent)
}
if s.BitsPerFlag != nil {
d.Set("BitsPerFlag", s.BitsPerFlag)
}
if s.Decode != nil {
d.Set("Decode", s.Decode)
}
if s.Function != nil {
if len(s.Function) == 1 {
d.Set("Function", s.Function[0].ToPdfObject())
} else {
farr := core.MakeArray()
for _, f := range s.Function {
farr.Append(f.ToPdfObject())
}
d.Set("Function", farr)
}
}
return s.container
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/coorrer/unipdf.git
git@gitee.com:coorrer/unipdf.git
coorrer
unipdf
unipdf
v1.3.0

搜索帮助