Cairo::Context Class Reference

List of all members.


Detailed Description

Context is the main class used to draw in cairomm.

In the simplest case, create a Context with its target Surface, set its drawing options (line width, color, etc), create shapes with methods like move_to() and line_to(), and then draw the shapes to the Surface using methods such as stroke() or fill().

Context is a reference-counted object that should be used via Cairo::RefPtr.

Public Types

typedef cairo_t cobject
 The base cairo C type that is wrapped by Cairo::Context.

Public Member Functions

void append_path (const Path &path)
 Append the path onto the current path.
void arc (double xc, double yc, double radius, double angle1, double angle2)
 Adds a circular arc of the given radius to the current path.
void arc_negative (double xc, double yc, double radius, double angle1, double angle2)
 Adds a circular arc of the given radius to the current path.
void begin_new_path ()
 Clears the current path.
void begin_new_sub_path ()
 Begin a new subpath.
void clip ()
 Establishes a new clip region by intersecting the current clip region with the current Path as it would be filled by fill() and according to the current fill rule.
void clip_preserve ()
 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.
void close_path ()
 Adds a line segment to the path from the current point to the beginning of the current subpath, (the most recent point passed to move_to()), and closes this subpath.
const cobjectcobj () const
 Gets a pointer to the base C type that is wrapped by the Context.
cobjectcobj ()
 Gets a pointer to the base C type that is wrapped by the Context.
 Context (cairo_t *cobject, bool has_reference=false)
 Create a C++ wrapper for the C instance.
void copy_clip_rectangle_list (std::vector< Rectangle > &rectangles) const
 Returns the current clip region as a list of rectangles in user coordinates.
void copy_page ()
Pathcopy_path () const
 Creates a copy of the current path and returns it to the user.
Pathcopy_path_flat () const
 Gets a flattened copy of the current path and returns it to the user.
void curve_to (double x1, double y1, double x2, double y2, double x3, double y3)
 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.
void device_to_user (double &x, double &y)
 Transform a coordinate from device space to user space by multiplying the given point by the inverse of the current transformation matrix (CTM).
void device_to_user_distance (double &dx, double &dy)
 Transform a distance vector from device space to user space.
void fill ()
 A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled).
void fill_preserve ()
 A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled).
Antialias get_antialias () const
 Gets the current shape antialiasing mode, as set by set_antialias().
void get_clip_extents (double &x1, double &y1, double &x2, double &y2) const
 Computes a bounding box in user coordinates covering the area inside the current clip.
void get_current_point (double &x, double &y) const
 Gets the current point of the current path, which is conceptually the final point reached by the path so far.
void get_dash (std::vector< double > &dashes, double &offset) const
 Gets the current dash array and offset.
void get_fill_extents (double &x1, double &y1, double &x2, double &y2) const
FillRule get_fill_rule () const
 Gets the current fill rule, as set by set_fill_rule().
void get_font_extents (FontExtents &extents) const
RefPtr< const FontFaceget_font_face () const
RefPtr< FontFaceget_font_face ()
void get_font_matrix (Matrix &matrix) const
void get_font_options (FontOptions &options) const
void get_glyph_extents (const std::vector< Glyph > &glyphs, TextExtents &extents) const
RefPtr< const Surfaceget_group_target () const
 Same as the non-const version but returns a reference to a const Surface.
RefPtr< Surfaceget_group_target ()
 Gets the target surface for the current group as started by the most recent call to push_group() or push_group_with_content().
LineCap get_line_cap () const
 Gets the current line cap style, as set by set_line_cap().
LineJoin get_line_join () const
 Gets the current line join style, as set by set_line_join().
double get_line_width () const
 Gets the current line width, as set by set_line_width().
Matrix get_matrix () const
void get_matrix (Matrix &matrix)
 Stores the current transformation matrix (CTM) into matrix.
double get_miter_limit () const
 Gets the current miter limit, as set by set_miter_limit().
Operator get_operator () const
 Gets the current compositing operator for a cairo Context.
void get_path_extents (double &x1, double &y1, double &x2, double &y2) const
 Computes a bounding box in user-space coordinates covering the points on the current path.
RefPtr< ScaledFontget_scaled_font ()
RefPtr< const Patternget_source () const
RefPtr< Patternget_source ()
 Gets the current source pattern for the Context.
void get_stroke_extents (double &x1, double &y1, double &x2, double &y2) const
RefPtr< const Surfaceget_target () const
 Gets the target surface associated with this Context.
RefPtr< Surfaceget_target ()
 Gets the target surface associated with this Context.
void get_text_extents (const std::string &utf8, TextExtents &extents) const
double get_tolerance () const
 Gets the current tolerance value, as set by set_tolerance().
void glyph_path (const std::vector< Glyph > &glyphs)
bool has_current_point () const
 Checks if there is a current point defined.
bool in_fill (double x, double y) const
bool in_stroke (double x, double y) const
void line_to (double x, double y)
 Adds a line to the path from the current point to position (x, y) in user-space coordinates.
void mask (const RefPtr< const Surface > &surface, double surface_x, double surface_y)
 A drawing operator that paints the current source using the alpha channel of surface as a mask.
void mask (const RefPtr< const Pattern > &pattern)
 A drawing operator that paints the current source using the alpha channel of pattern as a mask.
void move_to (double x, double y)
 If the current subpath is not empty, begin a new subpath.
void paint ()
 A drawing operator that paints the current source everywhere within the current clip region.
void paint_with_alpha (double alpha)
 A drawing operator that paints the current source everywhere within the current clip region using a mask of constant alpha value alpha.
RefPtr< Patternpop_group ()
 Terminates the redirection begun by a call to push_group() or push_group_with_content() and returns a new pattern containing the results of all drawing operations performed to the group.
void pop_group_to_source ()
 Terminates the redirection begun by a call to push_group() or push_group_with_content() and installs the resulting pattern as the source pattern in the given cairo Context.
void push_group ()
 Temporarily redirects drawing to an intermediate surface known as a group.
void push_group_with_content (Content content)
 Temporarily redirects drawing to an intermediate surface known as a group.
void rectangle (double x, double y, double width, double height)
 Adds a closed-subpath rectangle of the given size to the current path at position (x, y) in user-space coordinates.
void rel_curve_to (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
 Relative-coordinate version of curve_to().
void rel_line_to (double dx, double dy)
 Relative-coordinate version of line_to().
void rel_move_to (double dx, double dy)
 If the current subpath is not empty, begin a new subpath.
void reset_clip ()
 Reset the current clip region to its original, unrestricted state.
void restore ()
 Restores cr to the state saved by a preceding call to save() and removes that state from the stack of saved states.
void rotate (double angle_radians)
 Modifies the current transformation matrix (CTM) by rotating the user-space axes by angle radians.
void rotate_degrees (double angle_degres)
 A convenience wrapper around rotate() that accepts angles in degrees.
void save ()
 Makes a copy of the current state of the Context and saves it on an internal stack of saved states.
void scale (double sx, double sy)
 Modifies the current transformation matrix (CTM) by scaling the X and Y user-space axes by sx and sy respectively.
void select_font_face (const std::string &family, FontSlant slant, FontWeight weight)
void set_antialias (Antialias antialias)
 Set the antialiasing mode of the rasterizer used for drawing shapes.
void set_dash (std::vector< double > &dashes, double offset)
 Sets the dash pattern to be used by stroke().
void set_dash (std::valarray< double > &dashes, double offset)
 Alternate version of set_dash().
void set_fill_rule (FillRule fill_rule)
 Set the current fill rule within the cairo Context.
void set_font_face (const RefPtr< const FontFace > &font_face)
void set_font_matrix (const Matrix &matrix)
void set_font_options (const FontOptions &options)
void set_font_size (double size)
void set_identity_matrix ()
 Resets the current transformation matrix (CTM) by setting it equal to the identity matrix.
void set_line_cap (LineCap line_cap)
 Sets the current line cap style within the cairo Context.
void set_line_join (LineJoin line_join)
 Sets the current line join style within the cairo Context.
void set_line_width (double width)
 Sets the current line width within the cairo Context.
void set_matrix (const Matrix &matrix)
 Modifies the current transformation matrix (CTM) by setting it equal to matrix.
void set_miter_limit (double limit)
void set_operator (Operator op)
 Sets the compositing operator to be used for all drawing operations.
void set_scaled_font (const RefPtr< const ScaledFont > &scaled_font)
void set_source (const RefPtr< Surface > &surface, double x, double y)
 This is a convenience function for creating a pattern from a Surface and setting it as the source.
void set_source (const RefPtr< const Pattern > &source)
 Sets the source pattern within the Context to source.
void set_source_rgb (double red, double green, double blue)
 Sets the source pattern within the Context to an opaque color.
void set_source_rgba (double red, double green, double blue, double alpha)
 Sets the source pattern within the Context to a translucent color.
void set_tolerance (double tolerance)
 Sets the tolerance used when converting paths into trapezoids.
void show_glyphs (const std::vector< Glyph > &glyphs)
void show_page ()
void show_text (const std::string &utf8)
void show_text_glyphs (const std::string &utf8, const std::vector< Glyph > &glyphs, const std::vector< TextCluster > &clusters, TextClusterFlags cluster_flags)
void stroke ()
 A drawing operator that strokes the current Path according to the current line width, line join, line cap, and dash settings.
void stroke_preserve ()
 A drawing operator that strokes the current Path according to the current line width, line join, line cap, and dash settings.
void text_path (const std::string &utf8)
void transform (const Matrix &matrix)
 Modifies the current transformation matrix (CTM) by applying matrix as an additional transformation.
void translate (double tx, double ty)
 Modifies the current transformation matrix (CTM) by translating the user-space origin by (tx, ty).
void unset_dash ()
 This function disables a dash pattern that was set with set_dash().
void user_to_device (double &x, double &y)
 Transform a coordinate from user space to device space by multiplying the given point by the current transformation matrix (CTM).
void user_to_device_distance (double &dx, double &dy)
 Transform a distance vector from user space to device space.
virtual ~Context ()

Static Public Member Functions

static RefPtr< Contextcreate (const RefPtr< Surface > &target)

Protected Member Functions

 Context (const RefPtr< Surface > &target)

Protected Attributes

cobjectm_cobject


Member Typedef Documentation

typedef cairo_t Cairo::Context::cobject

The base cairo C type that is wrapped by Cairo::Context.


Constructor & Destructor Documentation

Cairo::Context::Context ( const RefPtr< Surface > &  target  )  [explicit, protected]

Cairo::Context::Context ( cairo_t *  cobject,
bool  has_reference = false 
) [explicit]

Create a C++ wrapper for the C instance.

This C++ instance should then be given to a RefPtr.

Parameters:
cobject The C instance.
has_reference Whether we already have a reference. Otherwise, the constructor will take an extra reference.

virtual Cairo::Context::~Context (  )  [virtual]


Member Function Documentation

void Cairo::Context::append_path ( const Path path  ) 

Append the path onto the current path.

The path may be either the return value from one of copy_path() or copy_path_flat() or it may be constructed manually.

Parameters:
path path to be appended

void Cairo::Context::arc ( double  xc,
double  yc,
double  radius,
double  angle1,
double  angle2 
)

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.

Angles are measured in radians. An angle of 0 is in the direction of the positive X axis (in user-space). An angle of M_PI 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 * (M_PI / 180.).)

This function gives the arc in the direction of increasing angles; see 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:

 context->save();
 context->translate(x, y);
 context->scale(width / 2.0, height / 2.0);
 context->arc(0.0, 0.0, 1.0, 0.0, 2 * M_PI);
 context->restore();

Parameters:
xc X position of the center of the arc
yc Y position of the center of the arc
radius the radius of the arc
angle1 the start angle, in radians
angle2 the end angle, in radians

void Cairo::Context::arc_negative ( double  xc,
double  yc,
double  radius,
double  angle1,
double  angle2 
)

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*M_PI until it is greater than angle1.

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

Parameters:
xc X position of the center of the arc
yc Y position of the center of the arc
radius the radius of the arc
angle1 the start angle, in radians
angle2 the end angle, in radians

void Cairo::Context::begin_new_path (  ) 

Clears the current path.

After this call there will be no current point.

void Cairo::Context::begin_new_sub_path (  ) 

Begin a new subpath.

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 subpaths are frequently started with move_to().

A call to begin_new_sub_path() is particularly useful when beginning a new subpath 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 move_to().

void Cairo::Context::clip (  ) 

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

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

See also:
set_fill_rule()

void Cairo::Context::clip_preserve (  ) 

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.

Unlike clip(), cairo_clip_preserve preserves the path within the cairo Context.

See also:
clip()

set_fill_rule()

void Cairo::Context::close_path (  ) 

Adds a line segment to the path from the current point to the beginning of the current subpath, (the most recent point passed to move_to()), and closes this subpath.

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

const cobject* Cairo::Context::cobj (  )  const [inline]

Gets a pointer to the base C type that is wrapped by the Context.

References m_cobject.

cobject* Cairo::Context::cobj (  )  [inline]

Gets a pointer to the base C type that is wrapped by the Context.

References m_cobject.

void Cairo::Context::copy_clip_rectangle_list ( std::vector< Rectangle > &  rectangles  )  const

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

This function will throw an exception if the clip region cannot be represented as a list of user-space rectangles.

1.4

void Cairo::Context::copy_page (  ) 

Path* Cairo::Context::copy_path (  )  const

Creates a copy of the current path and returns it to the user.

Todo:
See cairo_path_data_t for hints on how to iterate over the returned data structure.
Note:
The caller owns the Path object returned from this function. The Path object must be freed when you are finished with it.

Path* Cairo::Context::copy_path_flat (  )  const

Gets a flattened copy of the current path and returns it to the user.

Todo:
See cairo_path_data_t for hints on how to iterate over the returned data structure.
This function is like 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.

Note:
The caller owns the Path object returned from this function. The Path object must be freed when you are finished with it.

static RefPtr<Context> Cairo::Context::create ( const RefPtr< Surface > &  target  )  [static]

void Cairo::Context::curve_to ( double  x1,
double  y1,
double  x2,
double  y2,
double  x3,
double  y3 
)

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

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

void Cairo::Context::device_to_user ( double &  x,
double &  y 
)

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

Parameters:
x X value of coordinate (in/out parameter)
y Y value of coordinate (in/out parameter)

void Cairo::Context::device_to_user_distance ( double &  dx,
double &  dy 
)

Transform a distance vector from device space to user space.

This function is similar to device_to_user() except that the translation components of the inverse CTM will be ignored when transforming (dx,dy).

Parameters:
dx X component of a distance vector (in/out parameter)
dy Y component of a distance vector (in/out parameter)

void Cairo::Context::fill (  ) 

A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled).

After fill(), the current path will be cleared from the cairo context.

See also:
set_fill_rule()

fill_preserve()

void Cairo::Context::fill_preserve (  ) 

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

See also:
set_fill_rule()

fill().

Antialias Cairo::Context::get_antialias (  )  const

Gets the current shape antialiasing mode, as set by set_antialias().

void Cairo::Context::get_clip_extents ( double &  x1,
double &  y1,
double &  x2,
double &  y2 
) const

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

Parameters:
x1 left of the resulting extents
y1 top of the resulting extents
x2 right of the resulting extents
y2 bottom of the resulting extents
Since:
1.4

void Cairo::Context::get_current_point ( double &  x,
double &  y 
) const

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 then x and y will both be set to 0.0.

Most path construction functions alter the current point. See the following for details on how they affect the current point: clear_path(), move_to(), line_to(), curve_to(), arc(), rel_move_to(), rel_line_to(), rel_curve_to(), arc(), and text_path()

Parameters:
x return value for X coordinate of the current point
y return value for Y coordinate of the current point
See also:
has_current_point

void Cairo::Context::get_dash ( std::vector< double > &  dashes,
double &  offset 
) const

Gets the current dash array and offset.

Parameters:
dashes return value for the dash array.
offset return value for the current dash offset.
Since: 1.4

void Cairo::Context::get_fill_extents ( double &  x1,
double &  y1,
double &  x2,
double &  y2 
) const

FillRule Cairo::Context::get_fill_rule (  )  const

Gets the current fill rule, as set by set_fill_rule().

void Cairo::Context::get_font_extents ( FontExtents extents  )  const

RefPtr<const FontFace> Cairo::Context::get_font_face (  )  const

RefPtr<FontFace> Cairo::Context::get_font_face (  ) 

void Cairo::Context::get_font_matrix ( Matrix matrix  )  const

void Cairo::Context::get_font_options ( FontOptions options  )  const

Since:
1.8

void Cairo::Context::get_glyph_extents ( const std::vector< Glyph > &  glyphs,
TextExtents extents 
) const

RefPtr<const Surface> Cairo::Context::get_group_target (  )  const

Same as the non-const version but returns a reference to a const Surface.

RefPtr<Surface> Cairo::Context::get_group_target (  ) 

Gets the target surface for the current group as started by the most recent call to push_group() or push_group_with_content().

This function will return NULL if called "outside" of any group rendering blocks, (that is, after the last balancing call to pop_group() or pop_group_to_source()).

Exceptions:
 

LineCap Cairo::Context::get_line_cap (  )  const

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

LineJoin Cairo::Context::get_line_join (  )  const

Gets the current line join style, as set by set_line_join().

double Cairo::Context::get_line_width (  )  const

Gets the current line width, as set by set_line_width().

Matrix Cairo::Context::get_matrix (  )  const

Since:
1.8

void Cairo::Context::get_matrix ( Matrix matrix  ) 

Stores the current transformation matrix (CTM) into matrix.

Parameters:
matrix return value for the matrix

double Cairo::Context::get_miter_limit (  )  const

Gets the current miter limit, as set by set_miter_limit().

Operator Cairo::Context::get_operator (  )  const

Gets the current compositing operator for a cairo Context.

void Cairo::Context::get_path_extents ( double &  x1,
double &  y1,
double &  x2,
double &  y2 
) const

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 fill_extents() and stroke_extents() which return the extents of only the area that would be "inked" by the corresponding drawing operations.

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

Specifically, this means that zero-area sub-paths such as move_to();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 move_to() will not contribute to the results of path_extents().

Parameters:
x1 left of the resulting extents
y1 top of the resulting extents
x2 right of the resulting extents
y2 bottom of the resulting extents
Since:
1.6

RefPtr<ScaledFont> Cairo::Context::get_scaled_font (  ) 

Since:
1.8

RefPtr<const Pattern> Cairo::Context::get_source (  )  const

RefPtr<Pattern> Cairo::Context::get_source (  ) 

Gets the current source pattern for the Context.

void Cairo::Context::get_stroke_extents ( double &  x1,
double &  y1,
double &  x2,
double &  y2 
) const

RefPtr<const Surface> Cairo::Context::get_target (  )  const

Gets the target surface associated with this Context.

Exceptions:
 

RefPtr<Surface> Cairo::Context::get_target (  ) 

Gets the target surface associated with this Context.

Exceptions:
 

void Cairo::Context::get_text_extents ( const std::string &  utf8,
TextExtents extents 
) const

double Cairo::Context::get_tolerance (  )  const

Gets the current tolerance value, as set by set_tolerance().

void Cairo::Context::glyph_path ( const std::vector<