[perl-Cairo] Make Cairo::Surface->create_similar a class-static method



commit ad64cbd73aeeebb3f535322612f1283f9bb837f4
Author: Torsten SchÃnfeld <kaffeetisch gmx de>
Date:   Sat Aug 6 11:56:25 2011 +0200

    Make Cairo::Surface->create_similar a class-static method
    
    That is, make it callable as
    
      Cairo::Surface->create_similar ($other, ...)
    
    in addition to
    
      $other->create_similar (...)
    
    Since cairo_surface_create_similar() is a constructor, the former syntax
    seems to make more sense.  The latter syntax is still supported.

 CairoSurface.xs  |   30 ++++++++++++++++++++++++++----
 lib/Cairo.pm     |    8 +++++++-
 t/CairoSurface.t |   25 +++++++++++++++++++------
 3 files changed, 52 insertions(+), 11 deletions(-)
---
diff --git a/CairoSurface.xs b/CairoSurface.xs
index eedcdf5..59a4b06 100644
--- a/CairoSurface.xs
+++ b/CairoSurface.xs
@@ -293,14 +293,36 @@ void DESTROY (cairo_surface_t * surface);
     CODE:
 	cairo_surface_destroy (surface);
 
-cairo_surface_t_noinc * cairo_surface_create_similar (cairo_surface_t * other, cairo_content_t content, int width, int height);
-    POSTCALL:
+cairo_surface_t_noinc *
+cairo_surface_create_similar (...)
+    PREINIT:
+	int offset = 0;
+	cairo_surface_t * other = NULL;
+	cairo_content_t content = 0;
+	int width = 0;
+	int height = 0;
+    CODE:
+	if (items == 4) {
+		offset = 0;
+	} else if (items == 5) {
+		offset = 1;
+	} else {
+		croak ("Usage: Cairo::Surface->create_similar ($other, $content, $width, $height)\n"
+		       " -or-: $other->create_similar ($content, $width, $height)");
+	}
+	other = SvCairoSurface (ST (0 + offset));
+	content = SvCairoContent (ST (1 + offset));
+	width = SvIV (ST (2 + offset));
+	height = SvIV (ST (3 + offset));
+	RETVAL = cairo_surface_create_similar (other, content, width, height);
 #if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 2, 0)
-    {
+	{
 	const char *package = cairo_perl_package_table_lookup (other);
 	cairo_perl_package_table_insert (RETVAL, package ? package : "Cairo::Surface");
-    }
+	}
 #endif
+    OUTPUT:
+	RETVAL
 
 void cairo_surface_finish (cairo_surface_t *surface);
 
diff --git a/lib/Cairo.pm b/lib/Cairo.pm
index 164db2d..d83ab85 100644
--- a/lib/Cairo.pm
+++ b/lib/Cairo.pm
@@ -1245,10 +1245,12 @@ from a file:
 
 =over
 
-=item $new = $old->create_similar ($content, $width, $height)
+=item $similar = Cairo::Surface->create_similar ($other, $content, $width, $height)
 
 =over
 
+=item $other: I<Cairo::Surface>
+
 =item $content: I<Cairo::Content>
 
 =item $width: integer
@@ -1257,6 +1259,10 @@ from a file:
 
 =back
 
+For hysterical reasons, you can also use the following syntax:
+
+  $similar = $other->create_similar ($content, $width, $height)
+
 =item $new = Cairo::Surface->create_for_rectangle ($target, $x, $y, $width, $height) [1.10]
 
 =over
diff --git a/t/CairoSurface.t b/t/CairoSurface.t
index af97a2e..846cc1a 100644
--- a/t/CairoSurface.t
+++ b/t/CairoSurface.t
@@ -12,7 +12,7 @@ use warnings;
 
 use Config; # for byteorder
 
-use Test::More tests => 84;
+use Test::More tests => 88;
 
 use constant IMG_WIDTH => 256;
 use constant IMG_HEIGHT => 256;
@@ -72,18 +72,31 @@ is ($surf->get_height, IMG_HEIGHT);
 	}
 }
 
-my $similar = $surf->create_similar ('color', IMG_WIDTH, IMG_HEIGHT);
+my $similar = Cairo::Surface->create_similar ($surf, 'color', IMG_WIDTH, IMG_HEIGHT);
 isa_ok ($similar, 'Cairo::ImageSurface');
 isa_ok ($similar, 'Cairo::Surface');
 
+# Test that create_similar can be called with both conventions.
+{
+	my $similar = $surf->create_similar ('color', IMG_WIDTH, IMG_HEIGHT);
+	isa_ok ($similar, 'Cairo::ImageSurface');
+	isa_ok ($similar, 'Cairo::Surface');
+
+	eval { Cairo::Surface->create_similar (1, 2) };
+	like ($@, qr/Usage/);
+
+	eval { Cairo::Surface->create_similar (1, 2, 3, 4, 5) };
+	like ($@, qr/Usage/);
+}
+
 # Test that the enum wrappers differentiate between color and color-alpha.
 SKIP: {
 	skip 'content tests', 2
 		unless Cairo::VERSION >= Cairo::VERSION_ENCODE (1, 2, 0);
 
-	my $tmp = $surf->create_similar ('color-alpha', IMG_WIDTH, IMG_HEIGHT);
+	my $tmp = Cairo::Surface->create_similar ($surf, 'color-alpha', IMG_WIDTH, IMG_HEIGHT);
 	is ($tmp->get_content, 'color-alpha');
-	$tmp = $surf->create_similar ('color', IMG_WIDTH, IMG_HEIGHT);
+	$tmp = Cairo::Surface->create_similar ($surf, 'color', IMG_WIDTH, IMG_HEIGHT);
 	is ($tmp->get_content, 'color');
 }
 
@@ -218,7 +231,7 @@ SKIP: {
 	}
 
 	# create_similar might return any kind of surface
-	$surf = $surf->create_similar ('alpha', IMG_WIDTH, IMG_HEIGHT);
+	$surf = Cairo::Surface->create_similar ($surf, 'alpha', IMG_WIDTH, IMG_HEIGHT);
 	isa_ok ($surf, 'Cairo::Surface');
 
 	SKIP: {
@@ -282,7 +295,7 @@ SKIP: {
 	}
 
 	# create_similar might return any kind of surface
-	$surf = $surf->create_similar ('alpha', IMG_WIDTH, IMG_HEIGHT);
+	$surf = Cairo::Surface->create_similar ($surf, 'alpha', IMG_WIDTH, IMG_HEIGHT);
 	isa_ok ($surf, 'Cairo::Surface');
 
 	unlink 'tmp.ps';



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]