8 Coordinate Systems, Transformations and Units


8.1 Introduction

For all media, the term canvas describes "the space where the SVG document is rendered." The canvas is infinite for each dimension of the space, but rendering occurs within a finite rectangular region of the canvas. This finite rectangular region is called the viewport. For visual media, the viewport is the viewing area where the user sees the SVG document.

The size of the viewport (i.e., its width and height) is determined by a negotiation process (see Establishing the size of the initial viewport) between the SVG document and its parent (real or implicit). Once that negotiation process is completed, the SVG user agent is provided the following information:

Using the above information, the SVG user agent establishes an initial current coordinate system (also called user space) for the SVG document such that the origin of the current coordinate system matches the origin of the viewport, and one unit in the current coordinate system equals one "pixel" in the viewport.

Lengths in SVG can be specified as:

The supported CSS length unit specifiers are: em, ex, px, pt, pc, cm, mm, in, and percentages.

A new user space (i.e., a new current coordinate system) can be established at any place in the SVG document by specifying transformations in the form of transformation matrices or simple transformation operations such as rotation, skewing, scaling and translation.

A new viewport also can be established. New viewports provide a means for redefining the meaning of some of the various CSS unit specifiers (px, pt, pc, cm, mm, in, and percentages).

8.2 Establishing the initial viewport

The attributes of the initial viewport are established by either the CSS positioning parameters that are defined by the outermost <svg> element in combination with the width= and height= XML attributes that are required on the <svg> element.

The size (i.e., width and height) of the viewport into which an SVG document should be rendered is determined as follows. If the <svg> element contains CSS positioning properties which establish the width for the viewport, then the width of the viewport should be set to that size. If the CSS positioning properties on the <svg> element do not provide sufficient information to determine the width of the viewport, then the XML attributes width= determines the width of the viewport. Similarly, if the <svg> element contains CSS positioning properties which establish the height for the viewport, then the height of the viewport should be set to that size. If the CSS positioning properties on the <svg> element do not provide sufficient information to determine the height of the viewport, then the XML attributes height= determines the height of the viewport.

In the following example, an SVG graphic is embedded within a parent XML document which is formatted using CSS layout rules. The width="100px" and height="200px" attributes are used to set the size of the viewport:

<?xml version="1.0" standalone="yes"?>
<parent xmlns="http://some.url">
   
   <!-- SVG graphic -->
   <svg xmlns='http://www.w3.org/Graphics/SVG/svg-19990412.dtd'
      width="100px" height="200px">
      <path d="M100,100 Q200,400,300,100"/>
      <!-- rest of SVG graphic would go here -->
   </svg>   
   
</parent>

Download this example

The initial clipping path for an SVG document is determined by the values of the 'clip' and 'overflow' properties.

8.3 Establishing A New User Space: Transformations

To change the current user space coordinate system, you define a transformation which defines how to transform coordinates from the new user coordinate system into the previous user coordinate system. Mathematically, the transformation is represented by a transformation matrix which maps coordinates in the new user coordinate system into the previous user coordinate system. To illustrate:

(Insert an image which shows this concept.)

Transformation matrices define the mathematical mapping from one coordinate space into another. Of particular interest is the current transformation matrix (CTM) which defines the mapping from user space into viewport space.

(Insert an image showing the CTM mapping user space into device space.)

Transformation matrices are specified as 3x3 matrices of the following form:

(Insert an image showing [a b 0 c d 0 e f 1], but as a rectangular matrix.)

Because SVG's transformation matrices only have six entries that can be changed, these matrices will be called 2x3 transformation matrices, which for convenience are often written as an array of six numbers: [a b c d e f].

All coordinates in user space are expressed as (x,y) values. To calculate the transformation from the current user space coordinate system into viewport space, you multiply the vector (x,y,1) by the current transformation matrix (CTM) to yield (x',y',1):

(Insert an image showing [x',y',1]=[x,y,1][a b 0 c d 0 e f 1])

Whenever a new transformation is provided, a new CTM is calculated by the following formula. Note that the new transformation is pre-multiplied to the CTM:

(Insert an image which shows newCTM[a b c d e f]=[transformmatrix]*oldCTM[a b c d e f].)

It is important to understand the following key points regarding transformations in SVG:

Mathematically, all transformations can be expressed as matrices. To illustrate:

(Insert an image illustrates the above concept.)

8.4 The 'transform' property: Establishing a New User Coordinate System

All transformations are specified with the transform CSS property:

'transform'
Value:   matrix(<a> <b> <c> <d> <e> <f>) |
translate(<tx> [<ty>]) |
scale(<sx> [<sy> [<center-x> [<center-y>]]]) |
rotate(<rotate-angle> [<center-x> [<center-y>]]) |
fit(<bbox-left> <bbox-top> <bbox-right> <bbox-bottom>
    [ preserve-aspect-ratio [ left | center | right ] [ top | middle | bottom ]]) |
inherit
Initial:   see discussion below
Applies to:  all elements
Inherited:  no
Percentages:  N/A
Media:  visual

The 'transform' property defines a new coordinate system transformation and thus implicitly new user space and a new CTM. A transform property has exactly one of matrix, scale, rotate, translate, fit and inherit.

The parameters to matrix have the following meanings (see the explanation of simple transformations.):

<a> <b> <c> <d> <e> <f>
Specifies the transformation matrix which should be premultiplied to the old CTM to yield a new CTM. All values are real numbers.

The parameters to translate have the following meanings (see the explanation of simple transformations.): Translations are represented as [1 0 0 1 tx ty], where tx and ty are the distances to translate the origin of the coordinate system in x and y, respectively.

<tx> and <ty>
The distances to translate the origin of the coordinate system in x and y, respectively. If <ty> is not provided, it is assumed to be zero.

The parameters to scale have the following meanings (see the explanation of simple transformations.):

<sx> and <sy>
The factor used to scale the x and y coordinates so that one unit in the x and y directions of the new coordinate system is the same as <sx> and <sy> units in the previous coordinate system, respectively. If <sy> is not provided, it is assumed to be the same as <sx>.
<center-x> and <center-x>
The center point for the scaling operation, in the previous coordinate system. If a center point is provided, the resulting operation is as if the coordinate system were translated by [0 0 0 0 -<center-x> -<center-y>], then scaled, then translated again back to the center point. If either <center-x> or <center-y> is not provided, they default to zero.

The parameters to rotate have the following meanings (see the explanation of simple transformations.):

<rotate-angle>
The angle in degrees by which the coordinate system axes should be rotated (counterclockwise).
<center-x> and <center-x>
The center point for the rotation operation, in the previous coordinate system. If a center point is provided, the resulting operation is as if the coordinate system were translated by [0 0 0 0 -<center-x> -<center-y>], then rotated, then translated again back to the original center point. If either <center-x> or <center-y> is not provided, they default to zero.

(The working group is investigating providing convenience mechanisms for other simple transformations such as skew and reflect.)

fit provides a mechanism for having the SVG processor automatically compute a transformation matrix such that the coordinates <bbox-left> <bbox-top> <bbox-right> <bbox-bottom> (all <number>s) will align exactly with the corresponding corners of the viewport. You can specify preserve-aspect-ratio to ensure that the aspect ratio of the graphic is preserved (i.e., uniform scaling onto the viewport) and what kind of horizontal and vertical alignment within the viewport is desired.

<bbox-left> <bbox-top> <bbox-right> <bbox-bottom>
The bounding box for all graphical objects within the current element (i.e., the element containing the 'tranform: fit()' property) expressed in the same coordinate system as the contents of the current element. Typically (although this doesn't have to be the case), this is a rectangle which is defined to be just large enough to surround all drawn graphics for the current object and its children.
preserve-aspect-ratio
If present, then the transformations that are computed ensure that the aspect ratio of the graphic will be preserved (i.e., uniform scaling will be used to map the bbox onto the viewport).
left, center, right
If preserve-aspect-ratio is specified and the aspect ratio of the bbox does not match the aspect ratio of the viewport, and furthermore if the bbox's height/width is greater than the viewport's height/width, then either left, center or right justify the box inside the viewport, respectively.
top, middle, bottom
If preserve-aspect-ratio is specified and the aspect ratio of the bbox does not match the aspect ratio of the viewport, and furthermore if the bbox's height/width is less than the viewport's height/width, then either top, middle or bottom justify the box inside the viewport, respectively.

(Insert a drawing here.)

8.5 Establishing a New Viewport: the <svg> element within an SVG document

At any point in an SVG drawing, you can establish a new viewport into which all contained graphics should be drawn by including an <svg> element inside an SVG document. By establishing a new viewport, you also implicitly establish a new initial user space, new meanings for many of the CSS unit specifiers and, potentially, a new clipping path.

To establish a new viewport, you use the positioning properties from CSS such as left:, top:, right:, bottom:, width:, height:, margin properties and padding properties. Here is an example:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG April 1999//EN" 
  "http://www.w3.org/Graphics/SVG/svg-19990412.dtd">
<svg width="4in" height="3in">
  <desc>This SVG drawing embeds another one,
    thus establishing a new viewport
  </desc>
  <!-- The following statement establishing a new viewport
       and renders SVG drawing B into that viewport -->
  <svg style="left: 25%; top: 25%" width="50%" height="50%">
     <!-- drawing B goes here -->
  </svg>
</svg>

Download this example

You can also specify values for the 'clip' and 'overflow' properties for <svg> elements within an SVG document. If specified on an <svg> element, these properties will change the current clipping path. (Note that these properties will be ignored if used on any other type of element.)

8.6 Properties to Establish a New Viewport

(Extract sections from chapter 8 of the CSS spec. Make modifications as necessary.).

8.7 Units

All coordinates and lengths in SVG can be specified in one of the following ways:

If possible, the SVG processor should be passed the actual size of a px unit in inches or millimeters by its parent processor. (See Conformance Requirements and Recommendations.) If such information is not available from the parent processor, then the SVG processor should assume a px is defined to be exactly .28mm.

8.8 Redefining the meaning of CSS unit specifiers

The process of establishing a new viewport via an <svg> element inside of an SVG document changes the meaning of the following CSS unit specifiers: px, pt, cm, mm, in, and % (percentages). A "pixel" (the px unit) becomes equivalent to a single unit in the user coordinate system for the given <svg> element. The meaning of the other absolute unit specifiers (pt, cm, mm, in) are determined as an appropriate multiple of a px unit using the actual size of px unit (as passed from the parent processor to the SVG processor). Any percentage values that are relative to the current viewport will also represent new values.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG April 1999//EN" 
  "http://www.w3.org/Graphics/SVG/svg-19990412.dtd">
<svg width="300px" height="3oopx">
  <desc>Transformation with establishment of a new viewport
  </desc>
  <!-- The following two text elements will both draw with a 
        font height of 12 pixels -->
   <text style="font-size: 12">This prints 12 pixels high.</text>
   <text style="font-size: 12px">This prints 12 pixels high.</text>

   <!-- Now scale the coordinate system by 2. -->
   <g style="transform: scale(2)">

      <!-- The following text will actually draw 24 pixels high
           because each unit in the new coordinate system equals
           2 units in the previous coordinate system. -->
      <text style="font-size: 12">This prints 24 pixels high.</text>

      <!-- The following text will actually still draw 12 pixels high
           because the CSS unit specifier has been provided. -->
      <text style="font-size: 12px">This prints 12 pixels high.</text>
   </g>

   <!-- This time, scale the coordinate system by 3. -->
   <g style="transform: scale(3)">

      <!-- Establish a new viewport and thus change the meaning of
           some CSS unit specifiers. -->
      <svg style="left:0; top:0; right:100; bottom:100" 
           width="100%" height="100%">

         <!-- The following two text elements will both draw with a 
              font height of 36 screen pixels. The first text element
              defines its height in user coordinates, which have been
              scaled by 3. The second text element defines its height
              in CSS px units, which have been redefined to be three times
              as big as screen pixels due the <svg> element establishing
              a new viewport. -->
         <text style="font-size: 12">This prints 36 pixels high.</text>
         <text style="font-size: 12px">This prints 36 pixels high.</text>

      </svg>
   </g>
</svg>

Download this example

8.9 Further Examples

(Include an example which shows multiple viewports, multiple user spaces and multiple use of different units.)