seed r393 - trunk/modules/canvas



Author: racarr
Date: Sat Dec  6 01:50:30 2008
New Revision: 393
URL: http://svn.gnome.org/viewvc/seed?rev=393&view=rev

Log:
Hey wow a realish canvas implementation. and 6 tests.


Added:
   trunk/modules/canvas/test1.js   (contents, props changed)
   trunk/modules/canvas/test2.js   (contents, props changed)
   trunk/modules/canvas/test3.js   (contents, props changed)
   trunk/modules/canvas/test4.js   (contents, props changed)
   trunk/modules/canvas/test5.js   (contents, props changed)
   trunk/modules/canvas/test6.js   (contents, props changed)
Removed:
   trunk/modules/canvas/example.js
Modified:
   trunk/modules/canvas/Makefile.am
   trunk/modules/canvas/seed-canvas.c

Modified: trunk/modules/canvas/Makefile.am
==============================================================================
--- trunk/modules/canvas/Makefile.am	(original)
+++ trunk/modules/canvas/Makefile.am	Sat Dec  6 01:50:30 2008
@@ -7,7 +7,7 @@
 libsqlite_la_LDFLAGS = \
 	`pkg-config --libs seed cairo`
 
-EXTRA_DIST=example.js
+EXTRA_DIST=test1.js test2.js test3.js test4.js test5.js test6.js
 
 AM_CPPFLAGS = \
 	-I../../libseed/ \

Modified: trunk/modules/canvas/seed-canvas.c
==============================================================================
--- trunk/modules/canvas/seed-canvas.c	(original)
+++ trunk/modules/canvas/seed-canvas.c	Sat Dec  6 01:50:30 2008
@@ -1,5 +1,8 @@
 #include "../../libseed/seed.h"
 #include <cairo.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 
 SeedObject namespace_ref;
 SeedClass canvas_class;
@@ -13,14 +16,20 @@
 								   const SeedValue arguments[],
 								   SeedException * exception)
 {
+	cairo_t * cr;
 	if (argument_count != 1)
 	{
 		seed_make_exception(ctx, exception, "ArgumentError",
 							"Canvas.Canvas constructor expected 1 argument");
 		return (SeedObject)seed_make_null(ctx);
 	}
+
+	cr = seed_pointer_get_pointer(ctx, arguments[0]);
+	
+	cairo_set_source_rgba(cr, 0, 0, 0, 1.0);
+
 	return seed_make_object(ctx, canvas_class, 
-							seed_pointer_get_pointer(ctx, arguments[0]));
+							cr);
 }
 
 SeedValue seed_canvas_save  (SeedContext ctx,
@@ -157,22 +166,60 @@
 	gdouble x, y, width, height;
 	
 	x = seed_value_to_double(ctx, arguments[0], exception);
-	y = seed_value_to_double(ctx, arguments[0], exception);
-    width = seed_value_to_double(ctx, arguments[0], exception);
-	height = seed_value_to_double(ctx, arguments[0], exception);
+	y = seed_value_to_double(ctx, arguments[1], exception);
+    width = seed_value_to_double(ctx, arguments[2], exception);
+	height = seed_value_to_double(ctx, arguments[3], exception);
 		
 	cairo_save(cr);
 		
-	cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
+	cairo_set_source_rgb(cr, 1, 1, 1);
 	cairo_rectangle(cr, x, y, width, height);
-	cairo_clip(cr);
+	
 
-	cairo_paint(cr);
+	cairo_fill(cr);
 	
 	cairo_restore(cr);
 
 	return seed_make_null(ctx);
 }
+
+void seed_canvas_parse_color(cairo_t * cr, gchar * spec)
+{
+	if (*spec == '#')
+	{
+		guint r=0,g=0,b=0,a=0;
+		// Might be libc dependent (the alpha behaviour);
+		sscanf(spec, "#%2x%2x%2x%2x", &r, &g, &b, &a);
+		if (strlen(spec) < 8)
+			a = 255;
+		cairo_set_source_rgba(cr, r/255.0, g/255.0, b/255.0, a/255.0);
+		return;
+	}
+	else if (*spec == 'r')
+	{
+		switch(*(spec+3))
+		{
+		case 'a':
+		{
+			gint r, g, b;
+			gfloat a;
+			
+			sscanf(spec, "rgba(%d,%d,%d,%f)", &r, &g, &b, &a);
+			cairo_set_source_rgba(cr, r/255.0, g/255.0, b/255.0, a);
+			return;
+		}
+		case '(':
+		{
+			gint r, g, b;
+
+			sscanf(spec, "rgb(%d,%d,%d)", &r, &g, &b);
+			cairo_set_source_rgb(cr, r/255.0, g/255.0, b/255.0);
+			return;
+		}
+		}
+	}
+}
+
 SeedValue seed_canvas_stroke_rect  (SeedContext ctx,
 							  SeedObject function,
 							  SeedObject this_object,
@@ -182,11 +229,19 @@
 {
 	GET_CR;
 	gdouble x, y, width, height;
+	gchar * stroke_color = 
+		seed_value_to_string(ctx, 
+							 seed_object_get_property(ctx, this_object, 
+													  "strokeStyle"),
+							 exception);
+	
+	seed_canvas_parse_color(cr, stroke_color);
+	g_free(stroke_color);
 	
 	x = seed_value_to_double(ctx, arguments[0], exception);
-	y = seed_value_to_double(ctx, arguments[0], exception);
-    width = seed_value_to_double(ctx, arguments[0], exception);
-	height = seed_value_to_double(ctx, arguments[0], exception);
+	y = seed_value_to_double(ctx, arguments[1], exception);
+    width = seed_value_to_double(ctx, arguments[2], exception);
+	height = seed_value_to_double(ctx, arguments[3], exception);
 
 	cairo_rectangle(cr, x, y, width, height);
 	cairo_stroke(cr);
@@ -202,18 +257,181 @@
 {
 	GET_CR;
 	gdouble x, y, width, height;
+	gchar * stroke_color = 
+		seed_value_to_string(ctx, 
+							 seed_object_get_property(ctx, this_object, 
+													  "fillStyle"),
+							 exception);
+	
+	seed_canvas_parse_color(cr, stroke_color);
+	g_free(stroke_color);
 	
 	x = seed_value_to_double(ctx, arguments[0], exception);
-	y = seed_value_to_double(ctx, arguments[0], exception);
-    width = seed_value_to_double(ctx, arguments[0], exception);
-	height = seed_value_to_double(ctx, arguments[0], exception);
-
+	y = seed_value_to_double(ctx, arguments[1], exception);
+    width = seed_value_to_double(ctx, arguments[2], exception);
+	height = seed_value_to_double(ctx, arguments[3], exception);
+	
 	cairo_rectangle(cr, x, y, width, height);
 	cairo_fill(cr);
 	
 	return seed_make_null(ctx);
 }
 
+SeedValue seed_canvas_end_path  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+
+	cairo_close_path(cr);
+
+	return seed_make_null(ctx);
+}
+
+SeedValue seed_canvas_begin_path  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	
+	cairo_new_path(cr);
+
+	return seed_make_null(ctx);
+}
+
+SeedValue seed_canvas_move_to (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	gdouble x, y;
+	
+	x = seed_value_to_double(ctx, arguments[0], exception);
+	y = seed_value_to_double(ctx, arguments[1], exception);
+
+	cairo_move_to(cr, x, y);
+
+	return seed_make_null(ctx);
+}
+
+SeedValue seed_canvas_line_to  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	gdouble x, y;
+	
+	x = seed_value_to_double(ctx, arguments[0], exception);
+	y = seed_value_to_double(ctx, arguments[1], exception);
+
+	cairo_line_to(cr, x, y);
+}
+
+SeedValue seed_canvas_stroke (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	gchar * stroke_color = 
+		seed_value_to_string(ctx, 
+							 seed_object_get_property(ctx, this_object, 
+													  "strokeStyle"),
+							 exception);
+	
+	seed_canvas_parse_color(cr, stroke_color);
+	g_free(stroke_color);
+	
+
+	
+	cairo_stroke(cr);
+
+	return seed_make_null(ctx);
+}
+
+SeedValue seed_canvas_clip  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	
+	cairo_clip(cr);
+
+	return seed_make_null(ctx);
+}
+
+SeedValue seed_canvas_fill  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	gchar * stroke_color = 
+		seed_value_to_string(ctx, 
+							 seed_object_get_property(ctx, this_object, 
+													  "fillStyle"),
+							 exception);
+	
+	seed_canvas_parse_color(cr, stroke_color);
+	g_free(stroke_color);
+	
+	
+	cairo_fill(cr);
+
+	return seed_make_null(ctx);
+}
+
+SeedValue seed_canvas_arc  (SeedContext ctx,
+							  SeedObject function,
+							  SeedObject this_object,
+							  size_t argument_count,
+							  const SeedValue arguments[],
+							  SeedException * exception)
+{
+	GET_CR;
+	gdouble xc, yc, radius, start, end;
+	gboolean counter_clockwise;
+	
+	xc = seed_value_to_double(ctx, arguments[0], exception);
+	yc = seed_value_to_double(ctx, arguments[1], exception);
+	radius = seed_value_to_double(ctx, arguments[2], exception);
+	start = seed_value_to_double(ctx, arguments[3], exception);
+	end = seed_value_to_double(ctx, arguments[4], exception);
+	counter_clockwise = seed_value_to_boolean(ctx, arguments[5], exception);
+	
+	if (counter_clockwise)
+		cairo_arc_negative(cr, xc, yc, radius, end, start);
+	else
+		cairo_arc(cr, xc, yc, radius, start, end);
+
+	return seed_make_null(ctx);
+}
+
+
+static void canvas_finalize(SeedObject object)
+{
+	cairo_destroy((cairo_t *)seed_object_get_private(object));
+}
+
 seed_static_function canvas_funcs[] = {
 	{"save", seed_canvas_save, 0},
 	{"restore", seed_canvas_restore, 0},
@@ -225,6 +443,14 @@
 	{"clearRect", seed_canvas_clear_rect, 0},
 	{"fillRect", seed_canvas_fill_rect, 0},
 	{"strokeRect", seed_canvas_stroke_rect, 0},
+	{"beginPath", seed_canvas_begin_path, 0},
+	{"closePath", seed_canvas_end_path, 0},
+	{"moveTo", seed_canvas_move_to, 0},
+	{"lineTo", seed_canvas_line_to, 0},
+	{"fill", seed_canvas_fill, 0},
+	{"stroke", seed_canvas_stroke, 0},
+	{"clip", seed_canvas_clip, 0},
+	{"arc", seed_canvas_arc, 0},
 	{0, 0, 0}
 };
 
@@ -241,6 +467,8 @@
 							 namespace_ref);
 	
 	canvas_class_def.class_name = "CairoCanvas";
+	canvas_class_def.static_functions = canvas_funcs;
+	canvas_class_def.finalize = canvas_finalize;
 	
 	canvas_class = seed_create_class(&canvas_class_def);
 	

Added: trunk/modules/canvas/test1.js
==============================================================================
--- (empty file)
+++ trunk/modules/canvas/test1.js	Sat Dec  6 01:50:30 2008
@@ -0,0 +1,34 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("Gdk");
+Seed.import_namespace("Gtk");
+Seed.import_namespace("Canvas");
+
+
+Gtk.init(null, null);
+
+w = new Gtk.Window();
+d = new Gtk.DrawingArea();
+w.add(d);
+
+w.show_all();
+
+w.resize(150, 150);
+
+
+d.signal.expose_event.connect(function(){
+		var cr = Gdk.cairo_create(d.window);
+		var ctx = new Canvas.CairoCanvas(cr);
+
+		ctx.fillStyle = "rgb(200,0,0)";
+        ctx.fillRect (10, 10, 55, 50);
+
+        ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
+        ctx.fillRect (30, 30, 55, 50);
+
+		return true;
+});
+Gtk.main();
+
+
+
+

Added: trunk/modules/canvas/test2.js
==============================================================================
--- (empty file)
+++ trunk/modules/canvas/test2.js	Sat Dec  6 01:50:30 2008
@@ -0,0 +1,32 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("Gdk");
+Seed.import_namespace("Gtk");
+Seed.import_namespace("Canvas");
+
+
+Gtk.init(null, null);
+
+w = new Gtk.Window();
+d = new Gtk.DrawingArea();
+w.add(d);
+
+w.show_all();
+
+w.resize(150, 150);
+
+
+d.signal.expose_event.connect(function(){
+		var cr = Gdk.cairo_create(d.window);
+		var ctx = new Canvas.CairoCanvas(cr);
+
+		ctx.fillRect(25,25,100,100);
+		ctx.clearRect(45,45,60,60);
+		ctx.strokeRect(50,50,50,50);
+
+		return true;
+});
+Gtk.main();
+
+
+
+

Added: trunk/modules/canvas/test3.js
==============================================================================
--- (empty file)
+++ trunk/modules/canvas/test3.js	Sat Dec  6 01:50:30 2008
@@ -0,0 +1,33 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("Gdk");
+Seed.import_namespace("Gtk");
+Seed.import_namespace("Canvas");
+
+
+Gtk.init(null, null);
+
+w = new Gtk.Window();
+d = new Gtk.DrawingArea();
+w.add(d);
+
+w.show_all();
+
+w.resize(150, 150);
+
+
+d.signal.expose_event.connect(function(){
+		var cr = Gdk.cairo_create(d.window);
+		var ctx = new Canvas.CairoCanvas(cr);
+
+		ctx.beginPath();
+		ctx.moveTo(75,50);
+		ctx.lineTo(100,75);
+		ctx.lineTo(100,25);
+		ctx.fill();
+		return true;
+});
+Gtk.main();
+
+
+
+

Added: trunk/modules/canvas/test4.js
==============================================================================
--- (empty file)
+++ trunk/modules/canvas/test4.js	Sat Dec  6 01:50:30 2008
@@ -0,0 +1,38 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("Gdk");
+Seed.import_namespace("Gtk");
+Seed.import_namespace("Canvas");
+
+
+Gtk.init(null, null);
+
+w = new Gtk.Window();
+d = new Gtk.DrawingArea();
+w.add(d);
+
+w.resize(150, 150);
+
+w.show_all();
+
+d.signal.expose_event.connect(function(){
+		var cr = Gdk.cairo_create(d.window);
+		var ctx = new Canvas.CairoCanvas(cr);
+
+		ctx.moveTo(0, 0);
+		ctx.beginPath();
+		ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle
+		ctx.moveTo(110,75);
+		ctx.arc(75,75,35,0,Math.PI,false);   // Mouth (clockwise)
+		ctx.moveTo(65,65);
+		ctx.arc(60,65,5,0,Math.PI*2,true);  // Left eye
+		ctx.moveTo(95,65);
+		ctx.arc(90,65,5,0,Math.PI*2,true);  // Right eye
+		ctx.stroke();
+
+		return true;
+});
+Gtk.main();
+
+
+
+

Added: trunk/modules/canvas/test5.js
==============================================================================
--- (empty file)
+++ trunk/modules/canvas/test5.js	Sat Dec  6 01:50:30 2008
@@ -0,0 +1,43 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("Gdk");
+Seed.import_namespace("Gtk");
+Seed.import_namespace("Canvas");
+
+
+Gtk.init(null, null);
+
+w = new Gtk.Window();
+d = new Gtk.DrawingArea();
+w.add(d);
+
+w.resize(150, 150);
+
+w.show_all();
+
+d.signal.expose_event.connect(function(){
+		var cr = Gdk.cairo_create(d.window);
+		var ctx = new Canvas.CairoCanvas(cr);
+
+// Filled triangle
+		ctx.beginPath();
+		ctx.moveTo(25,25);
+		ctx.lineTo(105,25);
+		ctx.lineTo(25,105);
+		ctx.fill();
+		
+// Stroked triangle
+		ctx.beginPath();
+		ctx.moveTo(125,125);
+		ctx.lineTo(125,45);
+		ctx.lineTo(45,125);
+		ctx.closePath();
+		ctx.stroke();
+		
+		
+		return true;
+});
+Gtk.main();
+
+
+
+

Added: trunk/modules/canvas/test6.js
==============================================================================
--- (empty file)
+++ trunk/modules/canvas/test6.js	Sat Dec  6 01:50:30 2008
@@ -0,0 +1,47 @@
+#!/usr/local/bin/seed
+Seed.import_namespace("Gdk");
+Seed.import_namespace("Gtk");
+Seed.import_namespace("Canvas");
+
+
+Gtk.init(null, null);
+
+w = new Gtk.Window();
+d = new Gtk.DrawingArea();
+w.add(d);
+
+w.resize(150, 200);
+
+w.show_all();
+
+d.signal.expose_event.connect(function(){
+		var cr = Gdk.cairo_create(d.window);
+		var ctx = new Canvas.CairoCanvas(cr);
+		for (i=0;i<4;i++){
+			for(j=0;j<3;j++){
+				ctx.beginPath();
+				var x              = 25+j*50;               // x coordinate
+				var y              = 25+i*50;               // y coordinate
+				var radius         = 20;                    // Arc radius
+				var startAngle     = 0;                     // Starting point on circle
+				var endAngle       = Math.PI+(Math.PI*j)/2; // End point on circle
+				var anticlockwise  = i%2==0 ? false : true; // clockwise or anticlockwise
+				
+				ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise);
+				
+				if (i>1){
+					ctx.fill();
+				} else {
+					ctx.stroke();
+				}
+			}
+		}
+		
+		
+		return true;
+});
+Gtk.main();
+
+
+
+



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