[cairo-commit] rcairo/src rb_cairo_path.c,1.3,1.4

Kouhei Sutou commit at pdx.freedesktop.org
Wed May 2 19:35:55 PDT 2007


Committed by: kou

Update of /cvs/cairo/rcairo/src
In directory kemper:/tmp/cvs-serv10602/src

Modified Files:
	rb_cairo_path.c 
Log Message:
* src/rb_cairo_path.c
  (Cairo::Path#size, Cairo::Path#length, Cairo::Path#empty?): added.
  (Cairo::Path#[]): supported negative index.


Index: rb_cairo_path.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo_path.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- rb_cairo_path.c	3 May 2007 02:17:31 -0000	1.3
+++ rb_cairo_path.c	3 May 2007 02:35:42 -0000	1.4
@@ -132,27 +132,46 @@
 }
 
 static VALUE
-cr_path_each (VALUE self)
+cr_path_empty_p (VALUE self)
 {
-  cairo_path_t *path = _SELF(self);
-  int i;
+  cairo_path_t *path = _SELF (self);
 
-  for (i = 0; i < path->num_data; i += path->data[i].header.length)
-    {
-      rb_yield (cr_path_data_to_ruby_object (&(path->data[i])));
-    }
+  return path->num_data == 0 ? Qtrue : Qfalse;
+}
 
-  return self;
+static int
+cairo_path_get_size (cairo_path_t *path)
+{
+  int i, size;
+
+  for (i = 0, size = 0; i < path->num_data; i += path->data[i].header.length)
+    size++;
+
+  return size;
+}
+
+static VALUE
+cr_path_size (VALUE self)
+{
+  cairo_path_t *path = _SELF (self);
+  return INT2NUM (cairo_path_get_size (path));
 }
 
 static VALUE
 cr_path_ref (VALUE self, VALUE index)
 {
-  cairo_path_t *path = _SELF(self);
+  cairo_path_t *path = _SELF (self);
   int i, requested_index, real_index;
 
-#warning FIXME: support negative index
-  requested_index = NUM2INT(index);
+  requested_index = NUM2INT (index);
+  if (requested_index < 0)
+    {
+      requested_index += cairo_path_get_size (path);
+      if (requested_index < 0)
+        return Qnil;
+    }
+
+
   for (i = 0, real_index = 0; i < requested_index; i++)
     {
       if (real_index >= path->num_data)
@@ -166,6 +185,20 @@
     return Qnil;
 }
 
+static VALUE
+cr_path_each (VALUE self)
+{
+  cairo_path_t *path = _SELF(self);
+  int i;
+
+  for (i = 0; i < path->num_data; i += path->data[i].header.length)
+    {
+      rb_yield (cr_path_data_to_ruby_object (&(path->data[i])));
+    }
+
+  return self;
+}
+
 void
 Init_cairo_path (void)
 {
@@ -207,6 +240,9 @@
 
   rb_define_method (rb_cCairo_Path, "initialize", cr_path_initialize, 0);
 
+  rb_define_method (rb_cCairo_Path, "empty?", cr_path_empty_p, 0);
+  rb_define_method (rb_cCairo_Path, "size", cr_path_size, 0);
+  rb_define_alias (rb_cCairo_Path, "length", "size");
   rb_define_method (rb_cCairo_Path, "[]", cr_path_ref, 1);
 
   rb_define_method (rb_cCairo_Path, "each", cr_path_each, 0);



More information about the cairo-commit mailing list