[seed] Implement cairo_surface_t bindings



commit b8ecabf1e86efd372ba48e2462f2e4385d33553d
Author: Robert Carr <racarr svn gnome org>
Date:   Thu May 14 03:27:19 2009 -0400

    Implement cairo_surface_t bindings
---
 configure.ac                       |   20 ++
 libseed/seed-api.c                 |    6 +
 libseed/seed.h                     |    3 +
 modules/Makefile.am                |    2 +-
 modules/cairo/Makefile.am          |   26 +++
 modules/cairo/seed-cairo-surface.c |  350 ++++++++++++++++++++++++++++++++++++
 modules/cairo/seed-cairo-surface.h |    9 +
 modules/cairo/seed-cairo.c         |   33 ++++
 modules/cairo/seed-cairo.h         |   13 ++
 modules/os/os.c                    |    4 +-
 10 files changed, 463 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index a8985d8..7a1d70b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -106,6 +106,8 @@ fi
 AM_CONDITIONAL(BUILD_CANVAS_MODULE, test "x$want_canvas_module" = "xyes")
 AC_SUBST(BUILD_CANVAS_MODULE)
 
+
+
 dnl ==== readline ====
 AC_ARG_ENABLE(readline-module,
 			  AC_HELP_STRING([--enable-readline-module],
@@ -196,6 +198,22 @@ fi
 AM_CONDITIONAL(BUILD_LIBXML_MODULE, test "x$want_libxml_module" = "xyes")
 AC_SUBST(BUILD_LIBXML_MODULE)
 
+dnl ==== cairo ====
+AC_ARG_ENABLE(cairo-module,
+			  AC_HELP_STRING([--enable-cairo-module],
+							 [enable the cairo Seed module. [default=yes]]),
+			  [want_cairo_module=$enableval],[want_cairo_module="yes"])
+
+if test x"$want_cairo_module" == x"yes" ; then
+	PKG_CHECK_MODULES(CAIRO, cairo)
+	AC_SUBST(CAIRO_CFLAGS)
+	AC_SUBST(CAIRO_LDFLAGS)
+fi
+
+AM_CONDITIONAL(BUILD_CAIRO_MODULE, test "x$want_cairo_module" = "xyes")
+AC_SUBST(BUILD_CAIRO_MODULE)
+
+
 
 dnl =========================turtle example====================================
 AC_ARG_ENABLE(turtle-example,
@@ -346,6 +364,7 @@ modules/os/Makefile
 modules/dbus/Makefile
 modules/dbus/util/Makefile
 modules/libxml/Makefile
+modules/cairo/Makefile
 ])
 AC_OUTPUT
 
@@ -368,6 +387,7 @@ Modules:
    SQLite.....................$want_sqlite_module
    DBus.......................$want_dbus_module
    libxml.....................$want_libxml_module
+   cairo......................$want_cairo_module
 
 Examples:
    Turtle.....................$want_turtle_example
diff --git a/libseed/seed-api.c b/libseed/seed-api.c
index ee76e5c..14bb768 100644
--- a/libseed/seed-api.c
+++ b/libseed/seed-api.c
@@ -698,3 +698,9 @@ seed_object_get_prototype (JSContextRef ctx, JSObjectRef obj)
 {
   return (JSObjectRef) JSObjectGetPrototype (ctx, obj);
 }
+
+gboolean
+seed_object_is_of_class (JSContextRef ctx, JSObjectRef obj, JSClassRef class)
+{
+  return JSValueIsObjectOfClass (ctx, obj, class);
+}
diff --git a/libseed/seed.h b/libseed/seed.h
index 1f11720..8647560 100644
--- a/libseed/seed.h
+++ b/libseed/seed.h
@@ -411,5 +411,8 @@ seed_closure_warn_exception (GClosure *c, SeedContext ctx, SeedException excepti
 SeedObject
 seed_object_get_prototype (SeedContext ctx, SeedObject obj);
 
+gboolean
+seed_object_is_of_class (SeedContext ctx, SeedObject obj, SeedClass class);
+
 
 #endif
diff --git a/modules/Makefile.am b/modules/Makefile.am
index d3af447..a658a85 100644
--- a/modules/Makefile.am
+++ b/modules/Makefile.am
@@ -1 +1 @@
-SUBDIRS = example sqlite canvas Multiprocessing readline os sandbox dbus libxml
+SUBDIRS = example sqlite canvas Multiprocessing readline os sandbox dbus libxml cairo
diff --git a/modules/cairo/Makefile.am b/modules/cairo/Makefile.am
new file mode 100644
index 0000000..3122e48
--- /dev/null
+++ b/modules/cairo/Makefile.am
@@ -0,0 +1,26 @@
+if BUILD_CAIRO_MODULE
+
+seedlibdir = ${exec_prefix}/lib/seed
+EXTRA_DIST=run-tests.js
+
+seedlib_LTLIBRARIES = \
+	libcairo.la
+
+libcairo_la_SOURCES = \
+	seed-cairo.c \
+	seed-cairo-surface.c 
+
+libcairo_la_CFLAGS = \
+        $(GOBJECT_INTROSPECTION_CFLAGS) \
+	-I top_srcdir@/libseed/ \
+	$(CAIRO_CFLAGS) \
+	$(SEED_DEBUG_CFLAGS) \
+	$(SEED_PROFILE_CFLAGS)
+
+libcairo_la_LDFLAGS = \
+        $(GOBJECT_INTROSPECTION_LDFLAGS) \
+	$(CAIRO_LDFLAGS) \
+	$(SEED_PROFILE_LIBS)
+
+endif
+
diff --git a/modules/cairo/seed-cairo-surface.c b/modules/cairo/seed-cairo-surface.c
new file mode 100644
index 0000000..79785f4
--- /dev/null
+++ b/modules/cairo/seed-cairo-surface.c
@@ -0,0 +1,350 @@
+#include <seed.h>
+#include <cairo/cairo.h>
+
+#include "seed-cairo.h"
+
+#define CAIRO_SURFACE_PRIV(obj) ((cairo_surface_t *)seed_object_get_private(obj))
+
+#define CHECK_SURFACE(obj, res) ({						\
+    if (!seed_is_object_of_class (ctx, obj, seed_cairo_surface_class)){	\
+      seed_make_exception (ctx, exception, "ArgumentError", "Object is not a Cairo Surface"); \
+      return seed_make_##res (ctx);\
+    }									\
+    if (!seed_object_get_private (obj)){				\
+      seed_make_exception (ctx, exception, "ArgumentError", "Cairo surface has been destroyed"); \
+      return seed_make_##res (ctx);}})
+
+#define CHECK_THIS(res) if (!seed_object_get_private (this_object)){	\
+    seed_make_exception (ctx, exception, "ArgumentError", "Cairo surface has been destroyed"); \
+      return seed_make_##res (ctx);}
+
+  
+
+SeedClass seed_cairo_surface_class;
+
+static void
+seed_cairo_surface_finalize (SeedObject obj)
+{
+  cairo_surface_t *s = CAIRO_SURFACE_PRIV(obj);
+  cairo_surface_destroy (s);
+}
+
+cairo_surface_t *
+seed_object_to_cairo_surface (SeedContext ctx, SeedObject obj)
+{
+  if (seed_is_object_of_class (ctx, obj, seed_cairo_surface_class))
+    return CAIRO_SURFACE_PRIV (obj);
+  return NULL;
+}
+
+SeedObject
+seed_object_from_cairo_surface (SeedContext ctx, cairo_surface_t *surf)
+{
+  SeedObject jsobj;
+  
+  jsobj = cairo_surface_get_user_data (surf, seed_get_cairo_key());
+  if (jsobj)
+    return jsobj;
+  
+  jsobj = seed_make_object (ctx, seed_cairo_surface_class, surf);
+  cairo_surface_set_user_data (surf, seed_get_cairo_key(), jsobj, seed_cairo_destroy_func);
+  return jsobj;
+}
+
+static SeedValue
+seed_cairo_surface_create_similar (SeedContext ctx,
+				   SeedObject function,
+				   SeedObject this_object,
+				   gsize argument_count,
+				   const SeedValue arguments[],
+				   SeedException *exception)
+{
+  gint width, height;
+  cairo_surface_t *surface, *ret;
+  cairo_content_t content;
+  CHECK_THIS(null);
+  if (argument_count != 3)
+    {
+      EXPECTED_EXCEPTION("create_similar", "3 arguments", null);
+    }
+  
+  surface = seed_object_to_cairo_surface (ctx, this_object);
+  content = seed_value_to_long (ctx, arguments[0], exception);
+  width = seed_value_to_int (ctx, arguments[1], exception);
+  height = seed_value_to_int (ctx, arguments[2], exception);
+  
+  ret = cairo_surface_create_similar (surface, content, width, height);
+  return seed_object_from_cairo_surface (ctx, ret);
+}
+
+static SeedValue
+seed_cairo_surface_status (SeedContext ctx,
+			   SeedObject function,
+			   SeedObject this_object,
+			   gsize argument_count,
+			   const SeedValue arguments[],
+			   SeedException *exception)
+{
+  CHECK_THIS(null);
+  return seed_value_from_long (ctx, cairo_surface_status (seed_object_to_cairo_surface(ctx, this_object)), exception);
+}
+
+static SeedValue
+seed_cairo_surface_get_content (SeedContext ctx,
+				SeedObject function,
+				SeedObject this_object,
+				gsize argument_count,
+				const SeedValue arguments[],
+				SeedException *exception)
+{
+  CHECK_THIS(null);
+  return seed_value_from_long (ctx, cairo_surface_get_content (seed_object_to_cairo_surface(ctx, this_object)), exception);
+}
+
+static SeedValue
+seed_cairo_surface_finish (SeedContext ctx,
+			   SeedObject function,
+			   SeedObject this_object,
+			   gsize argument_count,
+			   const SeedValue arguments[],
+			   SeedException *exception)
+{
+  CHECK_THIS(undefined);
+  cairo_surface_finish (seed_object_to_cairo_surface(ctx, this_object));
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_flush (SeedContext ctx,
+			  SeedObject function,
+			  SeedObject this_object,
+			  gsize argument_count,
+			  const SeedValue arguments[],
+			  SeedException *exception)
+{
+  CHECK_THIS(null);
+  cairo_surface_flush (seed_object_to_cairo_surface(ctx, this_object));
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_mark_dirty_rectangle(SeedContext ctx,
+					SeedObject function,
+					SeedObject this_object,
+					gsize argument_count,
+					const SeedValue arguments[],
+					SeedException *exception)
+{
+  cairo_surface_t *surf;
+  guint x, y, width, height;
+  CHECK_THIS(undefined);
+  if (argument_count != 4)
+    {
+      EXPECTED_EXCEPTION("mark_dirty_rectangle", "4 arguments", undefined);
+    }
+  surf = seed_object_to_cairo_surface (ctx, this_object);
+  x = seed_value_to_int (ctx, arguments[0], exception);
+  y = seed_value_to_int (ctx, arguments[1], exception);
+  width = seed_value_to_int (ctx, arguments[2], exception);
+  height = seed_value_to_int (ctx, arguments[3], exception);
+  
+  cairo_surface_mark_dirty_rectangle (surf, x, y, width, height);
+
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_mark_dirty (SeedContext ctx,
+			       SeedObject function,
+			       SeedObject this_object,
+			       gsize argument_count,
+			       const SeedValue arguments[],
+			       SeedException *exception)
+{
+  CHECK_THIS(undefined);
+  cairo_surface_mark_dirty (seed_object_to_cairo_surface(ctx, this_object));
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_set_device_offset(SeedContext ctx,
+				     SeedObject function,
+				     SeedObject this_object,
+				     gsize argument_count,
+				     const SeedValue arguments[],
+				     SeedException *exception)
+{
+  cairo_surface_t *surf;
+  gdouble x, y;
+  CHECK_THIS(null);
+  if (argument_count != 2)
+    {
+      EXPECTED_EXCEPTION("set_device_offset", "2 arguments", undefined);
+    }
+  surf = seed_object_to_cairo_surface (ctx, this_object);
+  x = seed_value_to_double (ctx, arguments[0], exception);
+  y = seed_value_to_double (ctx, arguments[1], exception);
+  
+  cairo_surface_set_device_offset (surf, x, y);
+
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_get_device_offset(SeedContext ctx,
+				     SeedObject function,
+				     SeedObject this_object,
+				     gsize argument_count,
+				     const SeedValue arguments[],
+				     SeedException *exception)
+{
+  SeedValue offsets[2];
+  cairo_surface_t *surf;
+  gdouble x, y;
+  CHECK_THIS(null);
+  if (argument_count != 0)
+    {
+      EXPECTED_EXCEPTION("get_device_offset", "no arguments", null);
+    }
+  surf = seed_object_to_cairo_surface (ctx, this_object);
+  cairo_surface_get_device_offset (surf, &x, &y);
+  
+  offsets[0] = seed_value_from_double (ctx, x, exception);
+  offsets[1] = seed_value_from_double (ctx, y, exception);
+
+  return seed_make_array (ctx, offsets, 2, exception);
+}
+
+static SeedValue
+seed_cairo_surface_set_fallback_resolution(SeedContext ctx,
+				     SeedObject function,
+				     SeedObject this_object,
+				     gsize argument_count,
+				     const SeedValue arguments[],
+				     SeedException *exception)
+{
+  cairo_surface_t *surf;
+  gdouble x, y;
+  CHECK_THIS(null);
+  if (argument_count != 2)
+    {
+      EXPECTED_EXCEPTION("set_fallback_resolution", "2 arguments", undefined);
+    }
+  surf = seed_object_to_cairo_surface (ctx, this_object);
+  x = seed_value_to_double (ctx, arguments[0], exception);
+  y = seed_value_to_double (ctx, arguments[1], exception);
+  
+  cairo_surface_set_fallback_resolution (surf, x, y);
+
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_get_fallback_resolution(SeedContext ctx,
+				     SeedObject function,
+				     SeedObject this_object,
+				     gsize argument_count,
+				     const SeedValue arguments[],
+				     SeedException *exception)
+{
+  SeedValue offsets[2];
+  cairo_surface_t *surf;
+  gdouble x, y;
+  CHECK_THIS(null);
+  if (argument_count != 0)
+    {
+      EXPECTED_EXCEPTION("get_fallback_resolution", "no arguments", null);
+    }
+  surf = seed_object_to_cairo_surface (ctx, this_object);
+  cairo_surface_get_fallback_resolution (surf, &x, &y);
+  
+  offsets[0] = seed_value_from_double (ctx, x, exception);
+  offsets[1] = seed_value_from_double (ctx, y, exception);
+
+  return seed_make_array (ctx, offsets, 2, exception);
+}
+
+static SeedValue
+seed_cairo_surface_get_type (SeedContext ctx,
+				SeedObject function,
+				SeedObject this_object,
+				gsize argument_count,
+				const SeedValue arguments[],
+				SeedException *exception)
+{
+  CHECK_THIS(null);
+  return seed_value_from_long (ctx, cairo_surface_get_type (seed_object_to_cairo_surface(ctx, this_object)), exception);
+}
+
+static SeedValue
+seed_cairo_surface_copy_page (SeedContext ctx,
+			       SeedObject function,
+			       SeedObject this_object,
+			       gsize argument_count,
+			       const SeedValue arguments[],
+			       SeedException *exception)
+{
+  CHECK_THIS(undefined);
+  cairo_surface_copy_page (seed_object_to_cairo_surface(ctx, this_object));
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_show_page (SeedContext ctx,
+			       SeedObject function,
+			       SeedObject this_object,
+			       gsize argument_count,
+			       const SeedValue arguments[],
+			       SeedException *exception)
+{
+  CHECK_THIS(undefined);
+  cairo_surface_show_page (seed_object_to_cairo_surface(ctx, this_object));
+  return seed_make_undefined (ctx);
+}
+
+static SeedValue
+seed_cairo_surface_has_show_text_glyphs(SeedContext ctx,
+					SeedObject function,
+					SeedObject this_object,
+					gsize argument_count,
+					const SeedValue arguments[],
+					SeedException *exception)
+{
+  CHECK_THIS(null);
+  return seed_value_from_boolean (ctx, 
+				  cairo_surface_has_show_text_glpys (seed_object_to_cairo_surface(ctx, this_object)), exception);
+}
+
+
+seed_static_function surface_funcs[] = {
+  {"create_similar", seed_cairo_surface_create_similar, 0},
+  {"status", seed_cairo_surface_status, 0},
+  {"finish", seed_cairo_surface_finish, 0},
+  {"flush", seed_cairo_surface_flush, 0},
+  //  {"get_font_options", seed_cairo_surface_get_font_options, 0},
+  {"get_content", seed_cairo_surface_get_content, 0},
+  {"mark_dirty", seed_cairo_surface_mark_dirty, 0},
+  {"mark_dirty_rectangle", seed_cairo_surface_mark_dirty_rectangle, 0},
+  {"set_device_offset", seed_cairo_surface_set_device_offset, 0},
+  {"get_device_offset", seed_cairo_surface_get_device_offset, 0},
+  {"set_fallback_resolution", seed_cairo_surface_set_fallback_resolution, 0},
+  {"get_fallback_resolution", seed_cairo_surface_get_fallback_resolution, 0},
+  {"get_type", seed_cairo_surface_get_type, 0},
+  {"copy_page", seed_cairo_surface_copy_page, 0},
+  {"show_page", seed_cairo_surface_show_page, 0},
+  {"has_show_text_glyphs", seed_cairo_surface_has_show_text_glyphs, 0},
+  {0,0,0}
+};
+
+void
+seed_define_cairo_surface (SeedContext ctx,
+			   SeedObject namespace_ref)
+{
+  seed_class_definition surface_def = seed_empty_class;
+  
+  surface_def.class_name = "CairoSurface";
+  surface_def.finalize = seed_cairo_surface_finalize;
+  surface_def.static_functions = surface_funcs;
+
+  seed_cairo_surface_class = seed_create_class (&surface_def);
+}
diff --git a/modules/cairo/seed-cairo-surface.h b/modules/cairo/seed-cairo-surface.h
new file mode 100644
index 0000000..ac5bf13
--- /dev/null
+++ b/modules/cairo/seed-cairo-surface.h
@@ -0,0 +1,9 @@
+#ifndef _SEED_CAIRO_SURFACE_H
+#define _SEED_CAIRO_SURFACE_H
+
+void seed_define_cairo_surface (SeedContext ctx, SeedObject namespace_ref);
+
+cairo_surface_t *seed_cairo_surface_from_object (SeedContext ctx, SeedObject obj);
+SeedObject seed_object_from_cairo_surface (SeedContext ctx, cairo_surface_t *surf);
+
+#endif
diff --git a/modules/cairo/seed-cairo.c b/modules/cairo/seed-cairo.c
new file mode 100644
index 0000000..9eef50a
--- /dev/null
+++ b/modules/cairo/seed-cairo.c
@@ -0,0 +1,33 @@
+#include <seed.h>
+#include <cairo/cairo.h>
+#include "seed-cairo-surface.h"
+SeedEngine *eng;
+SeedObject namespace_ref;
+
+cairo_user_data_key_t *
+seed_get_cairo_key ()
+{
+  static cairo_user_data_key_t foobaz;
+  
+  return &foobaz;
+}
+
+void
+seed_cairo_destroy_func (void *obj)
+{
+  SeedObject object = (SeedObject)obj;
+  seed_object_set_private (object, NULL);
+}
+
+SeedObject
+seed_module_init(SeedEngine * local_eng)
+{
+  eng = local_eng;
+  namespace_ref = seed_make_object (eng->context, NULL, NULL);
+  
+  // Temporary hack until API changes.
+  seed_value_protect (eng->context, namespace_ref);
+  seed_define_cairo_surface (eng->context, namespace_ref);
+  
+  return namespace_ref;
+}
diff --git a/modules/cairo/seed-cairo.h b/modules/cairo/seed-cairo.h
new file mode 100644
index 0000000..d490a07
--- /dev/null
+++ b/modules/cairo/seed-cairo.h
@@ -0,0 +1,13 @@
+#ifndef _SEED_CAIRO_H
+#define _SEED_CAIRO_H
+#include <seed.h>
+#include <cairo/cairo.h>
+
+#define EXPECTED_EXCEPTION(name, argnum, res)				\
+  seed_make_exception (ctx, exception, "ArgumentError", name " expected " argnum " got %Zd", argument_count); \
+  return seed_make_##res (ctx);
+
+cairo_user_data_key_t *seed_get_cairo_key ();
+void seed_cairo_destroy_func (void *obj);
+
+#endif
diff --git a/modules/os/os.c b/modules/os/os.c
index 2dd97db..32a3d85 100644
--- a/modules/os/os.c
+++ b/modules/os/os.c
@@ -23,9 +23,9 @@
 
 SeedObject os_namespace;
 
-#define EXPECTED_EXCEPTION(name, argnum) \
+#define EXPECTED_EXCEPTION(name, argnum, res)				\
   seed_make_exception (ctx, exception, "ArgumentError", name " expected " argnum " got %Zd", argument_count); \
-  return seed_make_null (ctx);
+  return seed_make_##res (ctx);
 
 SeedValue
 seed_os_chdir (SeedContext ctx,



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