The OpenD Programming Language

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.

struct Context {
version(!D_Ddoc)
debug(RefCounted)
bool debugging;
}

Constructors

this
this(Surface target)

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().

this
this(cairo_t* ptr)

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.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

this(this)
this(this)

Constructor that tracks the reference count appropriately. If !refCountedIsInitialized, does nothing.

Members

Aliases

currentPoint
alias currentPoint = getCurrentPoint

convenience alias

groupTarget
alias groupTarget = getGroupTarget

convenience alias

resetFontFace
alias resetFontFace = setFontFace

convenience alias

target
alias target = getTarget

convenience alias

Functions

appendPath
void appendPath(Path p)

Append the path onto the current path. The path may be the return value from one of copyPath() or copyPathFlat().

appendPath
void appendPath(cairo_path_t* path)

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.

arc
void arc(Point!double center, double radius, double angle1, double angle2)
void arc(double centerX, double centerY, double radius, double angle1, double angle2)

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
void arcNegative(Point!double center, double radius, double angle1, double angle2)
void arcNegative(double centerX, double centerY, double radius, double angle1, double angle2)

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
void 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
Box clipExtents()

Computes a bounding box in user coordinates covering the area inside the current clip.

clipPreserve
void 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
void 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
Rectangle!(double)[] copyClipRectangles()

Gets the current clip region as a list of rectangles in user coordinates.

copyPage
void 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
Path 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
Path 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
void curveTo(Point!double p1, Point!double p2, Point!double p3)
void curveTo(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y)

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
Point!double deviceToUser(Point!double inp)

Transform a coordinate from device space to user space by multiplying the given point by the inverse of the current transformation matrix (CTM).

deviceToUserDistance
Point!double deviceToUserDistance(Point!double inp)

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
void dispose()

Explicitly drecrease the reference count.

fill
void 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
Box 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
void 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
FontExtents fontExtents()

Gets the font extents for the currently selected font.

getAntiAlias
AntiAlias getAntiAlias()

Gets the current shape antialiasing mode, as set by setAntiAlias.

getCurrentPoint
Point!double getCurrentPoint()

Gets the current point of the current path, which is conceptually the final point reached by the path so far.

getDash
double[] getDash(double offset)

Gets the current dash array.

getDashCount
int getDashCount()

This function returns the length of the dash array in cr (0 if dashing is not currently in effect).

getFillRule
FillRule getFillRule()

Gets the current fill rule, as set by setFillRule.

getFontFace
FontFace getFontFace()

Gets the current font face for a Context.

getFontMatrix
Matrix getFontMatrix()

Returns the current font matrix. See setFontMatrix.

getFontOptions
FontOptions 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
Surface 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
LineCap getLineCap()

Gets the current line cap style, as set by setLineCap().

getLineJoin
LineJoin getLineJoin()

Gets the current line join style, as set by setLineJoin

getLineWidth
double 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
Matrix getMatrix()

Returns the current transformation matrix (CTM)

getMiterLimit
double getMiterLimit()

Gets the current miter limit, as set by setMiterLimit

getOperator
Operator getOperator()

Gets the current compositing operator for a cairo context.

getScaledFont
ScaledFont getScaledFont()

Gets the current scaled font for a Context.

getSource
Pattern getSource()

Gets the current source pattern for cr.

getTarget
Surface getTarget()

Gets the target surface for the cairo context as passed to the constructor.

getTolerance
double getTolerance()

Gets the current tolerance value, as set by setTolerance

glyphExtents
TextExtents glyphExtents(Glyph[] glyphs)

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
void glyphPath(Glyph[] glyphs)

Adds closed paths for the glyphs to the current path. The generated path if filled, achieves an effect similar to that of showGlyphs().

hasCurrentPoint
bool hasCurrentPoint()

Returns whether a current point is defined on the current path. See getCurrentPoint() for details on the current point.

identityMatrix
void 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
bool inClip(Point!double point)

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
bool inFill(Point!double point)

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
bool inStroke(Point!double point)
bool inStroke(double x, double y)

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
void lineTo(Point!double p1)
void lineTo(double x, double y)

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
void mask(Pattern pattern)

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
void maskSurface(Surface surface, Point!double location)
void maskSurface(Surface surface, double x, double y)

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
void moveTo(Point!double p1)
void moveTo(double x, double y)

Begin a new sub-path. After this call the current point will be p1.

newPath
void newPath()

Clears the current path. After this call there will be no path and no current point.

newSubPath
void newSubPath()

Begin a new sub-path. Note that the existing path is not affected. After this call there will be no current point.

opAssign
void opAssign(typeof(this) rhs)

Assignment operator

paint
void paint()

A drawing operator that paints the current source everywhere within the current clip region.

paintWithAlpha
void paintWithAlpha(double alpha)

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
Box 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
Pattern 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
void 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
void 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
void pushGroup(Content cont)

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
void rectangle(Rectangle!double r)
void rectangle(double x, double y, double width, double height)

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
void relCurveTo(Point!double rp1, Point!double rp2, Point!double rp3)
void relCurveTo(double rp1x, double rp1y, double rp2x, double rp2y, double rp3x, double rp3y)

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
void relLineTo(Point!double rp1)
void relLineTo(double x, double y)

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
void relMoveTo(Point!double rp1)
void relMoveTo(double x, double y)

Begin a new sub-path. After this call the current point will offset by rp1.

resetClip
void 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
void 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
void rotate(double angle)

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
void 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
void scale(Point!double point)

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.

scale
void scale(double sx, double sy)

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
void selectFontFace(string family, FontSlant slant, FontWeight weight)

Note: The selectFontFace() function call is part of what the cairo designers call the "toy" text API. It is convenient for short demos and simple programs, but it is not expected to be adequate for serious text-using applications.

setAntiAlias
void setAntiAlias(AntiAlias antialias)

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
void setDash(const(double[]) dashes, double offset)

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
void setFillRule(FillRule rule)

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
void setFontFace()

Replaces the current FontFace object in the Context with the default font.

setFontFace
void setFontFace(FontFace font_face)

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
void setFontMatrix(Matrix matrix)

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
void setFontOptions(FontOptions options)

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
void setFontSize(double size)

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
void setLineCap(LineCap cap)

Sets the current line cap style within the cairo context. See LineCap for details about how the available line cap styles are drawn.

setLineJoin
void setLineJoin(LineJoin join)

Sets the current line join style within the cairo context. See LineJoin for details about how the available line join styles are drawn.

setLineWidth
void setLineWidth(double width)

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
void setMatrix(Matrix matrix)

Modifies the current transformation matrix (CTM) by setting it equal to matrix.

setMiterLimit
void setMiterLimit(double limit)

Sets the current miter limit within the cairo context.

setOperator
void setOperator(Operator op)

Sets the compositing operator to be used for all drawing operations. See Operator for details on the semantics of each available compositing operator.

setScaledFont
void setScaledFont(ScaledFont scaled_font)

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
void setSource(Pattern pat)

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
void setSourceRGB(double red, double green, double blue)
void setSourceRGB(RGB rgb)

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
void setSourceRGBA(double red, double green, double blue, double alpha)
void setSourceRGBA(RGBA rgba)

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
void setSourceSurface(Surface sur, double x, double y)
void setSourceSurface(Surface sur, Point!double p1)

This is a convenience function for creating a pattern from surface and setting it as the source in cr with Context.setSource().

setTolerance
void setTolerance(double tolerance)

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
void showGlyphs(Glyph[] glyphs)

A drawing operator that generates the shape from an array of glyphs, rendered according to the current fontFace, fontSize (fontMatrix), and font options.

showPage
void 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
void showText(string text)

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
void showTextGlyphs(TextGlyph glyph)

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
void 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
Box 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
void 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
TextExtents textExtents(string text)

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
void textPath(string text)

Adds closed paths for text to the current path. The generated path if filled, achieves an effect similar to that of showText().

transform
void transform(Matrix matrix)

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
void translate(double tx, double ty)

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
Point!double userToDevice(Point!double inp)

Transform a coordinate from user space to device space by multiplying the given point by the current transformation matrix (CTM).

userToDeviceDistance
Point!double userToDeviceDistance(Point!double inp)

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.

Properties

antiAlias
AntiAlias antiAlias [@property getter]

Convenience property

antiAlias
AntiAlias antiAlias [@property setter]
fillRule
FillRule fillRule [@property getter]

Convenience property

fillRule
FillRule fillRule [@property setter]

Convenience property

fontFace
FontFace fontFace [@property setter]
FontFace fontFace [@property getter]

Convenience property

fontMatrix
Matrix fontMatrix [@property setter]
Matrix fontMatrix [@property getter]

Convenience property

fontOptions
FontOptions fontOptions [@property setter]
FontOptions fontOptions [@property getter]
lineCap
LineCap lineCap [@property getter]

Convenience property

lineCap
LineCap lineCap [@property setter]
lineJoin
LineJoin lineJoin [@property getter]

Convenience property

lineJoin
LineJoin lineJoin [@property setter]
lineWidth
double lineWidth [@property getter]

Convenience property

lineWidth
double lineWidth [@property setter]

Convenience property

matrix
Matrix matrix [@property setter]
Matrix matrix [@property getter]
miterLimit
double miterLimit [@property getter]

Convenience property

miterLimit
double miterLimit [@property setter]
operator
Operator operator [@property getter]

Convenience property

operator
Operator operator [@property setter]

Convenience property

scaledFont
ScaledFont scaledFont [@property setter]
ScaledFont scaledFont [@property getter]
source
Pattern source [@property getter]

Convenience property

source
Pattern source [@property setter]
tolerance
double tolerance [@property getter]

Convenience property

tolerance
double tolerance [@property setter]

Convenience property

Variables

debugging
bool debugging;

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* nativePointer;

The underlying cairo_t* handle

Meta