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. BitsPerCoordinate is 1, 2, 4, 8, 12, 16, 24
or 32; BitsPerComponent 1, 2, 4, 8, 12 or 16; BitsPerFlag
2, 4 or 8. In a type 4 mesh, flag 0 starts a new triangle of three vertices and
flags 1 and 2 add one vertex to the previous triangle, sharing its edge
vbvc or vavc. In types 6
and 7, flag 0 starts a new patch of 12 or 16 points and four colours; flags 1 to 3
share an edge with the previous patch and give eight or twelve new points and two
new colours.
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 these operators, and there are no loops, names or strings:
arithmetic abs add atan ceiling
cos cvi cvr div exp
floor idiv ln log mod
mul neg round sin
sqrt sub truncate; relational and bitwise
and bitshift eq false
ge gt le lt ne
not or true xor; conditional
if ifelse; stack copy dup
exch index pop roll.
The graphics state
The graphics state is what q and Q save and restore. Its
parameters, and their values at the start of each page:
| Parameter | Initial value | Set by |
|---|---|---|
| Current transformation matrix | maps default user space to the device | cm |
| Clipping path | the page’s crop box | W, W* |
| Colour space, colour | DeviceGray, black | colour operators |
| Text state | below | text state operators |
| Line width | 1.0 | w, LW |
| Line cap, line join | 0, 0 | J, j, LC, LJ |
| Mitre limit | 10.0 | M, ML |
| Dash pattern | [] 0, solid | d, D |
| Rendering intent | RelativeColorimetric | ri, RI |
| Stroke adjustment | false | SA |
| Blend mode, soft mask | Normal, None | BM, SMask (1.4) |
| Alpha constant, alpha source | 1.0, false | CA, ca, AIS (1.4) |
| Overprint, overprint mode | false, 0 | OP, op, OPM |
| Black generation, undercolour removal, transfer, halftone | device default | BG, UCR, TR, HT |
| Flatness, smoothness | 1.0 (flatness); device default | i, FL, SM |
Text state parameters: character spacing Tc 0, word spacing Tw 0, horizontal scaling Th 100, leading Tl 0, no initial font or size, render mode 0, rise 0 and text knockout true.
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. Most readers on screen ignore halftones and transfer functions.
HalftoneType | Since | Kind | Keys |
|---|---|---|---|
| 1 | 1.2 | A screen: frequency, angle and spot function | Type Halftone, HalftoneName, Frequency, Angle, SpotFunction, AccurateScreens, TransferFunction |
| 5 | 1.2 | One halftone for each colourant | Type, HalftoneName, a key for each colourant, and Default (required) |
| 6 | 1.2 | A threshold array, 8 bits | A stream: Type, HalftoneName, Width, Height, TransferFunction |
| 10 | 1.2 | An angled threshold array | A stream: Type, HalftoneName, Xsquare, Ysquare, TransferFunction |
| 16 | 1.3 | 16-bit thresholds | A stream: Type, HalftoneName, Width, Height, Width2, Height2, TransferFunction |
A type 1 SpotFunction is a function, or one of the predefined names:
SimpleDot, InvertedSimpleDot, DoubleDot,
InvertedDoubleDot, CosineDot, Double,
InvertedDouble, Line, LineX,
LineY, Round, Ellipse,
EllipseA, InvertedEllipseA, EllipseB,
EllipseC, InvertedEllipseC, Square,
Cross, Rhomboid, Diamond.
Overprint
With overprint off, painting a colour knocks out what is beneath on every colourant.
With OP or op true, components that the current colour
space does not cover leave the ink beneath alone. With OPM 1 and a
DeviceCMYK colour, a component of value 0 also leaves its ink beneath;
with OPM 0 it paints a tint of 0.
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.
The value of Ref is a reference dictionary: F, the file
(required); Page, the page to import, a number from 0 or a label
(required); and ID, the identifiers of the file. A Subtype PS
XObject holds a fragment of PostScript for printers, optionally with
Level1, a stream of PostScript Level 1 code.
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.
With a backdrop colour cb and a source colour cs, each between 0 and 1, the separable modes give B(cb, cs):
| Mode | B(cb, cs) |
|---|---|
Normal, Compatible | cs |
Multiply | cb × cs |
Screen | cb + cs − cb × cs |
Overlay | HardLight(cs, cb) |
Darken, Lighten | min(cb, cs), max(cb, cs) |
ColorDodge | 1 if cs = 1; otherwise min(1, cb / (1 − cs)) |
ColorBurn | 0 if cs = 0; otherwise 1 − min(1, (1 − cb) / cs) |
HardLight | Multiply(cb, 2cs) if cs ≤ 0.5; otherwise Screen(cb, 2cs − 1) |
SoftLight | cb − (1 − 2cs)cb(1 − cb) if cs ≤ 0.5; otherwise cb + (2cs − 1)(D(cb) − cb), where D(x) is ((16x − 12)x + 4)x for x ≤ 0.25 and √x above |
Difference | |cb − cs| |
Exclusion | cb + cs − 2cbcs |
The non-separable modes work on the whole colour, using Lum(C) = 0.3 R +
0.59 G + 0.11 B: Hue is SetLum(SetSat(Cs, Sat(Cb)),
Lum(Cb)), Saturation SetLum(SetSat(Cb, Sat(Cs)),
Lum(Cb)), Color SetLum(Cs, Lum(Cb)) and
Luminosity SetLum(Cb, Lum(Cs)). The result is composited
with αr = αb + αs − αbαs
and Cr = (1 − αs/αr)Cb +
(αs/αr)[(1 − αb)Cs +
αbB(Cb, Cs)].
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 |