Graphics Contexts
Graphics contexts are a fundamental part of the drawing infrastructure in Cocoa applications. As the name suggests, a graphics context provides the context for subsequent drawing operations. It identifies the current drawing destination (screen, printer, file, and so on), the coordinate system and boundaries for the underlying canvas, and any graphics attributes associated with the destination.
Graphics Context是一个drawing系统中很重要的概念,context就是上下文了,可是猛一看,我又对drawing不熟,context都包含什么呢?drawing的目标,屏幕、打印机、文件、PDF等都是,坐标系统,底层Canvas的边界以及跟drawing 目标相关的任何属性。
在Cocoa应用中,NSGraphicsContext几乎可以表示所有的drawing destination(除了OpenGL需要用
NSOpenGLContext外
)。
The primary job of any graphics context object is to maintain information about the current state of the drawing environment. In Quartz, the graphics context object is associated with a window, bitmap, PDF file, or other output device and maintains information for that device. The same is true for a Cocoa graphics context, but because Cocoa drawing is view-based, some additional changes are made to the drawing environment before your view’s drawRect: method is called.
The current state of the drawing environment information ranges from the global rendering settings to the attributes used to render the current path and is the same state information saved by Quartz.当调用saveGraphicsState时就是把下面的信息做了一个Copy。
Attribute | Description |
---|---|
Maps points in the view’s coordinate system to points in the destination device's coordinate system. Cocoa modifies the CTM before calling your view’s | |
Specifies the area of the canvas that can be painted by drawing calls. Cocoa modifies the clipping region to the visible area of your view before calling its | |
Specifies the width of paths. The default line width is | |
Specifies how two connected lines are joined together. The default join style is | |
Specifies the appearance of an open end point on a path. The default line cap style is | |
Defines a broken pattern for lines, including the initial phase for the style. There is no default dash style, resulting in solid lines. You modify dash styles for a path using an | |
Determines when lines should be joined with a bevel instead of a miter. Applies only when the line join style is set to | |
Specifies the accuracy with which curves are rendered. (It is also the maximum error tolerance, measured in pixels.) Smaller numbers result in smoother curves at the expense of more calculations. The interpretation of this value may vary slightly on different rendering devices. The default value is | |
Specifies the color used for rendering paths. This color applies only to the path line itself, not the area the path encompasses. You can specify colors using any of the system-supported color spaces. This value includes alpha information. Color information is managed by the | |
Specifies the color used to fill the area enclosed by a path. You can specify colors using any of the system-supported color spaces. This value includes alpha information. Color information is managed by the | |
Specifies the shadow attributes to apply to rendered content. You set shadows using the | |
Specifies the technique used to map in-gamut colors to the gamut of the current color space. Cocoa does not support setting this attribute directly. Instead, you must use Quartz. For more information, see “Mapping Physical Colors to a Color Space.” | |
Specifies the font to use when drawing text. You modify font information using the | |
Specifies the font size to use when drawing text. You modify font information using the | |
Specifies the character spacing to use when drawing text. (This attribute is supported only indirectly by Cocoa.) For more information on drawing text, see“Text Attributes.” | |
Specifies how to render the text. (This attribute is supported only indirectly by Cocoa.) For more information on drawing text, see “Text Attributes.” | |
Specifies the process used to interpolate images during rendering. You use the | |
Specifies the process used to composite source and destination material together. (The compositing operations supported by Cocoa are related to the Quartz blend modes but differ in their usage and behavior.) You use the | |
Specifies a global alpha (transparency) value to apply in addition to the alpha value for a given color. Cocoa does not support this attribute directly. If you want to set it, you must use the | |
Specifies whether paths use aliasing to smooth lines as they cross pixel boundaries. You use the |
关于线程和Graphic Context
在Cocoa应用中,每个线程都有一个关联于某个Window的Graphic Context。可以使用[NSGraphicsContext currentContext]获得。它与哪个window关联呢?
Reference:
1. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40003290-CH201-SW1