package contentstream import ( "errors" "gitea.tecamino.com/paadi/pdfmerge/internal/pdf/common" "gitea.tecamino.com/paadi/pdfmerge/internal/pdf/core" "gitea.tecamino.com/paadi/pdfmerge/internal/pdf/model" ) // Basic graphics state implementation. // Initially only implementing and tracking a portion of the information specified. Easy to add more. type GraphicsState struct { ColorspaceStroking model.PdfColorspace ColorspaceNonStroking model.PdfColorspace ColorStroking model.PdfColor ColorNonStroking model.PdfColor } type GraphicStateStack []GraphicsState func (gsStack *GraphicStateStack) Push(gs GraphicsState) { *gsStack = append(*gsStack, gs) } func (gsStack *GraphicStateStack) Pop() GraphicsState { gs := (*gsStack)[len(*gsStack)-1] *gsStack = (*gsStack)[:len(*gsStack)-1] return gs } // ContentStreamProcessor defines a data structure and methods for processing a content stream, keeping track of the // current graphics state, and allowing external handlers to define their own functions as a part of the processing, // for example rendering or extracting certain information. type ContentStreamProcessor struct { graphicsStack GraphicStateStack operations []*ContentStreamOperation graphicsState GraphicsState handlers []HandlerEntry currentIndex int } type HandlerFunc func(op *ContentStreamOperation, gs GraphicsState, resources *model.PdfPageResources) error type HandlerEntry struct { Condition HandlerConditionEnum Operand string Handler HandlerFunc } type HandlerConditionEnum int func (ce HandlerConditionEnum) All() bool { return ce == HandlerConditionEnumAllOperands } func (ce HandlerConditionEnum) Operand() bool { return ce == HandlerConditionEnumOperand } const ( HandlerConditionEnumOperand HandlerConditionEnum = iota HandlerConditionEnumAllOperands HandlerConditionEnum = iota ) func NewContentStreamProcessor(ops []*ContentStreamOperation) *ContentStreamProcessor { csp := ContentStreamProcessor{} csp.graphicsStack = GraphicStateStack{} // Set defaults.. gs := GraphicsState{} csp.graphicsState = gs csp.handlers = []HandlerEntry{} csp.currentIndex = 0 csp.operations = ops return &csp } func (csp *ContentStreamProcessor) AddHandler(condition HandlerConditionEnum, operand string, handler HandlerFunc) { entry := HandlerEntry{} entry.Condition = condition entry.Operand = operand entry.Handler = handler csp.handlers = append(csp.handlers, entry) } func (csp *ContentStreamProcessor) getColorspace(name string, resources *model.PdfPageResources) (model.PdfColorspace, error) { switch name { case "DeviceGray": return model.NewPdfColorspaceDeviceGray(), nil case "DeviceRGB": return model.NewPdfColorspaceDeviceRGB(), nil case "DeviceCMYK": return model.NewPdfColorspaceDeviceCMYK(), nil case "Pattern": return model.NewPdfColorspaceSpecialPattern(), nil } // Next check the colorspace dictionary. cs, has := resources.ColorSpace.Colorspaces[name] if has { return cs, nil } // Lastly check other potential colormaps. switch name { case "CalGray": return model.NewPdfColorspaceCalGray(), nil case "CalRGB": return model.NewPdfColorspaceCalRGB(), nil case "Lab": return model.NewPdfColorspaceLab(), nil } // Otherwise unsupported. common.Log.Debug("Unknown colorspace requested: %s", name) return nil, errors.New("unsupported colorspace") } // Get initial color for a given colorspace. func (csp *ContentStreamProcessor) getInitialColor(cs model.PdfColorspace) (model.PdfColor, error) { switch cs := cs.(type) { case *model.PdfColorspaceDeviceGray: return model.NewPdfColorDeviceGray(0.0), nil case *model.PdfColorspaceDeviceRGB: return model.NewPdfColorDeviceRGB(0.0, 0.0, 0.0), nil case *model.PdfColorspaceDeviceCMYK: return model.NewPdfColorDeviceCMYK(0.0, 0.0, 0.0, 1.0), nil case *model.PdfColorspaceCalGray: return model.NewPdfColorCalGray(0.0), nil case *model.PdfColorspaceCalRGB: return model.NewPdfColorCalRGB(0.0, 0.0, 0.0), nil case *model.PdfColorspaceLab: l := 0.0 a := 0.0 b := 0.0 if cs.Range[0] > 0 { l = cs.Range[0] } if cs.Range[2] > 0 { a = cs.Range[2] } return model.NewPdfColorLab(l, a, b), nil case *model.PdfColorspaceICCBased: if cs.Alternate == nil { // Alternate not defined. // Try to fall back to DeviceGray, DeviceRGB or DeviceCMYK. common.Log.Trace("ICC Based not defined - attempting fall back (N = %d)", cs.N) switch cs.N { case 1: common.Log.Trace("Falling back to DeviceGray") return csp.getInitialColor(model.NewPdfColorspaceDeviceGray()) case 3: common.Log.Trace("Falling back to DeviceRGB") return csp.getInitialColor(model.NewPdfColorspaceDeviceRGB()) case 4: common.Log.Trace("Falling back to DeviceCMYK") return csp.getInitialColor(model.NewPdfColorspaceDeviceCMYK()) default: return nil, errors.New("alternate space not defined for ICC") } } return csp.getInitialColor(cs.Alternate) case *model.PdfColorspaceSpecialIndexed: if cs.Base == nil { return nil, errors.New("indexed base not specified") } return csp.getInitialColor(cs.Base) case *model.PdfColorspaceSpecialSeparation: if cs.AlternateSpace == nil { return nil, errors.New("alternate space not specified") } return csp.getInitialColor(cs.AlternateSpace) case *model.PdfColorspaceDeviceN: if cs.AlternateSpace == nil { return nil, errors.New("alternate space not specified") } return csp.getInitialColor(cs.AlternateSpace) case *model.PdfColorspaceSpecialPattern: // FIXME/check: A pattern does not have an initial color... return nil, nil } common.Log.Debug("Unable to determine initial color for unknown colorspace: %T", cs) return nil, errors.New("unsupported colorspace") } // Process the entire operations. func (ce *ContentStreamProcessor) Process(resources *model.PdfPageResources) error { // Initialize graphics state ce.graphicsState.ColorspaceStroking = model.NewPdfColorspaceDeviceGray() ce.graphicsState.ColorspaceNonStroking = model.NewPdfColorspaceDeviceGray() ce.graphicsState.ColorStroking = model.NewPdfColorDeviceGray(0) ce.graphicsState.ColorNonStroking = model.NewPdfColorDeviceGray(0) for _, op := range ce.operations { var err error // Internal handling. switch op.Operand { case "q": ce.graphicsStack.Push(ce.graphicsState) case "Q": ce.graphicsState = ce.graphicsStack.Pop() // Color operations (Table 74 p. 179) case "CS": err = ce.handleCommand_CS(op, resources) case "cs": err = ce.handleCommand_cs(op, resources) case "SC": err = ce.handleCommand_SC(op) case "SCN": err = ce.handleCommand_SCN(op) case "sc": err = ce.handleCommand_sc(op) case "scn": err = ce.handleCommand_scn(op) case "G": err = ce.handleCommand_G(op) case "g": err = ce.handleCommand_g(op) case "RG": err = ce.handleCommand_RG(op) case "rg": err = ce.handleCommand_rg(op) case "K": err = ce.handleCommand_K(op) case "k": err = ce.handleCommand_k(op) } if err != nil { common.Log.Debug("Processor handling error (%s): %v", op.Operand, err) common.Log.Debug("Operand: %#v", op.Operand) return err } // Check if have external handler also, and process if so. for _, entry := range ce.handlers { var err error if entry.Condition.All() { err = entry.Handler(op, ce.graphicsState, resources) } else if entry.Condition.Operand() && op.Operand == entry.Operand { err = entry.Handler(op, ce.graphicsState, resources) } if err != nil { common.Log.Debug("Processor handler error: %v", err) return err } } } return nil } // CS: Set the current color space for stroking operations. func (csp *ContentStreamProcessor) handleCommand_CS(op *ContentStreamOperation, resources *model.PdfPageResources) error { if len(op.Params) < 1 { common.Log.Debug("invalid cs command, skipping over") return errors.New("too few parameters") } if len(op.Params) > 1 { common.Log.Debug("cs command with too many parameters - continuing") return errors.New("too many parameters") } name, ok := op.Params[0].(*core.PdfObjectName) if !ok { common.Log.Debug("error: cs command with invalid parameter, skipping over") return errors.New("type check error") } // Set the current color space to use for stroking operations. // Either device based or referring to resource dict. cs, err := csp.getColorspace(string(*name), resources) if err != nil { return err } csp.graphicsState.ColorspaceStroking = cs // Set initial color. color, err := csp.getInitialColor(cs) if err != nil { return err } csp.graphicsState.ColorStroking = color return nil } // cs: Set the current color space for non-stroking operations. func (csp *ContentStreamProcessor) handleCommand_cs(op *ContentStreamOperation, resources *model.PdfPageResources) error { if len(op.Params) < 1 { common.Log.Debug("invalid CS command, skipping over") return errors.New("too few parameters") } if len(op.Params) > 1 { common.Log.Debug("CS command with too many parameters - continuing") return errors.New("too many parameters") } name, ok := op.Params[0].(*core.PdfObjectName) if !ok { common.Log.Debug("error: CS command with invalid parameter, skipping over") return errors.New("type check error") } // Set the current color space to use for non-stroking operations. // Either device based or referring to resource dict. cs, err := csp.getColorspace(string(*name), resources) if err != nil { return err } csp.graphicsState.ColorspaceNonStroking = cs // Set initial color. color, err := csp.getInitialColor(cs) if err != nil { return err } csp.graphicsState.ColorNonStroking = color return nil } // SC: Set the color to use for stroking operations in a device, CIE-based or Indexed colorspace. (not ICC based) func (sp *ContentStreamProcessor) handleCommand_SC(op *ContentStreamOperation) error { // For DeviceGray, CalGray, Indexed: one operand is required // For DeviceRGB, CalRGB, Lab: 3 operands required cs := sp.graphicsState.ColorspaceStroking if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorStroking = color return nil } func isPatternCS(cs model.PdfColorspace) bool { _, isPattern := cs.(*model.PdfColorspaceSpecialPattern) return isPattern } // SCN: Same as SC but also supports Pattern, Separation, DeviceN and ICCBased color spaces. func (sp *ContentStreamProcessor) handleCommand_SCN(op *ContentStreamOperation) error { cs := sp.graphicsState.ColorspaceStroking if !isPatternCS(cs) { if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorStroking = color return nil } // sc: Same as SC except used for non-stroking operations. func (sp *ContentStreamProcessor) handleCommand_sc(op *ContentStreamOperation) error { cs := sp.graphicsState.ColorspaceNonStroking if !isPatternCS(cs) { if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorNonStroking = color return nil } // scn: Same as SCN except used for non-stroking operations. func (sp *ContentStreamProcessor) handleCommand_scn(op *ContentStreamOperation) error { cs := sp.graphicsState.ColorspaceNonStroking if !isPatternCS(cs) { if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { common.Log.Debug("error: Fail to get color from params: %+v (CS is %+v)", op.Params, cs) return err } sp.graphicsState.ColorNonStroking = color return nil } // G: Set the stroking colorspace to DeviceGray, and the color to the specified graylevel (range [0-1]). // gray G func (sp *ContentStreamProcessor) handleCommand_G(op *ContentStreamOperation) error { cs := model.NewPdfColorspaceDeviceGray() if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorspaceStroking = cs sp.graphicsState.ColorStroking = color return nil } // g: Same as G, but for non-stroking colorspace and color (range [0-1]). // gray g func (sp *ContentStreamProcessor) handleCommand_g(op *ContentStreamOperation) error { cs := model.NewPdfColorspaceDeviceGray() if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorspaceNonStroking = cs sp.graphicsState.ColorNonStroking = color return nil } // RG: Sets the stroking colorspace to DeviceRGB and the stroking color to r,g,b. [0-1] ranges. // r g b RG func (sp *ContentStreamProcessor) handleCommand_RG(op *ContentStreamOperation) error { cs := model.NewPdfColorspaceDeviceRGB() if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorspaceStroking = cs sp.graphicsState.ColorStroking = color return nil } // rg: Same as RG but for non-stroking colorspace, color. func (sp *ContentStreamProcessor) handleCommand_rg(op *ContentStreamOperation) error { cs := model.NewPdfColorspaceDeviceRGB() if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorspaceNonStroking = cs sp.graphicsState.ColorNonStroking = color return nil } // K: Sets the stroking colorspace to DeviceCMYK and the stroking color to c,m,y,k. [0-1] ranges. // c m y k K func (sp *ContentStreamProcessor) handleCommand_K(op *ContentStreamOperation) error { cs := model.NewPdfColorspaceDeviceCMYK() if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorspaceStroking = cs sp.graphicsState.ColorStroking = color return nil } // k: Same as K but for non-stroking colorspace, color. func (sp *ContentStreamProcessor) handleCommand_k(op *ContentStreamOperation) error { cs := model.NewPdfColorspaceDeviceCMYK() if len(op.Params) != cs.GetNumComponents() { common.Log.Debug("invalid number of parameters for SC") common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("invalid number of parameters") } color, err := cs.ColorFromPdfObjects(op.Params) if err != nil { return err } sp.graphicsState.ColorspaceNonStroking = cs sp.graphicsState.ColorNonStroking = color return nil }