Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Programming with Quartz: 2D and PDF Graphics in Mac OS X
Programming with Quartz: 2D and PDF Graphics in Mac OS X
Programming with Quartz: 2D and PDF Graphics in Mac OS X
Ebook1,045 pages

Programming with Quartz: 2D and PDF Graphics in Mac OS X

Rating: 3.5 out of 5 stars

3.5/5

()

Read preview

About this ebook

Written by members of the development team at Apple, Programming with Quartz is the first book to describe the sophisticated graphics system of Mac OS X. By using the methods described in this book, developers will be able to fully exploit the state-of-the-art graphics capabilities of Mac OS X in their applications, whether for Cocoa or Carbon development. This book also serves as an introduction to 2D graphics concepts, including how images are drawn and how color is rendered. It includes guidance for working with PDF documents, drawing bitmap graphics, using Quartz built-in color management, and drawing text. Programming with Quartz is a rich resource for new and experienced Mac OS X developers, Cocoa and Carbon programmers, UNIX developers who are migrating to Mac OS X, and anyone interested in powerful 2D graphics systems.
  • This is the definitive guide to the revolutionary graphics system of Mac OS X that uses the Portable Document Format (PDF) as the basis of its imaging model
  • It contains the latest on programming with Quartz for Mac OS X version 10.4
  • Carefully crafted and extensive code examples show how to accomplish most of the drawing tasks possible with Quartz
LanguageEnglish
Release dateJul 26, 2010
ISBN9780080459639
Programming with Quartz: 2D and PDF Graphics in Mac OS X
Author

David Gelphman

David Gelphman is a senior software engineer on Apple's Graphics and Imaging team. He has over 20 years of experience working with the PostScript and PDF imaging model that is at the heart of Quartz graphics. His computer career began with scientific computing while he was earning his PhD in experimental particle physics at Stanford. Apple's introduction of the Macintosh in 1984 caused him to make a sharp turn into the world of user-friendly computing and he hasn't looked back.While working at Adobe Systems, David co-designed Apple's LaserWriter 8 printer driver and was team lead of the development project. At Adobe he also worked closely with third party developers, taught PostScript programming, wrote a number of technical notes, and contributed to the evolution of the PostScript language. After leaving Adobe he continued to combine software engineering work with documentation for software developers, authoring an article for Apple's develop technical journal in addition to a number of Apple technical notes. Today David writes system software for Mac OS X but continues to be involved with third party documentation and enjoys writing sample code for developers outside of Apple.

Related authors

Related to Programming with Quartz

Titles in the series (25)

View More

Software Development & Engineering For You

View More

Reviews for Programming with Quartz

Rating: 3.3333333333333335 out of 5 stars
3.5/5

3 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Programming with Quartz - David Gelphman

    Index

    Introducing Quartz 2D

    The Apple Mac OS X operating system contains a powerful 2D graphics imaging library called Quartz. A large part of what gives the Mac OS X user interface the rich look that it has is the use of Quartz drawing by the Aqua user interface and the built-in application toolkits.

    The Quartz 2D drawing library allows applications to draw text, curves and shapes, images, and Portable Document Format (PDF) documents in amazingly flexible ways. This rich drawing library supports drawing graphics to an onscreen window or an offscreen bitmap and creating new PDF documents or PostScript output data.

    Quartz 2D is also known as Core Graphics, which is frequently abbreviated as CG. Quartz is a term aimed at consumers, whereas Core Graphics is known to many programmers. The Quartz 2D technology is implemented primarily in the Apple Core Graphics framework (ApplicationServices.framework/Frameworks/CoreGraphics.framework), so it’s important that you equate one term with the other. All Quartz 2D function calls use the prefix CG. In this book, the terms Quartz, Quartz 2D, and Core Graphics are synonymous with Quartz 2D drawing library unless stated otherwise.

    A Bit of History

    The history of Quartz is partly told by the story of the evolution of graphics programming on the Macintosh computer, starting with the Mac’s introduction in 1984. While not strictly necessary to understand Quartz itself, its history provides insight into its power and what you can accomplish with it. There are two main players in this story—the graphics capabilities provided by the Macintosh and the breakthrough graphics applications created for the early Mac. As is the case with most evolutionary processes, outside influences—Adobe, Aldus, the NeXT programming environment, and a number of key individuals—played pivotal roles in this story.

    The graphics system on the Macintosh in its initial incarnation in 1984 was borrowed from the graphics system created for the Apple Lisa computer. Bill Atkinson designed and wrote the initial QuickDraw graphics programming library that was the cornerstone for all drawing done on the Macintosh display. QuickDraw was a pixel-oriented graphics system with a 16-bit, integer-based coordinate system. Initially, it had little support for color. These may seem like significant limitations today, but at the time QuickDraw was an amazing advance for graphics on a personal computer. Compare the typical user interface of a computer before QuickDraw graphics on the left in Figure 1.1 with that using QuickDraw graphics on the right.

    Figure 1.1 Graphics before (left) and after (right) QuickDraw

    Equally important in the early days of the Macintosh was MacPaint—the signature graphics application program that gave the Macintosh its immediate designation as a platform for creating graphics. Written by Bill Atkinson, this program was the first among a series of innovative graphics applications that ran on the Macintosh platform. MacPaint was a painting program for creating 1-bit-deep, black-and-white graphics, which was essentially what QuickDraw supported in its early days. Figure 1.2 shows the MacPaint program running on an early Macintosh computer, and Figure 1.3 shows a drawing done with MacPaint.

    Figure 1.2 MacPaint running on an early Macintosh computer

    Figure 1.3 A drawing done with MacPaint

    MacDraw was the second major new graphics application for the Macintosh that took graphics to a new level. It was an object-based graphics drawing program rather than a pixel painting program like MacPaint. A user could build a drawing by choosing one or more predefined shapes or creating a shape from a series of lines. Solid and pattern fills were available to enhance the image. Figure 1.4 shows MacDraw with an example drawing.

    Figure 1.4 MacDraw running on an early Macintosh computer

    In 1985, Apple Computer and Adobe Systems paired up to create the Apple LaserWriter printer, the next fundamental advance of graphics on the Macintosh. This was the first printer to incorporate the PostScript language developed by Adobe. Prior to the LaserWriter printer, the detailed onscreen graphics created by a Mac could only be printed to low-resolution dot matrix printers. The LaserWriter printer, with its 300 dots per inch (dpi) output, high-quality typography, and laser printer sharpness was an enormous boost to the appeal of the Mac. Figure 1.5 is a photograph of a Macintosh and the original LaserWriter printer.

    Figure 1.5 The Macintosh and the original LaserWriter printer

    The true power of the LaserWriter printer was more than just the fact that it could impressively print QuickDraw graphics. The LaserWriter printer used the PostScript programming language and imaging model, a much more versatile graphics system than Apple’s QuickDraw.

    The superiority of PostScript became apparent when Aldus Corporation introduced the PageMaker application program for the Mac. By generating PostScript for printing, PageMaker achieved results that far exceeded any other program on the Macintosh at the time. The Macintosh, PageMaker, and the LaserWriter were the tools that began the desktop printing revolution. When Linotype Corporation introduced PostScript-based typesetters that could produce output exceeding 2400 dpi, users were able to produce true professional print quality with PageMaker, a Macintosh computer, and a high-resolution PostScript output device.

    Adobe Systems took the next huge step to advance graphics applications on the Macintosh platform when it introduced the Adobe Illustrator application in early 1987. The Illustrator application allowed users to draw impressive line art graphics with precise curves and lines and place those together with text. The artistic beauty of what could be drawn with the Illustrator program was the best on any personal computer to date. Figure 1.6 shows one of the early Illustrator drawings. The Illustrator application didn’t use QuickDraw for its graphics but instead relied on a drawing engine based on Adobe PostScript software.

    Figure 1.6 A drawing done with the Adobe Illustrator application

    © 1987 Adobe Systems Incorporated. All rights reserved. Adobe and Illustrator are either registered trademarks of Adobe Systems Incorporated in the United States and/or other countries.

    Also in early 1987, Apple introduced the Macintosh II, the first color Macintosh computer. The QuickDraw drawing library was extended to allow drawing in color and was renamed Color QuickDraw. Color QuickDraw greatly expanded the capabilities of the Macintosh system software for drawing but it didn’t fundamentally change the drawing model of QuickDraw, which continued to be a pixel-oriented drawing system based on integer coordinates.

    Soon after the introduction of the Macintosh II, Fractal Software (later changed to Fractal Design) pushed the envelope for graphics on the Macintosh with the introduction of the ImageStudio application. ImageStudio was the first Macintosh program for editing grayscale images that were produced by scanners, and it could also be used as a grayscale pixel painting program. This was a precursor of things to come on the Macintosh platform.

    The next major graphics advance on the Macintosh occurred when the little-known BarneyScan image editing program was acquired by Adobe Systems and renamed Adobe Photoshop. After it shipped in 1990, Photoshop quickly became the industry standard for image editing on personal computers. When Photoshop later introduced layers, it utilized the technique of alpha compositing. Alpha compositing, formalized by Porter and Duff in 1984, allows the transparency of graphics to be controlled by the user. With alpha compositing, graphics drawn on top of other graphics can be translucent to expose graphics layers underneath. At the time, alpha compositing wasn’t available in QuickDraw, and the PostScript imaging model only supported the drawing of opaque graphics on top of other graphics.

    Meanwhile developments elsewhere were at work shaping the long-term history of Macintosh graphics. In 1988 Adobe Systems introduced The Display PostScript System, better known as Display PostScript. Display PostScript was an implementation of the Adobe PostScript interpreter with extensions to make it more suitable for use as an interactive graphics programming library. NeXT, the company started by Apple cofounder Steve Jobs, was the first to license it. The NeXT implementation of Display PostScript added the ability to draw with alpha compositing.

    The huge selling point of Display PostScript was the power of PostScript graphics. The integration of Display PostScript on the NeXT computer meant that developers for the platform had that power at their fingertips. Because of the popularity of PostScript printers, the notion of WYSIWYG (What you see is what you get) was taken to another level beyond that on any other platform. A disadvantage of Display PostScript was that programming it sometimes required in-depth understanding of the arcane PostScript language.

    As the Macintosh matured, it became clear that a more modern graphics subsystem than QuickDraw was necessary for the platform. Major third-party developers such as Adobe, Aldus, Fractal Design, and Quark were using much more powerful graphics libraries in their software than Apple had built into the Macintosh. Many of these graphics libraries were built on the PostScript imaging model. To produce high-quality graphics for the Macintosh, developers now had to write their own graphics routines since the capabilities provided by the Macintosh platform were lagging the state of the art, much of which was driven by PostScript.

    Over time Adobe realized that Display PostScript wasn’t going to take over the world. But Adobe had ideas on how some of the benefits of PostScript could be brought to users. Adobe began to focus on the fact that a PostScript page description of text and graphics had excellent potential as an archival form and many users were using it as such. While not editable, a PostScript page description could reproduce the visual form of the graphics and text. However, using PostScript to archive graphics required a PostScript interpreter to view the graphics. Adobe had only limited success in licensing Display PostScript, which caused Adobe to pursue another form for PostScript graphics.

    Adobe created an easily parseable form of a graphics description that didn’t utilize the programmability of the PostScript language but instead utilized its underlying imaging model. Adobe created the Portable Document Format (PDF) specification based in part on the Adobe Illustrator file format and using an imaging model based on the PostScript language. In 1993, Adobe introduced Adobe Acrobat and the PDF document format.

    In 1996, Apple bought NeXT, bringing Steve Jobs, the NeXTStep operating system with Display PostScript, and a new team of graphics programmers to Apple. The PostScript imaging model in Display PostScript was still a powerful 2D graphics imaging model, but by that time the landscape had changed.

    With its unsurpassed imaging model, PDF had become an industry standard for distributing documents. For Mac OS X, Apple created its own graphics library based on the PDF imaging model, enhancing it in various ways, including the support of alpha transparency. In addition to alpha compositing, this graphics library supports anti-aliasing, which produces higher-quality results on low-resolution output devices such as screen displays.

    Apple calls the implementation of this graphics library Core Graphics internally and uses the name Quartz 2D publicly. Quartz has virtually all the imaging power of PostScript and PDF while adding significant enhancements, greatly increasing the capabilities of the graphics system available to third-party software developers writing for the Macintosh. These and other features are discussed throughout the book.

    Quartz 2D Overview

    The Quartz 2D imaging model is based on the PDF imaging model. The PDF imaging model itself is based on the PostScript imaging model, which has proved to be a robust drawing model that is highly capable of describing complex drawing as well as document pages that contain text and images. The additional features added by the PDF imaging model combined with those uniquely added by Quartz add up to a flexible, high-quality library for graphics programming. The capabilities and quality of the graphics system available on the Mac OS X platform now approach that previously available in only the most advanced applications. By learning graphics programming with Quartz 2D, you will be able to harness the power of the Quartz imaging model.

    The Quartz coordinate system is extremely flexible. Coordinates are specified as floating-point values, and the coordinate system itself can be manipulated to produce complex results with minimal effort from the programmer. For example, you can rotate the entire coordinate system rather than recalculating a drawing so that it appears rotated. A great deal of the power of the Quartz drawing model is that it is resolution- and device-independent. Applications do not need to pay attention to the details of the output device; they draw their graphics independent of the resolution, color characteristics, and other details of the drawing destination. The Quartz drawing library takes care of mapping coordinates, matching colors, converting pixel formats, and handling other device issues.

    The Quartz drawing model supports just about any kind of 2D drawing—text, complex line art containing curves and shapes, sampled images, and PDF documents themselves, as shown in Figure 1.7 and Figure 1.8. Content can be rotated, clipped through a text mask, and transformed in a number of ways. In addition, alpha transparency is a built-in feature.

    Figure 1.7 Basic elements of Quartz drawing

    Figure 1.8 PDF created by layering images and text

    Color management is integrated into Quartz; colors can be specified in ways that match results across a wide range of output devices, including printers. Starting with Panther, Quartz can convert PostScript data into PDF files, thus allowing Quartz applications to draw PostScript data the same way they draw PDF data.

    Graphics contexts, an important concept in Quartz, represent a destination into which Quartz drawing is rendered. They come in a number of flavors including window, PDF, and bitmap. (You’ll read more about contexts in More About Graphics Contexts (page 41).) You can draw the same drawing to different graphics contexts to capture different representations of the drawing. For example, drawing to a window graphics context causes the Quartz drawing to appear in that window. That same drawing to a PDF graphics context creates a new PDF document that contains the equivalent Quartz drawing. So switching application drawing output from screen to printer or to PDF is as simple as changing the destination graphics context.

    The PDF file format is the graphics metafile format for Quartz. Creating a PDF file from Quartz drawing operations fully captures the content of the drawing without down-sampling or performing lossy compression. The PDF document format is also the native spool file format of the printing system. During printing, application drawing is captured into a PDF document, which is sent to a print server. The print server handles the conversion of the PDF document into a form appropriate for the destination printer and sends the data to the destination.

    What Software Can Use Quartz 2D?

    Just about any software that runs in Mac OS X can use Quartz 2D. End-user applications that implement a graphical user interface (GUI) use Quartz, either directly or through the Cocoa or Carbon frameworks, to draw the GUI. UNIX programmers and tool writers use Quartz to create and process graphical data.

    Cocoa is an object-oriented application environment designed specifically for developing Mac OS X applications. Quartz is the underlying graphics system used by Cocoa, and in most cases Cocoa provides methods and classes that directly parallel the functions and objects provided by Quartz. Because of this, the concepts of the Quartz drawing model typically translate directly into use of Cocoa drawing facilities.

    Although Cocoa developers can use the Objective-C implementation in Cocoa that sits on top of Quartz, using Quartz 2D directly can be beneficial in many situations. Developers who already have a drawing library may find Quartz 2D to be a useful replacement for the lowest-level portions of their drawing library. In such cases, it may make sense to use Quartz 2D directly, even when using Cocoa. Another situation where a Cocoa developer may want to use Quartz directly is when Apple introduces a new feature in Quartz that does not yet have an API in Cocoa. One example of such a feature is Quartz shadings (sometimes called blends). Cocoa Framework Drawing and Quartz (page 59) discusses details about when and why Cocoa programmers may want to use Quartz directly instead of the Cocoa drawing API.

    The Carbon programming framework is a modernized version of the programming interfaces that developers have used for programming Mac OS since its inception. Carbon is a procedural API that enables C and C++ developers to write applications for Mac OS X. The Carbon framework is also employed by Macintosh developers who want to convert older QuickDraw-based Mac applications to run in Mac OS X.

    Applications that replace or enhance their use of QuickDraw with Quartz are able to

    Add support for PDF content.

    Draw with alpha transparency.

    Use a resolution-independent coordinate system and coordinate transformations.

    Take advantage of more powerful graphics primitives.

    Obtain higher-quality imaging, including anti-aliasing.

    Achieve printing output more consistent with that seen onscreen.

    Benefit from efficient reuse of graphical objects, including images.

    Add support for encapsulated PostScript (EPS) file inclusion.

    Perform resolution-independent clipping to shapes.

    Applications that don’t have a GUI can also use Quartz 2D. Quartz can produce raster data from its drawing and this raster data can then be turned into data files in various file formats. Quartz can create PDF documents that correspond to its drawing, so it can be used to create PDF documents from various data sources. Because of its ability to draw the content of existing PDF documents and create new PDF documents, Quartz makes it easy to add new drawing content to existing PDF documents.

    Traditional UNIX tools in compiled programming languages such as C can tap into Quartz raster and PDF capabilities. The bindings added in Panther for the Python scripting language make it easy to quickly write simple Python scripts to perform interesting tasks. For example, a Python script can process PDF files to add new content, reorder the pages in such a document, and so on.

    The use of Quartz from UNIX tools and Python scripts opens up some exciting possibilities for using Quartz in a web-based application that delivers high-quality graphics driven by dynamic content. Again, the abilities of Quartz to produce high-quality raster data or PDF output make it a great tool for producing graphical data. You’ll read more about these capabilities as you progress through this book.

    Summary

    This chapter gives an overview of Quartz, discusses its lineage, and provides some details about Quartz 2D and the software that it’s suited for. Quartz has several parts; this book focuses on the Quartz 2D drawing library. Quartz is the result of a 20-year evolution of graphics programming on the Macintosh. It owes a lot of its power to its PostScript and PDF underpinnings. Just about any program, GUI-based or not, can use Quartz 2D.

    The rest of this book gets into the nitty-gritty of programming with Quartz. The next chapter gives an introduction to basic drawing using Quartz 2D and provides a number of short code examples to illustrate how drawing is done.

    See Also

    For in-depth information on the topics discussed in this chapter, see the following:

    Inside Macintosh: Imaging with QuickDraw, Apple Computer, Inc., Addison-Wesley, 1994.

    PostScript Language Reference, 3rd edition, Adobe Systems, Inc.: http://partners.adobe.com/public/developer/en/ps/PLRM.pdf

    Adobe Systems provides all the recent versions of the PDF specification, including versions 1.3 through 1.6. As of this writing the current version is PDF Reference: Version 1.6, 5th edition, Adobe Systems, Inc.:

    http://partners.adobe.com/public/developer/pdf/index_reference.html

    Compositing Digital Images, Thomas Porter and Tom Duff, Proceedings of the 11th Annual Conference on Computer Graphics and Interactive Techniques, 1984, pp. 253–259.

    You can find more information on the Quartz 2D drawing library in these documents:

    Quartz 2D Programming Guide provides conceptual information about Quartz 2D and how to use it:

    http://developer.apple.com/

    documentation/GraphicsImaging/

    Conceptual/drawingwithquartz2d/index.html

    Quartz 2D Programming Guide for QuickDraw Developers provides information for developers moving their application drawing from QuickDraw to Quartz 2D:

    http://developer.apple.com/

    documentation/Carbon/Conceptual/

    QuickDrawToQuartz2D/index.html

    Quartz 2D Reference is the API reference for Quartz 2D:

    http://developer.apple.com/

    documentation/Graphicslmaging/

    Reference/CGAPI-date.Html

    Quartz 2D Drawing Basics

    No matter how complex a drawing appears, it’s the result of performing a number of basic operations. Paths define shapes. Shapes can be filled, stroked, or both. The painting color can be opaque or have a degree of transparency. Lines can be solid or dashed. Clipping determines which portions of drawing are visible. Drawing can be scaled and otherwise transformed. PDF documents can be treated as a single graphic element but can themselves be arbitrarily complex.

    The basic operations available in Quartz produce results that would be difficult to obtain using most other imaging systems. This chapter contains relatively simple examples that illustrate basic drawing operations. Subsequent chapters build on the concepts in these simple examples to show how to achieve more and more complex results.

    Quartz Graphics Contexts

    Quartz always draws into a graphics context. As described in Quartz 2D Overview (page 8), a graphics context is an abstract representation of a drawing destination. A window, a bitmap, and a PDF file are examples of destinations that are represented by a graphics context. A Quartz graphics context is an opaque data structure called a CGContextRef. The Quartz routines that affect the context all take a CGContextRef as a parameter. Quartz has no notion of a current context; you always pass a context directly to Quartz functions.

    Depending on the task, an application either creates a graphics context or obtains one from a framework (such as when the application draws into a window). More About Graphics Contexts (page 41) gives more details, but to get you started here, these examples assume that a context is provided for drawing. In Using Quartz 2D in Cocoa (page 45) and Using Quartz 2D in Carbon (page 65), you’ll see how to obtain a graphics context for drawing into a window. In later chapters, you’ll learn how to create graphics contexts for other, specialized purposes.

    Filling a Rectangle

    The rectangle is one of the easiest to draw and most commonly used shapes. Consisting of four line segments, the rectangle forms the basis for windows, printed pages, text input fields, and numerous other widgets used in computer user interfaces as well as physical devices. Rectangles are so common that Quartz provides convenience functions to draw them. More complex shapes such as polygons and curved shapes are created by constructing a path. (You’ll see how to construct a path later.) This section uses a rectangle-drawing convenience function, CGContextFillRect, so it can focus on the notion of fills and fill colors rather than on how to construct a rectangular

    Enjoying the preview?
    Page 1 of 1