/// import { Font } from '../font/index'; import { Resources } from '../models'; import { Point, Size, Mat3 } from './geometry'; export declare class Color { clone(): Color; toString(): string; } export declare class RGBColor extends Color { r: number; g: number; b: number; constructor(r: number, g: number, b: number); clone(): RGBColor; toString(): string; } export declare class GrayColor extends Color { alpha: number; constructor(alpha: number); clone(): GrayColor; toString(): string; } export declare class CMYKColor extends Color { c: number; m: number; y: number; k: number; constructor(c: number, m: number, y: number, k: number); clone(): CMYKColor; toString(): string; } export declare enum RenderingMode { Fill = 0, Stroke = 1, FillThenStroke = 2, None = 3, FillClipping = 4, StrokeClipping = 5, FillThenStrokeClipping = 6, NoneClipping = 7 } export declare enum LineCapStyle { Butt = 0, Round = 1, ProjectingSquare = 2 } export declare enum LineJoinStyle { Miter = 0, Round = 1, Bevel = 2 } export declare class TextState { charSpacing: number; wordSpacing: number; horizontalScaling: number; leading: number; fontName: string; fontSize: number; renderingMode: RenderingMode; rise: number; clone(): TextState; } /** We need to be able to clone it since we need a copy when we process a `pushGraphicsState` (`q`) command, and it'd be easier to clone if the variables were in the constructor, but there are a lot of variables! */ export declare class GraphicsState { ctMatrix: Mat3; strokeColor: Color; fillColor: Color; lineWidth: number; lineCap: LineCapStyle; lineJoin: LineJoinStyle; miterLimit: number; dashArray: number[]; dashPhase: number; renderingIntent: string; flatnessTolerance: number; textState: TextState; /** clone() creates an blank new GraphicsState object and recursively copies all of `this`'s properties to it. */ clone(): GraphicsState; } /** DrawingContext is kind of like a Canvas state, keeping track of where we are in painting the canvas. It's an abstraction away from the content stream and the rest of the PDF. the textState persists across BT and ET markers, and can be modified anywhere the textMatrix and textLineMatrix do not persist between distinct BT ... ET blocks I don't think textState transfers to (or out of) "Do"-drawn XObjects. E.g., P13-4028.pdf breaks if textState carries out of the drawn object. */ export declare abstract class DrawingContext { resourcesStack: Resources[]; graphicsStateStack: GraphicsState[]; /** textMatrix is a full 3x3 transformation matrix, even though the values at [2], [5], and [8] cannot be changed. */ textMatrix: Mat3; /** textLineMatrix operates much like textMatrix; see the docs for textMatrix */ textLineMatrix: Mat3; constructor(resources: Resources, graphicsState: GraphicsState); readonly graphicsState: GraphicsState; readonly resources: Resources; /** This is called from the various text drawing operations, like showString and showStrings. This function only works / is only correct for horizontal writing mode. It modifies {this.textMatrix}, and returns the amount of x translation. Reference: PDF32000_2008.pdf:9.4.4 */ protected advanceTextMatrix(width_units: number, chars: number, spaces: number): number; /** Compute the current drawing space text insertion point by combining {this.graphicsState} and {this.textMatrix}. In the specifications formulas, 'fs' is replaced by `fontSize` and 'fsh' is replaced by `fontSize * (horizontalScaling / 100.0)`. */ protected getTextPosition(): Point; /** Compute the current font size by combining a limited number of properties from the {this.graphicsState} and {this.textMatrix} values. This only scales / skews the size of the font; it ignore the positions of the textMatrix / ctMatrix. */ protected getTextSize(): number; /** > `q`: Save the current graphics state on the graphics state stack (see 8.4.2). */ pushGraphicsState(): void; /** > `Q`: Restore the graphics state by removing the most recently saved state > from the stack and making it the current state (see 8.4.2). */ popGraphicsState(): void; /** > `a b c d e f cm`: Modify the current transformation matrix (CTM) by > concatenating the specified matrix. Although the operands specify a matrix, > they shall be written as six separate numbers, not as an array. > Translations shall be specified as [1 0 0 1 tx ty], where tx and ty shall be the distances to translate the origin of the coordinate system in the horizontal and vertical dimensions, respectively. > * Scaling shall be obtained by [sx 0 0 sy 0 0]. This scales the coordinates so that 1 unit in the horizontal and vertical dimensions of the new coordinate system is the same size as sx and sy units, respectively, in the previous coordinate system. > * Rotations shall be produced by [cos q sin q -sin q cos q 0 0], which has the effect of rotating the coordinate system axes by an angle q counter clockwise. > * Skew shall be specified by [1 tan a tan b 1 0 0], which skews the xaxis by an angle a and the y axis by an angle b. Also see http://en.wikipedia.org/wiki/Linear_map#Examples_of_linear_transformation_matrices Should we multiply by the current one instead? Yes. That's what they mean by concatenating, apparently. Weird stuff happens if you replace. */ setCTM(a: number, b: number, c: number, d: number, e: number, f: number): void; /** > `name Do`: Paint the specified XObject. The operand name shall appear as a > key in the XObject subdictionary of the current resource dictionary. The > associated value shall be a stream whose Type entry, if present, is XObject. > The effect of Do depends on the value of the XObject's Subtype entry, which > may be Image, Form, or PS. When the Do operator is applied to a form XObject, a conforming reader shall perform the following tasks: a) Saves the current graphics state, as if by invoking the q operator b) Concatenates the matrix from the form dictionary’s Matrix entry with the current transformation matrix (CTM) c) Clips according to the form dictionary’s BBox entry d) Paints the graphics objects specified in the form’s content stream e) Restores the saved graphics state, as if by invoking the Q operator Except as described above, the initial graphics state for the form shall be inherited from the graphics state that is in effect at the time Do is invoked. */ abstract drawObject(name: string): any; /** > `lineWidth w`: Set the line width in the graphics state. */ setLineWidth(lineWidth: number): void; /** > `lineCap J`: Set the line cap style in the graphics state. */ setLineCap(lineCap: LineCapStyle): void; /** > `lineJoin j`: Set the line join style in the graphics state. */ setLineJoin(lineJoin: LineJoinStyle): void; /** > `miterLimit M`: Set the miter limit in the graphics state. */ setMiterLimit(miterLimit: number): void; /** > `dashArray dashPhase d`: Set the line dash pattern in the graphics state. */ setDashPattern(dashArray: number[], dashPhase: number): void; /** > `intent ri`: Set the colour rendering intent in the graphics state. > (PDF 1.1) */ setRenderingIntent(intent: string): void; /** > `flatness i`: Set the flatness tolerance in the graphics state. flatness is > a number in the range 0 to 100; a value of 0 shall specify the output > device's default flatness tolerance. */ setFlatnessTolerance(flatness: number): void; /** > `dictName gs`: Set the specified parameters in the graphics state. > `dictName` shall be the name of a graphics state parameter dictionary in > the ExtGState subdictionary of the current resource dictionary (see the > next sub-clause). (PDF 1.2) LW number (Optional; PDF 1.3) The line width LC integer (Optional; PDF 1.3) The line cap style LJ integer (Optional; PDF 1.3) The line join style ML number (Optional; PDF 1.3) The miter limit D array (Optional; PDF 1.3) The line dash pattern, expressed as an array of the form [dashArray dashPhase], where dashArray shall be itself an array and dashPhase shall be an integer RI name (Optional; PDF 1.3) The name of the rendering intent OP boolean (Optional) A flag specifying whether to apply overprint. In PDF 1.2 and earlier, there is a single overprint parameter that applies to all painting operations. Beginning with PDF 1.3, there shall be two separate overprint parameters: one for stroking and one for all other painting operations. Specifying an OP entry shall set both parameters unless there is also an op entry in the same graphics state parameter dictionary, in which case the OP entry shall set only the overprint parameter for stroking. op boolean (Optional; PDF 1.3) A flag specifying whether to apply overprint for painting operations other than stroking. If this entry is absent, the OP entry, if any, shall also set this parameter. OPM integer (Optional; PDF1.3) The overprint mode Font array (Optional; PDF 1.3) An array of the form [font size], where font shall be an indirect reference to a font dictionary and size shall be a number expressed in text space units. These two objects correspond to the operands of the Tf operator (see 9.3, "Text State Parameters and Operators"); however, the first operand shall be an indirect object reference instead of a resource name. BG function (Optional) The black-generation function, which maps the interval [0.0 1.0] to the interval [0.0 1.0] BG2 function or name (Optional; PDF 1.3) Same as BG except that the value may also be the name Default, denoting the black-generation function that was in effect at the start of the page. If both BG and BG2 are present in the same graphics state parameter dictionary, BG2 shall take precedence. UCR function (Optional) The undercolor-removal function, which maps the interval [0.0 1.0] to the interval [−1.0 1.0] UCR2 function or name (Optional; PDF 1.3) Same as UCR except that the value may also be the name Default, denoting the undercolor-removal function that was in effect at the start of the page. If both UCR and UCR2 are present in the same graphics state parameter dictionary, UCR2 shall take precedence. TR function, array, or name (Optional) The transfer function, which maps the interval [0.0 1.0] to the interval [0.0 1.0]. The value shall be either a single function (which applies to all process colorants) or an array of four functions (which apply to the process colorants individually). The name Identity may be used to represent the identity function. TR2 function, array, or name (Optional; PDF 1.3) Same as TR except that the value may also be the name Default, denoting the transfer function that was in effect at the start of the page. If both TR and TR2 are present in the same graphics state parameter dictionary, TR2 shall take precedence. HT dictionary, stream, or name (Optional) The halftone dictionary or stream or the name Default, denoting the halftone that was in effect at the start of the page. FL number (Optional; PDF 1.3) The flatness tolerance SM number (Optional; PDF1.3) The smoothness tolerance SA boolean (Optional) A flag specifying whether to apply automatic stroke adjustment. BM name or array (Optional; PDF 1.4) The current blend mode to be used in the transparent imaging model SMask dictionary or name (Optional; PDF 1.4) The current soft mask, specifying the mask shape or mask opacity values that shall be used in the transparent imaging model (see 11.3.7.2, "Source Shape and Opacity" and 11.6.4.3, "Mask Shape and Opacity"). Although the current soft mask is sometimes referred to as a "soft clip," altering it with the gs operator completely replaces the old value with the new one, rather than intersecting the two as is done with the current clipping path parameter CA number (Optional; PDF 1.4) The current stroking alpha constant, specifying the constant shape or constant opacity value that shall be used for stroking operations in the transparent imaging model ca number (Optional; PDF 1.4) Same as CA, but for nonstroking operations. AIS boolean (Optional; PDF1.4) The alpha source flag ("alpha is shape"), specifying whether the current soft mask and alpha constant shall be interpreted as shape values (true) or opacity values (false). TK boolean (Optional; PDF1.4) The text knockout flag, shall determine the behaviour of overlapping glyphs within a text object in the transparent imaging model */ setGraphicsStateParameters(dictName: string): void; /** `x y m` */ moveTo(x: number, y: number): void; /** `x y l` */ appendLine(x: number, y: number): void; /** `x1 y1 x2 y2 x3 y3 c` */ appendCurve123(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number): void; /** `x2 y2 x3 y3 v` */ appendCurve23(x2: number, y2: number, x3: number, y3: number): void; /** `x1 y1 x3 y3 y` */ appendCurve13(x1: number, y1: number, x3: number, y3: number): void; /** `h` */ closePath(): void; /** > `x y width height re`: Append a rectangle to the current path as a complete > subpath, with lower-left corner (x, y) and dimensions width and height in > user space. The operation `x y width height re` is equivalent to: > x y m > (x + width) y l > (x + width) (y + height) l x (y + height) l > h */ appendRectangle(x: number, y: number, width: number, height: number): void; /** > `S`: Stroke the path. */ stroke(): void; /** ALIAS > `s`: Close and stroke the path. This operator shall have the same effect as the sequence h S. */ closeAndStroke(): void; /** > `f`: Fill the path, using the nonzero winding number rule to determine the region to fill. Any subpaths that are open shall be implicitly closed before being filled. */ fill(): void; /** ALIAS > `F`: Equivalent to f; included only for compatibility. Although PDF reader applications shall be able to accept this operator, PDF writer applications should use f instead. */ fillCompat(): void; /** > `f*`: Fill the path, using the even-odd rule to determine the region to fill. */ fillEvenOdd(): void; /** > `B`: Fill and then stroke the path, using the nonzero winding number rule to determine the region to fill. This operator shall produce the same result as constructing two identical path objects, painting the first with f and the second with S. > NOTE The filling and stroking portions of the operation consult different values of several graphics state parameters, such as the current colour. */ fillThenStroke(): void; /** > `B*`: Fill and then stroke the path, using the even-odd rule to determine the region to fill. This operator shall produce the same result as B, except that the path is filled as if with f* instead of f. */ fillThenStrokeEvenOdd(): void; /** ALIAS > `b`: Close, fill, and then stroke the path, using the nonzero winding number rule to determine the region to fill. This operator shall have the same effect as the sequence h B. */ closeAndFillThenStroke(): void; /** ALIAS > `b*`: Close, fill, and then stroke the path, using the even-odd rule to determine the region to fill. This operator shall have the same effect as the sequence h B*. */ closeAndFillThenStrokeEvenOdd(): void; /** > `n`: End the path object without filling or stroking it. This operator shall be a path- painting no-op, used primarily for the side effect of changing the current clipping path. */ closePathNoop(): void; /** > `name CS` */ setStrokeColorSpace(name: string): void; /** > `name cs`: Same as CS but used for nonstroking operations. */ setFillColorSpace(name: string): void; /** > `c1 cn SC` */ setStrokeColorSpace2(c1: number, cn: number): void; /** > `c1 cn [name] SCN` */ setStrokeColorSpace3(c1: number, cn: number, patternName?: string): void; /** > `c1 cn sc`: Same as SC but used for nonstroking operations. */ setFillColorSpace2(c1: number, cn: number): void; /** > `c1 cn [name] scn`: Same as SCN but used for nonstroking operations. */ setFillColorSpace3(c1: number, cn: number, patternName?: string): void; /** `gray G`: Set the stroking colour space to DeviceGray and set the gray level to use for stroking operations. `gray` shall be a number between 0.0 (black) and 1.0 (white). */ setStrokeGray(gray: number): void; /** `gray g`: Same as G but used for nonstroking operations. */ setFillGray(gray: number): void; /** `r g b RG`: Set the stroking colour space to DeviceRGB (or the DefaultRGB colour space; see 8.6.5.6, "Default Colour Spaces") and set the colour to use for stroking operations. Each operand shall be a number between 0.0 (minimum intensity) and 1.0 (maximum intensity). */ setStrokeColor(r: number, g: number, b: number): void; /** `r g b rg`: Same as RG but used for nonstroking operations. */ setFillColor(r: number, g: number, b: number): void; /** > `c m y k K`: Set the stroking colour space to DeviceCMYK (or the DefaultCMYK colour space) and set the colour to use for stroking operations. Each operand shall be a number between 0.0 (zero concentration) and 1.0 (maximum concentration). */ setStrokeCMYK(c: number, m: number, y: number, k: number): void; /** > `c m y k k`: Same as K but used for nonstroking operations. */ setFillCMYK(c: number, m: number, y: number, k: number): void; /** > `name sh`: Paint the shape and colour shading described by a shading dictionary, subject to the current clipping path. The current colour in the graphics state is neither used nor altered. The effect is different from that of painting a path using a shading pattern as the current colour. > name is the name of a shading dictionary resource in the Shading subdictionary of the current resource dictionary. All coordinates in the shading dictionary are interpreted relative to the current user space. (By contrast, when a shading dictionary is used in a type 2 pattern, the coordinates are expressed in pattern space.) All colours are interpreted in the colour space identified by the shading dictionary’s ColorSpace entry. The Background entry, if present, is ignored. > This operator should be applied only to bounded or geometrically defined shadings. If applied to an unbounded shading, it paints the shading’s gradient fill across the entire clipping region, which may be time-consuming. */ shadingPattern(name: string): void; beginInlineImage(): void; endInlineImage(...args: any[]): void; /** > `W`: Modify the current clipping path by intersecting it with the current path, using the nonzero winding number rule to determine which regions lie inside the clipping path. */ clip(): void; /** > `W*`: Modify the current clipping path by intersecting it with the current path, using the even-odd rule to determine which regions lie inside the clipping path. */ clipEvenOdd(): void; /** `BT` */ startTextBlock(): void; /** `ET` */ endTextBlock(): void; /** > `charSpace Tc`: Set the character spacing, Tc, to charSpace, which shall > be a number expressed in unscaled text space units. Character spacing shall > be used by the Tj, TJ, and ' operators. Initial value: 0. */ setCharSpacing(charSpace: number): void; /** > `wordSpace Tw`: Set the word spacing, Tw, to wordSpace, which shall be a > number expressed in unscaled text space units. Word spacing shall be used > by the Tj, TJ, and ' operators. Initial value: 0. */ setWordSpacing(wordSpace: number): void; /** > `scale Tz`: Set the horizontal scaling, Th, to (scale ÷ 100). scale shall > be a number specifying the percentage of the normal width. Initial value: > 100 (normal width). */ setHorizontalScale(scale: number): void; /** > `leading TL`: Set the text leading, Tl, to leading, which shall be a number > expressed in unscaled text space units. Text leading shall be used only by > the T*, ', and " operators. Initial value: 0. */ setLeading(leading: number): void; /** > `font size Tf`: Set the text font, Tf, to font and the text font size, > Tfs, to size. font shall be the name of a font resource in the Font > subdictionary of the current resource dictionary; size shall be a number > representing a scale factor. There is no initial value for either font or > size; they shall be specified explicitly by using Tf before any text is > shown. */ setFont(font: string, size: number): void; /** > `render Tr`: Set the text rendering mode, Tmode, to render, which shall > be an integer. Initial value: 0. */ setRenderingMode(render: RenderingMode): void; /** > `rise Ts`: Set the text rise, Trise, to rise, which shall be a number expressed in unscaled text space units. Initial value: 0. */ setRise(rise: number): void; /** > `x y Td`: Move to the start of the next line, offset from the start of the > current line by (tx, ty). tx and ty shall denote numbers expressed in > unscaled text space units. More precisely, this operator shall perform > these assignments: Tm = Tlm = [ [1 0 0], [0 1 0], [x y 1] ] x Tlm */ adjustCurrentPosition(x: number, y: number): void; /** COMPLETE (ALIAS) > `x y TD`: Move to the start of the next line, offset from the start of the > current line by (x, y). As a side effect, this operator shall set the > leading parameter in the text state. This operator shall have the same > effect as this code: `-ty TL tx ty Td` */ adjustCurrentPositionWithLeading(x: number, y: number): void; /** > `a b c d e f Tm`: Set the text matrix, Tm, and the text line matrix, Tlm: > Tm = Tlm = [ [a b 0], [c d 0], [e f 1] ] > The operands shall all be numbers, and the initial value for Tm and Tlm > shall be the identity matrix, [1 0 0 1 0 0]. Although the operands specify > a matrix, they shall be passed to Tm as six separate numbers, not as an > array. The matrix specified by the operands shall not be concatenated onto > the current text matrix, but shall replace it. */ setTextMatrix(a: number, b: number, c: number, d: number, e: number, f: number): void; /** COMPLETE (ALIAS) > `T*`: Move to the start of the next line. This operator has the same effect > as the code `0 -Tl Td` where Tl denotes the current leading parameter in the > text state. The negative of Tl is used here because Tl is the text leading > expressed as a positive number. Going to the next line entails decreasing > the y coordinate. */ newLine(): void; /** > `string Tj`: Show a text string. `string` is a list of bytes (most often, character codes), each in the range [0, 256). Because parsing hex strings depends on the current font, we cannot resolve the bytes into character codes until rendered in the context of a textState. */ abstract showString(buffer: Buffer): any; /** > `array TJ`: Show one or more text strings, allowing individual glyph > positioning. Each element of array shall be either a string or a number. > If the element is a string, this operator shall show the string. If it is > a number, the operator shall adjust the text position by that amount; that > is, it shall translate the text matrix, Tm. The number shall be expressed > in thousandths of a unit of text space (see 9.4.4, "Text Space Details"). > This amount shall be subtracted from the current horizontal or vertical > coordinate, depending on the writing mode. In the default coordinate system, > a positive adjustment has the effect of moving the next glyph painted either > to the left or down by the given amount. In other words: - large negative numbers equate to spaces - small positive amounts equate to kerning hacks For each item in `array`: If item is a Buffer, that indicates a string of character codes If item is a plain number, that indicates a spacing shift */ showStrings(array: Array): void; /** COMPLETE (ALIAS) > `string '` Move to the next line and show a text string. This operator shall have > the same effect as the code `T* string Tj` */ newLineAndShowString(buffer: Buffer): void; /** COMPLETE (ALIAS) > `wordSpace charSpace text "` Move to the next line and show a text string, > using `wordSpace` as the word spacing and `charSpace` as the character > spacing (setting the corresponding parameters in the text state). > `wordSpace` and `charSpace` shall be numbers expressed in unscaled text > space units. This operator shall have the same effect as this code: > `wordSpace Tw charSpace Tc text '` */ newLineAndShowStringWithSpacing(wordSpace: number, charSpace: number, buffer: Buffer): void; /** > `tag BMC`: Begin a marked-content sequence terminated by a balancing EMC operator. tag shall be a name object indicating the role or significance of the sequence. */ beginMarkedContent(tag: string): void; /** > `tag properties BDC`: Begin a marked-content sequence with an associated property list, terminated by a balancing EMC operator. tag shall be a name object indicating the role or significance of the sequence. properties shall be either an inline dictionary containing the property list or a name object associated with it in the Properties subdictionary of the current resource dictionary. */ beginMarkedContentWithDictionary(tag: string, dictionary: any): void; /** > `EMC`: End a marked-content sequence begun by a BMC or BDC operator. */ endMarkedContent(): void; } /** Add Resources tracking and drawObject support. */ export declare abstract class RecursiveDrawingContext extends DrawingContext { depth: number; constructor(resources: Resources, depth?: number); applyOperation(operator: string, operands: any[]): void; applyContentStream(content_stream_buffer: Buffer): void; /** Do */ drawObject(name: string): void; } /** fontSize is equivalent to height. */ export interface TextAtom extends Point, Size { fontName: string; font: Font; buffer: Buffer; text: string; } export declare class TextAtomDrawingContext extends RecursiveDrawingContext { textAtoms: TextAtom[]; constructor(textAtoms: TextAtom[], resources: Resources, depth?: number); /** Tj showString is called when processing a Tj ("showString") operation, and from showStrings, in turn. In the case of composite Fonts, each byte in `buffer` may not correspond to a single glyph, but for "simple" fonts, that is the case. */ showString(buffer: Buffer): void; }