[gjs: 4/45] [cairo] Add an ImageSurface prototype



commit 153700ef121c6bd88b6daabc067dd59def58a202
Author: Johan Dahlin <johan gnome org>
Date:   Wed Feb 17 19:52:15 2010 -0200

    [cairo] Add an ImageSurface prototype

 Makefile-modules.am           |    1 +
 modules/cairo-image-surface.c |  131 +++++++++++++++++++++++++++++++++++++++++
 modules/cairo-private.h       |    7 ++
 modules/cairo.c               |    8 +++
 4 files changed, 147 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 42e3a13..0fd7300 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -88,6 +88,7 @@ cairoNative_la_SOURCES =		\
 	modules/cairo-private.h	\
 	modules/cairo-context.c \
 	modules/cairo-surface.c \
+	modules/cairo-image-surface.c \
 	modules/cairo.c
 
 
diff --git a/modules/cairo-image-surface.c b/modules/cairo-image-surface.c
new file mode 100644
index 0000000..37e5e44
--- /dev/null
+++ b/modules/cairo-image-surface.c
@@ -0,0 +1,131 @@
+/* -*- 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"
+
+#ifdef CAIRO_HAS_IMAGE_SURFACE
+
+GJS_DEFINE_PROTO("CairoImageSurface", gjs_cairo_image_surface)
+
+static JSBool
+gjs_cairo_image_surface_constructor(JSContext *context,
+                                    JSObject  *obj,
+                                    uintN      argc,
+                                    jsval     *argv,
+                                    jsval     *retval)
+{
+    int format, width, height;
+    cairo_surface_t *surface;
+    cairo_status_t status;
+
+    if (!gjs_check_constructing(context))
+        return JS_FALSE;
+
+    // create_for_data optional parameter
+    if (!gjs_parse_args(context, "ImageSurface", "iii", argc, argv,
+                        "format", &format,
+                        "width", &width,
+                        "height", &height))
+        return JS_FALSE;
+
+    surface = cairo_image_surface_create(format, width, height);
+    status = cairo_surface_status(surface);
+    if (status != CAIRO_STATUS_SUCCESS) {
+        gjs_throw(context, "Failed to create cairo surface: %s",
+                  cairo_status_to_string(status));
+        return JS_FALSE;
+    }
+    gjs_cairo_surface_construct(context, obj, surface);
+
+    return JS_TRUE;
+}
+
+static void
+gjs_cairo_image_surface_finalize(JSContext *context,
+                                 JSObject  *obj)
+{
+    gjs_cairo_surface_finalize_surface(context, obj);
+}
+
+static JSPropertySpec gjs_cairo_image_surface_proto_props[] = {
+    { NULL }
+};
+
+static JSBool
+createFromPNG_func(JSContext *context,
+                   JSObject  *obj,
+                   uintN      argc,
+                   jsval     *argv,
+                   jsval     *retval)
+{
+    char *filename;
+    cairo_surface_t *surface;
+    JSObject *surface_wrapper;
+    cairo_status_t status;
+
+    if (!gjs_parse_args(context, "createFromPNG", "s", argc, argv,
+                        "filename", &filename))
+        return JS_FALSE;
+
+    surface = cairo_image_surface_create_from_png(filename);
+    status = cairo_surface_status(surface);
+    if (status != CAIRO_STATUS_SUCCESS) {
+        gjs_throw(context, "failed to create surface: %s", cairo_status_to_string(status));
+        return JS_FALSE;
+    }
+    surface_wrapper = JS_NewObject(context, &gjs_cairo_image_surface_class, NULL, NULL);
+    if (!surface_wrapper) {
+        gjs_throw(context, "failed to create surface");
+        return JS_FALSE;
+    }
+    gjs_cairo_surface_construct(context, surface_wrapper, surface);
+
+    *retval = OBJECT_TO_JSVAL(surface_wrapper);
+    return JS_TRUE;
+}
+
+static JSFunctionSpec gjs_cairo_image_surface_proto_funcs[] = {
+    { "createFromPNG", createFromPNG_func, 0, 0},
+    // getData
+    // getFormat
+    // getWidth
+    // getHeight
+    // getStride
+    { NULL }
+};
+
+void
+gjs_cairo_image_surface_init(JSContext *context, JSObject *module_obj)
+{
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "createFromPNG",
+                           createFromPNG_func,
+                           1, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+}
+
+#endif /* CAIRO_HAS_IMAGE_SURFACE */
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index feccc1b..0157008 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -46,5 +46,12 @@ void gjs_cairo_surface_finalize_surface(JSContext *context, JSObject  *obj);
 cairo_surface_t * gjs_cairo_surface_get_surface(JSContext *context,
                                                 JSObject *object);
 
+/* image surface */
+#ifdef CAIRO_HAS_IMAGE_SURFACE
+jsval gjs_cairo_image_surface_create_proto(JSContext *context, JSObject *module,
+                                           const char *proto_name, JSObject *parent);
+void gjs_cairo_image_surface_init(JSContext *context, JSObject *obj);
+#endif
+
 #endif /* __CAIRO_PRIVATE_H__ */
 
diff --git a/modules/cairo.c b/modules/cairo.c
index 7b6dd12..513a237 100644
--- a/modules/cairo.c
+++ b/modules/cairo.c
@@ -44,6 +44,14 @@ gjs_js_define_cairo_stuff(JSContext      *context,
         return JS_FALSE;
     surface_proto = JSVAL_TO_OBJECT(obj);
 
+#if CAIRO_HAS_IMAGE_SURFACE
+    obj = gjs_cairo_image_surface_create_proto(context, module_obj,
+                                               "ImageSurface", surface_proto);
+    if (obj == JSVAL_NULL)
+        return JS_FALSE;
+    gjs_cairo_image_surface_init(context, JSVAL_TO_OBJECT(obj));
+#endif
+
     return JS_TRUE;
 }
 



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