[cairo-commit] rcairo/src rb_cairo.c, 1.5, 1.6 rb_cairo.h, 1.1, 1.2 rb_cairo_pattern.c, 1.4, 1.5

Kouhei Sutou commit at pdx.freedesktop.org
Tue May 1 06:07:00 PDT 2007


Committed by: kou

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

Modified Files:
	rb_cairo.c rb_cairo.h rb_cairo_pattern.c 
Log Message:
* src/rb_cairo.[ch]: exported Cairo::Color and Cairo::Color::Base.
* src/lib/cairo.rb: require cairo/color before cairo.so to export
  Cairo::Color to cairo.so.

* src/rb_cairo_pattern.c: implemented src/lib/cairo/patter.rb
  methods in C.
* src/lib/cairo/pattern.rb: removed.
* src/lib/cairo.rb: removed cairo/pattern.


Index: rb_cairo.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- rb_cairo.c	1 May 2007 11:49:43 -0000	1.5
+++ rb_cairo.c	1 May 2007 13:06:47 -0000	1.6
@@ -17,7 +17,7 @@
 #include "rb_cairo.h"
 #include "rb_cairo_private.h"
 
-VALUE rb_mCairo;
+VALUE rb_mCairo, rb_mCairo_Color, rb_cCairo_Color_Base;
 
 static ID id__add_one_arg_setter;
 
@@ -60,6 +60,9 @@
                    rb_ary_new3 (4,
                                 INT2FIX (1), INT2FIX (6), INT2FIX (0), Qnil));
 
+  rb_mCairo_Color = rb_const_get (rb_mCairo, rb_intern ("Color"));
+  rb_cCairo_Color_Base = rb_const_get (rb_mCairo_Color, rb_intern ("Base"));
+
   Init_cairo_private ();
   Init_cairo_constants ();
 

Index: rb_cairo.h
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- rb_cairo.h	6 Mar 2007 12:17:34 -0000	1.1
+++ rb_cairo.h	1 May 2007 13:06:47 -0000	1.2
@@ -66,6 +66,10 @@
 RUBY_CAIRO_VAR VALUE rb_cCairo_Glyph;
 RUBY_CAIRO_VAR VALUE rb_cCairo_Surface;
 
+RUBY_CAIRO_VAR VALUE rb_mCairo_Color;
+RUBY_CAIRO_VAR VALUE rb_cCairo_Color_Base;
+
+
 #define RVAL2CRCONTEXT(obj)     (rb_cairo_context_from_ruby_object(obj))
 #define CRCONTEXT2RVAL(cr)      (rb_cairo_context_to_ruby_object(cr))
 

Index: rb_cairo_pattern.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo_pattern.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- rb_cairo_pattern.c	1 May 2007 12:03:21 -0000	1.4
+++ rb_cairo_pattern.c	1 May 2007 13:06:47 -0000	1.5
@@ -13,6 +13,7 @@
  */
 
 #include "rb_cairo.h"
+#include "rb_cairo_private.h"
 
 VALUE rb_cCairo_Pattern;
 VALUE rb_cCairo_SolidPattern;
@@ -21,18 +22,14 @@
 VALUE rb_cCairo_LinearPattern;
 VALUE rb_cCairo_RadialPattern;
 
-static VALUE rb_mCairo_Color = 0;
-static VALUE rb_cCairo_Color_Base = 0;
-static ID id_Color, id_Base, id_parse, id_to_rgb, id_to_a, id_inspect;
+static ID id_parse, id_to_rgb, id_to_a, id_inspect;
 
 #define _SELF(self)  (RVAL2CRPATTERN(self))
 
-static const char *
-cr_inspect (VALUE object)
+static VALUE
+cr_color_parse (VALUE color)
 {
-  VALUE inspected;
-  inspected = rb_funcall (object, id_inspect, 0);
-  return StringValueCStr (inspected);
+  return rb_funcall (rb_mCairo_Color, id_parse, 1, color);
 }
 
 static inline void
@@ -104,20 +101,13 @@
 
   n = rb_scan_args (argc, argv, "13", &red, &green, &blue, &alpha);
 
-  if (!rb_mCairo_Color)
-    rb_mCairo_Color = rb_const_get (rb_mCairo, id_Color);
-
   if (n == 1)
     {
-      VALUE parsed;
-
-      parsed = rb_funcall (rb_mCairo_Color, id_parse, 1, red);
-
-      if (!rb_cCairo_Color_Base)
-        rb_cCairo_Color_Base = rb_const_get (rb_mCairo_Color, id_Base);
+      VALUE color = red;
 
-      if (rb_obj_is_kind_of (parsed, rb_cCairo_Color_Base))
-        red = rb_funcall (rb_funcall (parsed, id_to_rgb, 0), id_to_a, 0);
+      color = cr_color_parse (color);
+      if (rb_obj_is_kind_of (color, rb_cCairo_Color_Base))
+        red = rb_funcall (rb_funcall (color, id_to_rgb, 0), id_to_a, 0);
     }
 
   if (n == 1 && rb_obj_is_kind_of (red, rb_cArray) &&
@@ -147,27 +137,12 @@
     }
   else
     {
-      const char *inspected;
-
-      if (argc == 1)
-        {
-          inspected = cr_inspect (red);
-        }
-      else
-        {
-          int i;
-          VALUE tmp_args;
-
-          tmp_args = rb_ary_new();
-          for (i = 0; i < argc; i++)
-            {
-              rb_ary_push (tmp_args, argv[i]);
-            }
-          inspected = cr_inspect (tmp_args);
-        }
+      VALUE inspected;
 
+      inspected = rb_funcall (argc == 1 ? red : rb_ary_new4 (argc, argv),
+                              id_inspect, 0);
       rb_raise (rb_eArgError,
-                "invalid argument (expect "
+                "invalid argument: %s (expect "
                 "(color_name), "
                 "(color_hex_triplet), "
                 "(Cairo::Color::RGB), "
@@ -177,7 +152,8 @@
                 "([red, green, blue]), "
                 "(red, green, blue, alpha) or "
                 "([red, green, blue, alpha])"
-                "): %s", inspected);
+                ")",
+                RVAL2CSTR (inspected));
     }
 
   cr_pattern_check_status (pattern);
@@ -226,52 +202,22 @@
 
 /* Cairo::GradientPattern */
 static VALUE
-cr_gradient_pattern_add_color_stop_rgb (int argc, VALUE *argv, VALUE self)
+cr_gradient_pattern_add_color_stop_generic (int argc, VALUE *argv, VALUE self)
 {
-  VALUE offset, red, green, blue;
+  VALUE offset, red, green, blue, alpha;
   int n;
 
-  n = rb_scan_args (argc, argv, "22", &offset, &red, &green, &blue);
+  n = rb_scan_args (argc, argv, "23", &offset, &red, &green, &blue, &alpha);
 
-  if (n == 2 && rb_obj_is_kind_of (red, rb_cArray))
+  if (n == 2)
     {
-      VALUE ary = red;
-      n = RARRAY (ary)->len + 1;
-
-      red = rb_ary_entry (ary, 0);
-      green = rb_ary_entry (ary, 1);
-      blue = rb_ary_entry (ary, 2);
-    }
+      VALUE color = red;
 
-  if (n == 4)
-    {
-      cairo_pattern_add_color_stop_rgb (_SELF (self), NUM2DBL (offset),
-                                        NUM2DBL (red), NUM2DBL (green),
-                                        NUM2DBL (blue));
-    }
-  else
-    {
-      VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
-      rb_raise (rb_eArgError,
-                "invalid argument: %s (expect "
-                "(offset, red, green, blue) or "
-                "(offset, [red, green, blue])"
-                ")",
-                StringValuePtr (inspected_arg));
+      color = cr_color_parse (color);
+      if (rb_obj_is_kind_of (color, rb_cCairo_Color_Base))
+        red = rb_funcall (rb_funcall (color, id_to_rgb, 0), id_to_a, 0);
     }
 
-  cr_pattern_check_status (_SELF (self));
-  return self;
-}
-
-static VALUE
-cr_gradient_pattern_add_color_stop_rgba (int argc, VALUE *argv, VALUE self)
-{
-  VALUE offset, red, green, blue, alpha;
-  int n;
-
-  n = rb_scan_args (argc, argv, "23", &offset, &red, &green, &blue, &alpha);
-
   if (n == 2 && rb_obj_is_kind_of (red, rb_cArray))
     {
       VALUE ary = red;
@@ -297,22 +243,28 @@
     }
   else
     {
-      VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
+      VALUE inspected;
+
+      inspected = rb_funcall (rb_ary_new4 (argc, argv), id_inspect, 0);
       rb_raise (rb_eArgError,
                 "invalid argument: %s (expect "
+                "(offset, color_name), "
+                "(offset, color_hex_triplet), "
+                "(offset, Cairo::Color::RGB), "
+                "(offset, Cairo::Color::CMYK), "
+                "(offset, Cairo::Color::HSV), "
                 "(offset, red, green, blue), "
                 "(offset, [red, green, blue]), "
                 "(offset, red, green, blue, alpha) or "
                 "(offset, [red, green, blue, alpha])"
                 ")",
-                StringValuePtr (inspected_arg));
+                RVAL2CSTR (inspected));
     }
 
   cr_pattern_check_status (_SELF (self));
   return self;
 }
 
-
 /* Cairo::Pattern */
 static VALUE
 cr_pattern_set_matrix (VALUE self, VALUE matrix)
@@ -373,6 +325,12 @@
 }
 
 static VALUE
+cr_solid_pattern_get_color (VALUE self)
+{
+  return cr_color_parse (cr_solid_pattern_get_rgba (self));
+}
+
+static VALUE
 cr_surface_pattern_get_surface (VALUE self)
 {
   cairo_surface_t *surface;
@@ -397,6 +355,24 @@
 }
 
 static VALUE
+cr_gradient_pattern_get_color_stop_color (VALUE self, VALUE index)
+{
+  cairo_status_t status;
+  double offset, red, green, blue, alpha;
+
+  status = cairo_pattern_get_color_stop_rgba (_SELF (self), NUM2INT (index),
+                                              &offset, &red, &green, &blue,
+                                              &alpha);
+  rb_cairo_check_status (status);
+  return rb_ary_new3 (2, rb_float_new (offset),
+                      cr_color_parse (rb_ary_new3 (4,
+                                                   rb_float_new (red),
+                                                   rb_float_new (green),
+                                                   rb_float_new (blue),
+                                                   rb_float_new (alpha))));
+}
+
+static VALUE
 cr_gradient_pattern_get_color_stop_count (VALUE self)
 {
   cairo_status_t status;
@@ -442,8 +418,6 @@
 void
 Init_cairo_pattern (void)
 {
-  id_Color = rb_intern ("Color");
-  id_Base = rb_intern ("Base");
   id_parse = rb_intern ("parse");
   id_to_rgb = rb_intern ("to_rgb");
   id_to_a = rb_intern ("to_a");
@@ -475,6 +449,8 @@
 #if CAIRO_CHECK_VERSION(1, 3, 0)
   rb_define_method (rb_cCairo_SolidPattern, "rgba",
                     cr_solid_pattern_get_rgba, 0);
+  rb_define_method (rb_cCairo_SolidPattern, "color",
+                    cr_solid_pattern_get_color, 0);
 #endif
 
   RB_CAIRO_DEF_SETTERS (rb_cCairo_SolidPattern);
@@ -494,15 +470,17 @@
   rb_cCairo_GradientPattern =
     rb_define_class_under (rb_mCairo, "GradientPattern", rb_cCairo_Pattern);
 
-  rb_define_method (rb_cCairo_GradientPattern, "add_color_stop_rgb",
-                    cr_gradient_pattern_add_color_stop_rgb, -1);
-  rb_define_method (rb_cCairo_GradientPattern, "add_color_stop_rgba",
-                    cr_gradient_pattern_add_color_stop_rgba, -1);
+  rb_define_method (rb_cCairo_GradientPattern, "add_color_stop",
+                    cr_gradient_pattern_add_color_stop_generic, -1);
   rb_define_alias (rb_cCairo_GradientPattern,
-                   "add_color_stop", "add_color_stop_rgba");
+                   "add_color_stop_rgb", "add_color_stop");
+  rb_define_alias (rb_cCairo_GradientPattern,
+                   "add_color_stop_rgba", "add_color_stop");
 #if CAIRO_CHECK_VERSION(1, 3, 0)
   rb_define_method (rb_cCairo_GradientPattern, "get_color_stop_rgba",
                     cr_gradient_pattern_get_color_stop_rgba, 1);
+  rb_define_method (rb_cCairo_GradientPattern, "get_color_stop_color",
+                    cr_gradient_pattern_get_color_stop_color, 1);
   rb_define_method (rb_cCairo_GradientPattern, "color_stop_count",
                     cr_gradient_pattern_get_color_stop_count, 0);
 #endif



More information about the cairo-commit mailing list