TPDFContentStateTracker

Content stream editing

Description

Applies parsed content instructions to a single persistent effective graphics state and a complete q/Q stack

Reuse one tracker across consecutive TPDFContentProgram instances when a page spreads its logical content sequence over several streams

Syntax

Delphi

TPDFContentStateTracker = Class Procedure Reset; Procedure ClearExtGStates; Procedure ClearXObjects; Procedure ClearFonts; Procedure ClearProperties; Function RegisterExtGState(Const ResourceName: AnsiString; Dictionary: TPDFDictionary): Boolean; Function RegisterXObject(Const ResourceName: AnsiString; XObjectStream: TPDFStream; ObjectNumber: Integer = 0): Boolean; Function RegisterFont(Const ResourceName: AnsiString; FontObject: TPDFObject; ResourceScope: Integer): Boolean; Function RegisterProperty(Const ResourceName: AnsiString; PropertyObject: TPDFObject): Boolean; Procedure BeginContentStream; Procedure SetContentLocation(Layer, ContentDepth, ContentInvocationID: Integer); Procedure InitializeForm(Const ParentState: TPDFContentGraphicsState; Const FormMatrix: TPDFContentMatrix; MatrixKnown: Boolean; BBoxLeft, BBoxBottom, BBoxRight, BBoxTop: Double); Procedure SetInitialClipBounds(Left, Bottom, Right, Top: Double); Procedure Apply(Inst: TPDFContentInstruction); Function Snapshot: TPDFContentGraphicsState; Function SnapshotPathSegments: TPDFContentClipSegmentArray; Property StackDepth: Integer; Property RestoreUnderflowCount: Integer; Property MarkedContentUnderflowCount: Integer; Property ResourceScope: Integer; Property OnMeasureText: TPDFContentMeasureTextEvent; End;

Remarks

Snapshot returns the effective graphics state, whilst SnapshotPathSegments returns a copy of the construction-path segments (moves, lines and curves) tracked so far, for consumers that need the current path geometry and not just its bounds

RegisterExtGState validates and precompiles a single external graphics-state dictionary so that repeated gs operators do not rescan the PDF resource tree

RegisterXObject precompiles Image, Form and PostScript stream metadata so that repeated Do operators resolve through constant-time name lookups; Form bounds use the form matrix, the active CTM and all four /BBox corners

RegisterFont binds a resource name to its indirect object and resource scope, so identical names in nested content do not clash

RegisterProperty precompiles one named property-list dictionary from the page's /Properties resources, capturing its entries and optional /MCID, so marked-content operators that refer to the property by name resolve through constant-time lookups; ClearProperties drops every registered property dictionary

BeginContentStream clears the marked-content stack and its underflow counter at a stream boundary, so tracing several consecutive streams keeps the BDC/EMC pairing depth anchored at zero; SetContentLocation records the current optional-content layer, nesting depth and content invocation counter for location-aware consumers of the trace

InitializeForm seeds a child tracker from the invoking state, concatenates the Form matrix and intersects clipping with the transformed /BBox

External parameters apply cumulatively and take part in complete q/Q save and restore operations

SetInitialClipBounds sets up the page boundary for standalone tracing; path construction is transformed straight away, W and W* stay pending until the next path-ending operator, and q/Q restores clipping without restoring the current path

OnMeasureText supplies the exact font displacement and writing mode for Tj, TJ, ' and "; without it, text anchors stay where they are and the snapshot reports the displacement as unresolved

RestoreUnderflowCount reports unmatched Q operators without corrupting the current state; MarkedContentUnderflowCount does the same for unmatched EMC operators

The tracker owns no instructions or PDF dictionaries and can be reused after Reset