Struct Context

The cairo drawing context

Context is the main object used when drawing with cairo. To draw with cairo, you create a Context, set the target surface, and drawing options for the Context, create shapes with functions like Context.moveTo() and Context.lineTo(), and then draw shapes with Context.stroke() or Context.fill().

Context's can be pushed to a stack via Context.save(). They may then safely be changed, without loosing the current state. Use Context.restore() to restore to the saved state.

Constructors

Name Description
this Create a Context from a existing cairo_t*. Context is a garbage collected class. It will call cairo_destroy when it gets collected by the GC or when dispose() is called.
this Creates a new Context 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 as new ImageSurface().

Fields

Name Type Description
debugging bool Enable / disable memory management debugging for this Context instance. Only available if both cairoD and the cairoD user code were compiled with "debug=RefCounted"
nativePointer cairo_t* The underlying cairo_t* handle

Properties

Name Type Description
antiAlias [get, set] cairo_antialias_t Convenience property
fillRule [get, set] cairo_fill_rule_t Convenience property
fontFace [get, set] FontFace Convenience property
fontMatrix [get, set] Matrix Convenience property
fontOptions [get, set] FontOptions Convenience property
lineCap [get, set] cairo_line_cap_t Convenience property
lineJoin [get, set] cairo_line_join_t Convenience property
lineWidth [get, set] double Convenience property
matrix [get, set] const(Matrix) Convenience property
miterLimit [get, set] double Convenience property
operator [get, set] cairo_operator_t Convenience property
scaledFont [get, set] ScaledFont Convenience property
source [get, set] Pattern Convenience property
tolerance [get, set] double Convenience property
_count [get] uint

Methods

Name Description
appendPath appendPath for user created paths. There is no high level API for user defined paths. Use appendPath(Path p) for paths which were obtained from cairo.
appendPath Append the path onto the current path. The path may be the return value from one of copyPath() or copyPathFlat().
arc Adds a circular arc of the given radius to the current path. The arc is centered at center, begins at angle1 and proceeds in the direction of increasing angles to end at angle2. If angle2 is less than angle1 it will be progressively increased by 2*PI until it is greater than angle1.
arcNegative Adds a circular arc of the given radius to the current path. The arc is centered at center, begins at angle1 and proceeds in the direction of decreasing angles to end at angle2. If angle2 is greater than angle1 it will be progressively decreased by 2*PI until it is less than angle1.
clip Establishes a new clip region by intersecting the current clip region with the current path as it would be filled by fill() and according to the current fill rule (see setFillRule()).
clipExtents Computes a bounding box in user coordinates covering the area inside the current clip.
clipPreserve Establishes a new clip region by intersecting the current clip region with the current path as it would be filled by fill() and according to the current fill rule (see setFillRule()).
closePath Adds a line segment to the path from the current point to the beginning of the current sub-path, (the most recent point passed to moveTo()), and closes this sub-path. After this call the current point will be at the joined endpoint of the sub-path.
copyClipRectangles Gets the current clip region as a list of rectangles in user coordinates.
copyPage Emits the current page for backends that support multiple pages, but doesn't clear it, so, the contents of the current page will be retained for the next page too. Use showPage() if you want to get an empty page after the emission.
copyPath Creates a copy of the current path and returns it to the user as a Path. See PathRange for hints on how to iterate over the returned data structure.
copyPathFlat Gets a flattened copy of the current path and returns it to the user as a Path. See PathRange for hints on how to iterate over the returned data structure.
curveTo Adds a cubic Bézier spline to the path from the current point to position p3 in user-space coordinates, using p1 and p2 as the control points. After this call the current point will be p3.
deviceToUser Transform a coordinate from device space to user space by multiplying the given point by the inverse of the current transformation matrix (CTM).
deviceToUserDistance Transform a distance vector from device space to user space. This function is similar to deviceToUser() except that the translation components of the inverse CTM will be ignored when transforming inp.
dispose Explicitly drecrease the reference count.
fill A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled). After cfill(), the current path will be cleared from the cairo context. See setFillRule() and fillPreserve().
fillExtends Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by a fill() operation 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.
fillPreserve A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled). Unlike fill(), fillPreserve() preserves the path within the cairo context.
fontExtents Gets the font extents for the currently selected font.
getAntiAlias Gets the current shape antialiasing mode, as set by setAntiAlias.
getCurrentPoint Gets the current point of the current path, which is conceptually the final point reached by the path so far.
getDash Gets the current dash array.
getDashCount This function returns the length of the dash array in cr (0 if dashing is not currently in effect).
getFillRule Gets the current fill rule, as set by setFillRule.
getFontFace Gets the current font face for a Context.
getFontMatrix Returns the current font matrix. See setFontMatrix.
getFontOptions Retrieves font rendering options set via setFontOptions(). Note that the returned options do not include any options derived from the underlying surface; they are literally the options passed to setFontOptions().
getGroupTarget Gets the current destination surface for the context. This is either the original target surface as passed to the Context constructor or the target surface for the current group as started by the most recent call to Context.pushGroup() or Context.pushGroup(Content).
getLineCap Gets the current line cap style, as set by setLineCap().
getLineJoin Gets the current line join style, as set by setLineJoin
getLineWidth 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 to setLineWidth() and getLineWidth().
getMatrix Returns the current transformation matrix (CTM)
getMiterLimit Gets the current miter limit, as set by setMiterLimit
getOperator Gets the current compositing operator for a cairo context.
getScaledFont Gets the current scaled font for a Context.
getSource Gets the current source pattern for cr.
getTarget Gets the target surface for the cairo context as passed to the constructor.
getTolerance Gets the current tolerance value, as set by setTolerance
glyphExtents Gets the extents for an array of glyphs. The extents describe a user-space rectangle that encloses the "inked" portion of the glyphs, (as they would be drawn by showGlyphs()). Additionally, the x_advance and y_advance values indicate the amount by which the current point would be advanced by showGlyphs().
glyphPath Adds closed paths for the glyphs to the current path. The generated path if filled, achieves an effect similar to that of showGlyphs().
hasCurrentPoint Returns whether a current point is defined on the current path. See getCurrentPoint() for details on the current point.
identityMatrix Resets the current transformation matrix (CTM) by setting it equal to the identity matrix. That is, the user-space and device-space axes will be aligned and one user-space unit will transform to one device-space unit.
inClip Tests whether the given point is inside the area that would be visible through the current clip, i.e. the area that would be filled by a cairo_paint() operation.
inFill Tests whether the given point is inside the area that would be affected by a cairo_fill() operation given the current path and filling parameters. Surface dimensions and clipping are not taken into account.
inStroke Tests whether the given point is inside the area that would be affected by a cairo_stroke() operation given the current path and stroking parameters. Surface dimensions and clipping are not taken into account.
lineTo Adds a line to the path from the current point to position p1 in user-space coordinates. After this call the current point will be p1.
mask 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.)
maskSurface 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.)
moveTo Begin a new sub-path. After this call the current point will be p1.
newPath Clears the current path. After this call there will be no path and no current point.
newSubPath Begin a new sub-path. Note that the existing path is not affected. After this call there will be no current point.
opAssign Assignment operator
paint A drawing operator that paints the current source everywhere within the current clip region.
paintWithAlpha 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 paint(), but the drawing is faded out using the alpha value.
pathExtends Computes a bounding box in user-space coordinates covering the points on the current path. If the current path is empty, returns an empty Box ((0,0), (0,0)). Stroke parameters, fill rule, surface dimensions and clipping are not taken into account.
popGroup Terminates the redirection begun by a call to Context.pushGroup() or Context.pushGroup(Content) and returns a new pattern containing the results of all drawing operations performed to the group.
popGroupToSource Terminates the redirection begun by a call to Context.pushGroup() or Context.pushGroup(Content) and installs the resulting pattern as the source pattern in the given cairo context.
pushGroup Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to Context.popGroup() or Context.popGroupToSource(). 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).
pushGroup Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to Context.popGroup() or Context.popGroupToSource(). 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).
rectangle Adds a closed sub-path rectangle of the given size to the current path at position r.point in user-space coordinates. This function is logically equivalent to:
relCurveTo Relative-coordinate version of curveTo(). All offsets are relative to the current point. Adds a cubic Bézier spline to the path from the current point to a point offset from the current point by rp3, using points offset by rp1 and rp2 as the control points. After this call the current point will be offset by rp3.
relLineTo Relative-coordinate version of lineTo(). Adds a line to the path from the current point to a point that is offset from the current point by rp1 in user space. After this call the current point will be offset by rp1.
relMoveTo Begin a new sub-path. After this call the current point will offset by rp1.
resetClip 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.
restore Restores cr to the state saved by a preceding call to Context.save() and removes that state from the stack of saved states.
rotate Modifies the current transformation matrix (CTM) by rotating the user-space axes by angle radians. The rotation of the axes takes places after any existing transformation of user space. The rotation direction for positive angles is from the positive X axis toward the positive Y axis.
save Makes a copy of the current state of cr and saves it on an internal stack of saved states for cr. When Context.restore() is called, cr will be restored to the saved state. Multiple calls to Context.save() and Context.restore() can be nested; each call to Context.restore() restores the state from the matching paired Context.save().
scale Modifies the current transformation matrix (CTM) by scaling the X and Y user-space axes by sx and sy respectively. The scaling of the axes takes place after any existing transformation of user space.
selectFontFace
setAntiAlias 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.
setDash Sets the dash pattern to be used by 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.
setFillRule 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 fill() and clip(). See FillRule for details on the semantics of each available fill rule.
setFontFace Replaces the current FontFace object in the Context with the default font.
setFontFace Replaces the current FontFace object in the Context with font_face. The replaced font face in the Context will be destroyed if there are no other references to it.
setFontMatrix Sets the current font matrix to matrix. The font matrix gives a transformation from the design space of the font (in this space, the em-square is 1 unit by 1 unit) to user space. Normally, a simple scale is used (see setFontSize()), but a more complex font matrix can be used to shear the font or stretch it unequally along the two axes
setFontOptions Sets a set of custom font rendering options for the Context. Rendering options are derived by merging these options with the options derived from underlying surface; if the value in options has a default value (like CAIRO_ANTIALIAS_DEFAULT), then the value from the surface is used.
setFontSize Sets the current font matrix to a scale by a factor of size, replacing any font matrix previously set with setFontSize() or setFontMatrix(). This results in a font size of size user space units. (More precisely, this matrix will result in the font's em-square being a size by size square in user space.)
setLineCap Sets the current line cap style within the cairo context. See LineCap for details about how the available line cap styles are drawn.
setLineJoin Sets the current line join style within the cairo context. See LineJoin for details about how the available line join styles are drawn.
setLineWidth 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).
setMatrix Modifies the current transformation matrix (CTM) by setting it equal to matrix.
setMiterLimit Sets the current miter limit within the cairo context.
setOperator Sets the compositing operator to be used for all drawing operations. See Operator for details on the semantics of each available compositing operator.
setScaledFont Replaces the current font face, font matrix, and font options in the Context with those of the ScaledFont. Except for some translation, the current CTM of the cairo_t should be the same as that of the ScaledFont, which can be accessed using ScaledFont.getCTM().
setSource 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.
setSourceRGB 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.
setSourceRGBA 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.
setSourceSurface This is a convenience function for creating a pattern from surface and setting it as the source in cr with Context.setSource().
setTolerance 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.) The accuracy of paths within Cairo is limited by the precision of its internal arithmetic, and the prescribed tolerance is restricted to the smallest representable internal value.
showGlyphs A drawing operator that generates the shape from an array of glyphs, rendered according to the current fontFace, fontSize (fontMatrix), and font options.
showPage Emits and clears the current page for backends that support multiple pages. Use copyPage() if you don't want to clear the page.
showText A drawing operator that generates the shape from a string of UTF-8 characters, rendered according to the current fontFace, fontSize (fontMatrix), and fontOptions.
showTextGlyphs This operation has rendering effects similar to showGlyphs() but, if the target surface supports it, uses the provided text and cluster mapping to embed the text for the glyphs shown in the output. If the target does not support the extended attributes, this function acts like the basic showGlyphs().
stroke A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. After stroke(), the current path will be cleared from the cairo context. See setLineWidth(), setLineJoin(), setLineCap(), setDash(), and strokePreserve().
strokeExtends Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by a stroke() operation given the current path and stroke parameters. If the current path is empty, returns an empty rectangle ((0,0), (0,0)). Surface dimensions and clipping are not taken into account.
strokePreserve A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. Unlike stroke(), strokePreserve() preserves the path within the cairo context.
textExtents Gets the extents for a string of text. The extents describe a user-space rectangle that encloses the "inked" portion of the text, (as it would be drawn by showText()). Additionally, the x_advance and y_advance values indicate the amount by which the current point would be advanced by showText().
textPath Adds closed paths for text to the current path. The generated path if filled, achieves an effect similar to that of showText().
transform Modifies the current transformation matrix (CTM) by applying matrix as an additional transformation. The new transformation of user space takes place after any existing transformation.
translate Modifies the current transformation matrix (CTM) by translating the user-space origin by (tx, ty). This offset is interpreted as a user-space coordinate according to the CTM in place before the new call to translate(). In other words, the translation of the user-space origin takes place after any existing transformation.
userToDevice Transform a coordinate from user space to device space by multiplying the given point by the current transformation matrix (CTM).
userToDeviceDistance Transform a distance vector from user space to device space. This function is similar to userToDevice() except that the translation components of the CTM will be ignored when transforming inp.
checkError
_dereference
_reference

Aliases

Name Description
currentPoint convenience alias
groupTarget convenience alias
resetFontFace convenience alias
target convenience alias

Authors

Johannes Pfau cairoD
Andrej Mitrovic cairoD
cairo team cairo

Copyright

License

cairoD wrapper/bindings Boost License 1.0
cairo LGPL 2.1 / MPL 1.1