[gjs: 3/45] [cairo] Add an abstract Surface prototype



commit 5188ac40e2cf1c731b28b87c7cb6f62145d46657
Author: Johan Dahlin <johan gnome org>
Date:   Fri Feb 19 20:14:33 2010 -0200

    [cairo] Add an abstract Surface prototype
    
    It will serve as a base prototype for all other surfaces

 Makefile-modules.am     |    1 +
 modules/cairo-private.h |   21 ++++++-
 modules/cairo-surface.c |  149 +++++++++++++++++++++++++++++++++++++++++++++++
 modules/cairo.c         |    7 ++
 4 files changed, 176 insertions(+), 2 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 9b2984c..42e3a13 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -87,6 +87,7 @@ cairoNative_la_LDFLAGS = 				\
 cairoNative_la_SOURCES =		\
 	modules/cairo-private.h	\
 	modules/cairo-context.c \
+	modules/cairo-surface.c \
 	modules/cairo.c
 
 
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index bc66490..feccc1b 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -23,11 +23,28 @@
 #ifndef __CAIRO_PRIVATE_H__
 #define __CAIRO_PRIVATE_H__
 
-JSBool gjs_js_define_cairo_stuff(JSContext      *context,
-                                 JSObject       *module_obj);
+#include <cairo.h>
+
+typedef struct {
+    void *dummy;
+    JSContext  *context;
+    JSObject   *object;
+    cairo_surface_t *surface;
+} GjsCairoSurface;
+
+JSBool gjs_js_define_cairo_stuff(JSContext *context,
+                                 JSObject  *module_obj);
 
 jsval gjs_cairo_context_create_proto(JSContext *context, JSObject *module,
                                      const char *proto_name, JSObject *parent);
 
+/* surface */
+jsval gjs_cairo_surface_create_proto(JSContext *context, JSObject *module,
+                                     const char *proto_name, JSObject *parent);
+void gjs_cairo_surface_construct(JSContext *context, JSObject *obj, cairo_surface_t *surface);
+void gjs_cairo_surface_finalize_surface(JSContext *context, JSObject  *obj);
+cairo_surface_t * gjs_cairo_surface_get_surface(JSContext *context,
+                                                JSObject *object);
+
 #endif /* __CAIRO_PRIVATE_H__ */
 
diff --git a/modules/cairo-surface.c b/modules/cairo-surface.c
new file mode 100644
index 0000000..d0dfcaa
--- /dev/null
+++ b/modules/cairo-surface.c
@@ -0,0 +1,149 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/* Copyright 2010 litl, LLC. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <gjs/gjs.h>
+#include <cairo.h>
+#include "cairo-private.h"
+
+GJS_DEFINE_PROTO_ABSTRACT("CairoSurface", gjs_cairo_surface)
+
+GJS_DEFINE_PRIV_FROM_JS(GjsCairoSurface, gjs_cairo_surface_class)
+
+static void
+gjs_cairo_surface_finalize(JSContext *context,
+                           JSObject  *obj)
+{
+    GjsCairoSurface *priv;
+    priv = JS_GetPrivate(context, obj);
+    if (priv == NULL)
+        return;
+    cairo_surface_destroy(priv->surface);
+    g_slice_free(GjsCairoSurface, priv);
+}
+
+void
+gjs_cairo_surface_construct(JSContext *context, JSObject *obj, cairo_surface_t *surface)
+{
+    GjsCairoSurface *priv;
+
+    priv = g_slice_new0(GjsCairoSurface);
+
+    g_assert(priv_from_js(context, obj) == NULL);
+    JS_SetPrivate(context, obj, priv);
+
+    priv->context = context;
+    priv->object = obj;
+    priv->surface = surface;
+}
+
+/* Properties */
+static JSPropertySpec gjs_cairo_surface_proto_props[] = {
+    { NULL }
+};
+
+/* Methods */
+static JSBool
+writeToPNG_func(JSContext *context,
+                JSObject  *obj,
+                uintN      argc,
+                jsval     *argv,
+                jsval     *retval)
+{
+    char *filename;
+    cairo_surface_t *surface;
+
+    if (!gjs_parse_args(context, "writeToPNG", "s", argc, argv,
+                        "filename", &filename))
+        return JS_FALSE;
+
+    surface = gjs_cairo_surface_get_surface(context, obj);
+    if (!surface)
+        return JS_FALSE;
+    cairo_surface_write_to_png(surface, filename);
+    g_free(filename);
+    return JS_TRUE;
+}
+
+static JSBool
+getType_func(JSContext *context,
+             JSObject  *object,
+             uintN      argc,
+             jsval     *argv,
+             jsval     *retval)
+{
+    cairo_surface_t *surface;
+    cairo_surface_type_t type;
+
+    if (argc > 1) {
+        gjs_throw(context, "FIXME");
+        return JS_FALSE;
+    }
+
+    surface = gjs_cairo_surface_get_surface(context, object);
+    type = cairo_surface_get_type(surface);
+
+    *retval = INT_TO_JSVAL(type);
+    return JS_TRUE;
+}
+
+static JSFunctionSpec gjs_cairo_surface_proto_funcs[] = {
+    // flush
+    // getContent
+    // getFontOptions
+    { "getType", getType_func, 0, 0 },
+    // markDirty
+    // markDirtyRectangle
+    // setDeviceOffset
+    // getDeviceOffset
+    // setFallbackResolution
+    // getFallbackResolution
+    // copyPage
+    // showPage
+    // hasShowTextGlyphs
+    { "writeToPNG", writeToPNG_func, 0, 0 },
+    { NULL }
+};
+
+/* Public API */
+void
+gjs_cairo_surface_finalize_surface(JSContext *context,
+                                   JSObject  *obj)
+{
+    g_return_if_fail(context != NULL);
+    g_return_if_fail(obj != NULL);
+
+    gjs_cairo_surface_finalize(context, obj);
+}
+
+cairo_surface_t *
+gjs_cairo_surface_get_surface(JSContext *context,
+                              JSObject *object)
+{
+    GjsCairoSurface *priv;
+    priv = JS_GetPrivate(context, object);
+    if (priv == NULL)
+        return NULL;
+    return priv->surface;
+}
+
diff --git a/modules/cairo.c b/modules/cairo.c
index ef05b84..7b6dd12 100644
--- a/modules/cairo.c
+++ b/modules/cairo.c
@@ -31,12 +31,19 @@ gjs_js_define_cairo_stuff(JSContext      *context,
                           JSObject       *module_obj)
 {
     jsval obj;
+    JSObject *surface_proto;
 
     obj = gjs_cairo_context_create_proto(context, module_obj,
                                          "Context", NULL);
     if (obj == JSVAL_NULL)
         return JS_FALSE;
 
+    obj = gjs_cairo_surface_create_proto(context, module_obj,
+                                         "Surface", NULL);
+    if (obj == JSVAL_NULL)
+        return JS_FALSE;
+    surface_proto = JSVAL_TO_OBJECT(obj);
+
     return JS_TRUE;
 }
 



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