[gjs] [cairo] Wrap cairo_copy_path, cairo_copy_path_flat, and cairo_append_path



commit a39c3428570f82ba914835ab9564510c75ab485b
Author: Colin Walters <walters verbum org>
Date:   Mon May 3 14:11:34 2010 -0400

    [cairo] Wrap cairo_copy_path, cairo_copy_path_flat, and cairo_append_path
    
    Currently we don't support iterating over the path as the language
    binding guide suggests; this simply allows one to call .copyPath()
    and then later call .appendPath() basically.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=617568

 Makefile-modules.am     |    1 +
 doc/cairo.txt           |    2 +-
 modules/cairo-context.c |   73 +++++++++++++++++++++++++++--
 modules/cairo-path.c    |  117 +++++++++++++++++++++++++++++++++++++++++++++++
 modules/cairo-private.h |    7 +++
 test/js/testCairo.js    |   10 ++++
 6 files changed, 205 insertions(+), 5 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 604e4a1..8b177ff 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-path.c                 \
 	modules/cairo-surface.c                 \
 	modules/cairo-image-surface.c           \
 	modules/cairo-ps-surface.c              \
diff --git a/doc/cairo.txt b/doc/cairo.txt
index 82d2c6d..17b1152 100644
--- a/doc/cairo.txt
+++ b/doc/cairo.txt
@@ -87,7 +87,7 @@ TODO:
 * image surface methods
 * matrix
 * version
-* cairo_path_t and iteration
+* iterating over cairo_path_t
 
 Fonts & Glyphs are not wrapped, use PangoCairo instead.
 * glyphs
diff --git a/modules/cairo-context.c b/modules/cairo-context.c
index 0d50721..cca2758 100644
--- a/modules/cairo-context.c
+++ b/modules/cairo-context.c
@@ -224,7 +224,6 @@ typedef struct {
 } GjsCairoContext;
 
 GJS_DEFINE_PROTO("CairoContext", gjs_cairo_context)
-
 GJS_DEFINE_PRIV_FROM_JS(GjsCairoContext, gjs_cairo_context_class);
 
 static void
@@ -376,6 +375,72 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC2(translate, cairo_translate, "ff", double, tx, do
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC0AFF(userToDevice, cairo_user_to_device)
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC0AFF(userToDeviceDistance, cairo_user_to_device_distance)
 
+
+static JSBool
+appendPath_func(JSContext *context,
+                JSObject  *obj,
+                uintN      argc,
+                jsval     *argv,
+                jsval     *retval)
+{
+    JSObject *path_wrapper;
+    cairo_path_t *path;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "path", "o", argc, argv,
+                        "path", &path_wrapper))
+        return JS_FALSE;
+
+    path = gjs_cairo_path_get_path(context, path_wrapper);
+    if (!path) {
+        gjs_throw(context, "first argument to appendPath() should be a path");
+        return JS_FALSE;
+    }
+
+    cr = gjs_cairo_context_get_context(context, obj);
+    cairo_append_path(cr, path);
+    *retval = JSVAL_VOID;
+    return JS_TRUE;
+}
+
+static JSBool
+copyPath_func(JSContext *context,
+              JSObject  *obj,
+              uintN      argc,
+              jsval     *argv,
+              jsval     *retval)
+{
+    cairo_path_t *path;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "", "", argc, argv))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_context(context, obj);
+    path = cairo_copy_path(cr);
+    *retval = OBJECT_TO_JSVAL(gjs_cairo_path_from_path(context, path));
+    return JS_TRUE;
+}
+
+static JSBool
+copyPathFlat_func(JSContext *context,
+                  JSObject  *obj,
+                  uintN      argc,
+                  jsval     *argv,
+                  jsval     *retval)
+{
+    cairo_path_t *path;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "", "", argc, argv))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_context(context, obj);
+    path = cairo_copy_path_flat(cr);
+    *retval = OBJECT_TO_JSVAL(gjs_cairo_path_from_path(context, path));
+    return JS_TRUE;
+}
+
 static JSBool
 mask_func(JSContext *context,
           JSObject  *obj,
@@ -691,7 +756,7 @@ getGroupTarget_func(JSContext *context,
 }
 
 static JSFunctionSpec gjs_cairo_context_proto_funcs[] = {
-    // appendPath
+    { "appendPath", appendPath_func, 0, 0},
     { "arc", arc_func, 0, 0 },
     { "arcNegative", arcNegative_func, 0, 0 },
     { "clip", clip_func, 0, 0 },
@@ -699,8 +764,8 @@ static JSFunctionSpec gjs_cairo_context_proto_funcs[] = {
     { "clipPreserve", clipPreserve_func, 0, 0 },
     { "closePath", closePath_func, 0, 0 },
     { "copyPage", copyPage_func, 0, 0 },
-    // copyPath
-    // copyPathFlat
+    { "copyPath", copyPath_func, 0, 0 },
+    { "copyPathFlat", copyPathFlat_func, 0, 0 },
     { "curveTo", curveTo_func, 0, 0 },
     { "deviceToUser", deviceToUser_func, 0, 0 },
     { "deviceToUserDistance", deviceToUserDistance_func, 0, 0 },
diff --git a/modules/cairo-path.c b/modules/cairo-path.c
new file mode 100644
index 0000000..8772823
--- /dev/null
+++ b/modules/cairo-path.c
@@ -0,0 +1,117 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/* Copyright 2010 Red Hat, Inc. 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"
+
+typedef struct {
+    JSContext       *context;
+    JSObject        *object;
+    cairo_path_t    *path;
+} GjsCairoPath;
+
+GJS_DEFINE_PROTO_ABSTRACT("CairoPath", gjs_cairo_path)
+GJS_DEFINE_PRIV_FROM_JS(GjsCairoPath, gjs_cairo_path_class)
+
+static void
+gjs_cairo_path_finalize(JSContext *context,
+                        JSObject  *obj)
+{
+    GjsCairoPath *priv;
+    priv = JS_GetPrivate(context, obj);
+    if (priv == NULL)
+        return;
+    cairo_path_destroy(priv->path);
+    g_slice_free(GjsCairoPath, priv);
+}
+
+/* Properties */
+static JSPropertySpec gjs_cairo_path_proto_props[] = {
+    { NULL }
+};
+
+static JSFunctionSpec gjs_cairo_path_proto_funcs[] = {
+    { NULL }
+};
+
+/**
+ * gjs_cairo_path_from_path:
+ * @context: the context
+ * @path: cairo_path_t to attach to the object
+ *
+ * Constructs a pattern wrapper given cairo pattern.
+ * NOTE: This function takes ownership of the path.
+ */
+JSObject *
+gjs_cairo_path_from_path(JSContext    *context,
+                         cairo_path_t *path)
+{
+    JSObject *object;
+    GjsCairoPath *priv;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(path != NULL, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_path_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create path");
+        return NULL;
+    }
+
+    priv = g_slice_new0(GjsCairoPath);
+
+    g_assert(priv_from_js(context, object) == NULL);
+    JS_SetPrivate(context, object, priv);
+
+    priv->context = context;
+    priv->object = object;
+    priv->path = path;
+
+    return object;
+}
+
+
+/**
+ * gjs_cairo_path_get_path:
+ * @context: the context
+ * @object: path wrapper
+ *
+ * Returns: the path attached to the wrapper.
+ *
+ */
+cairo_path_t *
+gjs_cairo_path_get_path(JSContext *context,
+                        JSObject  *object)
+{
+    GjsCairoPath *priv;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(object != NULL, NULL);
+
+    priv = JS_GetPrivate(context, object);
+    if (priv == NULL)
+        return NULL;
+    return priv->path;
+}
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 9d5948d..67aae0f 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -42,6 +42,13 @@ JSObject *       gjs_cairo_context_from_context         (JSContext       *contex
                                                          cairo_t         *cr);
 void             gjs_cairo_context_init                 (JSContext       *context);
 
+
+/* cairo_path_t */
+JSObject *       gjs_cairo_path_from_path               (JSContext       *context,
+                                                         cairo_path_t    *path);
+cairo_path_t *   gjs_cairo_path_get_path                (JSContext       *context,
+                                                         JSObject        *path_wrapper);
+
 /* surface */
 jsval            gjs_cairo_surface_create_proto         (JSContext       *context,
                                                          JSObject        *module,
diff --git a/test/js/testCairo.js b/test/js/testCairo.js
index 3dbaee3..6dbd0f1 100644
--- a/test/js/testCairo.js
+++ b/test/js/testCairo.js
@@ -110,6 +110,16 @@ function testContextMethods() {
     assertEquals("deviceToUserDistance", rv.length, 2);
 
     cr.showText("foobar");
+
+    cr.moveTo(0, 0);
+    cr.lineTo(1, 0);
+    cr.lineTo(1, 1);
+    cr.lineTo(0, 1);
+    cr.closePath();
+    let path = cr.copyPath();
+    cr.fill();
+    cr.appendPath(path);
+    cr.stroke();
 }
 
 function testSolidPattern() {



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