[gjs/cairo] [cairo] Add an override for CairoContext



commit 3c19e937bc414e4e6da092a6ec489187f9e14fa9
Author: Johan Dahlin <johan gnome org>
Date:   Thu Feb 18 00:24:38 2010 -0200

    [cairo] Add an override for CairoContext
    
    Also include an example of Poppler/Gdk integration of
    GdkCairo

 Makefile-examples.am          |    1 +
 Makefile-modules.am           |    7 ++-
 examples/cairo/poppler-gdk.js |   32 +++++++++++++++
 modules/cairo-context.c       |   84 ++++++++++++++++++++++++++++++++++++----
 modules/cairo-private.h       |    4 ++
 modules/cairo.c               |    2 +
 6 files changed, 119 insertions(+), 11 deletions(-)
---
diff --git a/Makefile-examples.am b/Makefile-examples.am
index cce1196..a2ee379 100644
--- a/Makefile-examples.am
+++ b/Makefile-examples.am
@@ -1,5 +1,6 @@
 EXTRA_DIST +=			        \
 	examples/cairo/fill.js                 \
+	examples/cairo/poppler-gdk.js                 \
 	examples/cairo/stroke.js                 \
 	examples/clutter.js		\
 	examples/gio-cat.js                 \
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 9854325..da9326e 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -74,10 +74,13 @@ gettextNative_la_SOURCES =		\
 
 cairoNative_la_CFLAGS = 				\
 	$(JS_NATIVE_MODULE_CFLAGS)  \
-    $(GJS_CAIRO_CFLAGS)
+    $(GJS_CAIRO_CFLAGS)         \
+	$(GJS_GI_CFLAGS)
 cairoNative_la_LIBADD = \
+	libgjs-gi.la				\
 	$(JS_NATIVE_MODULE_LIBADD)		\
-	$(GJS_CAIRO_LIBS)
+	$(GJS_CAIRO_LIBS)               \
+	$(GJS_GI_LIBS)
 cairoNative_la_LDFLAGS = 				\
 	$(JS_NATIVE_MODULE_LDFLAGS)
 
diff --git a/examples/cairo/poppler-gdk.js b/examples/cairo/poppler-gdk.js
new file mode 100644
index 0000000..1463e68
--- /dev/null
+++ b/examples/cairo/poppler-gdk.js
@@ -0,0 +1,32 @@
+const Gdk = imports.gi.Gdk;
+const Gtk = imports.gi.Gtk;
+const Poppler = imports.gi.Poppler;
+const Cairo = imports.cairo;
+
+function onExpose(window, event) {
+  var cr = Gdk.cairo_create(window.window);
+  page.render(cr);
+}
+
+function onDestroy() {
+  Gtk.main_quit();
+}
+
+Gtk.init(0, null);
+
+var doc = Poppler.Document.new_from_file("file://" + ARGV[0], "");
+var page = doc.get_page(0); 
+
+var win = new Gtk.Window({
+  'type': Gtk.WindowType.TOPLEVEL
+});
+
+var draw = new Gtk.DrawingArea();
+win.add(draw);
+
+win.connect("destroy", onDestroy);
+draw.connect("expose-event", onExpose);
+
+win.show_all();
+
+Gtk.main();
diff --git a/modules/cairo-context.c b/modules/cairo-context.c
index c2389ad..9d1ccb8 100644
--- a/modules/cairo-context.c
+++ b/modules/cairo-context.c
@@ -23,6 +23,8 @@
 #include <config.h>
 
 #include <gjs/gjs.h>
+#include <gi/override.h>
+
 #include <cairo.h>
 #include "cairo-private.h"
 
@@ -59,6 +61,23 @@ cairo_context_new_resolve(JSContext *context,
     return JS_TRUE;
 }
 
+static void
+_gjs_cairo_context_construct_internal(JSContext *context,
+                                      JSObject *obj,
+                                      cairo_t *cr)
+{
+    GjsCairoContext *priv;
+
+    priv = g_slice_new0(GjsCairoContext);
+
+    g_assert(priv_from_js(context, obj) == NULL);
+    JS_SetPrivate(context, obj, priv);
+
+    priv->context = context;
+    priv->object = obj;
+    priv->cr = cr;
+}
+
 static JSBool
 cairo_context_constructor(JSContext *context,
                           JSObject  *obj,
@@ -66,7 +85,6 @@ cairo_context_constructor(JSContext *context,
                           jsval     *argv,
                           jsval     *retval)
 {
-    GjsCairoContext *priv;
     JSObject *surface_wrapper;
     cairo_surface_t *surface;
     cairo_t *cr;
@@ -86,14 +104,7 @@ cairo_context_constructor(JSContext *context,
         return JS_FALSE;
     }
 
-    priv = g_slice_new0(GjsCairoContext);
-
-    g_assert(priv_from_js(context, obj) == NULL);
-    JS_SetPrivate(context, obj, priv);
-
-    priv->context = context;
-    priv->object = obj;
-    priv->cr = cr;
+    _gjs_cairo_context_construct_internal(context, obj, cr);
 
     return JS_TRUE;
 }
@@ -322,6 +333,20 @@ gjs_cairo_context_create_proto(JSContext *context)
     return cairo_context;
 }
 
+JSObject *
+gjs_cairo_context_from_cr(JSContext *context,
+                          cairo_t *cr)
+{
+    JSObject *object;
+
+    object = JS_NewObject(context, &gjs_js_cairo_context_class, NULL, NULL);
+    if (!object)
+        return NULL;
+
+    _gjs_cairo_context_construct_internal(context, object, cr);
+    return object;
+}
+
 cairo_t *
 gjs_cairo_context_get_cr(JSContext *context,
                          JSObject *object)
@@ -330,6 +355,47 @@ gjs_cairo_context_get_cr(JSContext *context,
     priv = priv_from_js(context, object);
     if (priv == NULL)
         return NULL;
+
     return priv->cr;
 }
 
+static JSBool
+context_to_g_argument(JSContext      *context,
+                      jsval           value,
+                      GArgument      *arg)
+{
+    JSObject *obj;
+    cairo_t *cr;
+
+    obj = JSVAL_TO_OBJECT(value);
+    cr = gjs_cairo_context_get_cr(context, obj);
+    if (!cr)
+        return JS_FALSE;
+
+    arg->v_pointer = cr;
+    return JS_TRUE;
+}
+
+static JSBool
+context_from_g_argument (JSContext  *context,
+                         jsval      *value_p,
+                         GArgument  *arg)
+{
+    JSObject *obj;
+
+    obj = gjs_cairo_context_from_cr(context, (cairo_t*)arg->v_pointer);
+    if (!obj)
+        return JS_FALSE;
+
+    *value_p = OBJECT_TO_JSVAL(obj);
+    return JS_TRUE;
+}
+
+void
+_cairo_context_init (JSContext *context)
+{
+    gjs_arg_override_register("cairo", "Context",
+                              context_to_g_argument,
+                              context_from_g_argument);
+}
+
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index e996886..882e254 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -31,6 +31,10 @@ JSBool gjs_js_define_cairo_stuff(JSContext *context,
 jsval gjs_cairo_context_create_proto(JSContext *context);
 cairo_t *gjs_cairo_context_get_cr(JSContext *context,
                                   JSObject *object);
+JSObject * gjs_cairo_context_from_cr(JSContext *context,
+                                     cairo_t *cr);
+void _cairo_context_init (JSContext *context);
+
 jsval gjs_cairo_image_surface_create_proto(JSContext *context);
 cairo_surface_t * gjs_cairo_image_surface_get_surface(JSContext *context,
                                                       JSObject *object);
diff --git a/modules/cairo.c b/modules/cairo.c
index d7367b2..82bbe6f 100644
--- a/modules/cairo.c
+++ b/modules/cairo.c
@@ -44,6 +44,8 @@ gjs_js_define_cairo_stuff(JSContext      *context,
                            obj, NULL, NULL, GJS_MODULE_PROP_FLAGS))
         return JS_FALSE;
 
+    _cairo_context_init(context);
+
     return JS_TRUE;
 }
 



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