10cairo Module

10.1Overview

The cairo module provides methods to draw 2-D graphics using Cairo library. Official site of Cairo is http://cairographics.org/.

10.2Drawing

10.2.1cairo.context - The cairo drawing context

10.2.1.1Functions

cairo.context#status()
Checks whether an error has previously occurred for this context.
cairo.context#save():reduce {block?}

Makes a copy of the current state of cr and saves it on an internal stack of saved states for cr. When cairo.context#restore() is called, cr will be restored to the saved state. Multiple calls to cairo.context#save() and cairo.context#restore() can be nested; each call to cairo.context#restore() restores the state from the matching paired cairo.context#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 to cairo.context#destroy(), any saved states will be freed along with the cairo_t.

cairo.context#restore():reduce
Restores cr to the state saved by a preceding call to cairo.context#save() and removes that state from the stack of saved states.
cairo.context#get_target()
Gets the target surface for the cairo context as passed to cairo.context constructor.
cairo.context#push_group():reduce

Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to cairo.context#pop_group() or cairo.context#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.context#push_group()/cairo.context#pop_group(). Each call pushes/pops the new target group onto/from a stack.

The cairo.context#push_group() function calls cairo_save() so that any changes to the graphics state will not be visible outside the group, (the pop_group functions call cairo_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.context#push_group_with_content() instead.

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.context#push_group_with_content(content:number):reduce

Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to cairo.context#pop_group() or cairo.context#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).

The group will have a content type of content. The ability to control this content type is the only distinction between this function and cairo.context#push_group() which you should see for a more detailed description of group rendering.

cairo.context#pop_group() {block?}

Terminates the redirection begun by a call to cairo.context#push_group() or cairo.context#push_group_with_content() and returns a new pattern containing the results of all drawing operations performed to the group.

The cairo.context#pop_group() function calls cairo_restore(), (balancing a call to cairo_save() by the push_group function), so that any changes to the graphics state will not be visible outside the group.

cairo.context#pop_group_to_source():reduce

Terminates the redirection begun by a call to cairo.context#push_group() or cairo.context#push_group_with_content() and installs the resulting pattern as the source pattern in the given cairo context.

The cairo.context#pop_group() function calls cairo_restore(), (balancing a call to cairo_save() by the push_group function), so that any changes to the graphics state will not be visible outside the group.

cairo.context#get_group_target() {block?}
Gets the current destination surface for the context. This is either the original target surface as passed to cairo.context constructor or the target surface for the current group as started by the most recent call to cairo.context#push_group() or cairo.context#push_group_with_content().
cairo.context#set_source_rgb(red:number, green:number, blue:number):reduce

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.

The default source pattern is opaque black, (that is, it is equivalent to cr.set_source_rgb(0.0, 0.0, 0.0)).

cairo.context#set_source_rgba(red:number, green:number, blue:number, alpha:number):reduce

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.

The default source pattern is opaque black, (that is, it is equivalent to cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)).

cairo.context#set_source(source:cairo.pattern):reduce

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.context#set_source(). This means that further modifications of the current transformation matrix will not affect the source pattern. See cairo.pattern#set_matrix().

The default source pattern is a solid pattern that is opaque black, (that is, it is equivalent to cr.set_source_rgb(0.0, 0.0, 0.0)).

cairo.context#set_source_surface(surface:cairo.surface, x:number, y:number):reduce

This is a convenience function for creating a pattern from surface and setting it as the source in cr with cairo.context#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 parameters 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 with cairo.context#get_source() so that these attributes can be modified if desired, (eg. to create a repeating pattern with cairo.pattern#set_extend()).

cairo.context#get_source() {block?}
Gets the current source pattern for cr.
cairo.context#set_antialias(antialias:number):reduce

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

cairo.context#get_antialias()
Gets the current shape antialiasing mode, as set by cairo.context#set_antialias().
cairo.context#set_dash(dashes[]:number, offset:number):reduce

Sets the dash pattern to be used by cairo.context#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.context#set_dash().

If length of dashes is 0 dashing is disabled.

If length of 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 cr will be put into an error state with a status of cairo.STATUS_INVALID_DASH.

cairo.context#get_dash()
Gets the current dash array.
cairo.context#set_fill_rule(fill_rule:number):reduce

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.context#fill() and cairo.context#clip(). See cairo_fill_rule_t for details on the semantics of each available fill rule.

The default fill rule is cairo.FILL_RULE_WINDING.

cairo.context#get_fill_rule()
Gets the current fill rule, as set by cairo.context#set_fill_rule().
cairo.context#set_line_cap(line_cap:number):reduce

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.context#stroke(), cairo.context#stroke_extents(), and cairo.context#stroke_to_path(), but does not have any effect during path construction.

The default line cap style is cairo.LINE_CAP_BUTT.

cairo.context#get_line_cap()
Gets the current line cap style, as set by cairo.context#set_line_cap().
cairo.context#set_line_join(line_join:number):reduce

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.context#stroke(), cairo.context#stroke_extents(), and cairo.context#stroke_to_path(), but does not have any effect during path construction.

The default line join style is cairo.LINE_JOIN_MITER.

cairo.context#get_line_join()
Gets the current line join style, as set by cairo.context#set_line_join().
cairo.context#set_line_width(width:number):reduce

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.context#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 to cairo.context#set_line_width() and the stroking operation, then one can just pass user-space values to cairo.context#set_line_width() and ignore this note.

As with the other stroke parameters, the current line width is examined by cairo.context#stroke(), cairo.context#stroke_extents(), and cairo.context#stroke_to_path(), but does not have any effect during path construction.

The default line width value is 2.0.

cairo.context#get_line_width()
This function returns the current line width value exactly as set by cairo.context#set_line_width(). Note that the value is unchanged even if the CTM has changed between the calls to cairo.context#set_line_width() and cairo.context#get_line_width().
cairo.context#set_miter_limit(limit:number):reduce

Sets the current miter limit within the cairo context.

If the current line join style is set to cairo.LINE_JOIN_MITER (see cairo.context#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.context#stroke(), cairo.context#stroke_extents(), and cairo.context#stroke_to_path(), but does not have any effect during path construction.

The default miter limit value is 10.0, which will convert joins with interior angles less than 11 degrees to bevels instead of miters. For reference, a miter limit of 2.0 makes the miter cutoff at 60 degrees, and a miter limit of 1.414 makes the cutoff at 90 degrees.

A miter limit for a desired angle can be computed as: miter limit = 1/sin(angle/2)

cairo.context#get_miter_limit()
Gets the current miter limit, as set by cairo.context#set_miter_limit().
cairo.context#set_operator(op:number):reduce

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.

The default operator is cairo.OPERATOR_OVER.

cairo.context#get_operator()
Gets the current compositing operator for a cairo context.
cairo.context#set_tolerance(tolerance:number):reduce
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.
cairo.context#get_tolerance()
Gets the current tolerance value, as set by cairo.context#set_tolerance().
cairo.context#clip():reduce

Establishes a new clip region by intersecting the current clip region with the current path as it would be filled by cairo.context#fill() and according to the current fill rule (see cairo.context#set_fill_rule()).

After cairo.context#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.context#clip() can 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 calling cairo.context#clip() within a cairo.context#save()/cairo.context#restore() pair. The only other means of increasing the size of the clip region is cairo.context#reset_clip().

cairo.context#clip_preserve():reduce

Establishes a new clip region by intersecting the current clip region with the current path as it would be filled by cairo.context#fill() and according to the current fill rule (see cairo.context#set_fill_rule()). Unlike cairo.context#clip(), cairo.context#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.context#clip_preserve() can 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 calling cairo.context#clip_preserve() within a cairo.context#save()/cairo.context#restore() pair. The only other means of increasing the size of the clip region is cairo.context#reset_clip().

cairo.context#clip_extents()
Computes a bounding box in user coordinates covering the area inside the current clip.
cairo.context#in_clip(x:number, y:number)

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.context#paint() operation.

See cairo.context#clip(), and cairo.context#clip_preserve().

cairo.context#reset_clip():reduce

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.context#reset_clip() as it will cause results unexpected by higher-level code which calls cairo.context#clip(). Consider using cairo.context#save() and cairo.context#restore() around cairo.context#clip() as a more robust means of temporarily restricting the clip region.

cairo.context#copy_clip_rectangle_list()

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

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.

cairo.context#fill():reduce
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.context#fill(), the current path will be cleared from the cairo context. See cairo.context#set_fill_rule() and cairo.context#fill_preserve().
cairo.context#fill_preserve():reduce

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.context#fill(), cairo.context#fill_preserve() preserves the path within the cairo context.

See cairo.context#set_fill_rule() and cairo.context#fill().

cairo.context#fill_extents():reduce

Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by a cairo.context#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.

Contrast with cairo.context#path_extents(), which is similar, but returns non-zero extents for some paths with no inked area, (such as a simple line segment).

Note that cairo.context#fill_extents() must necessarily do more work to compute the precise inked areas in light of the fill rule, so cairo.context#path_extents() may be more desirable for sake of performance if the non-inked path extents are desired.

See cairo.context#fill(), cairo.context#set_fill_rule() and cairo.context#fill_preserve().

cairo.context#in_fill(x:number, y:number)

Tests whether the given point is inside the area that would be affected by a cairo.context#fill() operation given the current path and filling parameters. Surface dimensions and clipping are not taken into account.

See cairo.context#fill(), cairo.context#set_fill_rule() and cairo.context#fill_preserve().

cairo.context#mask(pattern:cairo.pattern):reduce
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.)
cairo.context#mask_surface(surface:cairo.surface, surface_x:number, surface_y:number):reduce
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.)
cairo.context#paint():reduce
A drawing operator that paints the current source everywhere within the current clip region.
cairo.context#paint_with_alpha(alpha:number):reduce
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.context#paint(), but the drawing is faded out using the alpha value.
cairo.context#stroke():reduce

A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. After cairo.context#stroke(), the current path will be cleared from the cairo context. See cairo.context#set_line_width(), cairo.context#set_line_join(), cairo.context#set_line_cap(), cairo.context#set_dash(), and cairo.context#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.context#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.context#move_to() followed by either a cairo.context#close_path() or one or more calls to cairo.context#line_to() to the same coordinate as the cairo.context#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.

cairo.context#stroke_preserve():reduce

A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. Unlike cairo.context#stroke(), cairo.context#stroke_preserve() preserves the path within the cairo context.

See cairo.context#set_line_width(), cairo.context#set_line_join(), cairo.context#set_line_cap(), cairo.context#set_dash(), and cairo.context#stroke_preserve().

cairo.context#stroke_extents()

Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by a cairo.context#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.

Note that if the line width is set to exactly zero, then cairo.context#stroke_extents() will return an empty rectangle. Contrast with cairo.context#path_extents() which can be used to compute the non-empty bounds as the line width approaches zero.

Note that cairo.context#stroke_extents() must necessarily do more work to compute the precise inked areas in light of the stroke parameters, so cairo.context#path_extents() may be more desirable for sake of performance if non-inked path extents are desired.

See cairo.context#stroke(), cairo.context#set_line_width(), cairo.context#set_line_join(), cairo.context#set_line_cap(), cairo.context#set_dash(), and cairo.context#stroke_preserve().

cairo.context#in_stroke(x:number, y:number)
Tests whether the given point is inside the area that would be affected by a cairo.context#stroke() operation given the current path and stroking parameters. Surface dimensions and clipping are not taken into account. See cairo.context#stroke(), cairo.context#set_line_width(), cairo.context#set_line_join(), cairo.context#set_line_cap(), cairo.context#_set_dash(), and cairo.context#stroke_preserve().
cairo.context#copy_page():reduce

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 cairo.cairo#show_page() if you want to get an empty page after the emission.

This is a convenience function that simply calls cairo.context#surface_copy_page() on cr's target.

cairo.context#show_page():reduce

Emits and clears the current page for backends that support multiple pages. Use cairo.context#copy_page() if you don't want to clear the page.

This is a convenience function that simply calls cairo.context#surface_show_page() on cr's target.

10.2.1.2Types and Values

cairo.antialias

  • cairo.ANTIALIAS_DEFAULT
  • cairo.ANTIALIAS_NONE
  • cairo.ANTIALIAS_GRAY
  • cairo.ANTIALIAS_SUBPIXEL
  • cairo.ANTIALIAS_FAST
  • cairo.ANTIALIAS_GOOD
  • cairo.ANTIALIAS_BEST

cairo.fill_fule

  • cairo.FILL_RULE_WINDING
  • cairo.FILL_RULE_EVEN_ODD

cairo.line_cap

  • cairo.LINE_CAP_BUTT
  • cairo.LINE_CAP_ROUND
  • cairo.LINE_CAP_SQUARE

cairo.line_join

  • cairo.LINE_JOIN_MITER
  • cairo.LINE_JOIN_ROUND
  • cairo.LINE_JOIN_BEVEL

cairo.operator

  • cairo.OPERATOR_CLEAR
  • cairo.OPERATOR_SOURCE
  • cairo.OPERATOR_OVER
  • cairo.OPERATOR_IN
  • cairo.OPERATOR_OUT
  • cairo.OPERATOR_ATOP
  • cairo.OPERATOR_DEST
  • cairo.OPERATOR_DEST_OVER
  • cairo.OPERATOR_DEST_IN
  • cairo.OPERATOR_DEST_OUT
  • cairo.OPERATOR_DEST_ATOP
  • cairo.OPERATOR_XOR
  • cairo.OPERATOR_ADD
  • cairo.OPERATOR_SATURATE
  • cairo.OPERATOR_MULTIPLY
  • cairo.OPERATOR_SCREEN
  • cairo.OPERATOR_OVERLAY
  • cairo.OPERATOR_DARKEN
  • cairo.OPERATOR_LIGHTEN
  • cairo.OPERATOR_COLOR_DODGE
  • cairo.OPERATOR_COLOR_BURN
  • cairo.OPERATOR_HARD_LIGHT
  • cairo.OPERATOR_SOFT_LIGHT
  • cairo.OPERATOR_DIFFERENCE
  • cairo.OPERATOR_EXCLUSION
  • cairo.OPERATOR_HSL_HUE
  • cairo.OPERATOR_HSL_SATURATION
  • cairo.OPERATOR_HSL_COLOR
  • cairo.OPERATOR_HSL_LUMINOSITY

10.2.2Paths - Creating paths and manipulating path data

10.2.2.1Functions

cairo.context#copy_path()

Creates a copy of the current path and returns it to the user as a cairo.path. See cairo_path_data_t for hints on how to iterate over the returned data structure.

The result will have no data (data==nullptr and num_data==0), if either of the following conditions hold:

  1. If there is insufficient memory to copy the path. In this case path->status will be set to cairo.STATUS_NO_MEMORY.
  2. If cr is already in an error state. In this case path.status will contain the same status that would be returned by cairo.context#status().
cairo.context#copy_path_flat()

Gets a flattened copy of the current path and returns it to the user as a cairo.path. See cairo_path_data_t for hints on how to iterate over the returned data structure.

This function is like cairo.context#copy_path() except that any curves in the path will be approximated with piecewise-linear approximations, (accurate to within the current tolerance value). That is, the result is guaranteed to not have any elements of type cairo.PATH_CURVE_TO which will instead be replaced by a series of cairo.PATH_LINE_TO elements.

The result will have no data (data==nullptr and num_data==0), if either of the following conditions hold:

  1. If there is insufficient memory to copy the path. In this case path.status will be set to cairo.STATUS_NO_MEMORY.
  2. If cr is already in an error state. In this case path->status will contain the same status that would be returned by cairo.context#status().
cairo.context#append_path(path:cairo.path):reduce
Append the path onto the current path. The path may be either the return value from one of cairo.context#copy_path() or cairo.context#copy_path_flat() or it may be constructed manually. See cairo.path for details on how the path data structure should be initialized, and note that path.status must be initialized to cairo.STATUS_SUCCESS.
cairo.context#has_current_point()
Returns whether a current point is defined on the current path. See cairo.context#get_current_point() for details on the current point.
cairo.context#get_current_point()

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

The current point is returned in the user-space coordinate system. If there is no defined current point or if cr is in an error status, x and y will both be set to 0.0. It is possible to check this in advance with cairo.context#has_current_point().

Most path construction functions alter the current point. See the following for details on how they affect the current point: cairo.context#new_path(), cairo.context#new_sub_path(), cairo.context#append_path(), cairo.context#close_path(), cairo.context#move_to(), cairo.context#line_to(), cairo.context#curve_to(), cairo.context#rel_move_to(), cairo.context#rel_line_to(), cairo.context#rel_curve_to(), cairo.context#arc(), cairo.context#arc_negative(), cairo.context#rectangle(), cairo.context#text_path(), cairo.context#glyph_path(), cairo.context#stroke_to_path().

Some functions use and alter the current point but do not otherwise change current path: cairo.context#show_text().

Some functions unset the current path and as a result, current point: cairo.context#fill(), cairo.context#stroke().

cairo.context#new_path():reduce
Clears the current path. After this call there will be no path and no current point.
cairo.context#new_sub_path():reduce

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

In many cases, this call is not needed since new sub-paths are frequently started with cairo.context#move_to().

A call to cairo.context#new_sub_path() is particularly useful when beginning a new sub-path with one of the cairo.context#arc() calls. This makes things easier as it is no longer necessary to manually compute the arc's initial coordinates for a call to cairo.context#move_to().

cairo.context#close_path():reduce

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 cairo.context#move_to()), and closes this sub-path. After this call the current point will be at the joined endpoint of the sub-path.

The behavior of cairo.context#close_path() is distinct from simply calling cairo.context#line_to() with the equivalent coordinate in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead, there is a line join connecting the final and initial segments of the sub-path.

If there is no current point before the call to cairo.context#close_path(), this function will have no effect.

Note: As of cairo version 1.2.4 any call to cairo.context#close_path() will place an explicit MOVE_TO element into the path immediately after the CLOSE_PATH element, (which can be seen in cairo.context#copy_path() for example). This can simplify path processing in some cases as it may not be necessary to save the "last move_to point" during processing as the MOVE_TO immediately after the CLOSE_PATH will provide that point.

cairo.context#arc(xc:number, yc:number, radius:number, angle1?:number, angle2?:number):map:reduce:[deg]

Adds a circular arc of the given radius to the current path. The arc is centered at (xc, yc), 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*M_PI until it is greater than angle1.

If there is a current point, an initial line segment will be added to the path to connect the current point to the beginning of the arc. If this initial line is undesired, it can be avoided by calling cairo.context#new_sub_path() before calling cairo.context#arc().

Angles are measured in radians. An angle of 0.0 is in the direction of the positive X axis (in user space). An angle of math.pi/2.0 radians (90 degrees) is in the direction of the positive Y axis (in user space). Angles increase in the direction from the positive X axis toward the positive Y axis. So with the default transformation matrix, angles increase in a clockwise direction.

(To convert from degrees to radians, use degrees * (math.pi / 180.).)

This function gives the arc in the direction of increasing angles; see cairo.context#arc_negative() to get the arc in the direction of decreasing angles.

The arc is circular in user space. To achieve an elliptical arc, you can scale the current transformation matrix by different amounts in the X and Y directions. For example, to draw an ellipse in the box given by x, y, width, height:

cr.save() cr.translate(x + width / 2., y + height / 2.) cr.scale(width / 2., height / 2.) cr.arc(0., 0., 1., 0., 2 * math.pi)cr.restore()

Gura: If attribute :deg is specified, angle1 and angle2 are represented in degrees instead of radians.

cairo.context#arc_negative(xc:number, yc:number, radius:number, angle1?:number, angle2?:number):map:reduce:[deg]

Adds a circular arc of the given radius to the current path. The arc is centered at (xc, yc), 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*math.pi until it is less than angle1.

See cairo.context#arc() for more details. This function differs only in the direction of the arc between the two angles.

Gura: If attribute :deg is specified, angle1 and angle2 are represented in degrees instead of radians.

cairo.context#curve_to(x1:number, y1:number, x2:number, y2:number, x3:number, y3:number):map:reduce

Adds a cubic Bezier spline to the path from the current point to position (x3, y3) in user-space coordinates, using (x1, y1) and (x2, y2) as the control points. After this call the current point will be (x3, y3).

If there is no current point before the call to cairo.context#curve_to() this function will behave as if preceded by a call to cr.move_to(x1, y1).

cairo.context#line_to(x:number, y:number):map:reduce

Adds a line to the path from the current point to position (x, y) in user-space coordinates. After this call the current point will be (x, y).

If there is no current point before the call to cairo.context#line_to() this function will behave as cr.move_to(x, y).

cairo.context#move_to(x:number, y:number):map:reduce
Begin a new sub-path. After this call the current point will be (x, y).
cairo.context#rectangle(x:number, y:number, width:number, height:number):map:reduce

Adds a closed sub-path rectangle of the given size to the current path at position (x, y) in user-space coordinates.

This function is logically equivalent to:

cr.move_to(x, y) cr.rel_line_to(width, 0) cr.rel_line_to(0, height) cr.rel_line_to(-width, 0) cr.close_path()

cairo.context#text_path(text:string):map:reduce

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

Text conversion and positioning is done similar to cairo.context#show_text().

Like cairo.context#show_text(), After this call the current point is moved to the origin of where the next glyph would be placed in this same progression. That is, the current point will be at the origin of the final glyph offset by its advance values. This allows for chaining multiple calls to to cairo.context#text_path() without having to set current point in between.

Note: The cairo.context#text_path() 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. See cairo.context#glyph_path() for the "real" text path API in cairo.

cairo.context#rel_curve_to(dx1:number, dy1:number, dx2:number, dy2:number, dx3:number, dy3:number):map:reduce

Relative-coordinate version of cairo.context#curve_to(). All offsets are relative to the current point. Adds a cubic Bezier spline to the path from the current point to a point offset from the current point by (dx3, dy3), using points offset by (dx1, dy1) and (dx2, dy2) as the control points. After this call the current point will be offset by (dx3, dy3).

Given a current point of (x, y), cr.rel_curve_to(dx1, dy1, dx2, dy2, dx3, dy3) is logically equivalent to cr.curve_to(x+dx1, y+dy1, x+dx2, y+dy2, x+dx3, y+dy3).

It is an error to call this function with no current point. Doing so will cause cr to shutdown with a status of cairo.STATUS_NO_CURRENT_POINT.

cairo.context#rel_line_to(dx:number, dy:number):map:reduce

Relative-coordinate version of cairo.context#line_to(). Adds a line to the path from the current point to a point that is offset from the current point by (dx, dy) in user space. After this call the current point will be offset by (dx, dy).

Given a current point of (x, y), cr.rel_line_to(dx, dy) is logically equivalent to cr.line_to(x + dx, y + dy).

It is an error to call this function with no current point. Doing so will cause cr to shutdown with a status of cairo.STATUS_NO_CURRENT_POINT.

cairo.context#rel_move_to(dx:number, dy:number):map:reduce

Begin a new sub-path. After this call the current point will offset by (dx, dy).

Given a current point of (x, y), cr.rel_move_to(dx, dy) is logically equivalent to cr.move_to(x + dx, y + dy).

It is an error to call this function with no current point. Doing so will cause cr to shutdown with a status of cairo.STATUS_NO_CURRENT_POINT.

cairo.context#path_extents()

Computes a bounding box in user-space coordinates covering the points on the current path. If the current path is empty, returns an empty rectangle ((0,0), (0,0)). Stroke parameters, fill rule, surface dimensions and clipping are not taken into account.

Contrast with cairo.context#fill_extents() and cairo.context#stroke_extents() which return the extents of only the area that would be "inked" by the corresponding drawing operations.

The result of cairo.context#path_extents() is defined as equivalent to the limit of cairo.context#stroke_extents() with cairo.LINE_CAP_ROUND as the line width approaches 0.0, (but never reaching the empty-rectangle returned by cairo.context#stroke_extents() for a line width of 0.0).

Specifically, this means that zero-area sub-paths such as cairo.context#move_to();cairo.context#line_to() segments, (even degenerate cases where the coordinates to both calls are identical), will be considered as contributing to the extents. However, a lone cairo.context#move_to() will not contribute to the results of cairo.context#path_extents().

10.2.2.2Types and Values

10.2.3cairo.pattern - Sources for drawing

10.2.3.1Functions

cairo.pattern#add_color_stop_rgb(offset:number, red:number, green:number, blue:number):reduce

Adds an opaque color stop to a gradient pattern. The offset specifies the location along the gradient's control vector. For example, a linear gradient's control vector is from (x0,y0) to (x1,y1) while a radial gradient's control vector is from any point on the start circle to the corresponding point on the end circle.

The color is specified in the same way as in cairo.context#set_source_rgb().

If two (or more) stops are specified with identical offset values, they will be sorted according to the order in which the stops are added, (stops added earlier will compare less than stops added later). This can be useful for reliably making sharp color transitions instead of the typical blend.

Note: If the pattern is not a gradient pattern, (eg. a linear or radial pattern), then the pattern will be put into an error status with a status of cairo.STATUS_PATTERN_TYPE_MISMATCH.

cairo.pattern#add_color_stop_rgba(offset:number, red:number, green:number, blue:number, alpha:number):reduce

Adds a translucent color stop to a gradient pattern. The offset specifies the location along the gradient's control vector. For example, a linear gradient's control vector is from (x0,y0) to (x1,y1) while a radial gradient's control vector is from any point on the start circle to the corresponding point on the end circle.

The color is specified in the same way as in cairo.context#set_source_rgba().

If two (or more) stops are specified with identical offset values, they will be sorted according to the order in which the stops are added, (stops added earlier will compare less than stops added later). This can be useful for reliably making sharp color transitions instead of the typical blend.

Note: If the pattern is not a gradient pattern, (eg. a linear or radial pattern), then the pattern will be put into an error status with a status of cairo.STATUS_PATTERN_TYPE_MISMATCH.

cairo.pattern#get_color_stop_count()
Gets the number of color stops specified in the given gradient pattern.
cairo.pattern#get_color_stop_rgba(index:number)
Gets the color and offset information at the given index for a gradient pattern. Values of index are 0 to 1 less than the number returned by cairo.pattern#get_color_stop_count().
cairo.pattern.create_rgb(red:number, green:number, blue:number):static {block?}
Creates a new cairo.pattern corresponding to an opaque color. 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.
cairo.pattern.create_rgba(red:number, green:number, blue:number, alpha:number):static {block?}
Creates a new cairo,pattern corresponding to a translucent color. 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.
cairo.pattern#get_rgba()
Gets the solid color for a solid color pattern.
cairo.pattern.create_for_surface(surface:cairo.surface):static {block?}
Create a new cairo.pattern for the given surface.
cairo.pattern#get_surface()
Gets the surface of a surface pattern. The reference returned in surface is owned by the pattern; the caller should call cairo_surface_reference() if the surface is to be retained.
cairo.pattern.create_linear(x0:number, y0:number, x1:number, y1:number):static {block?}

Create a new linear gradient cairo.pattern along the line defined by (x0, y0) and (x1, y1). Before using the gradient pattern, a number of color stops should be defined using cairo.pattern#add_color_stop_rgb() or cairo.pattern#add_color_stop_rgba().

Note: The coordinates here are in pattern space. For a new pattern, pattern space is identical to user space, but the relationship between the spaces can be changed with cairo.pattern#set_matrix().

cairo.pattern#get_linear_points()
Gets the gradient endpoints for a linear gradient.
cairo.pattern.create_radial(cx0:number, cy0:number, radius0:number, cx1:number, cy1:number, radius1:number):static {block?}

Creates a new radial gradient cairo_pattern_t between the two circles defined by (cx0, cy0, radius0) and (cx1, cy1, radius1). Before using the gradient pattern, a number of color stops should be defined using cairo.pattern#add_color_stop_rgb() or cairo.pattern#add_color_stop_rgba().

Note: The coordinates here are in pattern space. For a new pattern, pattern space is identical to user space, but the relationship between the spaces can be changed with cairo.pattern#set_matrix().

cairo.pattern#get_radial_circles()
Gets the gradient endpoint circles for a radial gradient, each specified as a center coordinate and a radius.
cairo.mesh_pattern.create():static {block?}
cairo.mesh_pattern#begin_patch():reduce
cairo.mesh_pattern#end_patch():reduce
cairo.mesh_pattern#move_to(x:number, y:number):reduce
cairo.mesh_pattern#line_to(x:number, y:number):reduce
cairo.mesh_pattern#curve_to(x1:number, y1:number, x2:number, y2:number, x3:number, y3:number):reduce
cairo.mesh_pattern#set_control_point(point_num:number, x:number, y:number):reduce
cairo.mesh_pattern#set_corner_color_rgb(corner_num:number, red:number, green:number, blue:number):reduce
cairo.mesh_pattern#set_corner_color_rgba(corner_num:number, red:number, green:number, blue:number, alpha:number):reduce
cairo.pattern#status()
Checks whether an error has previously occurred for this pattern.
cairo.pattern#set_extend(extend:number):reduce

Sets the mode to be used for drawing outside the area of a pattern. See cairo_extend_t for details on the semantics of each extend strategy.

The default extend mode is cairo.EXTEND_NONE for surface patterns and cairo.EXTEND_PAD for gradient patterns.

cairo.pattern#get_extend()
Gets the current extend mode for a pattern. See cairo_extend_t for details on the semantics of each extend strategy.
cairo.pattern#set_filter(filter:number):reduce

Sets the filter to be used for resizing when using this pattern. See cairo_filter_t for details on each filter.

  • Note that you might want to control filtering even when you do not have an explicit cairo.pattern object, (for example when using cairo.context#set_source_surface()). In these cases, it is convenient to use cairo.context#get_source() to get access to the pattern that cairo creates implicitly. For example:

    cr.set_source_surface(image, x, y) cr.get_source().set_filter(cairo.FILTER_NEAREST)

cairo.pattern#get_filter()
Gets the current filter for a pattern. See cairo_filter_t for details on each filter.
cairo.pattern#set_matrix(array:array@double):reduce

Sets the pattern's transformation matrix to matrix. This matrix is a transformation from user space to pattern space.

When a pattern is first created it always has the identity matrix for its transformation matrix, which means that pattern space is initially identical to user space.

Important: Please note that the direction of this transformation matrix is from user space to pattern space. This means that if you imagine the flow from a pattern to user space (and on to device space), then coordinates in that flow will be transformed by the inverse of the pattern matrix.

For example, if you want to make a pattern appear twice as large as it does by default the correct code to use is:

cairo_matrix_init_scale (&matrix, 0.5, 0.5); cairo_pattern_set_matrix (pattern, &matrix);

Meanwhile, using values of 2.0 rather than 0.5 in the code above would cause the pattern to appear at half of its default size.

Also, please note the discussion of the user-space locking semantics of cairo.context#set_source().

cairo.pattern#get_matrix()
Stores the pattern's transformation matrix into matrix.
cairo.pattern#get_type()
This function returns the type a pattern. See cairo_pattern_type_t for available types.

10.2.3.2Types and Values

cairo.extend

  • cairo.EXTEND_NONE
  • cairo.EXTEND_REPEAT
  • cairo.EXTEND_REFLECT
  • cairo.EXTEND_PAD

cairo.filter

  • cairo.FILTER_FAST
  • cairo.FILTER_GOOD
  • cairo.FILTER_BEST
  • cairo.FILTER_NEAREST
  • cairo.FILTER_BILINEAR
  • cairo.FILTER_GAUSSIAN

cairo.pattern_type

  • cairo.PATTERN_TYPE_SOLID
  • cairo.PATTERN_TYPE_SURFACE
  • cairo.PATTERN_TYPE_LINEAR
  • cairo.PATTERN_TYPE_RADIAL
  • cairo.PATTERN_TYPE_MESH
  • cairo.PATTERN_TYPE_RASTER_SOURCE

10.2.4Regions - Representing a pixel-aligned area

cairo.region_overlap

  • cairo.REGION_OVERLAP_IN
  • cairo.REGION_OVERLAP_OUT
  • cairo.REGION_OVERLAP_PART

10.2.4.1Functions

cairo.region.create():static {block?}
cairo.region.create_rectangle(rectangle:cairo.rectangle_int):static {block?}
cairo.region.create_rectangles(rects[]:cairo.rectangle_int):static {block?}
cairo.region#copy() {block?}
cairo.region#status()
cairo.region#get_extents()
cairo.region#get_rectangle(nth:number)
cairo.region#is_empty()
cairo.region#contains_point(x:number, y:number)
cairo.region#contains_rectangle(rectangle:cairo.rectangle_int)
cairo.region#equal(region:cairo.region)
cairo.region#translate(dx:number, dy:number)
cairo.region#intersect(other:cairo.region)
cairo.region#intersect_rectangle(rectangle:cairo.rectangle_int)
cairo.region#union(other:cairo.region)
cairo.region#union_rectangle(rectangle:cairo.rectangle_int)
cairo.region#xor(other:cairo.region)
cairo.region#xor_rectangle(rectangle:cairo.rectangle_int)

10.2.4.2Types and Values

10.2.5Transformations - Manipulating the current transformation matrix

10.2.5.1Functions

cairo.context#translate(tx:number, ty:number):reduce
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 cairo.context#translate(). In other words, the translation of the user-space origin takes place after any existing transformation.
cairo.context#scale(sx:number, sy:number):reduce
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.
cairo.context#rotate(angle:number):reduce:[deg]

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.

Gura: If attribute :deg is specified, angle is represented in degrees instead of radians.

cairo.context#transform(array:array@double):reduce
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.
cairo.context#set_matrix(array:array@double):reduce
Modifies the current transformation matrix (CTM) by setting it equal to matrix.
cairo.context#get_matrix()
Stores the current transformation matrix (CTM) into matrix.
cairo.context#identity_matrix():reduce
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.
cairo.context#user_to_device(x:number, y:number)
Transform a coordinate from user space to device space by multiplying the given point by the current transformation matrix (CTM).
cairo.context#user_to_device_distance(dx:number, dy:number)
Transform a distance vector from user space to device space. This function is similar to cairo.context#user_to_device() except that the translation components of the CTM will be ignored when transforming (dx,dy).
cairo.context#device_to_user(x:number, y:number)
Transform a coordinate from device space to user space by multiplying the given point by the inverse of the current transformation matrix (CTM).
cairo.context#device_to_user_distance(dx:number, dy:number)
Transform a distance vector from device space to user space. This function is similar to cairo.context#device_to_user() except that the translation components of the inverse CTM will be ignored when transforming (dx,dy).

10.2.6text - Rendering text and glyphs

10.2.6.1Functions

cairo.context#select_font_face(family:string, slant:number, weight:number):reduce

Note: The cairo.context#select_font_face() 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.

Selects a family and style of font from a simplified description as a family name, slant and weight. Cairo provides no operation to list available family names on the system (this is a "toy", remember), but the standard CSS2 generic family names, ("serif", "sans-serif", "cursive", "fantasy", "monospace"), are likely to work as expected.

If family starts with the string "cairo:", or if no native font backends are compiled in, cairo will use an internal font family. The internal font family recognizes many modifiers in the family string, most notably, it recognizes the string "monospace". That is, the family name "cairo:monospace" will use the monospace version of the internal font family.

For "real" font selection, see the font-backend-specific font_face_create functions for the font backend you are using. (For example, if you are using the freetype-based cairo-ft font backend, see cairo_ft_font_face_create_for_ft_face() or cairo_ft_font_face_create_for_pattern().) The resulting font face could then be used with cairo.scaled_font_create() and cairo.context#set_scaled_font().

Similarly, when using the "real" font support, you can call directly into the underlying font system, (such as fontconfig or freetype), for operations such as listing available fonts, etc.

It is expected that most applications will need to use a more comprehensive font handling and text layout library, (for example, pango), in conjunction with cairo.

If text is drawn without a call to cairo.context#select_font_face(), (nor cairo.context#set_font_face() nor cairo.context#set_scaled_font()), the default family is platform-specific, but is essentially "sans-serif". Default slant is cairo.FONT_SLANT_NORMAL, and default weight is cairo.FONT_WEIGHT_NORMAL.

This function is equivalent to a call to cairo.toy_font_face.create() followed by cairo.context#set_font_face().

cairo.context#set_font_size(size:number):reduce

Sets the current font matrix to a scale by a factor of size, replacing any font matrix previously set with cairo.context#set_font_size() or cairo.context#set_font_matrix(). 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.)

If text is drawn without a call to cairo.context#set_font_size(), (nor cairo.context#set_font_matrix() nor cairo.context#set_scaled_font()), the default font size is 10.0.

cairo.context#set_font_matrix(array:array@double):reduce
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 cairo_set_font_size()), but a more complex font matrix can be used to shear the font or stretch it unequally along the two axes.
cairo.context#get_font_matrix()
Stores the current font matrix into matrix. See cairo.context#set_font_matrix().
cairo.context#set_font_options(options:cairo.font_options):reduce
Sets a set of custom font rendering options for the cairo_t. 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.
cairo.context#get_font_options() {block?}
Retrieves font rendering options set via cairo.context#set_font_options. Note that the returned options do not include any options derived from the underlying surface; they are literally the options passed to cairo.context#set_font_options().
cairo.context#set_font_face(font_face:cairo.font_face):reduce
Replaces the current cairo_font_face_t object in the cairo_t with font_face. The replaced font face in the cairo_t will be destroyed if there are no other references to it.
cairo.context#get_font_face() {block?}
Gets the current font face for a cairo_t.
cairo.context#set_scaled_font(scaled_font:cairo.scaled_font):reduce
Replaces the current font face, font matrix, and font options in the cairo_t with those of the cairo_scaled_font_t. Except for some translation, the current CTM of the cairo_t should be the same as that of the cairo_scaled_font_t, which can be accessed using cairo.context#scaled_font_get_ctm().
cairo.context#get_scaled_font() {block?}
Gets the current scaled font for a cairo_t.
cairo.context#show_text(text:string):map:reduce

A drawing operator that generates the shape from a string of UTF-8 characters, rendered according to the current font_face, font_size (font_matrix), and font_options.

This function first computes a set of glyphs for the string of text. The first glyph is placed so that its origin is at the current point. The origin of each subsequent glyph is offset from that of the previous glyph by the advance values of the previous glyph.

After this call the current point is moved to the origin of where the next glyph would be placed in this same progression. That is, the current point will be at the origin of the final glyph offset by its advance values. This allows for easy display of a single logical string with multiple calls to cairo.context#show_text().

Note: The cairo.context#show_text() 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. See cairo.context#show_glyphs() for the "real" text display API in cairo.

cairo.context#show_glyphs(glyphs:cairo.glyph):reduce
A drawing operator that generates the shape from an array of glyphs, rendered according to the current font face, font size (font matrix), and font options.
cairo.context#font_extents() {block?}
Gets the font extents for the currently selected font.
cairo.context#text_extents(text:string):map {block?}

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 cairo.context#show_text()). Additionally, the x_advance and y_advance values indicate the amount by which the current point would be advanced by cairo.context#show_text().

Note that whitespace characters do not directly contribute to the size of the rectangle (extents.width and extents.height). They do contribute indirectly by changing the position of non-whitespace characters. In particular, trailing whitespace characters are likely to not affect the size of the rectangle, though they will affect the x_advance and y_advance values.

cairo.context#glyph_extents(glyphs:cairo.glyph) {block?}

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 cairo.context#show_glyphs()). Additionally, the x_advance and y_advance values indicate the amount by which the current point would be advanced by cairo.context#show_glyphs().

Note that whitespace glyphs do not contribute to the size of the rectangle (extents.width and extents.height).

cairo.toy_font_face.create(family:string, slant:number, weight:number):static {block?}

Creates a font face from a triplet of family, slant, and weight. These font faces are used in implementation of the the cairo_t "toy" font API.

If family is the zero-length string "", the platform-specific default family is assumed. The default family then can be queried using cairo.toy_font_face#get_family().

The cairo.context#select_font_face() function uses this to create font faces. See that function for limitations and other details of toy font faces.

cairo.toy_font_face#get_family()
Gets the familly name of a toy font.
cairo.toy_font_face#get_slant()
Gets the slant a toy font.
cairo.toy_font_face#get_weight()
Gets the weight a toy font.

10.2.6.2Types and Values

10.2.7Raster Sources - Supplying arbitary image data

10.2.7.1Functions

10.3Fonts

10.3.1cairo.font_face - Base class for font faces

10.3.1.1Functions

10.3.2cairo.scaled_font - Font face at particular size and options

10.3.2.1Functions

cairo.scaled_font.create(font_face:cairo.font_face, font_matrix:array@double, ctm:array@double, options):static {block?}

10.3.3cairo_font_options_t - How a font should be rendered

10.3.3.1Functions

cairo.font_options.create():static {block?}
cairo.font_options#status()
cairo.font_options#merge(other:cairo.font_options):void
cairo.font_options#hash()
cairo.font_options#equal(other:cairo.font_options)
cairo.font_options#set_antialias(antialias:number):void
cairo.font_options#get_antialias()
cairo.font_options#set_subpixel_order(subpixel_order:number):void
cairo.font_options#get_subpixel_order()
cairo.font_options#set_hint_style(hint_style:number):void
cairo.font_options#get_hint_style()
cairo.font_options#set_hint_metrics(hint_metrics:number):void
cairo.font_options#get_hint_metrics()

10.3.4FreeType Fonts - Font support for FreeType

10.3.4.1Functions

10.3.5Win32 Fonts - Font support for Microsoft Windows

10.3.5.1Functions

10.3.6Quartz (CGFont) Fonts - Font support via CGFont on OS X

10.3.6.1Functions

10.3.7User Fonts - Font support with font data provided by the user

10.3.7.1Functions

10.4Surfaces

10.4.1cairo.device - interface to underlying rendering system

10.4.1.1Functions

cairo.device#status()
cairo.device#finish():reduce
cairo.device#flush():reduce
cairo.device#get_type()
cairo.device#acquire()
cairo.device#release():void

10.4.2cairo.surface - Base class for surfaces

10.4.2.1Functions

cairo.surface.create_similar(other:cairo.surface, content:number, width:number, height:number):static {block?}

Create a new surface that is as compatible as possible with an existing surface. For example the new surface will have the same fallback resolution and font options as other. Generally, the new surface will also use the same backend as other, unless that is not possible for some reason. The type of the returned surface may be examined with cairo.surface#get_type().

Initially the surface contents are all 0 (transparent if contents have transparency, black otherwise.)

Use cairo.surface.create_similar_image() if you need an image surface which can be painted quickly to the target surface.

cairo.surface.create_similar_image(other:cairo.surface, format:number, width:number, height:number):static {block?}

Create a new image surface that is as compatible as possible for uploading to and the use in conjunction with an existing surface. However, this surface can still be used like any normal image surface.

Initially the surface contents are all 0 (transparent if contents have transparency, black otherwise.)

Use cairo.surface.create_similar() if you don't need an image surface.

cairo.surface.create_for_rectangle(other:cairo.surface, format:number, width:number, height:number):static {block?}

Create a new surface that is a rectangle within the target surface. All operations drawn to this surface are then clipped and translated onto the target surface. Nothing drawn via this sub-surface outside of its bounds is drawn onto the target surface, making this a useful method for passing constrained child surfaces to library routines that draw directly onto the parent surface, i.e. with no further backend allocations, double buffering or copies.

Note: The semantics of subsurfaces have not been finalized yet unless the rectangle is in full device units, is contained within the extents of the target surface, and the target or subsurface's device transforms are not changed.

cairo.surface#status()
Checks whether an error has previously occurred for this surface.
cairo.surface#finish():reduce

This function finishes the surface and drops all references to external resources. For example, for the Xlib backend it means that cairo will no longer access the drawable, which can be freed. After calling cairo.surface#finish() the only valid operations on a surface are getting and setting user, referencing and destroying, and flushing and finishing it. Further drawing to the surface will not affect the surface but will instead trigger a cairo.STATUS_SURFACE_FINISHED error.

When the last call to cairo_surface_destroy() decreases the reference count to zero, cairo will call cairo_surface_finish() if it hasn't been called already, before freeing the resources associated with the surface.

cairo.surface#flush():reduce
Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state. This function must be called before switching from drawing on the surface with cairo to drawing on it directly with native APIs. If the surface doesn't support direct access, then this function does nothing.
cairo.surface#get_device()
This function returns the device for a surface. See cairo.device.
cairo.surface#get_font_options()
Retrieves the default font rendering options for the surface. This allows display surfaces to report the correct subpixel order for rendering on them, print surfaces to disable hinting of metrics and so forth. The result can then be used with cairo.scaled_font.create().
cairo.surface#get_content()
This function returns the content type of surface which indicates whether the surface contains color and/or alpha information. See cairo_content_t.
cairo.surface#mark_dirty():reduce
Tells cairo that drawing has been done to surface using means other than cairo, and that cairo should reread any cached areas. Note that you must call cairo.surface#flush() before doing such drawing.
cairo.surface#mark_dirty_rectangle(x:number, y:number, width:number, height:number):reduce

Like cairo.surface#mark_dirty(), but drawing has been done only to the specified rectangle, so that cairo can retain cached contents for other parts of the surface.

Any cached clip set on the surface will be reset by this function, to make sure that future cairo calls have the clip set that they expect.

cairo.surface#set_device_offset(x_offset:number, y_offset:number):reduce

Sets an offset that is added to the device coordinates determined by the CTM when drawing to surface. One use case for this function is when we want to create a cairo.surface that redirects drawing for a portion of an onscreen surface to an offscreen surface in a way that is completely invisible to the user of the cairo API. Setting a transformation via cairo.context#translate() isn't sufficient to do this, since functions like cairo.context#device_to_user() will expose the hidden offset.

Note that the offset affects drawing to the surface as well as using the surface in a source pattern.

cairo.surface#get_device_offset()
This function returns the previous device offset set by cairo.surface#set_device_offset().
cairo.surface#set_fallback_resolution(x_pixels_per_inch:number, y_pixels_per_inch:number):reduce

Set the horizontal and vertical resolution for image fallbacks.

When certain operations aren't supported natively by a backend, cairo will fallback by rendering operations to an image and then overlaying that image onto the output. For backends that are natively vector-oriented, this function can be used to set the resolution used for these image fallbacks, (larger values will result in more detailed images, but also larger file sizes).

Some examples of natively vector-oriented backends are the ps, pdf, and svg backends.

For backends that are natively raster-oriented, image fallbacks are still possible, but they are always performed at the native device resolution. So this function has no effect on those backends.

Note: The fallback resolution only takes effect at the time of completing a page (with cairo.context#show_page() or cairo.context#copy_page()) so there is currently no way to have more than one fallback resolution in effect on a single page.

The default fallback resoultion is 300 pixels per inch in both dimensions.

cairo.surface#get_fallback_resolution()
This function returns the previous fallback resolution set by cairo.surface#set_fallback_resolution(), or default fallback resolution if never set.
cairo.surface#get_type()
This function returns the type of the backend used to create a surface. See cairo_surface_type_t for available types.
cairo.surface#copy_page():reduce

Emits the current page for backends that support multiple pages, but doesn't clear it, so that the contents of the current page will be retained for the next page. Use cairo.surface#show_page() if you want to get an empty page after the emission.

There is a convenience function for this that takes a cairo.context, namely cairo.context#copy_page().

cairo.surface#show_page():reduce

Emits and clears the current page for backends that support multiple pages. Use cairo.surface#copy_page() if you don't want to clear the page.

There is a convenience function for this that takes a cairo.context, namely cairo.context#show_page().

cairo.surface#has_show_text_glyphs()

Returns whether the surface supports sophisticated cairo.context#show_text_glyphs() operations. That is, whether it actually uses the provided text and cluster data to a cairo.context#show_text_glyphs() call.

Note: Even if this function returns false, a cairo.context#show_text_glyphs() operation targeted at surface will still succeed. It just will act like a cairo.context#show_glyphs() operation. Users can use this function to avoid computing UTF-8 text and cluster mapping if the target surface does not use it.

cairo.surface#set_mime_data():reduce
cairo.surface#get_mime_data()
cairo.surface#supports_mime_type()
cairo.surface#map_to_image(extents:cairo.rectangle_int)
cairo.surface#unmap_image()
cairo.surface#write_to_png(stream:stream:w):reduce

10.4.3Image Surfaces - Rendering to memory buffers

10.4.3.1Functions

cairo.image_surface.create(image:image):static {block?}
cairo.image_surface.create_from_png(stream:stream:r):static {block?}
cairo.image_surface#get_format()
cairo.image_surface#get_width()
cairo.image_surface#get_height()
cairo.image_surface#get_stride()

10.4.4PDF Surfaces - Rendering PDF documents

10.4.4.1Functions

cairo.pdf_surface.create(stream:stream:w, width_in_points:number, height_in_points:number):static {block?}
cairo.pdf_surface#restrict_to_version(version:number):reduce
cairo.pdf_surface#set_size(width_in_points:number, height_in_points:number):reduce

10.4.5PNG Support - Reading and writing PNG images

10.4.5.1Functions

10.4.6PostScript Surfaces - Rendering PostScript documents

10.4.6.1Functions

10.4.7Recording Surfaces - Records all drawing operations

10.4.7.1Functions

10.4.8Win32 Surfaces - Microsoft Windows surface support

10.4.8.1Functions

10.4.9SVG Surfaces - Rendering SVG documents

10.4.9.1Functions

cairo.svg_surface.create(stream:stream:w, width_in_points:number, height_in_points:number):static {block?}
cairo.svg_surface#restrict_to_version(version:number):reduce

10.4.10Quartz Surfaces - Rendering to Quartz surfaces

10.4.10.1Functions

10.4.11XCB Surfaces - X Window System rendering using the XCB library

10.4.11.1Functions

10.4.12XLib Surfaces - X Window System rendering using XLib

10.4.12.1Functions

10.4.13XLib-XRender Backend - X Window System rendering using XLib and the X Render extension

10.4.13.1Functions

10.4.14Script Surfaces - Rendering to replayable scripts

10.4.14.1Functions

10.5Thanks

This module uses Cairo library which is distributed in the following site:

http://cairographics.org/