Features of Quartz 2D


Quartz is a fundamental part of the Mac OS X operating system. It is also, by nature, a highly visible one. You will no doubt recognize many of the features of Quartz 2D as hallmark features of Mac OS X. Quartz 2D extends many of these same features to your application. Incorporating it into your application is a vital part of ensuring that your application provides the finest user experience possible. This exploration begins by looking at the features of Quartz 2D that your application can take advantage of.

Device Independence

The Quartz 2D graphics library exemplifies two of the design goals of the Mac OS X graphics system. The first goal is that the graphics system should support as many different devices as possible. It has to work with printers, displays, offscreen bitmaps, image files, and any of a number of other devices that can reproduce drawings. The second goal of the graphics system is that it should make it as easy as possible for your application to take advantage of the power those devices have to offer.

Quartz 2D addresses both of these design goals. First, the library allows programs to establish connections to the full range of supported graphics devices. This opens those devices to any application that works with the library. The computer maintains the connection between an application and a device through an object called a graphics context.

To simplify the drawing process, each graphics context that Quartz creates understands the same drawing commands. In most cases, your application only needs to know how to use this common, abstract drawing model. It can leave the complexities of reproducing those graphics on the output side to the library.

The abstraction allows the application to treat all of the graphics devices uniformly. A Quartz 2D graphics application uses the same drawing code to draw its document in a window that it uses to print the document on a printer. The fact that you don't have to maintain separate code for each device simplifies programming and improves the consistency of the graphics.

To give you an idea of the device independence of the Quartz 2D library, Table 3.1 gives a list of some of the devices that Quartz 2D supports.

Table 3.1. Some of the Devices Supported by Quartz 2D

Type of Device

Description

Notes

Bitmap

Any pixel drawing environment

Examples of bitmap drawing environments include windows, views, and offscreen bitmaps.

Printing

Printers of various types including both raster and PostScript printers

Quartz 2D and the Mac OS X printing system support a wide variety of printer types. The support includes both raster and PostScript printers.

PDF

A stream of PDF data

PDF is a convenient format for recording Quartz 2D drawing commands. Most of the time, the PDF stream will be stored in a file, but Quartz 2D can also create a PDF stream in memory.

OpenGL

An OpenGL Context

Using CGGLContextCreate, Quartz 2D can draw its graphics on an OpenGL context. This makes it easy to incorporate graphics that have traditionally been difficult to draw through the OpenGL API. A notable example is text which OpenGL does not support directly.


Resolution Independence

To some extent, the resolution independence of the Quartz 2D graphics library follows logically from its device independence. The resolution of an ink-jet printer, for example, is usually much higher than the resolution of a computer display screen and Quartz 2D draws to both.

Like device independence, the resolution independence of Quartz 2D stems from the abstract drawing environment that Quartz 2D sets up when it connects an application to a particular graphics device. The abstract drawing environment accepts primitives that are not tied to any particular resolution. It is the graphics context's responsibility to translate the resolution independent primitives to the device's fixed resolution drawing space.

Chapter 4, "The Graphics Context," examines the characteristics of the graphics context and present short code samples that demonstrate how to create contexts for a variety of devices.

Rich Transformations

One of the features of QuickDraw, a feature that distinguished it from its predecessors, was the ability to record a series of drawing commands into a picture and then play the picture back in an arbitrary rectangle. What made this feature so compelling was the fact that QuickDraw would scale the drawing if the source and destination rectangles did not match.

Because QuickDraw recorded its graphics at a fixed resolution, scaling the graphics larger resulted in a jagged representation of the graphic. Scaling a graphic much smaller lost a lot of the detail in the graphic. In spite of these shortcomings, the ability to reproduce QuickDraw graphics at any scale was quite revolutionary at the time. Scaling was not QuickDraw's only coordinate transformation; it could also translate the origin of the coordinate system to move graphics within a port. These two operations are the only coordinate space transformations in QuickDraw. Notably missing is the ability to rotate graphics.

Quartz 2D provides a much richer environment for transforming graphics, wherein an application is free to apply any of three basic transformations to its graphics, translations (moving a graphic along the coordinate axes, similar to changing the origin in QuickDraw), scaling, or rotation. By combining transformations, an application can create complex effects. You also can use these transformations to greatly simplify complicated drawing tasks. Because the drawing primitives in the Quartz imaging model are largely resolution independent, scaling and rotating Quartz 2D graphics does not yield the ungainly characteristic of scaled QuickDraw graphics.

Chapter 5, "Transformations," discusses transformations in Quartz 2D and the routines to combine them.

Integrated Color Management

When a department store puts out a catalog advertising their latest winter fashions, it is important that the catalog correctly reproduce the exact shade of purple in their finest cashmere sweater. The science that ensures the color of that sweater is faithfully reproduced is called color management. This technology is useful in many different environments, not just in the complex world of professional publishing. Applications like iPhoto apply color science principles to ensure that the photographs of your child's kindergarten graduation look as close as possible to the actual event during a computer slide show.

ColorSync was among the first color management systems to debut on personal computers and is an excellent technology that strikes a good balance between features and performance. Apple originally built the ColorSync functionality into the operating system as technology to complement QuickDraw. The library was built as a separate component in part because it would introduce a large number of changes to the QuickDraw imaging model.

Because Quartz 2D began with a clean slate, the library integrated ColorSync color management directly into its drawing model. Every graphics context includes information about its color characteristics. Colors drawn into the context are carefully matched against those color characteristics.

Quartz 2D offers applications a remarkable amount of control over color. You can decide to what extent your application participates in the color management process. If color fidelity is not a critical feature of your application, you can rely on the default behavior of the operating system to setup and manage the color matching environment. If, however, you need to have fine-grained control over the color matching process, your code can examine or change the color characteristics of the context, the colors you provide for drawing, and the colors of incoming images.

Integrated color management plays a role in simplifying the drawing process while supporting a large number of devices. For example, an application is free to use whatever color model is most appropriate for representing its content. Programs that present their graphics on the screen can specify colors in the RGB space of the computer display and the color management system does its best to ensure that the RGB colors are reproduced as faithfully as possible when printed to an ink jet printer. By the same token, printing applications can draw their graphics using CMYK colors while the application can set up the color environment so that the display screen matches the printed results in spite of the colors being specified in a completely different color space.

Transparency and Antialiasing

Computers spend a good amount of time combining smaller graphics elements into larger compositions. Consider, for example, the way that windowing systems combine the separate images of different windows into a single picture on the display. The process of combining graphics is called compositing and is a fundamental operation of the Quartz 2D imaging model.

A Quartz 2D application creates pictures by drawing individual graphics primitives and then uses compositing to combine those elements into the final presentation. One of the most interesting features of Quartz 2D, a feature that is not found in many of the other graphics libraries that preceded it, is the ability to use transparency when compositing graphics together. Using Quartz 2D, your application can ask that a particular graphics primitive be drawn using a translucent paint. When the computer composites that graphic with other graphics in the context, some of the picture behind the primitive will show through the translucent paint.

If you want to see the results of translucent compositing, you need look no further than the Mac OS X UI. The computer uses Quartz transparency in the backgrounds of menus, in the title bars of inactive windows, and to display the scaled icons of the Dock.

To support transparency, Quartz 2D employs a technique pioneered by Thomas Porter and Tom Duff. They co-authored a paper called "Compositing Digital Images," which was part of the 1984 SIGGRAPH conference. The technique that Porter and Duff developed incorporates transparency into an image by providing an additional component to each color. This component is called the alpha channel. The value of the component is interpreted as the level of transparency in the color.

The Core Graphics system integrates alpha channel compositing into the Quartz 2D graphics model. The API of Quartz 2D requires you to specify both the color components and the alpha channel value for each color you create. The Core Graphics windowing system honors the transparency of window graphics. The software and hardware renderers use that transparency when putting together an image for the computer's display.

Not all of the output devices that Quartz 2D can draw to support transparency as well as computer displays do. For example, the PostScript system does not include direct support for transparency. As a result, PostScript printers might have difficulty with transparent graphics. If your application draws to such a device with Quartz 2D, the computer will do its best to ensure that the image is reproduced as faithfully as possible, but the additional effort may require additional computing resources, compromise in the quality of the image, or a combination of both to reproduce the effect.


The alpha channel also plays a role in another important Quartz 2D feature, antialiasing. Antialiasing refers to a technique that softens the jagged edges of computer graphics when they are drawn on pixel-oriented devices like the computer display. The technique decreases the contrast of the jagged edges and makes graphics look smoother. Antialiasing improves the appearance of line art and can provide a marked improvement to the legibility of text. Figure 3.1 uses some text to convey the difference between antialiased and non-antialiased graphics.

Figure 3.1. Non-Antialiased and Antialiased Text


We've magnified the graphics in Figure 3.1 to exaggerate the pixels of the image and make them easier to discern. The non-antialiased text clearly shows the jagged edges, or jaggies, that are characteristic of computer graphics. In the antialiased text, the jagged edges have been replaced with pixels that appear, in the figure, as different shades of gray. In practice Quartz 2D would not use different colors of gray to achieve the effect. Instead, the computer would use black pixels with different levels of transparency. Those pixels only appear gray in Figure 3.1 because they are translucent black pixels printed on a white background.

While Figure 3.1 uses text to demonstrate the effects of antialiasing, Quartz 2D apples the same technique to all of its drawing. To implement antialiasing, Quartz uses a technique called sub-pixel sampling. The computer breaks each pixel into a number of smaller, sub-pixels and uses information about which of those sub-pixels are touched during drawing to decide the transparency level of the larger composite pixel. The net effect is that of increasing the resolution of the graphics device. More information on sub-pixel sampling and antialiasing can be found in most computer graphics textbooks.

Antialiasing is not the only technique that Quartz can use to improve the legibility of on-screen text. The computer also can employ a technique that Apple calls font smoothing. Font smoothing also employs sub-pixels to reduce the jaggedness of text on-screen. Unlike antialiasing, however, font smoothing is based on the physical sub-pixels of an LCD monitor. On such a monitor, each pixel is physically constructed of separate red, green, and blue sub-pixels. The computer can exploit this fact to smooth the edges of text by applying colors to the edge pixels. The different colors turn on and off the LCD sub-pixels, and your brain ignores the colors in favor of perceiving a smooth edge. While font smoothing works well on LCD screens, it is of limited use on traditional phosphor monitors.


Rich Text Support

Mac OS X supports the Unicode text encoding standard and enables developers to create software that can adapt to the rich set of diverse text systems used by cultures across the globe. In this environment, writing software that supports the variations of the written word is quite a complex process. Mac OS X handles the complexity by breaking the text layout and presentation process into a number of discrete steps.

In the first step, the computer converts the text that a user types into a collection of drawings called glyphs. The computer may use the same character to represent the letter "A," but that A will be represented by a very different glyph when drawn in Times than when it is drawn in Helvetica. Once the computer has converted the characters into a series of glyphs, the software arranges the glyphs so that they occur in the proper order defined by the writing system. In the final step, the computer draws those glyphs on a graphics device.

The graphics system in Mac OS X separates the steps of the text presentation process. The complexities of glyphs selection and layout are handled by text processing libraries such as ATSUI and the Cocoa Text system. When the time comes to draw those glyphs on a graphics device, however, Mac OS X turns to Quartz 2D.

For the most part, the text capabilities in Quartz 2D are limited to the final stages of the text pipeline. The role of Quartz 2D largely is to combine the layout information of higher level libraries, with the graphics information it extracts from fonts to draw glyphs on the graphics device. The Quartz context API does include some routines that can take text, usually text encoded using the MacRoman text encoding, through the entire text processing pipeline. Those routines provide very rudimentary text layout support, and their use is discouraged. Applications that use higher-level text libraries enjoy the added flexibility those library offer. The text layout libraries, in turn, can draw their text using all the advantages Quartz 2D has to offer.

Performance

The previous chapter devoted some time to discussing the performance of modern graphics systems and the increasingly important role of dedicated graphics hardware. One of the advantages of a high level graphics library like Quartz 2D is that it isolates your application from many of the complexities of the hardware. Quartz 2D is carefully crafted to take advantage of hardware features like AltiVec and programmable graphics cards. Your application will enjoy the advantages of those hardware services by virtue of the fact that it draws graphics through Quartz 2D.

That doesn't free your application from all responsibility, however. Choices you make in your application and the way you interact with Quartz 2D can have an impact on the library's ability to make use of the hardware. Quartz 2D can help you balance the efficient use of hardware resources to ensure your application runs at the peak of performance on any machine.




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