PDF’s graphics model is PostScript’s: paths filled and stroked in colour, glyphs, and sampled images, placed through a matrix on a page that is painted in order, each mark covering what is under it. PDF 1.1 added device-independent colour, PDF 1.2 patterns and spot colours, PDF 1.3 smooth shading and ICC colour, and PDF 1.4 transparency, which changed painting from opaque marks to blending.
Coordinate spaces
Every position is given in user space and mapped to the device by the current
transformation matrix (CTM). Default user space has x to the right, y up, and a unit
of 1/72 inch, the PostScript point. The page’s MediaBox gives its
corners in this space, usually with (0, 0) at the lower left. A matrix is written
as six numbers
[a b c d e f], standing for
| a b 0 |
| c d 0 | x′ = a·x + c·y + e
| e f 1 | y′ = b·x + d·y + f
| Transformation | Matrix |
|---|---|
| Move by (tx, ty) | [1 0 0 1 tx ty] |
| Scale by sx, sy | [sx 0 0 sy 0 0] |
| Rotate anticlockwise by θ | [cos θ sin θ −sin θ cos θ 0 0] |
| Skew x by α and y by β | [1 tan α tan β 1 0 0] |
cm multiplies a new matrix onto the CTM, so the last one given is
applied to points first. Other spaces sit on top of user space:
- text space, set by the text matrix
Tm, in which glyphs are placed; - glyph space, in which a font’s glyphs are drawn, 1,000 units to the em for most fonts, mapped by the font matrix;
- image space, in which an image of w × h samples always fills the
unit square of user space; a page draws it at size with
w 0 0 h x y cmbeforeDo; - form space, set by a form XObject’s
Matrix; - pattern space, set by a pattern’s
Matrix, which maps to the default coordinate space of the page, not the CTM at the time of use.
A page’s Rotate turns the whole page when shown; it does not change
user space.
Colour spaces
A colour space is a name for the device spaces and the Pattern space without
parameters, or an array of the family name and its parameters. Content streams name
it in the ColorSpace resources.
| Family | Since | Components | Written as |
|---|---|---|---|
DeviceGray | 1.0 | 1: 0 black to 1 white | a name |
DeviceRGB | 1.0 | 3: red, green, blue, 0 to 1 | a name |
DeviceCMYK | 1.0 | 4: cyan, magenta, yellow, black, 0 to 1 | a name |
Indexed | 1.0 | 1: an index 0 to hival | [/Indexed base hival lookup]: lookup is a string or stream of (hival + 1) × n bytes, one colour of the base space an entry |
CalGray | 1.1 | 1 | [/CalGray << /WhitePoint [ ] /BlackPoint [ ] /Gamma g >>] |
CalRGB | 1.1 | 3 | [/CalRGB << /WhitePoint /BlackPoint /Gamma [3] /Matrix [9] >>] |
Lab | 1.1 | 3: L* 0 to 100, a* and b* in Range | [/Lab << /WhitePoint /BlackPoint /Range [amin amax bmin bmax] >>] |
Separation | 1.2 | 1: the tint, 0 none to 1 full | [/Separation name alternateSpace tintTransform]: a spot colour, with a function that gives its look in the alternate space |
Pattern | 1.2 | none, or the underlying space’s for uncoloured patterns | /Pattern or [/Pattern underlyingSpace] |
ICCBased | 1.3 | 1, 3 or 4, from the stream’s N | [/ICCBased stream]: an ICC profile, with N, Alternate, Range and Metadata (1.4) |
DeviceN | 1.3 | n named colourants | [/DeviceN names alternateSpace tintTransform attributes] |
WhitePoint is required in the CIE spaces, the diffuse white in CIE
1931 XYZ, with Y equal to 1. The Separation names All (every colourant,
for registration marks) and None (paint nothing) are special. The
ICC profile in the small PDF 1.3 example is
<< /Length 10 0 R /N 3 /Alternate /DeviceRGB /Filter /FlateDecode >>.
Rendering intents, set by ri or an image’s Intent, say how
to map colours a device cannot show: AbsoluteColorimetric,
RelativeColorimetric (the default), Saturation or
Perceptual.
Patterns
A pattern paints an area with a repeated cell or a smooth shading, used as a
colour: /Pattern cs /P1 scn and then a fill. Patterns are in the
Pattern resources.
Tiling patterns (PatternType 1, PDF 1.2)
A content stream that draws one cell, repeated across the area:
| Key | Meaning |
|---|---|
Type | Pattern |
PatternType | 1 |
PaintType | 1, coloured: the cell sets its own colours. 2, uncoloured: a stencil painted in the colour given with scn, in the underlying space |
TilingType | 1 constant spacing, 2 no distortion, 3 constant spacing and faster tiling |
BBox | The cell’s bounding box, in pattern space |
XStep, YStep | The spacing of the cells |
Resources | The resources the cell uses |
Matrix | Pattern space to default page space; default identity |
Shading patterns (PatternType 2, PDF 1.3)
A dictionary with Shading, a shading,
Matrix and an optional ExtGState. Filling with it paints
the shading inside the path. The sh operator paints a shading
directly, without a pattern, over the clipping region.
Shadings
From PDF 1.3, seven types of smooth colour transition. Every shading dictionary has these keys:
| Key | Meaning |
|---|---|
ShadingType | Required: 1 to 7 |
ColorSpace | Required: any space except Pattern |
Background | A colour for the area outside the shape, in a pattern only |
BBox | A clipping box in shading space |
AntiAlias | Whether to smooth the edges; default false |
| Type | Shading | Its own keys |
|---|---|---|
| 1 | Function-based: the colour of every point (x, y) is a function of x and y | Domain (default [0 1 0 1]), Matrix, Function |
| 2 | Axial: colour varies along a line | Coords [x0 y0 x1 y1], Domain (default [0 1]), Function, Extend [false false] to carry the end colours on |
| 3 | Radial: colour varies between two circles | Coords [x0 y0 r0 x1 y1 r1], Domain, Function, Extend |
| 4 | Free-form triangle mesh, in a stream | BitsPerCoordinate, BitsPerComponent, BitsPerFlag, Decode, Function |
| 5 | Lattice-form triangle mesh, in a stream | BitsPerCoordinate, BitsPerComponent, VerticesPerRow, Decode, Function |
| 6 | Coons patch mesh: patches bounded by four cubic curves, in a stream | as type 4 |
| 7 | Tensor-product patch mesh: as type 6 with four inner control points, in a stream | as type 4 |
The mesh types are streams of packed vertices: a flag (types 4, 6 and 7) that says
how the next vertex or patch joins the previous one, coordinates, and colour
components or a single function input, each in the given number of bits and mapped
through Decode.
Functions
Functions turn input numbers into output numbers, for shadings, Separation and
DeviceN tint transforms, transfer functions and soft masks. Every function has
FunctionType, Domain (the input ranges, required) and
Range (output ranges; required for types 0 and 4). Inputs and outputs
are clipped to them.
| Type | Since | Function | Keys |
|---|---|---|---|
| 0 | 1.2 | Sampled: a table of samples in a stream, interpolated | Size, BitsPerSample (1, 2, 4, 8, 12, 16, 24, 32), Order (1 linear, 3 cubic), Encode, Decode |
| 2 | 1.3 | Exponential interpolation of one input: C0 + xN × (C1 − C0) | C0 (default [0]), C1 (default [1]), N |
| 3 | 1.3 | Stitching: other one-input functions, each over part of the domain | Functions, Bounds, Encode |
| 4 | 1.3 | PostScript calculator: a small program in braces, in a stream | the stream holds the code, such as { 2 exch sub } |
Type 4 programs use only arithmetic, comparison, boolean and stack operators,
with if and ifelse; there are no loops, names or strings.
Graphics state parameter dictionaries
From PDF 1.2 the gs operator sets several parameters at once from a
dictionary in the ExtGState resources. Parameters that have no
operator of their own can be set only this way.
| Key | Since | Parameter |
|---|---|---|
Type | 1.2 | ExtGState, optional |
LW, LC, LJ, ML, D | 1.3 | Line width, cap, join, mitre limit, dash pattern ([[3 2] 0]) |
RI | 1.3 | Rendering intent |
OP | 1.2 | Overprint for stroking, and for everything else if op is absent |
op | 1.3 | Overprint for non-stroking |
OPM | 1.3 | Overprint mode: 1 means zero CMYK components leave the colour beneath alone |
Font | 1.3 | An array of a font reference and a size |
BG, BG2 | 1.2, 1.3 | Black generation function, for converting to CMYK; BG2 may be Default |
UCR, UCR2 | 1.2, 1.3 | Undercolour removal function |
TR, TR2 | 1.2, 1.3 | Transfer function, or four of them; Identity, and Default for TR2 |
HT | 1.2 | Halftone: a dictionary or stream, or Default |
FL | 1.3 | Flatness |
SM | 1.3 | Smoothness tolerance for shadings, 0 to 1 |
SA | 1.2 | Automatic stroke adjustment |
BM | 1.4 | Blend mode, a name or an array of names |
SMask | 1.4 | Soft mask, or None |
CA, ca | 1.4 | Constant opacity for stroking and for other painting, 0 to 1 |
AIS | 1.4 | Alpha source: whether the mask and constant are shape (true) or opacity (false) |
TK | 1.4 | Text knockout: whether glyphs in one text object knock each other out |
The small PDF 1.3 example uses << /Type /ExtGState /OPM 1 >>.
Halftones
From PDF 1.2 a document can set how a printer makes shades out of dots. Halftone types: 1 (a screen of frequency, angle and spot function), 5 (one halftone a colourant), 6 (a threshold array in a stream), 10 (a threshold array for a screen at an angle) and 16 (PDF 1.3, 16-bit thresholds). Most readers on screen ignore halftones and transfer functions.
Form XObjects
A form XObject is a content stream that can be drawn many times with
Do, like a PostScript form: a logo on every page, or an annotation’s
appearance.
| Key | Since | Meaning |
|---|---|---|
Type | 1.0 | XObject |
Subtype | 1.0 | Required: Form |
FormType | 1.0 | 1, the only type |
BBox | 1.0 | Required: the bounding box in form space, which clips the form |
Matrix | 1.0 | Form space to user space; default identity |
Resources | 1.2 | The form’s own resources; before 1.2 it used the page’s |
Group | 1.4 | Makes it a transparency group |
Ref | 1.4 | Makes it a reference XObject: a page of another PDF to show in its place |
Metadata | 1.4 | XMP metadata |
PieceInfo, LastModified | 1.3 | Private data of the program that made it |
StructParent, StructParents | 1.3 | Its place in the logical structure |
OPI | 1.2 | Open Prepress Interface: a high-resolution version kept elsewhere |
Drawing a form saves the graphics state, multiplies Matrix onto the
CTM, clips to BBox, runs the stream and restores the state.
Transparency (PDF 1.4)
PDF 1.4 made every painting operation blend with what is beneath it, using a shape and an opacity. With the defaults (Normal blend mode, opacity 1, no soft mask) the result is the same as opaque painting, so older files look as they did.
Blend modes
| Kind | Blend modes |
|---|---|
| Separable, per component | Normal (default), Compatible (the same as Normal), Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion |
| Non-separable, on the whole colour | Hue, Saturation, Color, Luminosity |
BM may be an array of names; a reader uses the first it knows.
Soft masks
A soft mask gives an opacity or shape that varies across the page. In a graphics state it is a dictionary:
| Key | Meaning |
|---|---|
Type | Mask |
S | Required: Alpha, the mask is the group’s opacity, or Luminosity, it is the group’s brightness |
G | Required: a transparency group XObject that is painted to make the mask |
BC | For Luminosity: the backdrop colour, in the group’s colour space; default black |
TR | A function applied to the mask values; default Identity |
Images can carry their own soft mask too: see Images.
Transparency groups
A form XObject or a page with a Group dictionary of
/S /Transparency is painted as a unit, then blended as a whole:
| Key | Default | Meaning |
|---|---|---|
S | – | Required: Transparency |
CS | – | The colour space in which the group is composited |
I | false | Isolated: the group starts from a transparent backdrop, not what is beneath |
K | false | Knockout: each object in the group covers the earlier ones instead of blending with them |