The Quartz 2D is a two-dimensional graphics drawing engine that supports both iOS and Mac OS x environments. We can use the Quartz 2D API to implement many features, such as drawing of basic paths, transparency, shadowing, drawing shadows, transparent layers, color management, anti-aliasing, PDF document generation, and PDF metadata access. When needed, the Quartz 2D can also be used with graphics hardware capabilities.
In Mac OS x, Quartz 2D can be used in combination with other graphic image technologies, such as core image, Core Video, OpenGL, QuickTime. For example, by using the Graphicsimportcreatecgimage function of QuickTime, you can create an image from a QuickTime graphics importer with quartz.
Page
Quartz 2D uses the painter model in the image (painter ' s models). In the painter model, each successive drawing operation places a drawing layer (a layer of ' paint ') on a canvas (' canvas '), which we usually call the canvas (Page). The drawing on the page can overlay more drawings with additional drawing operations. Graphical objects on a page can only be changed by overlaying more drawings. This model allows us to use small primitives to construct complex graphs.
Figure 1-1 shows how the painter model works. You can see that the different drawing order produces the same effect.
The page can be a piece of paper (if the output device is a printer), or it can be a virtual paper (if the output device is a PDF file), or it can be a bitmap image. This depends on the actual graphics context used.
Draw target: Graphics Context
The graphics context is a data type (CGCONTEXTREF) that encapsulates the information that quartz draws an image to an output device. The device can be a PDF file, a bitmap, or a display window. The information in the graphics context includes graphical drawing parameters and device-related representations of the images in the page. All objects in the quartz are drawn into a graphics context.
We can think of the graphics context as a drawing target, as shown in 1-2. When drawing with quartz, all device-related features are included in the graphics context we use. In other words, we can simply assign a different graphics Context to the quartz drawing sequence to draw the same image to different devices. We do not need any equipment-related calculations; these are done by quartz for us.
Quartz provides the following types of graphics Context, which are described in detail in subsequent chapters.
- Bitmap Graphics Context
- PDF Graphics Context
- Window Graphics Context
- Layer Context
- Post Graphics Context
Quartz 2D Data type
In addition to the Graphics Context, the Quartz 2D API also defines some data types. Because these APIs are part of the core graphics framework, these data types start with CG.
Quartz 2D uses these data types to create objects that manipulate these objects to obtain specific graphics. Figure 1-3 provides an example of three images obtained using the Quartz 2D drawing operation.
The data types included in Quartz 2D are listed below:
- cgpathref: Used for vector plots to create paths and fill or paint (stroke)
- cgimageref: Used to represent bitmap images and bitmap image masks based on sampled data.
- cglayerref: Used to represent drawing layers that can be used for repeating drawing (such as background) and behind-the-scenes (offscreen) painting
- cgpatternref: For redrawing the
- cgshadingref, Cggradientref: Used to draw a gradient
- cgfunctionref: Used to define a callback function that contains a random floating-point value parameter. When you create a gradient for a shadow, use the type
- cgcolorref, cgcolorspaceref: To tell quartz how to interpret the color
- cgimagesourceref, Cgimagedestinationref: Used to move in quartz to move out data
- cgfontref: For drawing text
- Cgpdfdictionaryref,cgpdfobjectref, Cgpdfpageref,cgpdfstream, Cgpdfstringref, and Cgpdfarrayref: metadata for accessing the PDF
- cgpdfscannerref, Cgpdfcontentstreamref: Used to parse PDF metadata
- cgpsconverterref: Used to convert PostScript to PDF. Not available in iOS.
Graphics status
The quartz modifies the result of the drawing operation by modifying the current drawing state (present graphics States). The graphics state contains the parameters that are used to draw the program. The drawing program determines how the results are rendered based on these drawing states. For example, when you call a function that sets the fill color, you will change the color value stored in the current drawing state.
The graphics context contains a drawing state stack. When Quartz creates a graphics context, the stack is empty. When you save the graphics state, quartz presses a copy of the current drawing state into the stack. When the graphics state is restored, quartz the graph state at the top of the stack. The state of the out stack becomes the current drawing state.
You can use the function cgcontextsavegstate to save the graphics state, cgcontextrestoregstate to restore the graphics state.
Note: Not all aspects of the current drawing environment are elements of the graphical state. For example, the graphics state does not contain the current path. The following is a list of graph state-related parameters:
- Current transformation matrix (CTM): Present transformation matrix
- Clipping Area: Clipping region
- Line: Lines
- Accuracy of curve estimation (flatness): curve smoothness
- Anti-aliasing setting: Anti-aliasing settings
- Color: Colors
- Alpha Value (transparency): transparency
- Rendering Intent: Render target
- Color space: Colour spaces
- Text: Literal
- Blend Mode: Blending modes
quartz 2D coordinate system
coordinate system definition is the position and size range of the object being drawn to the page, As shown in 1-4. We specify the position and size of the graphic in the user space coordinate system (User-space coordination System, abbreviated user space). Coordinate values are defined by floating-point numbers.
Because different devices have different graphics functions, the location and size of the images depend on the device. For example, a display device might show only less than 96 pixels per inch, while a printer might display 300 pixels per inch. If you define a coordinate system at the device level, graphics drawn on one device cannot be displayed properly on other devices.
Quartz solves device dependency problems by mapping a separate coordinate system (user space) to the coordinate system (device space) of the output device by using the current transformation matrix (transformation matrix, CTM). CTM is a special type of matrix (affine transform, affine matrix) that maps points from one coordinate space to another by panning (translation), rotation (rotation), scaling (scale) operations.
CTM has another purpose: to allow you to determine how objects are drawn through transformations. For example, to draw a box that rotates 45 degrees, we can rotate the page's coordinate system before the box is drawn. The quartz uses a rotated coordinate system to draw the box to the output device.
The point of the user space is represented by a coordinate pair (x, y), and (0, 0) represents the origin of the coordinates. The default coordinate system in Quartz is: the value of the coordinates from left to right along the x-axis increases gradually, along the y-axis from the bottom to the upper coordinate.
Some techniques use a different default coordinate system than quartz when setting up their graphics context. In contrast to quartz, these coordinate systems are modified coordinate systems (modified coordinate system), which must be converted when the graphics quartz drawn are displayed in these coordinate systems. The most common kind of modified coordinate system is that the origin is in the upper-left corner, and the value of the coordinate along the y-axis from top to bottom gradually increases. We can see this coordinate system in some of the following places:
- In Mac OS x, override the Isflipped method to return the subclass of the NSView class for Yes
- In iOS, the drawing context returned by UIView
- In iOS, the drawing context returned by calling the Uigraphicsbeginimagecontextwithoptions function
If your application wants to draw on a UIView object and a PDF graphics context with the same drawing program, you need to make a transformation so that the PDF graphics context uses the same coordinate system as the UIView. To achieve this, you only need to translate the origin of the context of the PDF (move to the upper-left corner) and scale the y-coordinate value by 1. Figure 1-5 shows this transformation operation:
Our application is responsible for adjusting the quartz call to ensure that there is a transition applied to the context. For example, if you want a picture or PDF correctly drawn into a graphics context, your application may need to temporarily adjust the graphics context of the CTM. In iOS, if you use the UIImage object to wrap the created Cgimage object, you do not need to modify the CTM. The uiimage will automatically compensate for the Uikit coordinate system.
Important: If you are going to develop a quartz-related program on iOS, it is useful to know what is discussed above, but not necessary. In iOS 3.2 and later, when Uikit creates a drawing context for your application, it also makes additional changes to the context to match the Uikit conventions. In particular, patterns and shadows (not affected by CTM) are individually adjusted to match the Uikit coordinate system. In this case, there is no equivalent mechanism for CTM to transform the context of quartz and uikit. We must recognize the context in which to draw and adjust the behavior to match the expectations of the context.
Memory Management: Object ownership
Quartz uses the core Foundation memory management model (reference count). Therefore, the creation and destruction of objects is the same as the usual way. In quartz, here are some rules to keep in mind:
- If you create or copy an object, you will have it, so you must release it. In general, if you get an object using a function that contains a "Create" or "Copy" word, you must release it when you are done with it, or it will cause a memory leak.
- If you get an object using a function that does not contain a "Create" or "Copy" word, you will not have a reference to the object, and you do not need to release it.
- If you do not own an object and intend to keep it, you must retain it and release it when it is not needed. You can use the Quartz 2D function to specify retain and release an object. For example, if you create a Cgcolorspace object, use the functions Cgcolorspaceretain and cgcolorspacerelease to retain and release the object. Similarly, you can use the Cfretain and cfrelease of the core Foundation, but be aware that you cannot pass null values to these functions.
Quartz 2D Overview