Table of Contents### 4.2. Coordinate Systems

#### 4.2.1. Coordinate Spaces

##### Device Space

##### Figure 4.2. Device space

##### User Space

##### Figure 4.3. User space

##### Other Coordinate Spaces

##### Relationships among Coordinate Spaces

##### Figure 4.4. Relationships among coordinate systems

#### 4.2.2. Common Transformations

##### Figure 4.5. Effects of coordinate transformations

##### Figure 4.6. Effect of transformation order

#### 4.2.3. Transformation Matrices

Entire Site

Coordinate systems define the canvas on which all painting occurs. They determine the position, orientation, and size of the text, graphics, and images that appear on a page. This section describes each of the coordinate systems used in PDF, how they are related, and how transformations among them are specified.

Note

The coordinate systems discussed in this section apply to two-dimensional graphics. PDF 1.6 introduces the ability to display 3D artwork, in which objects are described in a three-dimensional coordinate system, as described in Section 9.5.4, “Coordinate Systems for 3D Annotations.”

Paths and positions are defined in terms of pairs of coordinates on the Cartesian plane. A coordinate pair is a pair of real numbers x and y that locate a point horizontally and vertically within a two-dimensional coordinate space. A coordinate space is determined by the following properties with respect to the current page:

The location of the origin

The orientation of the x and y axes

The lengths of the units along each axis

PDF defines several coordinate spaces in which the coordinates specifying graphics objects are interpreted. The following sections describe these spaces and the relationships among them.

Transformations among coordinate spaces are defined by transformation matrices, which can specify any linear mapping of two-dimensional coordinates, including translation, scaling, rotation, reflection, and skewing. Transformation matrices are discussed in Sections 4.2.2, “Common Transformations,” and 4.2.3, “Transformation Matrices.”

The contents of a page ultimately appear on a raster output device such as a display or a printer. Such devices vary greatly in the built-in coordinate systems they use to address pixels within their imageable areas. A particular device's coordinate system is called its device space. The origin of the device space on different devices can fall in different places on the output page; on displays, the origin can vary depending on the window system. Because the paper or other output medium moves through different printers and imagesetters in different directions, the axes of their device spaces may be oriented differently. For instance, vertical (y) coordinates may increase from the top of the page to the bottom on some devices and from bottom to top on others. Finally, different devices have different resolutions; some even have resolutions that differ in the horizontal and vertical directions.

If coordinates in a PDF file were specified in device space, the file would be device-dependent and would appear differently on different devices. For example, images specified in the typical device spaces of a 72-pixel-per-inch display and a 600-dot-per-inch printer would differ in size by more than a factor of 8; an 8-inch line segment on the display would appear less than 1 inch long on the printer. Figure 4.2 shows how the same graphics object, specified in device space, can appear drastically different when rendered on different output devices.

To avoid the device-dependent effects of specifying objects in device space, PDF defines a device-independent coordinate system that always bears the same relationship to the current page, regardless of the output device on which printing or displaying occurs. This device-independent coordinate system is called user space.

The user space coordinate system is initialized to a default state for each page of a document. The `CropBox` entry in the page dictionary specifies the rectangle of user space corresponding to the visible area of the intended output medium (display window or printed page). The positive x axis extends horizontally to the right and the positive y axis vertically upward, as in standard mathematical practice (subject to alteration by the `Rotate` entry in the page dictionary). The length of a unit along both the x and y axes is set by the `UserUnit` entry (PDF 1.6) in the page dictionary (see Table 3.27). If that entry is not present or supported, the default value of 1/72 inch is used. This coordinate system is called default user space.

Note

In PostScript, the origin of default user space always corresponds to the lower-left corner of the output medium. While this convention is common in
PDF
documents as well, it is not required; the page dictionary's
`CropBox`
entry can specify any rectangle of default user space to be made visible on the medium.

Note

The default for the size of the unit in default user space (1/72 inch) is approximately the same as a point, a unit widely used in the printing industry. It is not exactly the same, however; there is no universal definition of a point.

Conceptually, user space is an infinite plane. Only a small portion of this plane corresponds to the imageable area of the output device: a rectangular region defined by the `CropBox` entry in the page dictionary. The region of default user space that is viewed or printed can be different for each page and is described in Section 10.10.1, “Page Boundaries.”

Note

Because coordinates in user space (as in any other coordinate space) may be specified as either integers or real numbers, the unit size in default user space does not constrain positions to any arbitrary grid. The resolution of coordinates in user space is not related in any way to the resolution of pixels in device space.

The transformation from user space to device space is defined by the current transformation matrix (CTM), an element of the PDF graphics state (see Section 4.3, “Graphics State”). A PDF consumer application can adjust the CTM for the native resolution of a particular output device, maintaining the device-independence of the PDF page description. Figure 4.3 shows how this allows an object specified in user space to appear the same regardless of the device on which it is rendered.

The default user space provides a consistent, dependable starting place for PDF page descriptions regardless of the output device used. If necessary, a PDF content stream may modify user space to be more suitable to its needs by applying the coordinate transformation operator, `cm` (see Section 4.3.3, “Graphics State Operators”). Thus, what may appear to be absolute coordinates in a content stream are not absolute with respect to the current page because they are expressed in a coordinate system that may slide around and shrink or expand. Coordinate system transformation not only enhances device-independence but is a useful tool in its own right. For example, a content stream originally composed to occupy an entire page can be incorporated without change as an element of another page by shrinking the coordinate system in which it is drawn.

In addition to device space and user space, PDF uses a variety of other coordinate spaces for specialized purposes:

The coordinates of text are specified in text space. The transformation from text space to user space is defined by a text matrix in combination with several text-related parameters in the graphics state (see Section 5.3.1, “Text-Positioning Operators”).

Character glyphs in a font are defined in glyph space (see Section 5.1.3, “Glyph Positioning and Metrics”). The transformation from glyph space to text space is defined by the font matrix. For most types of fonts, this matrix is predefined to map 1000 units of glyph space to 1 unit of text space; for Type 3 fonts, the font matrix is given explicitly in the font dictionary (see Section 5.5.4, “Type 3 Fonts”).

All sampled images are defined in image space. The transformation from image space to user space is predefined and cannot be changed. All images are 1 unit wide by 1 unit high in user space, regardless of the number of samples in the image. To be painted, an image is mapped to a region of the page by temporarily altering the CTM.

Note

In PostScript, unlike PDF, the relationship between image space and user space can be specified explicitly. The fixed transformation prescribed in PDF corresponds to the convention that is recommended for use in PostScript.

A form XObject (discussed in Section 4.9, “Form XObjects”) is a self-contained content stream that can be treated as a graphical element within another content stream. The space in which it is defined is called form space. The transformation from form space to user space is specified by a form matrix contained in the form XObject.

PDF 1.2 defines a type of color known as a pattern, discussed in Section 4.6, “Patterns.” A pattern is defined either by a content stream that is invoked repeatedly to tile an area or by a shading whose color is a function of position. The space in which a pattern is defined is called pattern space. The transformation from pattern space to user space is specified by a pattern matrix contained in the pattern.

PDF 1.6 introduces embedded 3D artwork, which is described in three-dimensional coordinates (see Section 9.5.4, “Coordinate Systems for 3D Annotations”) that are projected into an annotation's target coordinate system (see Section 9.5.1, “3D Annotations”).

Figure 4.4 shows the relationships among the coordinate spaces described above. Each arrow in the figure represents a transformation from one coordinate space to another. PDF allows modifications to many of these transformations.

Because PDF coordinate spaces are defined relative to one another, changes made to one transformation can affect the appearance of objects defined in several coordinate spaces. For example, a change in the CTM, which defines the transformation from user space to device space, affects forms, text, images, and patterns, since they are all upstream from user space.

A transformation matrix specifies the relationship between two coordinate spaces. By modifying a transformation matrix, objects can be scaled, rotated, translated, or transformed in other ways.

A transformation matrix in PDF is specified by six numbers, usually in the form of an array containing six elements. In its most general form, this array is denoted [`a`
`b`
`c`
`d`
`e`
`f`]; it can represent any linear transformation from one coordinate system to another. This section lists the arrays that specify the most common transformations; Section 4.2.3, “Transformation Matrices,” discusses more mathematical details of transformations, including information on specifying transformations that are combinations of those listed here:

Translations are specified as

`[1 0 0 1``t`_{x}`t`_{y}`]`, where`t`and_{x}`t`_{y}are the distances to translate the origin of the coordinate system in the horizontal and vertical dimensions, respectively.Scaling is obtained by

`[``s`_{x}`0 0``s`_{y}`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`s`and_{x}`s`units, respectively, in the previous coordinate system._{y}Rotations are produced by [cos θ sin θ -sin θ cos θ

`0 0`], which has the effect of rotating the coordinate system axes by an angle θ counterclockwise.Skew is specified by

`[1`tan α tan β`1 0 0]`, which skews the x axis by an angle α and the y axis by an angle β.

Figure 4.5 shows examples of each transformation. The directions of translation, rotation, and skew shown in the figure correspond to positive values of the array elements.

If several transformations are combined, the order in which they are applied is significant. For example, first scaling and then translating the x axis is not the same as first translating and then scaling it. In general, to obtain the expected results, transformations should be done in the following order:

1. | Translate |

2. | Rotate |

3. | Scale or skew |

Figure 4.6 shows the effect of the order in which transformations are applied. The figure shows two sequences of transformations applied to a coordinate system. After each successive transformation, an outline of the letter `n` is drawn.

The following transformations are shown in the figure:

A translation of 10 units in the x direction and 20 units in the y direction

A rotation of 30 degrees

A scaling by a factor of 3 in the x direction

In the figure, the axes are shown with a dash pattern having a 2-unit dash and a 2-unit gap. In addition, the original (untransformed) axes are shown in a lighter color for reference. Notice that the scale-rotate-translate ordering results in a distortion of the coordinate system, leaving the x and y axes no longer perpendicular; the recommended translate-rotate-scale ordering results in no distortion.

This section discusses the mathematics of transformation matrices. It is not necessary to read this section to use the transformations described previously; the information is presented for the benefit of readers who want to gain a deeper understanding of the theoretical basis of coordinate transformations.

To understand the mathematics of coordinate transformations in PDF, it is vital to remember two points:

Transformations alter coordinate systems, not graphics objects. All objects painted before a transformation is applied are unaffected by the transformation. Objects painted after the transformation is applied are interpreted in the transformed coordinate system.

Transformation matrices specify the transformation from the new (transformed) coordinate system to the original (untransformed) coordinate system. All coordinates used after the transformation are expressed in the transformed coordinate system. PDF applies the transformation matrix to find the equivalent coordinates in the untransformed coordinate system.

Note

Many computer graphics textbooks consider transformations of graphics objects rather than of coordinate systems. Although either approach is correct and self-consistent, some details of the calculations differ depending on which point of view is taken.

PDF represents coordinates in a two-dimensional space. The point (x, y) in such a space can be expressed in vector form as `[``x y`
`1]`. The constant third element of this vector (`1`) is needed so that the vector can be used with 3-by-3 matrices in the calculations described below.

The transformation between two coordinate systems is represented by a 3-by-3 transformation matrix written as follows:

Because a transformation matrix has only six elements that can be changed, it is usually specified in PDF as the six-element array `[``a b c d e f``]`.

Coordinate transformations are expressed as matrix multiplications:

Because PDF transformation matrices specify the conversion from the transformed coordinate system to the original (untransformed) coordinate system, x ' and y ' in this equation are the coordinates in the untransformed coordinate system, and x and y are the coordinates in the transformed system. The multiplication is carried out as follows:

If a series of transformations is carried out, the matrices representing each of the individual transformations can be multiplied together to produce a single equivalent matrix representing the composite transformation.

Matrix multiplication is not commutative—the order in which matrices are multiplied is significant. Consider a sequence of two transformations: a scaling transformation applied to the user space coordinate system, followed by a conversion from the resulting scaled user space to device space. Let M_{S} be the matrix specifying the scaling and M_{C} the current transformation matrix, which transforms user space to device space. Recalling that coordinates are always specified in the transformed space, the correct order of transformations must first convert the scaled coordinates to default user space and then the default user space coordinates to device space. This can be expressed as

where

X

_{D}denotes the coordinates in device spaceX

_{U}denotes the coordinates in default user spaceX

_{S}denotes the coordinates in scaled user space

This shows that when a new transformation is concatenated with an existing one, the matrix representing it must be multiplied before (premultiplied with) the existing transformation matrix.

This result is true in general for PDF: when a sequence of transformations is carried out, the matrix representing the combined transformation (M') is calculated by premultiplying the matrix representing the additional transformation (M_{T}) with the one representing all previously existing transformations (M):

Note

When rendering graphics objects, it is sometimes necessary for an application to perform the inverse of a transformation—that is, to find the user space coordinates that correspond to a given pair of device space coordinates. Not all transformations are invertible, however. For example, if a matrix contains a, b, c, and d elements that are all zero, all user coordinates map to the same device coordinates and there is no unique inverse transformation. Such noninvertible transformations are not very useful and generally arise from unintended operations, such as scaling by 0. Use of a noninvertible matrix when painting graphics objects can result in unpredictable behavior.