The cairo drawing context
<cairo-t> is the main object used when drawing with cairo. To draw with
cairo, you create a <cairo-t>, set the target surface, and drawing
options for the <cairo-t>, create shapes with functions like
cairo-move-to and cairo-line-to, and then draw shapes with
cairo-stroke or cairo-fill.
<cairo-t>'s can be pushed to a stack via cairo-save. They may then
safely be changed, without loosing the current state. Use cairo-restore
to restore to the saved state.
<cairo-surface-t>) => (ret <cairo-t>)Creates a new
<cairo-t>with all graphics state parameters set to default values and with target as a target surface. The target surface should be constructed with a backend-specific function such ascairo-image-surface-create(or any other `cairo_<backend>_surface_create' variant).
- target
- target surface for the context
- ret
- a newly allocated
<cairo-t>.
<cairo-t>)Makes a copy of the current state of cr and saves it on an internal stack of saved states for cr. When
cairo-restoreis called, cr will be restored to the saved state. Multiple calls tocairo-saveandcairo-restorecan be nested; each call tocairo-restorerestores the state from the matching pairedcairo-save.It isn't necessary to clear all saved states before a
<cairo-t>is freed. If the reference count of a<cairo-t>drops to zero in response to a call tocairo-destroy, any saved states will be freed along with the<cairo-t>.
- cr
- a
<cairo-t>
<cairo-t>)Restores cr to the state saved by a preceding call to
cairo-saveand removes that state from the stack of saved states.
- cr
- a
<cairo-t>
<cairo-t>) => (ret <cairo-surface-t>)Gets the target surface for the cairo context as passed to
cairo-create.This function will always return a valid pointer, but the result can be a "nil" surface if cr is already in an error state, (ie.
cairo-status`!='`CAIRO_STATUS_SUCCESS'). A nil surface is indicated bycairo-surface-status`!='`CAIRO_STATUS_SUCCESS'.
- cr
- a cairo context
- ret
- the target surface. This object is owned by cairo. To keep a reference to it, you must call
cairo-surface-reference.
<cairo-t>)Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to
cairo-pop-grouporcairo-pop-group-to-source. These calls provide the result of any drawing to the group as a pattern, (either as an explicit object, or set as the source pattern).This group functionality can be convenient for performing intermediate compositing. One common use of a group is to render objects as opaque within the group, (so that they occlude each other), and then blend the result with translucence onto the destination.
Groups can be nested arbitrarily deep by making balanced calls to
cairo-push-group/cairo-pop-group. Each call pushes/pops the new target group onto/from a stack.The
cairo-push-groupfunction callscairo-saveso that any changes to the graphics state will not be visible outside the group, (the pop_group functions callcairo-restore).By default the intermediate group will have a content type of CAIRO_CONTENT_COLOR_ALPHA. Other content types can be chosen for the group by using
cairo-push-group-with-contentinstead.As an example, here is how one might fill and stroke a path with translucence, but without any portion of the fill being visible under the stroke:
cairo_push_group (cr); cairo_set_source (cr, fill_pattern); cairo_fill_preserve (cr); cairo_set_source (cr, stroke_pattern); cairo_stroke (cr); cairo_pop_group_to_source (cr); cairo_paint_with_alpha (cr, alpha);
- cr
- a cairo context
Since 1.2
<cairo-t>) => (ret <cairo-pattern-t>)Terminates the redirection begun by a call to
cairo-push-grouporcairo-push-group-with-contentand returns a new pattern containing the results of all drawing operations performed to the group.The
cairo-pop-groupfunction callscairo-restore, (balancing a call tocairo-saveby the push_group function), so that any changes to the graphics state will not be visible outside the group.
- cr
- a cairo context
- ret
- a newly created (surface) pattern containing the results of all drawing operations performed to the group. The caller owns the returned object and should call
cairo-pattern-destroywhen finished with it.Since 1.2
<cairo-t>)Terminates the redirection begun by a call to
cairo-push-grouporcairo-push-group-with-contentand installs the resulting pattern as the source pattern in the given cairo context.The behavior of this function is equivalent to the sequence of operations:
cairo_pattern_t *group = cairo_pop_group (cr); cairo_set_source (cr, group); cairo_pattern_destroy (group);but is more convenient as their is no need for a variable to store the short-lived pointer to the pattern.
The
cairo-pop-groupfunction callscairo-restore, (balancing a call tocairo-saveby the push_group function), so that any changes to the graphics state will not be visible outside the group.
- cr
- a cairo context
Since 1.2
<cairo-t>) => (ret <cairo-surface-t>)Gets the target surface for the current group as started by the most recent call to
cairo-push-grouporcairo-push-group-with-content.This function will return NULL if called "outside" of any group rendering blocks, (that is, after the last balancing call to
cairo-pop-grouporcairo-pop-group-to-source).
- cr
- a cairo context
- ret
- the target group surface, or NULL if none. This object is owned by cairo. To keep a reference to it, you must call
cairo-surface-reference.Since 1.2
<cairo-t>) (red <double>) (green <double>) (blue <double>)Sets the source pattern within cr to an opaque color. This opaque color will then be used for any subsequent drawing operation until a new source pattern is set.
The color components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, they will be clamped.
- cr
- a cairo context
- red
- red component of color
- green
- green component of color
- blue
- blue component of color
<cairo-t>) (red <double>) (green <double>) (blue <double>) (alpha <double>)Sets the source pattern within cr to a translucent color. This color will then be used for any subsequent drawing operation until a new source pattern is set.
The color and alpha components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, they will be clamped.
- cr
- a cairo context
- red
- red component of color
- green
- green component of color
- blue
- blue component of color
- alpha
- alpha component of color
<cairo-t>) (source <cairo-pattern-t>)Sets the source pattern within cr to source. This pattern will then be used for any subsequent drawing operation until a new source pattern is set.
Note: The pattern's transformation matrix will be locked to the user space in effect at the time of
cairo-set-source. This means that further modifications of the current transformation matrix will not affect the source pattern. Seecairo-pattern-set-matrix.XXX: I'd also like to direct the reader's attention to some (not-yet-written) section on cairo's imaging model. How would I do that if such a section existed? (cworth).
- cr
- a cairo context
- source
- a
<cairo-pattern-t>to be used as the source for subsequent drawing operations.
<cairo-t>) (surface <cairo-surface-t>) (x <double>) (y <double>)This is a convenience function for creating a pattern from surface and setting it as the source in cr with
cairo-set-source.The x and y parameters give the user-space coordinate at which the surface origin should appear. (The surface origin is its upper-left corner before any transformation has been applied.) The x and y patterns are negated and then set as translation values in the pattern matrix.
Other than the initial translation pattern matrix, as described above, all other pattern attributes, (such as its extend mode), are set to the default values as in
cairo-pattern-create-for-surface. The resulting pattern can be queried withcairo-get-sourceso that these attributes can be modified if desired, (eg. to create a repeating pattern withcairo-pattern-set-extend).
- cr
- a cairo context
- surface
- a surface to be used to set the source pattern
- x
- User-space X coordinate for surface origin
- y
- User-space Y coordinate for surface origin
<cairo-t>) => (ret <cairo-pattern-t>)Gets the current source pattern for cr.
- cr
- a cairo context
- ret
- the current source pattern. This object is owned by cairo. To keep a reference to it, you must call
cairo-pattern-reference.
<cairo-t>) (antialias <cairo-antialias-t>)Set the antialiasing mode of the rasterizer used for drawing shapes. This value is a hint, and a particular backend may or may not support a particular value. At the current time, no backend supports `CAIRO_ANTIALIAS_SUBPIXEL' when drawing shapes.
Note that this option does not affect text rendering, instead see
cairo-font-options-set-antialias.
- cr
- a
<cairo-t>- antialias
- the new antialiasing mode
<cairo-t>) => (ret <cairo-antialias-t>)Gets the current shape antialiasing mode, as set by
cairo-set-shape-antialias.
- cr
- a cairo context
- ret
- the current shape antialiasing mode.
<cairo-t>) => (dashes <double>) (num-dashes <int>) (offset <double>)Sets the dash pattern to be used by
cairo-stroke. A dash pattern is specified by dashes, an array of positive values. Each value provides the length of alternate "on" and "off" portions of the stroke. The offset specifies an offset into the pattern at which the stroke begins.Each "on" segment will have caps applied as if the segment were a separate sub-path. In particular, it is valid to use an "on" length of 0.0 with CAIRO_LINE_CAP_ROUND or CAIRO_LINE_CAP_SQUARE in order to distributed dots or squares along a path.
Note: The length values are in user-space units as evaluated at the time of stroking. This is not necessarily the same as the user space at the time of
cairo-set-dash.If num-dashes is 0 dashing is disabled.
If num-dashes is 1 a symmetric pattern is assumed with alternating on and off portions of the size specified by the single value in dashes.
If any value in dashes is negative, or if all values are 0, then cairo-t will be put into an error state with a status of
<cairo-status-invalid-dash>.
- cr
- a cairo context
- dashes
- an array specifying alternate lengths of on and off stroke portions
- num-dashes
- the length of the dashes array
- offset
- an offset into the dash pattern at which the stroke should start
<cairo-t>) => (ret <int>)This function returns the length of the dash array in cr (0 if dashing is not currently in effect).
See also
cairo-set-dashandcairo-get-dash.
- cr
- a
<cairo-t>- ret
- the length of the dash array, or 0 if no dash array set.
Since 1.4
<cairo-t>) (fill-rule <cairo-fill-rule-t>)Set the current fill rule within the cairo context. The fill rule is used to determine which regions are inside or outside a complex (potentially self-intersecting) path. The current fill rule affects both cairo_fill and cairo_clip. See
<cairo-fill-rule-t>for details on the semantics of each available fill rule.
- cr
- a
<cairo-t>- fill-rule
- a fill rule, specified as a
<cairo-fill-rule-t>
<cairo-t>) => (ret <cairo-fill-rule-t>)Gets the current fill rule, as set by
cairo-set-fill-rule.
- cr
- a cairo context
- ret
- the current fill rule.
<cairo-t>) (line-cap <cairo-line-cap-t>)Sets the current line cap style within the cairo context. See
<cairo-line-cap-t>for details about how the available line cap styles are drawn.As with the other stroke parameters, the current line cap style is examined by
cairo-stroke,cairo-stroke-extents, andcairo-stroke-to-path, but does not have any effect during path construction.
- cr
- a cairo context
- line-cap
- a line cap style
<cairo-t>) => (ret <cairo-line-cap-t>)Gets the current line cap style, as set by
cairo-set-line-cap.
- cr
- a cairo context
- ret
- the current line cap style.
<cairo-t>) (line-join <cairo-line-join-t>)Sets the current line join style within the cairo context. See
<cairo-line-join-t>for details about how the available line join styles are drawn.As with the other stroke parameters, the current line join style is examined by
cairo-stroke,cairo-stroke-extents, andcairo-stroke-to-path, but does not have any effect during path construction.
- cr
- a cairo context
- line-join
- a line joint style
<cairo-t>) => (ret <cairo-line-join-t>)Gets the current line join style, as set by
cairo-set-line-join.
- cr
- a cairo context
- ret
- the current line join style.
<cairo-t>) (width <double>)Sets the current line width within the cairo context. The line width value specifies the diameter of a pen that is circular in user space, (though device-space pen may be an ellipse in general due to scaling/shear/rotation of the CTM).
Note: When the description above refers to user space and CTM it refers to the user space and CTM in effect at the time of the stroking operation, not the user space and CTM in effect at the time of the call to
cairo-set-line-width. The simplest usage makes both of these spaces identical. That is, if there is no change to the CTM between a call tocairo-set-line-withand the stroking operation, then one can just pass user-space values tocairo-set-line-widthand ignore this note.As with the other stroke parameters, the current line width is examined by
cairo-stroke,cairo-stroke-extents, andcairo-stroke-to-path, but does not have any effect during path construction.The default line width value is 2.0.
- cr
- a
<cairo-t>- width
- a line width
<cairo-t>) => (ret <double>)This function returns the current line width value exactly as set by
cairo-set-line-width. Note that the value is unchanged even if the CTM has changed between the calls tocairo-set-line-widthandcairo-get-line-width.
- cr
- a cairo context
- ret
- the current line width.
<cairo-t>) (limit <double>)Sets the current miter limit within the cairo context.
If the current line join style is set to `CAIRO_LINE_JOIN_MITER' (see
cairo-set-line-join), the miter limit is used to determine whether the lines should be joined with a bevel instead of a miter. Cairo divides the length of the miter by the line width. If the result is greater than the miter limit, the style is converted to a bevel.As with the other stroke parameters, the current line miter limit is examined by
cairo-stroke,cairo-stroke-extents, andcairo-stroke-to-path, but does not have any effect during path construction.
- cr
- a cairo context
- limit
- miter limit to set
<cairo-t>) => (ret <double>)Gets the current miter limit, as set by
cairo-set-miter-limit.
- cr
- a cairo context
- ret
- the current miter limit.
<cairo-t>) (op <cairo-operator-t>)Sets the compositing operator to be used for all drawing operations. See
<cairo-operator-t>for details on the semantics of each available compositing operator.XXX: I'd also like to direct the reader's attention to some (not-yet-written) section on cairo's imaging model. How would I do that if such a section existed? (cworth).
- cr
- a
<cairo-t>- op
- a compositing operator, specified as a
<cairo-operator-t>
<cairo-t>) => (ret <cairo-operator-t>)Gets the current compositing operator for a cairo context.
- cr
- a cairo context
- ret
- the current compositing operator.
<cairo-t>) (tolerance <double>)Sets the tolerance used when converting paths into trapezoids. Curved segments of the path will be subdivided until the maximum deviation between the original path and the polygonal approximation is less than tolerance. The default value is 0.1. A larger value will give better performance, a smaller value, better appearance. (Reducing the value from the default value of 0.1 is unlikely to improve appearance significantly.)
- cr
- a
<cairo-t>- tolerance
- the tolerance, in device units (typically pixels)
<cairo-t>) => (ret <double>)Gets the current tolerance value, as set by
cairo-set-tolerance.
- cr
- a cairo context
- ret
- the current tolerance value.
<cairo-t>)Establishes a new clip region by intersecting the current clip region with the current path as it would be filled by
cairo-filland according to the current fill rule (seecairo-set-fill-rule).After cairo_clip, the current path will be cleared from the cairo context.
The current clip region affects all drawing operations by effectively masking out any changes to the surface that are outside the current clip region.
Calling
cairo-clipcan only make the clip region smaller, never larger. But the current clip is part of the graphics state, so a temporary restriction of the clip region can be achieved by callingcairo-clipwithin acairo-save/cairo-restorepair. The only other means of increasing the size of the clip region iscairo-reset-clip.
- cr
- a cairo context
<cairo-t>)Establishes a new clip region by intersecting the current clip region with the current path as it would be filled by
cairo-filland according to the current fill rule (seecairo-set-fill-rule).Unlike
cairo-clip, cairo_clip_preserve preserves the path within the cairo context.The current clip region affects all drawing operations by effectively masking out any changes to the surface that are outside the current clip region.
Calling
cairo-clipcan only make the clip region smaller, never larger. But the current clip is part of the graphics state, so a temporary restriction of the clip region can be achieved by callingcairo-clipwithin acairo-save/cairo-restorepair. The only other means of increasing the size of the clip region iscairo-reset-clip.
- cr
- a cairo context
<cairo-t>) => (x1 <double>) (y1 <double>) (x2 <double>) (y2 <double>)Computes a bounding box in user coordinates covering the area inside the current clip.
- cr
- a cairo context
- x1
- left of the resulting extents
- y1
- top of the resulting extents
- x2
- right of the resulting extents
- y2
- bottom of the resulting extents
Since 1.4
<cairo-t>)Reset the current clip region to its original, unrestricted state. That is, set the clip region to an infinitely large shape containing the target surface. Equivalently, if infinity is too hard to grasp, one can imagine the clip region being reset to the exact bounds of the target surface.
Note that code meant to be reusable should not call
cairo-reset-clipas it will cause results unexpected by higher-level code which callscairo-clip. Consider usingcairo-saveandcairo-restorearoundcairo-clipas a more robust means of temporarily restricting the clip region.
- cr
- a cairo context
<cairo-t>) => (ret <cairo-rectangle-list-t>)Gets the current clip region as a list of rectangles in user coordinates. Never returns `
#f'.The status in the list may be CAIRO_STATUS_CLIP_NOT_REPRESENTABLE to indicate that the clip region cannot be represented as a list of user-space rectangles. The status may have other values to indicate other errors.
The caller must always call cairo_rectangle_list_destroy on the result of this function.
- cr
- a cairo context
- ret
- the current clip region as a list of rectangles in user coordinates.
Since 1.4
<cairo-t>)A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled). After cairo_fill, the current path will be cleared from the cairo context. See
cairo-set-fill-ruleandcairo-fill-preserve.
- cr
- a cairo context
<cairo-t>)A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled). Unlike
cairo-fill, cairo_fill_preserve preserves the path within the cairo context.See
cairo-set-fill-ruleandcairo-fill.
- cr
- a cairo context
<cairo-t>) => (x1 <double>) (y1 <double>) (x2 <double>) (y2 <double>)Computes a bounding box in user coordinates covering the area that would be affected by a
cairo-filloperation given the current path and fill parameters. If the current path is empty, returns an empty rectangle (0,0, 0,0). Surface dimensions and clipping are not taken into account.See
cairo-fill,cairo-set-fill-ruleandcairo-fill-preserve.
- cr
- a cairo context
- x1
- left of the resulting extents
- y1
- top of the resulting extents
- x2
- right of the resulting extents
- y2
- bottom of the resulting extents
<cairo-t>) (x <double>) (y <double>) => (ret <cairo-bool-t>)Tests whether the given point is inside the area that would be affected by a
cairo-filloperation given the current path and filling parameters. Surface dimensions and clipping are not taken into account.See
cairo-fill,cairo-set-fill-ruleandcairo-fill-preserve.
- cr
- a cairo context
- x
- X coordinate of the point to test
- y
- Y coordinate of the point to test
- ret
- A non-zero value if the point is inside, or zero if outside.
<cairo-t>) (pattern <cairo-pattern-t>)A drawing operator that paints the current source using the alpha channel of pattern as a mask. (Opaque areas of pattern are painted with the source, transparent areas are not painted.)
- cr
- a cairo context
- pattern
- a
<cairo-pattern-t>
<cairo-t>) (surface <cairo-surface-t>) (surface-x <double>) (surface-y <double>)A drawing operator that paints the current source using the alpha channel of surface as a mask. (Opaque areas of surface are painted with the source, transparent areas are not painted.)
- cr
- a cairo context
- surface
- a
<cairo-surface-t>- surface-x
- X coordinate at which to place the origin of surface
- surface-y
- Y coordinate at which to place the origin of surface
<cairo-t>)A drawing operator that paints the current source everywhere within the current clip region.
- cr
- a cairo context
<cairo-t>) (alpha <double>)A drawing operator that paints the current source everywhere within the current clip region using a mask of constant alpha value alpha. The effect is similar to
cairo-paint, but the drawing is faded out using the alpha value.
- cr
- a cairo context
- alpha
- alpha value, between 0 (transparent) and 1 (opaque)
<cairo-t>)A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. After cairo_stroke, the current path will be cleared from the cairo context. See
cairo-set-line-width,cairo-set-line-join,cairo-set-line-cap,cairo-set-dash, andcairo-stroke-preserve.Note: Degenerate segments and sub-paths are treated specially and provide a useful result. These can result in two different situations:
1. Zero-length "on" segments set in
cairo-set-dash. If the cap style is CAIRO_LINE_CAP_ROUND or CAIRO_LINE_CAP_SQUARE then these segments will be drawn as circular dots or squares respectively. In the case of CAIRO_LINE_CAP_SQUARE, the orientation of the squares is determined by the direction of the underlying path.2. A sub-path created by
cairo-move-tofollowed by either acairo-close-pathor one or more calls tocairo-line-toto the same coordinate as thecairo-move-to. If the cap style is CAIRO_LINE_CAP_ROUND then these sub-paths will be drawn as circular dots. Note that in the case of CAIRO_LINE_CAP_SQUARE a degenerate sub-path will not be drawn at all, (since the correct orientation is indeterminate).In no case will a cap style of CAIRO_LINE_CAP_BUTT cause anything to be drawn in the case of either degenerate segments or sub-paths.
- cr
- a cairo context
<cairo-t>)A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. Unlike
cairo-stroke, cairo_stroke_preserve preserves the path within the cairo context.See
cairo-set-line-width,cairo-set-line-join,cairo-set-line-cap,cairo-set-dash, andcairo-stroke-preserve.
- cr
- a cairo context
<cairo-t>) => (x1 <double>) (y1 <double>) (x2 <double>) (y2 <double>)Computes a bounding box in user coordinates covering the area that would be affected by a
cairo-strokeoperation operation given the current path and stroke parameters. If the current path is empty,
- cr
- a cairo context
- x1
- left of the resulting extents
- y1
- top of the resulting extents
- x2
- right of the resulting extents
- y2
- bottom of the resulting extents
<cairo-t>) (x <double>) (y <double>) => (ret <cairo-bool-t>)Tests whether the given point is inside the area that would be affected by a
cairo-strokeoperation given the current path and stroking parameters. Surface dimensions and clipping are not taken into account.See
cairo-stroke,cairo-set-line-width,cairo-set-line-join,cairo-set-line-cap,cairo-set-dash, andcairo-stroke-preserve.
- cr
- a cairo context
- x
- X coordinate of the point to test
- y
- Y coordinate of the point to test
- ret
- A non-zero value if the point is inside, or zero if outside.