User Space and the Page


An important feature of Quartz 2D is its device and resolution independence. To provide those features, Quartz 2D defines a drawing environment called user space.

User space is an idealized drawing environment that provides a level of indirection to the drawing process. Your application creates its graphics in this idealized drawing environment, and the computer transfers those drawings to the output device as optimally as possible. Two features that demonstrate how this is an idealized graphics environment are the environment's independence in both resolution or color space. Neither the drawing space itself nor the primitives you place into it are tied to a particular resolution. Applications specify colors from a virtually limitless number of color spaces. In contrast, computer displays and printers have both fixed resolutions and set color spaces.

Because an abstract drawing environment has a nice name, you might also like to have a nice name for the abstracted drawing destination. For this, Quartz 2D draws upon its legacy. As the descendent of an imaging system, which laser printers used to describe drawings destined for paper, the generalized name for the drawing destination is called the page. In actuality, the page could be a window, an offscreen bitmap, or (heaven forbid) even an actual page on a printer! Regardless of the destination, however, Quartz 2D literature will often speak of drawing on the page as a means of sending graphics to any of a number of graphics devices.

User Space Coordinates

Like most other graphics libraries. Quartz 2D allows you to create and position graphics primitives with a Cartesian coordinate system. In fact, when working with graphics primitives, the coordinates you provide most often identify locations in user space. However, there are some aspects of this coordinate space that may seem unusual to someone familiar with traditional screen-based graphics libraries.

The first unusual feature is that the coordinate system of user space does not have a fixed orientation, or scale, relative to device space. When Quartz creates a context on a given device, it puts user space into a default orientation on the page. From there, programs are free to change that orientation to suit their drawing needs.

Most screen-based computer graphics libraries place the origin of their coordinate system in the upper left corner of the display. This was done to make computer hardware easier to implement. With the coordinates in the upper left corner, subsequent bits in the frame buffer would fall in the same order that the CRT monitor's electron beam would scan the screen. Because user space is an idealized drawing environment, however, its default orientation is a coordinate orientation based on traditional mathematics. Figure 4.1 demonstrates this difference.

Figure 4.1. Orientations of Quartz and Traditional Coordinate Systems


The diagram illustrates the location of the origin of the coordinate system and the direction of the positive x and y axes. The orientation of the typical screen graphics library is shown on the left side of the figure. Graphics systems like QuickDraw place their origin, the point (0, 0), in the upper left corner of the device. From that vantage point, increasing y coordinates extend down the screen as shown in Figure 4.1. This axis orientation is often called a left-handed coordinate system.

In Quartz 2D, in contrast, the default location of the origin is the lower left corner of the page. From there the increasing the y coordinates extend upward on the device. This is illustrated in the right half of Figure 4.1. This is commonly referred to as a right-handed coordinate system.

To understand the difference between left-handed and right-handed coordinate systems, imagine a third axis, the z coordinate axis, extends from the screen to the tip of your nose. If you were to grasp this axis with your hand so that your thumb points away from the screen, with either hand. your fingers would curl in opposite directions. With your left hand, your fingers curl in a clockwise direction. In a left-handed coordinate system you turn the x axis clockwise to make it meet the y axis. In gripping the z axes, the fingers on your right hand curl counter-clockwise. In a right handed coordinate system, you have to rotate the x axis counter-clockwise for it to meet the y axis.


The difference in the default orientations of these two coordinate systems can be a real source of confusion for programmers making the transition from other graphics libraries. When porting code from Windows, or Carbon, programs have to take the different y axis into account.

Keep in mind, however, that this is only the default orientation for the coordinates. This is the orientation of the axes when the Quartz creates a new context and attaches user space to a device. Programs can use the CGContext API to reorient the coordinate space at any time. Chapter 5, "Transformations," focuses on the ability to reorient user space and describes how applications can turn that ability (no pun intended) to their advantage.

This ability to reorient the coordinate axes can be a handy technique to apply when changing code from QuickDraw to Quartz 2D. The basic idea is to reorient the Quartz coordinate axes so that they match the default location and orientation of the QuickDraw coordinate system. Your drawing code then uses the same coordinates with Quartz that it used with QuickDraw.

The HIView system, for example, employs this technique so that Carbon programmers can reuse drawing code from their custom controls when implementing modern HIViews to replace them.

Changing the coordinate axes like this does introduce a few challenges, however. Quartz 2D relies on orientation of the axes when it draws text and images. Those graphics may draw upside down without careful planning. The HIView system actually includes a routine, HIViewDrawCGImage, to handle this very situation. Should you decide to port your QuickDraw code by changing the axes, keep in mind that Images and Text may require special attention.


The relationship between user space and the page is not defined by the orientation of the coordinates alone. In considering the way graphics move from user space to the page, you also have to consider the scale of the coordinate system.

Coordinate axes usually have some unit of measurement associated with them. In pixel-based graphics libraries like QuickDraw, the unit of measurement is the pixel. Each unit along the coordinate axis corresponds to the number of pixels between that coordinate and the origin. When Quartz 2D creates a context on a bitmap device, be it a window or an offscreen bitmap, it sets up user space so that its scale corresponds to the underlying pixel coordinate system of the bitmap. In other words, the computer scales user space so that one unit in user space corresponds to one pixel in the bitmap.

Quartz, however, is a device independent API. Mac OS X can create a CGCon-text for a device that doesn't even have pixels. This leads to a bit of a quandary. When working with a device that doesn't have pixels, say a PostScript laser printer, there is no fundamental unit to apply to the coordinates of user space. There is no obvious measurement standard.

The Quartz imaging model descends from a graphics system used to print on paper. It follows, therefore, that a natural scale for the imaging model was the same scale used to describe paper. The Quartz imaging model, therefore, defines the units of user space in terms of inches.

Quartz doesn't use the inch as the basic unit of user space, however. Instead, it uses a unit of measurement called the Point. The naming of the Point is rather unfortunate as it is easy to confuse it with the term "point" referring to a location in the coordinate system, a point in space. More accurate terms for this unit of measurement might be the Typographer's Point or Printer's Point.

"Printer," in this case, doesn't refer to the modern electronic device. The term actually hearkens to the hearty craftsman of old who printed pages one at a time by inking cast lead blocks and then feeding single sheets into a manual printing press. The Printer's Point evolved as a unit of measurement describing the relative sizes of different typefaces. Modern computers uphold this tradition to this day. When you are working in your favorite word processor and want to select a font size, you might choose a 12-point font for body text, but a 24-point font for a title.

Over the years the point has referred to any of several different measurements. There have even been regional variations and competing standards that each defined a point to be a different unit of measurement. Quartz 2D follows a precedent set by PostScript and defines a Point to be exactly of an inch.

When the computer initially creates a CGContext on a device that is not a bitmap device, it scales the coordinate system so that one unit along the coordinate axes in user space corresponds to one point on the graphics device. That means that if you drew a line in user space from (0, 0) to (0, 72) you would have a vertical line that is one inch long.

Floating Point Geometry

The positions in user space use floating point values to represent the coordinates. The locations your application works with can include fractional positions. You can, for example, draw a line from (1.23957, 23.3338) to (39.4727, 8.8282). The computer will do its best to represent that line on whatever device it is drawn.

The resolution independence of user space is, in part, taken from the use of floating point coordinates. User space is a "dense" coordinate space where the distance between coordinate positions the computer can represent is very small. This does have some consequences that may not be immediately apparent. For example, when working with floating point values, the x coordinates 1.000 and 1.001 are very close together but not equal. The program logic of graphics code that used to work fine for integer coordinates may have to change to accomodate floating point coordinates.

As an idealized drawing environment, user space extends to the limits of the computer's floating point number representation. For practical purposes this means that user space is, in essence, unbounded. When Quartz 2D maps a graphic from user space onto the destination device, any drawing that falls outside the imaging area of device space is simply clipped away.

The fundamental structure for storing a location in a Quartz coordinate system is the CGPoint structure. There is also the CGSize structure for storing width and height dimensions, and the CGRect structure, which combines a point and size to represent a rectangle. The declarations of these structures as well as routines for creating and manipulating them can be found in the CGGeometry.h header file.




Quartz 2D Graphics for Mac OS X Developers
Quartz 2D Graphics for Mac OS X Developers
ISBN: 0321336631
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net