ContextProtocol

public protocol ContextProtocol

The ContextProtocol protocol exposes the methods and properties of an underlying cairo_t instance. The default implementation of these can be found in the protocol extension below. For a concrete class that implements these methods and properties, see Context. Alternatively, use ContextRef as a lighweight, unowned reference if you already have an instance you just want to use.

  • ptr

    Untyped pointer to the underlying cairo_t instance.

    Declaration

    Swift

    var ptr: UnsafeMutableRawPointer { get }
  • _ptr Default implementation

    Typed pointer to the underlying cairo_t instance.

    Default Implementation

    Return the stored, untyped pointer as a typed pointer to the cairo_t instance.

    Declaration

    Swift

    var _ptr: UnsafeMutablePointer<cairo_t> { get }
  • ref() Extension method

    Increases the reference count on cr by one. This prevents cr from being destroyed until a matching call to unref() is made.

    Declaration

    Swift

    @discardableResult
    func ref() -> ContextRef
  • unref() Extension method

    Decreases the reference count on cr by one. If the result is zero, then cr and all associated resources are freed. See ref().

    Declaration

    Swift

    func unref()
  • referenceCount Extension method

    Cairo context reference count

    Declaration

    Swift

    var referenceCount: Int { get }
  • status Extension method

    Error status of the context

    Declaration

    Swift

    var status: cairo_status_t { get }
  • save() Extension method

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

    Declaration

    Swift

    func save()
  • restore() Extension method

    Restores cr to the state saved by a preceding call to save() and removes that state from the stack of saved states.

    Declaration

    Swift

    func restore()
  • target Extension method

    Gets the target surface for the cairo context as passed to cairo_create().

    This function will always return a valid pointer, but the result can be a nil surface if cr is already in an error state, (ie. status != CAIRO_STATUS_SUCCESS). A nil surface is indicated by surface.status != CAIRO_STATUS_SUCCESS.

    Declaration

    Swift

    var target: SurfaceRef { get }
  • pushGroup() Extension method

    Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to cairo_pop_group() or cairo_pop_group_to_source(). These calls provide the result of any drawing to the group as a pattern, (either as an explicit object, or set as the source pattern).

    This group functionality can be convenient for performing intermediate compositing. One common use of a group is to render objects as opaque within the group, (so that they occlude each other), and then blend the result with translucence onto the destination.

    Groups can be nested arbitrarily deep by making balanced calls to cairo_push_group()/cairo_pop_group(). Each call pushes/pops the new target group onto/from a stack.

    The cairo_push_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_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_push_group (cr); cairo_set_source (cr, fill_pattern); cairo_fill_preserve (cr); cairo_set_source (cr, stroke_pattern); cairo_stroke (cr); cairo_pop_group_to_source (cr); cairo_paint_with_alpha (cr, alpha);

    Declaration

    Swift

    func pushGroup()
  • pushGroup(content:) Extension method

    Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call to cairo_pop_group() or cairo_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_push_group() which you should see for a more detailed description of group rendering.

    Declaration

    Swift

    func pushGroup(content: Content)
  • popGroup() Extension method

    Terminates the redirection begun by a call to cairo_push_group() or cairo_push_group_with_content() and returns a new pattern containing the results of all drawing operations performed to the group.

    The cairo_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.

    Declaration

    Swift

    @discardableResult
    func popGroup() -> PatternRef
  • popGroupToSource() Extension method

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

    The behavior of this method is equivalent to the sequence of operations: cairo_pattern_t *group = cairo_pop_group (cr); cairo_set_source (cr, group); cairo_pattern_destroy (group); but is more convenient as their is no need for a variable to store the short-lived pointer to the pattern.

    The cairo_pop_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.

    Declaration

    Swift

    func popGroupToSource()
  • groupTarget Extension method

    Returns the current destination surface for the context. This is either the original target surface as passed to cairo_create() or the target surface for the current group as started by the most recent call to cairo_push_group() or cairo_push_group_with_content(). This property will always return a valid pointer, but the result can be a nil surface if cr is already in an error state, (ie. cairo_status() != CAIRO_STATUS_SUCCESS). A nil surface is indicated by cairo_surface_status() != CAIRO_STATUS_SUCCESS.

    Declaration

    Swift

    var groupTarget: SurfaceRef { get }
  • setSource(red:green:blue:) Extension method

    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 setSource(red: 0.0, green: 0.0, blue: 0.0)).

    Declaration

    Swift

    func setSource(red: Double, green: Double, blue: Double)
  • Sets the source pattern within cr to a translucent color. This color will then be used for any subsequent drawing operation until a new source pattern is set.

    The color and alpha components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, they will be clamped.

    The default source pattern is opaque black, (that is, it is equivalent to setSource(red: 0.0, green: 0.0, blue: 0.0)).

    Declaration

    Swift

    func setSource(red: Double, green: Double, blue: Double, alpha: Double)
  • source Extension method

    Source pattern within cr. This pattern will then be used for any subsequent drawing operation until a new source pattern is set.

    Note: The pattern’s transformation matrix will be locked to the user space in effect at the time of cairo_set_source(). This means that further modifications of the current transformation matrix will not affect the source pattern. See cairo_pattern_set_matrix().

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

    Declaration

    Swift

    var source: PatternRef { get set }
  • antiAlias Extension method

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

    Declaration

    Swift

    var antiAlias: cairo_antialias_t { get set }
  • setDash(_:offset:) Extension method

    Sets the dash pattern to be used by cairo_stroke(). A dash pattern is specified by dashes , an array of positive values. Each value provides the length of alternate on and off portions of the stroke. The offset specifies an offset into the pattern at which the stroke begins.

    Each on segment will have caps applied as if the segment were a separate sub-path. In particular, it is valid to use an on length of 0.0 with CAIRO_LINE_CAP_ROUND or CAIRO_LINE_CAP_SQUARE in order to distributed dots or squares along a path.

    Note: The length values are in user-space units as evaluated at the time of stroking. This is not necessarily the same as the user space at the time of cairo_set_dash().

    If num_dashes is 0 dashing is disabled.

    If num_dashes is 1 a symmetric pattern is assumed with alternating on and off portions of the size specified by the single value in dashes .

    If any value in dashes is negative, or if all values are 0, then cr will be put into an error state with a status of CAIRO_STATUS_INVALID_DASH.

    Declaration

    Swift

    func setDash(_ dashes: [Double], offset: Double = 0.0)

    Parameters

    dashes

    an array specifying alternate lengths of on and off stroke portions

    offset

    an offset into the dash pattern at which the stroke should start

  • dashCount Extension method

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

    Declaration

    Swift

    var dashCount: Int { get }
  • dash Extension method

    Sets the dash pattern to be used by cairo_stroke(). A dash pattern is specified by dashes , an array of positive values. Each value provides the length of alternate on and off portions of the stroke. The offset specifies an offset into the pattern at which the stroke begins.

    Each on segment will have caps applied as if the segment were a separate sub-path. In particular, it is valid to use an on length of 0.0 with CAIRO_LINE_CAP_ROUND or CAIRO_LINE_CAP_SQUARE in order to distributed dots or squares along a path.

    Note: The length values are in user-space units as evaluated at the time of stroking. This is not necessarily the same as the user space at the time of cairo_set_dash().

    If num_dashes is 0 dashing is disabled.

    If num_dashes is 1 a symmetric pattern is assumed with alternating on and off portions of the size specified by the single value in dashes .

    If any value in dashes is negative, or if all values are 0, then cr will be put into an error state with a status of CAIRO_STATUS_INVALID_DASH.

    Declaration

    Swift

    var dash: [Double] { get set }
  • fillRule Extension method

    Current fill rule within the cairo context. The fill rule is used to determine which regions are inside or outside a complex (potentially self-intersecting) path. The current fill rule affects both cairo_fill() and cairo_clip(). See cairo_fill_rule_t for details on the semantics of each available fill rule.

    The default fill rule is CAIRO_FILL_RULE_WINDING.

    Declaration

    Swift

    var fillRule: cairo_fill_rule_t { get set }
  • lineCap Extension method

    Current line cap style within the cairo context. See cairo_line_cap_t for details about how the available line cap styles are drawn.

    As with the other stroke parameters, the current line cap style is examined by cairo_stroke(), cairo_stroke_extents(), and cairo_stroke_to_path(), but does not have any effect during path construction.

    Declaration

    Swift

    var lineCap: cairo_line_cap_t { get set }
  • lineJoin Extension method

    Current line join style within the cairo context. See cairo_line_join_t for details about how the available line join styles are drawn.

    As with the other stroke parameters, the current line join style is examined by cairo_stroke(), cairo_stroke_extents(), and cairo_stroke_to_path(), but does not have any effect during path construction.

    The default line join style is CAIRO_LINE_JOIN_MITER.

    Declaration

    Swift

    var lineJoin: cairo_line_join_t { get set }
  • lineWidth Extension method

    Current line width within the cairo context. The line width value specifies the diameter of a pen that is circular in user space, (though device-space pen may be an ellipse in general due to scaling/shear/rotation of the CTM).

    Note: When the description above refers to user space and CTM it refers to the user space and CTM in effect at the time of the stroking operation, not the user space and CTM in effect at the time of the call to cairo_set_line_width(). The simplest usage makes both of these spaces identical. That is, if there is no change to the CTM between a call to cairo_set_line_width() and the stroking operation, then one can just pass user-space values to cairo_set_line_width() and ignore this note.

    As with the other stroke parameters, the current line width is examined by cairo_stroke(), cairo_stroke_extents(), and cairo_stroke_to_path(), but does not have any effect during path construction.

    The default line width value is 2.0.

    Declaration

    Swift

    var lineWidth: Double { get set }
  • miterLimit Extension method

    Current miter limit within the cairo context.

    If the current line join style is set to CAIRO_LINE_JOIN_MITER (see cairo_set_line_join()), the miter limit is used to determine whether the lines should be joined with a bevel instead of a miter. Cairo divides the length of the miter by the line width. If the result is greater than the miter limit, the style is converted to a bevel.

    As with the other stroke parameters, the current line miter limit is examined by cairo_stroke(), cairo_stroke_extents(), and cairo_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)

    Declaration

    Swift

    var miterLimit: Double { get set }
  • mitreLimit Extension method

    Current mitre limit within the cairo context.

    If the current line join style is set to CAIRO_LINE_JOIN_MITER (see cairo_set_line_join()), the mitre limit is used to determine whether the lines should be joined with a bevel instead of a miter. Cairo divides the length of the mitre by the line width. If the result is greater than the mitre limit, the style is converted to a bevel.

    As with the other stroke parameters, the current line mitre limit is examined by cairo_stroke(), cairo_stroke_extents(), and cairo_stroke_to_path(), but does not have any effect during path construction.

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

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

    Declaration

    Swift

    var mitreLimit: Double { get set }
  • operator Extension method

    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.

    Declaration

    Swift

    var `operator`: cairo_operator_t { get set }
  • compositingOperator Extension method

    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.

    Declaration

    Swift

    var compositingOperator: cairo_operator_t { get set }
  • tolerance Extension method

    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.

    Declaration

    Swift

    var tolerance: Double { get set }
  • clip() Extension method

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

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

    Declaration

    Swift

    func clip()
  • clipPreserve() Extension method

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

    Unlike clip(), clipPreserve() 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 clipPreserve() 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 clipPreserve() within a save()/restore() pair. The only other means of increasing the size of the clip region is resetClip().

    Declaration

    Swift

    func clipPreserve()
  • clipExtents Extension method

    Bounding box in user coordinates covering the area inside the current clip.

    Declaration

    Swift

    var clipExtents: (x1: Double, y1: Double, x2: Double, y2: Double) { get }
  • isInClip(_:_:) Extension method

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

    See clip() and clipPreserve().

    Declaration

    Swift

    func isInClip(_ x: Double, _ y: Double) -> Bool
  • resetClip() Extension method

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

    Declaration

    Swift

    func resetClip()
  • clipRectangleList Extension method

    Current clip region as a list of rectangles in user coordinates.

    Declaration

    Swift

    var clipRectangleList: [cairo_rectangle_t]? { get }
  • fill() Extension method

    A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled). After fill(), the current path will be cleared from the cairo context. See fillRule() and fillPreserve().

    Declaration

    Swift

    func fill()
  • fillPreserve() Extension method

    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.

    Declaration

    Swift

    func fillPreserve()
  • fillExtents Extension method

    Bounding box in user coordinates covering the area that would be affected, (the inked area), by a cairo_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 pathExtents(), which is similar, but returns non-zero extents for some paths with no inked area, (such as a simple line segment).

    Note that fillExtents must necessarily do more work to compute the precise inked areas in light of the fill rule, so pathExtents may be more desirable for sake of performance if the non-inked path extents are desired.

    See fill(), fillRule, and fillPreserve().

    Declaration

    Swift

    var fillExtents: (x1: Double, y1: Double, x2: Double, y2: Double) { get }
  • isInFill(_:_:) Extension method

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

    See fill(), fillRule, and fillPreserve().

    Declaration

    Swift

    func isInFill(_ x: Double, _ y: Double) -> Bool
  • mask(pattern:) Extension method

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

    Declaration

    Swift

    func mask<P>(pattern: P) where P : PatternProtocol
  • mask(surface:x:y:) Extension method

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

    Declaration

    Swift

    func mask<S>(surface: S, x: Double = 0.0, y: Double = 0.0) where S : SurfaceProtocol

    Parameters

    surface

    a Cairo Surface

    x

    X coordinate at which to place the origin of surface

    y

    Y coordinate at which to place the origin of surface

  • paint() Extension method

    Drawing operator that paints the current source everywhere within the current clip region.

    Declaration

    Swift

    func paint()
  • paint(alpha:) Extension method

    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.

    Declaration

    Swift

    func paint(alpha: Double)
  • stroke() Extension method

    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 lineWidth, lineJoin, lineCap, dash, setDash(), and strokePreserve().

    Note: Degenerate segments and sub-paths are treated specially and provide a useful result. These can result in two different situations:

    Zero-length on segments set in dash/setDash(). 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.

    A sub-path created by moveTo() followed by either a closePath() or one or more calls to lineTo() to the same coordinate as the moveTo(). 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.

    Declaration

    Swift

    func stroke()
  • strokePreserve() Extension method

    Drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. Unlike cairo_stroke(), cairo_stroke_preserve() preserves the path within the cairo context.

    See lineWidth, lineJoin, lineCap, dash, setDash(), and stroke().

    Declaration

    Swift

    func strokePreserve()
  • strokeExtents Extension method

    Computes a bounding box in user coordinates covering the area that would be affected, (the inked area), by a cairo_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 strokeExtents will return an empty rectangle. Contrast with pathExtents which can be used to compute the non-empty bounds as the line width approaches zero.

    Note that strokeExtents must necessarily do more work to compute the precise inked areas in light of the stroke parameters, so pathExtents may be more desirable for sake of performance if non-inked path extents are desired.

    See stroke(), lineWidth, lineJoin, lineCap, dash, setDash(), and strokePreserve().

    Declaration

    Swift

    var strokeExtents: (x1: Double, y1: Double, x2: Double, y2: Double) { get }

    Return Value

    • x1: left of the resulting extents
    • y1: top of the resulting extents
    • x2: right of the resulting extents
    • y2: bottom of the resulting extents

  • isInStroke(_:_:) Extension method

    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.

    See stroke(), lineWidth, lineJoin, lineCap, dash, setDash(), and strokePreserve().

    Declaration

    Swift

    func isInStroke(_ x: Double, _ y: Double) -> Bool
  • copyPage() Extension method

    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.

    This is a convenience method that simply calls cairo_surface_copy_page() on cr ‘s target.

    Declaration

    Swift

    func copyPage()
  • showPage() Extension method

    Emits and clears the current page for backends that support multiple pages. Use cairo_copy_page() if you don’t want to clear the page.

    This is a convenience function that simply calls cairo_surface_show_page() on cr ‘s target.

    Declaration

    Swift

    func showPage()
  • userData(key:) Extension method

    Return user data previously attached to cr using the specified key. If no user data has been attached with the given key this function returns NULL.

    Declaration

    Swift

    func userData(key: UnsafePointer<cairo_user_data_key_t>) -> UnsafeMutableRawPointer

    Parameters

    key

    the address of the cairo_user_data_key_t the user data was attached to

  • Attach user data to cr . To remove user data from a surface, call this function with the key that was used to set it and NULL for data .

    Declaration

    Swift

    @discardableResult
    func setUserData(key: UnsafePointer<cairo_user_data_key_t>, value: UnsafeMutableRawPointer, destroy: cairo_destroy_func_t! = nil) -> cairo_status_t

    Parameters

    key

    the address of a cairo_user_data_key_t to attach the user data to

    value

    the user data to attach to the cairo_t

    destroy

    a cairo_destroy_func_t which will be called when the cairo_t is destroyed or when new user data is attached using the same key.

    Return Value

    CAIRO_STATUS_SUCCESS or CAIRO_STATUS_NO_MEMORY if a slot could not be allocated for the user data.

  • newPath() Extension method

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

    Declaration

    Swift

    func newPath()
  • path Extension method

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

    This function will always return a valid pointer, but the result will have no data (data==NULL and num_data==0), if either of the following conditions hold:

    If there is insufficient memory to copy the path. In this case path->status will be set to CAIRO_STATUS_NO_MEMORY. If cr is already in an error state. In this case path->status will contain the same status that would be returned by cairo_status().

    Declaration

    Swift

    var path: PathRef { get }

    Return Value

    The copy of the current path.

  • flatPath Extension method

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

    This function is like 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.

    This function will always return a valid pointer, but the result will have no data (data==NULL and num_data==0), if either of the following conditions hold:

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

    Declaration

    Swift

    var flatPath: PathRef { get }
  • append(path:) Extension method

    Append the path onto the current path. The path may be either the return value from one of cairo_copy_path() or cairo_copy_path_flat() or it may be constructed manually. See cairo_path_t for details on how the path data structure should be initialized, and note that path->status must be initialized to CAIRO_STATUS_SUCCESS.

    Declaration

    Swift

    func append<P>(path: P) where P : PathProtocol

    Parameters

    path

    Path to be appended

  • hasCurrentPoint Extension method

    Returns whether a current point is defined on the current path. See currentPoint for details on the current point.

    Declaration

    Swift

    var hasCurrentPoint: Bool { get }
  • currentPoint Extension method

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

    Most path construction functions alter the current point. See the following for details on how they affect the current point: cairo_new_path(), cairo_new_sub_path(), cairo_append_path(), cairo_close_path(), cairo_move_to(), cairo_line_to(), cairo_curve_to(), cairo_rel_move_to(), cairo_rel_line_to(), cairo_rel_curve_to(), cairo_arc(), cairo_arc_negative(), cairo_rectangle(), cairo_text_path(), cairo_glyph_path(), cairo_stroke_to_path().

    Some functions use and alter the current point but do not otherwise change current path: cairo_show_text().

    Some functions unset the current path and as a result, current point: cairo_fill(), cairo_stroke().

    Declaration

    Swift

    var currentPoint: (x: Double, y: Double) { get }
  • newSubPath() Extension method

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

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

    Declaration

    Swift

    func newSubPath()
  • closePath() Extension method

    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.

    The behavior of closePath() is distinct from simply calling lineTo() 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 closePath(), this function will have no effect.

    Note: As of cairo version 1.2.4 any call to closePath() will place an explicit MOVE_TO element into the path immediately after the CLOSE_PATH element, (which can be seen in cairo_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.

    Declaration

    Swift

    func closePath()
  • 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 .pi*2 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_new_sub_path() before calling cairo_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 .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 * (.pi / 180.).)

    This function gives the arc in the direction of increasing angles; see cairo_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(xc: 0, yc: 0, radius: 1, angle1: 0, angle2: .pi * 2);
    cr.restore();
    

    Declaration

    Swift

    func arc(xc: Double = 0, yc: Double = 0, radius: Double = 1, angle1: Double = 0, angle2: Double = .pi*2)
  • 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 .pi*2 until it is less than angle1 .

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

    Declaration

    Swift

    func arcNegative(xc: Double = 0, yc: Double = 0, radius: Double = 1, angle1: Double = .pi*2, angle2: Double = 0)
  • curveTo(x1:y1:x2:y2:x3:y3:) Extension method

    Adds a cubic Bézier 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 curveTo() this function will behave as if preceded by a call to moveTo(cr , x1 , y1 ).

    Declaration

    Swift

    func curveTo(x1: Double, y1: Double, x2: Double, y2: Double, x3: Double, y3: Double)

    Parameters

    x1

    the X coordinate of the first control point

    y1

    the Y coordinate of the first control point

    x2

    the X coordinate of the second control point

    y2

    the Y coordinate of the second control point

    x3

    the X coordinate of the end of the curve

    y3

    the Y coordinate of the end of the curve

  • lineTo(_:_:) Extension method

    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_line_to() this function will behave as cairo_move_to(cr , x , y ).

    Declaration

    Swift

    func lineTo(_ x: Double, _ y: Double)

    Parameters

    x

    the X coordinate of the end of the new line

    y

    the Y coordinate of the end of the new line

  • moveTo(_:_:) Extension method

    Begin a new sub-path. After this call the current point will be (x , y ).

    Declaration

    Swift

    func moveTo(_ x: Double, _ y: Double)

    Parameters

    x

    the X coordinate of the end of the new position

    y

    the Y coordinate of the end of the new position

  • rectangle(x:y:width:height:) Extension method

    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.moveTo(x, y);
    cr.relLineTo(width, 0);
    cr.relLineTo(0, height);
    cr.relLineTo(-width, 0);
    cr.closePath();
    

    Declaration

    Swift

    func rectangle(x: Double, y: Double, width: Double, height: Double)
  • glyphPath(glyphs:) Extension method

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

    Declaration

    Swift

    func glyphPath(glyphs: [cairo_glyph_t])
  • textPath(_:) Extension method

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

    Text conversion and positioning is done similar to cairo_show_text().

    Like showText(), 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_text_path() without having to set current point in between.

    Note: The textPath() 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_glyph_path() for the real text path API in cairo.

    Declaration

    Swift

    func textPath(_ text: UnsafePointer<CChar>)
  • Relative-coordinate version of cairo_curve_to(). 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 (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.relCurveTo(dx1 , dy1 , dx2 , dy2 , dx3 , dy3 ) is logically equivalent to cr.curveTo(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.

    Declaration

    Swift

    func relCurveTo(dx1: Double, dy1: Double, dx2: Double, dy2: Double, dx3: Double, dy3: Double)

    Parameters

    dx1

    the X offset of the first control point

    dy1

    the Y offset of the first control point

    dx2

    the X offset of the second control point

    dy2

    the Y offset of the second control point

    dx3

    the X offset of the end of the curve

    dy3

    the Y offset of the end of the curve

  • relLineTo(_:_:) Extension method

    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 (dx , dy ) in user space. After this call the current point will be offset by (dx , dy ).

    Given a current point of (x, y), relLineTo(dx , dy ) is logically equivalent to lineTo(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.

    Declaration

    Swift

    func relLineTo(_ dx: Double, _ dy: Double)

    Parameters

    dx

    the X offset of the end of the new line

    dy

    the Y offset of the end of the new line

  • relMoveTo(_:_:) Extension method

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

    Given a current point of (x, y), relMoveTo(dx , dy ) is logically equivalent to moveTo(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.

    Declaration

    Swift

    func relMoveTo(_ dx: Double, _ dy: Double)

    Parameters

    x

    the X coordinate of the end of the new position

    y

    the Y coordinate of the end of the new position

  • pathExtents Extension method

    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 fillExtents and strokeExtents which return the extents of only the area that would be inked by the corresponding drawing operations.

    The result of pathExtents is defined as equivalent to the limit of strokeExtents with CAIRO_LINE_CAP_ROUND as the line width approaches 0.0, (but never reaching the empty-rectangle returned by strokeExtents for a line width of 0.0).

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

    Declaration

    Swift

    var pathExtents: (x1: Double, y1: Double, x2: Double, y2: Double) { get }
  • 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.

    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_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_select_font_face(), (nor selectFontFace() nor setScaledFont()), 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_set_font_face().

    Declaration

    Swift

    func selectFontFace(_ family: UnsafePointer<CChar>, slant: cairo_font_slant_t, weight: cairo_font_weight_t)
  • fontSize Extension method

    Sets the current font matrix to a scale by a factor of @size, replacing any font matrix previously set with cairo_set_font_size() or cairo_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 setting the font size, (nor the font matrix nor setScaledFont()), the default font size is 10.0.

    Declaration

    Swift

    var fontSize: Double { get set }
  • fontMatrix Extension method

    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.

    Declaration

    Swift

    var fontMatrix: cairo_matrix_t { get set }
  • setFont(options:) Extension method

    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.

    Declaration

    Swift

    func setFont(options: UnsafePointer<cairo_font_options_t>)
  • getFont(options:) Extension method

    Retrieves font rendering options set via #cairo_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_set_font_options().

    Declaration

    Swift

    func getFont(options: UnsafeMutablePointer<cairo_font_options_t>)

    Parameters

    options

    a #cairo_font_options_t pointer into which to store the retrieved options. All existing values are overwritten

  • fontFace Extension method

    Current font face for a #cairo_t.

    This object is owned by cairo. To keep a reference to it, you must call fontFace.ref().

    This function never returns %NULL. If memory cannot be allocated, a special nil #cairo_font_face_t object will be returned on which cairo_font_face_status() returns %CAIRO_STATUS_NO_MEMORY. Using this nil object will cause its error state to propagate to other objects it is passed to, (for example, calling cairo_set_font_face() with a nil font will trigger an error that will shutdown the #cairo_t object).

    Declaration

    Swift

    var fontFace: UnsafeMutablePointer<cairo_font_face_t> { get set }
  • scaledFont Extension method

    the current scaled font for a #cairo_t.

    Return value: the current scaled font. This object is owned by cairo.

    This function never returns %NULL. If memory cannot be allocated, a special nil #cairo_scaled_font_t object will be returned on which cairo_scaled_font_status() returns %CAIRO_STATUS_NO_MEMORY. Using this nil object will cause its error state to propagate to other objects it is passed to, (for example, setting scaledFont with a nil font will trigger an error that will shutdown the #cairo_t object).

    Declaration

    Swift

    var scaledFont: ScaledFont { get set }
  • showText(_:) Extension method

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

    Note: The cairo_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_show_glyphs() for the real text display API in cairo.

    Declaration

    Swift

    func showText(_ text: UnsafePointer<CChar>)
  • 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 cairo_show_glyphs() as if it had been passed @glyphs and @num_glyphs.

    The mapping between @utf8 and @glyphs is provided by an array of clusters. Each cluster covers a number of text bytes and glyphs, and neighboring clusters cover neighboring areas of @utf8 and @glyphs. The clusters should collectively cover @utf8 and @glyphs in entirety.

    The first cluster always covers bytes from the beginning of @utf8. If @cluster_flags do not have the %CAIRO_TEXT_CLUSTER_FLAG_BACKWARD set, the first cluster also covers the beginning of @glyphs, otherwise it covers the end of the @glyphs array and following clusters move backward.

    See #cairo_text_cluster_t for constraints on valid clusters.

    Declaration

    Swift

    func showText(_ text: UnsafePointer<CChar>, length: Int, glyphs: [cairo_glyph_t], clusters: [cairo_text_cluster_t], flags: cairo_text_cluster_flags_t)
  • textExtents(_:) Extension method

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

    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.

    Declaration

    Swift

    func textExtents(_ text: UnsafePointer<CChar>) -> cairo_text_extents_t
  • glyphExtents(_:) Extension method

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

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

    Declaration

    Swift

    func glyphExtents(_ glyphs: [cairo_glyph_t]) -> cairo_text_extents_t
  • fontExtents Extension method

    Font extents for the currently selected font

    Declaration

    Swift

    var fontExtents: cairo_font_extents_t { get }