class Cairo::Context - cairo-1.14.6 Documentation

# class Cairo::Context

### Public Class Methods

new(p1) click to toggle source

    static VALUE
    cr_initialize (VALUE self, VALUE target)
    {
      cairo_t *cr;
      VALUE result = Qnil;

      cr = cairo_create (RVAL2CRSURFACE (target));
      cr_check_status (cr);
      rb_ivar_set (self, cr_id_surface, target);
      rb_ivar_set (self, cr_id_source, Qnil);
      if (rb_ivar_defined (target, rb_cairo__io_id_output))
        cr_set_user_data (cr,
                          &cr_object_holder_key,
                          cr_object_holder_new (self),
                          cr_object_holder_free);
      DATA_PTR (self) = cr;
      if (rb_block_given_p ())
        result = rb_ensure (rb_yield, self, cr_destroy_with_destroy_check, self);
      return result;
    }

wrap(p1) click to toggle source

    static VALUE
    cr_s_wrap (VALUE self, VALUE pointer)
    {
      VALUE result;
      VALUE rb_cr;
      cairo_t *cr;

      if (NIL_P (rb_cairo__cFFIPointer))
        {
          rb_raise (rb_eNotImpError,
                    "%s: FFI::Pointer is required",
                    rb_id2name (rb_frame_this_func ()));
        }

      if (!RTEST (rb_obj_is_kind_of (pointer, rb_cairo__cFFIPointer)))
        {
          rb_raise (rb_eArgError,
                    "must be FFI::Pointer: %s",
                    rb_cairo__inspect (pointer));
        }

      {
        VALUE rb_cr_address;
        rb_cr_address = rb_funcall (pointer, rb_intern ("address"), 0);
        cr = NUM2PTR (rb_cr_address);
        cr_check_status (cr);
      }

      rb_cr = rb_obj_alloc (self);
      cairo_reference (cr);
      DATA_PTR (rb_cr) = cr;
      rb_ivar_set (rb_cr, cr_id_surface, Qnil);

      if (rb_block_given_p ())
        {
          result = rb_ensure (rb_yield, rb_cr, cr_destroy_with_destroy_check, rb_cr);
        }
      else
        {
          result = rb_cr;
        }

      return result;
    }

### Public Instance Methods

antialias() click to toggle source

    static VALUE
    cr_get_antialias(VALUE self)
    {
      return INT2NUM (cairo_get_antialias (_SELF));
    }

append_path(p1) click to toggle source

    static VALUE
    cr_copy_append_path (VALUE self, VALUE path)
    {
      cairo_append_path (_SELF, RVAL2CRPATH (path));
      cr_check_status (_SELF);
      return self;
    }

arc(p1, p2, p3, p4, p5) click to toggle source

    static VALUE
    cr_arc (VALUE self, VALUE xc, VALUE yc, VALUE radius,
            VALUE angle1, VALUE angle2)
    {
      cairo_arc (_SELF, NUM2DBL (xc), NUM2DBL (yc), NUM2DBL (radius),
                 NUM2DBL (angle1), NUM2DBL (angle2));
      cr_check_status (_SELF);
      return self;
    }

arc_negative(p1, p2, p3, p4, p5) click to toggle source

    static VALUE
    cr_arc_negative (VALUE self, VALUE xc, VALUE yc, VALUE radius,
                     VALUE angle1, VALUE angle2)
    {
      cairo_arc_negative (_SELF, NUM2DBL (xc), NUM2DBL (yc), NUM2DBL (radius),
                          NUM2DBL (angle1), NUM2DBL (angle2));
      cr_check_status (_SELF);
      return self;
    }

clip(p1 = v1) click to toggle source

    static VALUE
    cr_clip (int argc, VALUE *argv, VALUE self)
    {
      VALUE preserve;

      rb_scan_args(argc, argv, "01", &preserve);

      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }

      if (RVAL2CBOOL (preserve))
        cairo_clip_preserve(_SELF);
      else
        cairo_clip (_SELF);

      cr_check_status (_SELF);
      return self;
    }

clip_extents() click to toggle source

    static VALUE
    cr_clip_extents (VALUE self)
    {
      double x1, y1, x2, y2;
      cairo_clip_extents (_SELF, &x1, &y1, &x2, &y2);
      cr_check_status (_SELF);
      return rb_ary_new3 (4,
                          rb_float_new (x1), rb_float_new (y1),
                          rb_float_new (x2), rb_float_new (y2));
    }

clip_preserve(&block) click to toggle source

    # File lib/cairo/context.rb, line 25
    def clip_preserve(&block)
      clip(true, &block)
    end

clip_rectangle_list() click to toggle source

    static VALUE
    cr_clip_rectangle_list (VALUE self)
    {
      VALUE rb_rectangles;
      cairo_rectangle_list_t *rectangles;
      int i;

      rectangles = cairo_copy_clip_rectangle_list (_SELF);
      rb_cairo_check_status (rectangles->status);

      rb_rectangles = rb_ary_new2 (rectangles->num_rectangles);
      for (i = 0; i < rectangles->num_rectangles; i++) {
        VALUE argv[4];
        cairo_rectangle_t rectangle = rectangles->rectangles[i];

        argv[0] = rb_float_new (rectangle.x);
        argv[1] = rb_float_new (rectangle.y);
        argv[2] = rb_float_new (rectangle.width);
        argv[3] = rb_float_new (rectangle.height);
        rb_ary_push (rb_rectangles,
                     rb_class_new_instance (4, argv, rb_cCairo_Rectangle));
      }
      cairo_rectangle_list_destroy (rectangles);

      return rb_rectangles;
    }

close_path() click to toggle source

    static VALUE
    cr_close_path (VALUE self)
    {
      cairo_close_path (_SELF);
      cr_check_status (_SELF);
      return self;
    }

copy_page() click to toggle source

    static VALUE
    cr_copy_page (VALUE self)
    {
      cairo_copy_page (_SELF);
      cr_check_status (_SELF);
      return self;
    }

copy_path() click to toggle source

Paths

    static VALUE
    cr_copy_path (VALUE self)
    {
      cairo_path_t *path;

      path = cairo_copy_path (_SELF);
      rb_cairo_check_status (path->status);
      return CRPATH2RVAL (path);
    }

copy_path_flat() click to toggle source

    static VALUE
    cr_copy_path_flat (VALUE self)
    {
      cairo_path_t *path;

      path =  cairo_copy_path_flat (_SELF);
      rb_cairo_check_status (path->status);
      return CRPATH2RVAL (path);
    }

current_point() click to toggle source

    static VALUE
    cr_get_current_point (VALUE self)
    {
      double point[2];
      cairo_get_current_point (_SELF, point, point + 1);
      return rb_cairo__float_array (point, 2);
    }

curve_to(*args) click to toggle source

    static VALUE
    cr_curve_to_generic (int argc, VALUE *argv, VALUE self)
    {
      VALUE x1, y1, x2, y2, x3, y3;

      rb_scan_args (argc, argv, "42", &x1, &y1, &x2, &y2, &x3, &y3);

      if (!(argc == 4 || argc == 6))
        {
          VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
          rb_raise (rb_eArgError,
                    "invalid argument: %s (expect "
                    "(x1, y1, x2, y2) (quadratic) or "
                    "(x1, y1, x2, y2, x3, y3) (cubic))",
                    StringValuePtr (inspected_arg));
        }

      if (argc == 4)
        return cr_quadratic_curve_to (self, x1, y1, x2, y2);
      else
        return cr_curve_to (self, x1, y1, x2, y2, x3, y3);
    }

dash() click to toggle source

    static VALUE
    cr_get_dash (VALUE self)
    {
      int count;
      double *dashes, offset;

      count = cairo_get_dash_count (_SELF);
      dashes = ALLOCA_N (double, count);
      cairo_get_dash (_SELF, dashes, &offset);

      return rb_ary_new3 (2,
                          rb_cairo__float_array (dashes, count),
                          rb_float_new (offset));
    }

dash_count() click to toggle source

    static VALUE
    cr_get_dash_count (VALUE self)
    {
      return INT2NUM (cairo_get_dash_count (_SELF));
    }

destroy() click to toggle source

    static VALUE
    cr_destroy (VALUE self)
    {
      cairo_t *cr;

      cr = _SELF;
      cr_set_user_data (cr, &cr_object_holder_key, NULL, NULL);
      cairo_destroy (cr);

      DATA_PTR (self) = NULL;
      return Qnil;
    }

device_to_user(p1, p2) click to toggle source

    static VALUE
    cr_device_to_user (VALUE self, VALUE x, VALUE y)
    {
      double pair[2];
      pair[0] = NUM2DBL (x);
      pair[1] = NUM2DBL (y);
      cairo_device_to_user (_SELF, pair, pair + 1);
      cr_check_status (_SELF);
      return rb_cairo__float_array (pair, 2);
    }

device_to_user_distance(p1, p2) click to toggle source

    static VALUE
    cr_device_to_user_distance (VALUE self, VALUE dx, VALUE dy)
    {
      double pair[2];
      pair[0] = NUM2DBL (dx);
      pair[1] = NUM2DBL (dy);
      cairo_device_to_user_distance (_SELF, pair, pair + 1);
      cr_check_status (_SELF);
      return rb_cairo__float_array (pair, 2);
    }

fill(*args) click to toggle source

    static VALUE
    cr_fill (int argc, VALUE *argv, VALUE self)
    {
      VALUE preserve;

      rb_scan_args (argc, argv, "01", &preserve);

      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }

      if (RVAL2CBOOL (preserve))
        cairo_fill_preserve (_SELF);
      else
        cairo_fill (_SELF);

      cr_check_status (_SELF);
      return self;
    }

fill_extents() click to toggle source

    static VALUE
    cr_fill_extents (VALUE self)
    {
      double extents[4];
      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }
      cairo_fill_extents (_SELF, extents, extents + 1, extents + 2, extents + 3);
      return rb_cairo__float_array (extents, 4);
    }

fill_preserve(&block) click to toggle source

    # File lib/cairo/context.rb, line 21
    def fill_preserve(&block)
      fill(true, &block)
    end

fill_rule() click to toggle source

    static VALUE
    cr_get_fill_rule (VALUE self)
    {
      return INT2FIX (cairo_get_fill_rule (_SELF));
    }

font_extents() click to toggle source

    static VALUE
    cr_font_extents (VALUE self)
    {
      cairo_font_extents_t extents;
      cairo_font_extents (_SELF, &extents);
      cr_check_status (_SELF);
      return CRFONTEXTENTS2RVAL (&extents);
    }

font_face() click to toggle source

    static VALUE
    cr_get_font_face (VALUE self)
    {
      cairo_font_face_t *face;

      face = cairo_get_font_face (_SELF);
      rb_cairo_check_status (cairo_font_face_status (face));
      return CRFONTFACE2RVAL (face);
    }

font_matrix() click to toggle source

    static VALUE
    cr_get_font_matrix (VALUE self)
    {
      cairo_matrix_t matrix;
      cairo_get_font_matrix (_SELF, &matrix);
      cr_check_status (_SELF);
      return CRMATRIX2RVAL (&matrix);
    }

font_options() click to toggle source

    static VALUE
    cr_get_font_options (VALUE self)
    {
      cairo_font_options_t *options = cairo_font_options_create ();
      rb_cairo_check_status (cairo_font_options_status (options));
      cairo_get_font_options (_SELF, options);
      rb_cairo_check_status (cairo_font_options_status (options));
      return CRFONTOPTIONS2RVAL (options);
    }

glyph_extents(p1) click to toggle source

    static VALUE
    cr_glyph_extents (VALUE self, VALUE rb_glyphs)
    {
      cairo_text_extents_t extents;
      cairo_glyph_t *glyphs;
      int length;

      RB_CAIRO__GLYPHS_TO_ARRAY (rb_glyphs, glyphs, length);
      cairo_glyph_extents (_SELF, glyphs, length, &extents);
      cr_check_status (_SELF);
      return CRTEXTEXTENTS2RVAL (&extents);
    }

glyph_path(p1) click to toggle source

    static VALUE
    cr_glyph_path (VALUE self, VALUE rb_glyphs)
    {
      int count;
      cairo_glyph_t *glyphs;

      RB_CAIRO__GLYPHS_TO_ARRAY (rb_glyphs, glyphs, count);
      cairo_glyph_path (_SELF, glyphs, count);
      cr_check_status (_SELF);

      return self;
    }

group_target() click to toggle source

    static VALUE
    cr_get_group_target (VALUE self)
    {
      cairo_surface_t *surface;

      surface = cairo_get_group_target (_SELF);
      if (!surface)
        return Qnil;
      rb_cairo_check_status (cairo_surface_status (surface));
      return CRSURFACE2RVAL (surface);
    }

has_current_point?()
Alias for: have_current_point?
have_current_point?() click to toggle source

    static VALUE
    cr_has_current_point(VALUE self)
    {
      return RTEST (cairo_has_current_point (_SELF));
    }

Also aliased as: has_current_point?
identity_matrix() click to toggle source

    static VALUE
    cr_identity_matrix (VALUE self)
    {
      cairo_identity_matrix (_SELF);
      cr_check_status (_SELF);
      return self;
    }

in_clip?(p1, p2) click to toggle source

    static VALUE
    cr_in_clip (VALUE self, VALUE x, VALUE y)
    {
      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }
      return CBOOL2RVAL (cairo_in_clip (_SELF, NUM2DBL (x), NUM2DBL (y)));
    }

in_fill?(p1, p2) click to toggle source

    static VALUE
    cr_in_fill (VALUE self, VALUE x, VALUE y)
    {
      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }
      return CBOOL2RVAL (cairo_in_fill (_SELF, NUM2DBL (x), NUM2DBL (y)));
    }

in_stroke?(p1, p2) click to toggle source

Insideness testing

    static VALUE
    cr_in_stroke (VALUE self, VALUE x, VALUE y)
    {
      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }
      return CBOOL2RVAL (cairo_in_stroke (_SELF, NUM2DBL (x), NUM2DBL (y)));
    }

line_cap() click to toggle source

    static VALUE
    cr_get_line_cap (VALUE self)
    {
      return INT2FIX (cairo_get_line_cap (_SELF));
    }

line_join() click to toggle source

    static VALUE
    cr_get_line_join (VALUE self)
    {
      return INT2FIX (cairo_get_line_join (_SELF));
    }

line_to(p1, p2) click to toggle source

    static VALUE
    cr_line_to (VALUE self, VALUE x, VALUE y)
    {
      cairo_line_to (_SELF, NUM2DBL (x), NUM2DBL (y));
      cr_check_status (_SELF);
      return self;
    }

line_width() click to toggle source

    static VALUE
    cr_get_line_width (VALUE self)
    {
      return rb_float_new (cairo_get_line_width (_SELF));
    }

mask(*args) click to toggle source

    static VALUE
    cr_mask_generic (int argc, VALUE *argv, VALUE self)
    {
      VALUE arg1, arg2, arg3;
      int n;

      n = rb_scan_args (argc, argv, "12", &arg1, &arg2, &arg3);

      if (n == 1)
        {
          return cr_mask (self, arg1);
        }
      else if (n == 3)
        {
          return cr_mask_surface (self, arg1, arg2, arg3);
        }
      else
        {
          rb_raise (rb_eArgError,
                    "invalid argument (expect (pattern) or (surface, x, y))");
        }
    }

matrix() click to toggle source

    static VALUE
    cr_get_matrix (VALUE self)
    {
      cairo_matrix_t matrix;
      cairo_get_matrix (_SELF, &matrix);
      cr_check_status (_SELF);
      return CRMATRIX2RVAL (&matrix);
    }

miter_limit() click to toggle source

    static VALUE
    cr_get_miter_limit (VALUE self)
    {
      return rb_float_new (cairo_get_miter_limit (_SELF));
    }

move_to(p1, p2) click to toggle source

    static VALUE
    cr_move_to (VALUE self, VALUE x, VALUE y)
    {
      cairo_move_to (_SELF, NUM2DBL (x), NUM2DBL (y));
      cr_check_status (_SELF);
      return self;
    }

new_path() click to toggle source

Path creation functions

    static VALUE
    cr_new_path (VALUE self)
    {
      cairo_new_path (_SELF);
      cr_check_status (_SELF);
      return self;
    }

new_sub_path() click to toggle source

    static VALUE
    cr_new_sub_path (VALUE self)
    {
      cairo_new_sub_path (_SELF);
      cr_check_status (_SELF);
      return self;
    }

operator() click to toggle source

Query functions

    static VALUE
    cr_get_operator (VALUE self)
    {
      return INT2FIX (cairo_get_operator (_SELF));
    }

paint(*args) click to toggle source

    static VALUE
    cr_paint_generic (int argc, VALUE *argv, VALUE self)
    {
      VALUE alpha;
      int n;

      n = rb_scan_args (argc, argv, "01", &alpha);

      if (n == 0 || (n == 1 && NIL_P (alpha)))
        {
          return cr_paint (self);
        }
      if (n == 1)
        {
          return cr_paint_with_alpha (self, alpha);
        }
      else
        {
          rb_raise (rb_eArgError,
                    "invalid argument (expect () or (alpha))");
        }
    }

path_extents() click to toggle source

    static VALUE
    cr_path_extents (VALUE self)
    {
      double x1, y1, x2, y2;
      cairo_path_extents (_SELF, &x1, &y1, &x2, &y2);
      cr_check_status (_SELF);
      return rb_ary_new3 (4,
                          rb_float_new(x1), rb_float_new(y1),
                          rb_float_new(x2), rb_float_new(y2));
    }

pop_group(*args) click to toggle source

    static VALUE
    cr_pop_group_generic (int argc, VALUE *argv, VALUE self)
    {
      VALUE to_source;
      rb_scan_args (argc, argv, "01", &to_source);
      if (RVAL2CBOOL (to_source))
        return cr_pop_group_to_source (self);
      else
        return cr_pop_group (self);
    }

pop_group_to_source() click to toggle source

    static VALUE
    cr_pop_group_to_source (VALUE self)
    {
      cairo_pop_group_to_source (_SELF);
      cr_check_status (_SELF);
      return Qnil;
    }

push_group(*args) click to toggle source

    static VALUE
    cr_push_group (int argc, VALUE *argv, VALUE self)
    {
      VALUE result = Qnil;
      VALUE content, pop_to_source;
      rb_scan_args (argc, argv, "02", &content, &pop_to_source);

      if (NIL_P(content))
        cairo_push_group (_SELF);
      else
        cairo_push_group_with_content (_SELF, RVAL2CRCONTENT(content));
      cr_check_status (_SELF);

      if (rb_block_given_p ())
        {
          int state = 0;

          if (NIL_P (pop_to_source))
            pop_to_source = Qtrue;

          result = rb_protect (rb_yield, self, &state);
          if (cairo_status(_SELF) == CAIRO_STATUS_SUCCESS)
            {
              if (RVAL2CBOOL (pop_to_source))
                cr_pop_group_to_source (self);
              else
                result = cr_pop_group (self);
            }

          if (state)
            rb_jump_tag (state);
        }

      return result;
    }

rectangle(p1, p2, p3, p4) click to toggle source

    static VALUE
    cr_rectangle (VALUE self, VALUE x, VALUE y, VALUE width, VALUE height)
    {
      cairo_rectangle (_SELF, NUM2DBL (x), NUM2DBL (y),
                       NUM2DBL (width), NUM2DBL (height));
      cr_check_status (_SELF);
      return self;
    }

rel_curve_to(*args) click to toggle source

    static VALUE
    cr_rel_curve_to_generic (int argc, VALUE *argv, VALUE self)
    {
      VALUE dx1, dy1, dx2, dy2, dx3, dy3;

      rb_scan_args (argc, argv, "42", &dx1, &dy1, &dx2, &dy2, &dx3, &dy3);

      if (!(argc == 4 || argc == 6))
        {
          VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
          rb_raise (rb_eArgError,
                    "invalid argument: %s (expect "
                    "(dx1, dy1, dx2, dy2) (quadratic) or "
                    "(dx1, dy1, dx2, dy2, dx3, dy3) (cubic))",
                    StringValuePtr (inspected_arg));
        }

      if (argc == 4)
        return cr_rel_quadratic_curve_to (self, dx1, dy1, dx2, dy2);
      else
        return cr_rel_curve_to (self, dx1, dy1, dx2, dy2, dx3, dy3);
    }

rel_line_to(p1, p2) click to toggle source

    static VALUE
    cr_rel_line_to (VALUE self, VALUE x, VALUE y)
    {
      cairo_rel_line_to (_SELF, NUM2DBL (x), NUM2DBL (y));
      cr_check_status (_SELF);
      return self;
    }

rel_move_to(p1, p2) click to toggle source

    static VALUE
    cr_rel_move_to (VALUE self, VALUE x, VALUE y)
    {
      cairo_rel_move_to (_SELF, NUM2DBL (x), NUM2DBL (y));
      cr_check_status (_SELF);
      return self;
    }

reset_clip() click to toggle source

Clipping

    static VALUE
    cr_reset_clip (VALUE self)
    {
      cairo_reset_clip (_SELF);
      cr_check_status (_SELF);
      return self;
    }

restore() click to toggle source

    static VALUE
    cr_restore (VALUE self)
    {
      cairo_restore (_SELF);
      cr_check_status (_SELF);
      return Qnil;
    }

rotate(p1) click to toggle source

    static VALUE
    cr_rotate (VALUE self, VALUE radians)
    {
      cairo_rotate (_SELF, NUM2DBL (radians));
      cr_check_status (_SELF);
      return self;
    }

save() click to toggle source

    static VALUE
    cr_save (VALUE self)
    {
      VALUE result = Qnil;
      cairo_save (_SELF);
      cr_check_status (_SELF);
      if (rb_block_given_p ())
        {
          result = rb_ensure (rb_yield, self, cr_restore, self);
        }
      return result;
    }

scale(p1, p2) click to toggle source

    static VALUE
    cr_scale (VALUE self, VALUE sx, VALUE sy)
    {
      cairo_scale (_SELF, NUM2DBL (sx), NUM2DBL (sy));
      cr_check_status (_SELF);
      return self;
    }

scaled_font() click to toggle source

    static VALUE
    cr_get_scaled_font (VALUE self)
    {
      return CRSCALEDFONT2RVAL (cairo_get_scaled_font (_SELF));
    }

select_font_face(p1 = v1, p2 = v2, p3 = v3) click to toggle source

Font/Text functions

    static   VALUE
    cr_select_font_face (int argc, VALUE *argv, VALUE self)
    {
      VALUE rb_family, rb_slant, rb_weight;
      const char *family;
      cairo_font_slant_t slant;
      cairo_font_weight_t weight;

      rb_scan_args(argc, argv, "03", &rb_family, &rb_slant, &rb_weight);

      if (NIL_P (rb_family))
        {
          family = "";
        }
      else if (rb_cairo__is_kind_of (rb_family, rb_cString))
        {
          family = RSTRING_PTR (rb_family);
        }
      else if (rb_cairo__is_kind_of (rb_family, rb_cSymbol))
        {
          family = rb_id2name (SYM2ID (rb_family));
        }
      else
        {
          rb_raise (rb_eArgError,
                    "family name should be nil, String or Symbol: %s",
                    rb_cairo__inspect (rb_family));
        }

      if (NIL_P (rb_slant))
        slant = CAIRO_FONT_SLANT_NORMAL;
      else
        slant = RVAL2CRFONTSLANT (rb_slant);

      if (NIL_P (rb_weight))
        weight = CAIRO_FONT_WEIGHT_NORMAL;
      else
        weight = RVAL2CRFONTWEIGHT (rb_weight);

      cairo_select_font_face (_SELF, family, slant, weight);
      cr_check_status (_SELF);
      return self;
    }

set_antialias(p1) click to toggle source

    static VALUE
    cr_set_antialias(VALUE self, VALUE antialias)
    {
      cairo_set_antialias(_SELF, RVAL2CRANTIALIAS (antialias));
      cr_check_status(_SELF);
      return self;
    }

set_dash(p1, p2 = v2) click to toggle source

    static VALUE
    cr_set_dash (int argc, VALUE *argv, VALUE self)
    {
      VALUE dash_array, rb_offset;
      double offset;
      cairo_bool_t is_num;

      rb_scan_args(argc, argv, "11", &dash_array, &rb_offset);

      is_num = rb_cairo__is_kind_of (dash_array, rb_cNumeric);
      if (!(NIL_P (dash_array) || is_num))
        {
          Check_Type (dash_array, T_ARRAY);
        }

      if (NIL_P (rb_offset))
        offset = 0.0;
      else
        offset = NUM2DBL (rb_offset);

      if (is_num)
        {
          double values[1];
          values[0] = NUM2DBL (dash_array);
          cairo_set_dash (_SELF, values, 1, offset);
        }
      else if (NIL_P (dash_array) || RARRAY_LEN (dash_array) == 0)
        {
          cairo_set_dash (_SELF, NULL, 0, offset);
        }
      else
        {
          int i, length;
          double *values;
          length = RARRAY_LEN (dash_array);
          values = ALLOCA_N (double, length);
          if (!values)
            {
              rb_cairo_check_status (CAIRO_STATUS_NO_MEMORY);
            }
          for (i = 0; i < length; i++)
            {
              values[i] = NUM2DBL (RARRAY_PTR (dash_array)[i]);
            }
          cairo_set_dash (_SELF, values, length, offset);
        }

      cr_check_status (_SELF);
      return self;
    }

set_fill_rule(p1) click to toggle source

    static VALUE
    cr_set_fill_rule (VALUE self, VALUE rule)
    {
      cairo_set_fill_rule (_SELF, RVAL2CRFILLRULE (rule));
      cr_check_status (_SELF);
      return self;
    }

set_font_face(p1) click to toggle source

    static VALUE
    cr_set_font_face (VALUE self, VALUE face)
    {
      cairo_set_font_face (_SELF, NIL_P (face) ? NULL : RVAL2CRFONTFACE (face));
      cr_check_status (_SELF);
      return self;
    }

set_font_matrix(p1) click to toggle source

    static VALUE
    cr_set_font_matrix (VALUE self, VALUE matrix)
    {
      cairo_set_font_matrix (_SELF, RVAL2CRMATRIX (matrix));
      cr_check_status (_SELF);
      return self;
    }

set_font_options(p1) click to toggle source

    static VALUE
    cr_set_font_options (VALUE self, VALUE options)
    {
      cairo_set_font_options (_SELF, RVAL2CRFONTOPTIONS (options));
      cr_check_status (_SELF);
      return self;
    }

set_font_size(p1) click to toggle source

    static VALUE
    cr_set_font_size (VALUE self, VALUE scale)
    {
      cairo_set_font_size (_SELF, NUM2DBL (scale));
      cr_check_status (_SELF);
      return self;
    }

set_line_cap(p1) click to toggle source

    static VALUE
    cr_set_line_cap (VALUE self, VALUE cap)
    {
      cairo_set_line_cap (_SELF, RVAL2CRLINECAP (cap));
      cr_check_status (_SELF);
      return self;
    }

set_line_join(p1) click to toggle source

    static VALUE
    cr_set_line_join (VALUE self, VALUE join)
    {
      cairo_set_line_join (_SELF, RVAL2CRLINEJOIN (join));
      cr_check_status (_SELF);
      return self;
    }

set_line_width(p1) click to toggle source

    static VALUE
    cr_set_line_width (VALUE self, VALUE width)
    {
      cairo_set_line_width (_SELF, NUM2DBL (width));
      return self;
    }

set_matrix(p1) click to toggle source

    static VALUE
    cr_set_matrix (VALUE self, VALUE matrix)
    {
      cairo_set_matrix (_SELF, RVAL2CRMATRIX (matrix));
      cr_check_status (_SELF);
      return self;
    }

set_miter_limit(p1) click to toggle source

    static VALUE
    cr_set_miter_limit (VALUE self, VALUE limit)
    {
      cairo_set_miter_limit (_SELF, NUM2DBL (limit));
      cr_check_status (_SELF);
      return self;
    }

set_operator(p1) click to toggle source

Modify state

    static VALUE
    cr_set_operator (VALUE self, VALUE operator)
    {
      cairo_set_operator (_SELF, RVAL2CROPERATOR (operator));
      cr_check_status (_SELF);
      return self;
    }

set_scaled_font(p1) click to toggle source

    static VALUE
    cr_set_scaled_font (VALUE self, VALUE scaled_font)
    {
      cairo_set_scaled_font (_SELF, RVAL2CRSCALEDFONT (scaled_font));
      cr_check_status (_SELF);
      return self;
    }

set_source(*args) click to toggle source

    static VALUE
    cr_set_source_generic (int argc, VALUE *argv, VALUE self)
    {
      VALUE arg1, arg2, arg3, arg4;
      int n;

      n = rb_scan_args (argc, argv, "13", &arg1, &arg2, &arg3, &arg4);

      if (n == 1 && rb_cairo__is_kind_of (arg1, rb_cArray))
        {
          return cr_set_source_rgba (argc, argv, self);
        }
      else if (n == 1 && rb_cairo__is_kind_of (arg1, rb_cCairo_Surface))
        {
          return cr_set_source_surface (self, arg1,
                                        rb_float_new (0),
                                        rb_float_new (0));
        }
      else if (n == 1)
        {
          return cr_set_source (self, arg1);
        }
      else if (n == 3 && rb_cairo__is_kind_of (arg1, rb_cCairo_Surface))
        {
          return cr_set_source_surface (self, arg1, arg2, arg3);
        }
      else if (n == 3 || n == 4)
        {
          return cr_set_source_rgba (argc, argv, self);
        }
      else
        {
          rb_raise (rb_eArgError,
                    "invalid argument (expect "
                    "(red, green, blue), (red, green, blue, alpha), "
                    "([red, green, blue]), ([red, green, blue, alpha]), "
                    "(surface), (pattern) or (surface, x, y))");
        }
    }

set_source_rgb(*args) click to toggle source

    static VALUE
    cr_set_source_rgb (int argc, VALUE *argv, VALUE self)
    {
      VALUE red, green, blue;
      int n;

      n = rb_scan_args (argc, argv, "12", &red, &green, &blue);

      if (n == 1 && rb_cairo__is_kind_of (red, rb_cArray))
        {
          VALUE ary = red;
          n = RARRAY_LEN (ary);
          red = rb_ary_entry (ary, 0);
          green = rb_ary_entry (ary, 1);
          blue = rb_ary_entry (ary, 2);
        }

      if (n == 3)
        {
          cairo_set_source_rgb (_SELF,
                                NUM2DBL (red),
                                NUM2DBL (green),
                                NUM2DBL (blue));
        }
      else
        {
          VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
          rb_raise (rb_eArgError,
                    "invalid RGB: %s (expect "
                    "(red, green, blue) or ([red, green, blue]))",
                    StringValuePtr (inspected_arg));
        }
      cr_check_status (_SELF);
      rb_ivar_set (self, cr_id_source, Qnil);
      return self;
    }

set_source_rgba(*args) click to toggle source

    static VALUE
    cr_set_source_rgba (int argc, VALUE *argv, VALUE self)
    {
      VALUE red, green, blue, alpha;
      int n;

      n = rb_scan_args (argc, argv, "13", &red, &green, &blue, &alpha);

      if (n == 1 && rb_cairo__is_kind_of (red, rb_cArray))
        {
          VALUE ary = red;
          n = RARRAY_LEN (ary);
          red = rb_ary_entry (ary, 0);
          green = rb_ary_entry (ary, 1);
          blue = rb_ary_entry (ary, 2);
          alpha = rb_ary_entry (ary, 3);
        }

      if (n == 3)
        {
          cairo_set_source_rgb (_SELF,
                                NUM2DBL (red),
                                NUM2DBL (green),
                                NUM2DBL (blue));
        }
      else if (n == 4)
        {
          cairo_set_source_rgba (_SELF,
                                 NUM2DBL (red),
                                 NUM2DBL (green),
                                 NUM2DBL (blue),
                                 NUM2DBL (alpha));
        }
      else
        {
          VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
          rb_raise (rb_eArgError,
                    "invalid RGB%s: %s (expect "
                    "(red, green, blue), (red, green, blue, alpha), "
                    "([red, green, blue]) or ([red, green, blue, alpha]))",
                    n == 4 ? "A" : "",
                    StringValuePtr (inspected_arg));
        }
      cr_check_status (_SELF);
      rb_ivar_set (self, cr_id_source, Qnil);
      return self;
    }

set_tolerance(p1) click to toggle source

    static VALUE
    cr_set_tolerance (VALUE self, VALUE tolerance)
    {
      cairo_set_tolerance (_SELF, NUM2DBL (tolerance));
      cr_check_status (_SELF);
      return self;
    }

show_glyphs(p1) click to toggle source

    static VALUE
    cr_show_glyphs (VALUE self, VALUE rb_glyphs)
    {
      int count;
      cairo_glyph_t *glyphs;

      RB_CAIRO__GLYPHS_TO_ARRAY (rb_glyphs, glyphs, count);
      cairo_show_glyphs (_SELF, glyphs, count);
      cr_check_status (_SELF);
      return self;
    }

show_page() click to toggle source

    static VALUE
    cr_show_page (VALUE self)
    {
      cairo_show_page (_SELF);
      cr_check_status (_SELF);
      return self;
    }

show_text(p1) click to toggle source

    static VALUE
    cr_show_text (VALUE self, VALUE utf8)
    {
      cairo_show_text (_SELF, RVAL2CSTR (utf8));
      cr_check_status (_SELF);
      return self;
    }

show_text_glyphs(p1, p2, p3, p4) click to toggle source

    static VALUE
    cr_show_text_glyphs (VALUE self, VALUE rb_utf8, VALUE rb_glyphs,
                         VALUE rb_clusters, VALUE rb_cluster_flags)
    {
      cairo_t *cr;
      const char *utf8;
      int utf8_len;
      cairo_glyph_t *glyphs = NULL;
      int num_glyphs = 0;
      cairo_text_cluster_t *clusters = NULL;
      int num_clusters = 0;
      cairo_text_cluster_flags_t cluster_flags;

      cr = _SELF;
      utf8 = RSTRING_PTR (rb_utf8);
      utf8_len = RSTRING_LEN (rb_utf8);
      rb_cairo__glyphs_from_ruby_object (rb_glyphs, &glyphs, &num_glyphs);
      rb_cairo__text_clusters_from_ruby_object (rb_clusters,
                                                &clusters, &num_clusters);
      cluster_flags = RVAL2CRTEXTCLUSTERFLAGS (rb_cluster_flags);
      cairo_show_text_glyphs (cr, utf8, utf8_len,
                              glyphs, num_glyphs,
                              clusters, num_clusters,
                              cluster_flags);
      if (glyphs)
        cairo_glyph_free (glyphs);
      if (clusters)
        cairo_text_cluster_free (clusters);

      return self;
    }

source() click to toggle source

    static VALUE
    cr_get_source (VALUE self)
    {
      VALUE rb_source = Qnil;
      cairo_pattern_t *source;
      source = cairo_get_source (_SELF);

      if (source)
        {
          rb_cairo_check_status (cairo_pattern_status (source));
          rb_source = rb_ivar_get (self, cr_id_source);
          if (NIL_P (rb_source) || RVAL2CRPATTERN (rb_source) != source)
            {
              rb_source = CRPATTERN2RVAL (source);
              rb_ivar_set (self, cr_id_source, rb_source);
            }
        }
      else
        {
          rb_source = Qnil;
          rb_ivar_set (self, cr_id_source, rb_source);
        }

      return rb_source;
    }

stroke(*args) click to toggle source

    static VALUE
    cr_stroke (int argc, VALUE *argv, VALUE self)
    {
      VALUE preserve;

      rb_scan_args (argc, argv, "01", &preserve);

      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }

      if (RVAL2CBOOL (preserve))
        cairo_stroke_preserve (_SELF);
      else
        cairo_stroke (_SELF);

      cr_check_status (_SELF);
      return self;
    }

stroke_extents() click to toggle source

Rectangular extents

    static VALUE
    cr_stroke_extents (VALUE self)
    {
      double extents[4];
      if (rb_block_given_p ())
        {
          cr_new_path (self);
          rb_yield (self);
        }
      cairo_stroke_extents (_SELF, extents, extents + 1, extents + 2, extents + 3);
      return rb_cairo__float_array (extents, 4);
    }

stroke_preserve(&block) click to toggle source

    # File lib/cairo/context.rb, line 17
    def stroke_preserve(&block)
      stroke(true, &block)
    end

target() click to toggle source

    static VALUE
    cr_get_target (VALUE self)
    {
      cairo_surface_t *surface;
      VALUE rb_surface = Qnil;

      surface = cairo_get_target (_SELF);
      rb_cairo_check_status (cairo_surface_status (surface));

      if (RTEST (rb_ivar_defined (self, cr_id_surface)))
          rb_surface = rb_ivar_get (self, cr_id_surface);
      if (NIL_P (rb_surface) || RVAL2CRSURFACE (rb_surface) != surface)
        {
          rb_surface = CRSURFACE2RVAL (surface);
          rb_ivar_set (self, cr_id_surface, rb_surface);
        }

      return rb_surface;
    }

text_extents(p1) click to toggle source

    static VALUE
    cr_text_extents (VALUE self, VALUE utf8)
    {
      cairo_text_extents_t extents;
      cairo_text_extents (_SELF, StringValuePtr (utf8), &extents);
      cr_check_status (_SELF);
      return CRTEXTEXTENTS2RVAL (&extents);
    }

text_path(p1) click to toggle source

    static VALUE
    cr_text_path (VALUE self, VALUE utf8)
    {
      cairo_text_path (_SELF, StringValuePtr (utf8));
      cr_check_status (_SELF);
      return self;
    }

to_ptr() click to toggle source

    static VALUE
    cr_to_ptr (VALUE self)
    {
      if (NIL_P (rb_cairo__cFFIPointer))
        return Qnil;

      return rb_funcall (rb_cairo__cFFIPointer, rb_intern ("new"),
                         1, PTR2NUM (_SELF));
    }

tolerance() click to toggle source

    static VALUE
    cr_get_tolerance (VALUE self)
    {
      return rb_float_new (cairo_get_tolerance (_SELF));
    }

transform(p1) click to toggle source

    static VALUE
    cr_transform (VALUE self, VALUE matrix)
    {
      cairo_transform (_SELF, RVAL2CRMATRIX (matrix));
      cr_check_status (_SELF);
      return self;
    }

translate(p1, p2) click to toggle source

    static VALUE
    cr_translate (VALUE self, VALUE tx, VALUE ty)
    {
      cairo_translate (_SELF, NUM2DBL (tx), NUM2DBL (ty));
      cr_check_status (_SELF);
      return self;
    }

user_to_device(p1, p2) click to toggle source

    static VALUE
    cr_user_to_device (VALUE self, VALUE x, VALUE y)
    {
      double pair[2];
      pair[0] = NUM2DBL (x);
      pair[1] = NUM2DBL (y);
      cairo_user_to_device (_SELF, pair, pair + 1);
      cr_check_status (_SELF);
      return rb_cairo__float_array (pair, 2);
    }

user_to_device_distance(p1, p2) click to toggle source

    static VALUE
    cr_user_to_device_distance (VALUE self, VALUE dx, VALUE dy)
    {
      double pair[2];
      pair[0] = NUM2DBL (dx);
      pair[1] = NUM2DBL (dy);
      cairo_user_to_device_distance (_SELF, pair, pair + 1);
      cr_check_status (_SELF);
      return rb_cairo__float_array (pair, 2);
    }