[gjs: 9/45] [cairo] Add Context constructor



commit 9ee65fe2e7948ade1d650d4dd2c1ab90eabb8296
Author: Johan Dahlin <johan gnome org>
Date:   Wed Feb 17 20:20:40 2010 -0200

    [cairo] Add Context constructor
    
    Also add public API to:
    * create a Context from a cairo_t
    * get out a cairo_t from a Context

 modules/cairo-context.c |   75 +++++++++++++++++++++++++++++++++++++++++++----
 modules/cairo-private.h |    4 ++
 2 files changed, 73 insertions(+), 6 deletions(-)
---
diff --git a/modules/cairo-context.c b/modules/cairo-context.c
index 1411d84..fabd114 100644
--- a/modules/cairo-context.c
+++ b/modules/cairo-context.c
@@ -30,18 +30,17 @@ typedef struct {
     void *dummy;
     JSContext  *context;
     JSObject   *object;
+    cairo_t * cr;
 } GjsCairoContext;
 
 GJS_DEFINE_PROTO("CairoContext", gjs_cairo_context)
 
 GJS_DEFINE_PRIV_FROM_JS(GjsCairoContext, gjs_cairo_context_class);
 
-static JSBool
-gjs_cairo_context_constructor(JSContext *context,
-                              JSObject  *obj,
-                              uintN      argc,
-                              jsval     *argv,
-                              jsval     *retval)
+static void
+_gjs_cairo_context_construct_internal(JSContext *context,
+                                      JSObject *obj,
+                                      cairo_t *cr)
 {
     GjsCairoContext *priv;
 
@@ -52,6 +51,43 @@ gjs_cairo_context_constructor(JSContext *context,
 
     priv->context = context;
     priv->object = obj;
+    priv->cr = cr;
+}
+
+static JSBool
+gjs_cairo_context_constructor(JSContext *context,
+                          JSObject  *obj,
+                          uintN      argc,
+                          jsval     *argv,
+                          jsval     *retval)
+{
+    JSObject *surface_wrapper;
+    cairo_surface_t *surface;
+    cairo_t *cr;
+    cairo_status_t status;
+
+    if (!gjs_check_constructing(context))
+        return JS_FALSE;
+
+    if (!gjs_parse_args(context, "Context", "o", argc, argv,
+                        "surface", &surface_wrapper))
+        return JS_FALSE;
+
+    surface = gjs_cairo_surface_get_surface(context, surface_wrapper);
+    if (!surface) {
+        gjs_throw(context, "first argument to Context() should be a surface");
+        return JS_FALSE;
+    }
+
+    cr = cairo_create(surface);
+    status = cairo_status(cr);
+    if (status != CAIRO_STATUS_SUCCESS) {
+        gjs_throw(context, "Could not create context: %s",
+                  cairo_status_to_string(status));
+        return JS_FALSE;
+    }
+
+    _gjs_cairo_context_construct_internal(context, obj, cr);
 
     return JS_TRUE;
 }
@@ -65,6 +101,7 @@ gjs_cairo_context_finalize(JSContext *context,
     if (priv == NULL)
         return;
 
+    cairo_destroy(priv->cr);
     g_slice_free(GjsCairoContext, priv);
 }
 
@@ -78,3 +115,29 @@ static JSFunctionSpec gjs_cairo_context_proto_funcs[] = {
     { NULL }
 };
 
+JSObject *
+gjs_cairo_context_from_cr(JSContext *context,
+                          cairo_t *cr)
+{
+    JSObject *object;
+
+    object = JS_NewObject(context, &gjs_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)
+{
+    GjsCairoContext *priv;
+    priv = priv_from_js(context, object);
+    if (priv == NULL)
+        return NULL;
+
+    return priv->cr;
+}
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 032c62f..c86439b 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -37,6 +37,10 @@ JSBool gjs_js_define_cairo_stuff(JSContext *context,
 
 jsval gjs_cairo_context_create_proto(JSContext *context, JSObject *module,
                                      const char *proto_name, JSObject *parent);
+cairo_t *gjs_cairo_context_get_cr(JSContext *context,
+                                  JSObject *object);
+JSObject * gjs_cairo_context_from_cr(JSContext *context,
+                                     cairo_t *cr);
 
 /* surface */
 jsval gjs_cairo_surface_create_proto(JSContext *context, JSObject *module,



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