[gjs/cairo] [cairo] Add a couple of methods to CairoContext



commit e327f2b9bb733604f627225854cc4ab83c06d7f8
Author: Johan Dahlin <johan gnome org>
Date:   Wed Feb 17 21:51:42 2010 -0200

    [cairo] Add a couple of methods to CairoContext
    
    scale/rectangle/set_source_rgb/stroke/set_line_width, and port
    over the first cairo example from the tutorial.

 Makefile-examples.am     |    2 +-
 examples/cairo.js        |    6 --
 examples/cairo/stroke.js |   10 ++++
 modules/cairo-context.c  |  135 ++++++++++++++++++++++++++++++++++++++++++++++
 modules/cairo-private.h  |    3 +
 5 files changed, 149 insertions(+), 7 deletions(-)
---
diff --git a/Makefile-examples.am b/Makefile-examples.am
index 1889a4d..b2c3b59 100644
--- a/Makefile-examples.am
+++ b/Makefile-examples.am
@@ -1,5 +1,5 @@
 EXTRA_DIST +=			        \
-	examples/cairo.js                 \
+	examples/cairo/stroke.js                 \
 	examples/clutter.js		\
 	examples/gio-cat.js                 \
 	examples/gtk.js                 \
diff --git a/examples/cairo/stroke.js b/examples/cairo/stroke.js
new file mode 100644
index 0000000..d78c5a6
--- /dev/null
+++ b/examples/cairo/stroke.js
@@ -0,0 +1,10 @@
+const Cairo = imports.cairo;
+
+let surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 120, 120);
+let cr = new Cairo.Context(surface);
+cr.scale(120, 120);
+cr.set_line_width(0.1);
+cr.set_source_rgb(0, 0, 0);
+cr.rectangle(0.25, 0.25, 0.5, 0.5);
+cr.stroke();
+surface.write_to_png("stroke.png");
diff --git a/modules/cairo-context.c b/modules/cairo-context.c
index c853f7c..ce5e343 100644
--- a/modules/cairo-context.c
+++ b/modules/cairo-context.c
@@ -111,6 +111,125 @@ cairo_context_finalize(JSContext *context,
     g_slice_free(GjsCairoContext, priv);
 }
 
+static JSBool
+rectangle_func(JSContext *context,
+               JSObject  *obj,
+               uintN      argc,
+               jsval     *argv,
+               jsval     *retval)
+{
+    double x, y, width, height;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "rectangle", "ffff", argc, argv,
+                        "x", &x, "y", &y, "width", &width, "height", &height))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_cr(context, obj);
+    cairo_rectangle(cr, x, y, width, height);
+
+    return JS_TRUE;
+}
+
+static JSBool
+scale_func(JSContext *context,
+           JSObject  *obj,
+           uintN      argc,
+           jsval     *argv,
+           jsval     *retval)
+{
+    double sx, sy;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "scale", "ff", argc, argv,
+                        "sx", &sx, "sy", &sy))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_cr(context, obj);
+    cairo_scale(cr, sx, sy);
+
+    return JS_TRUE;
+}
+static JSBool
+set_line_width_func(JSContext *context,
+                    JSObject  *obj,
+                    uintN      argc,
+                    jsval     *argv,
+                    jsval     *retval)
+{
+    double width;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "set_line_width", "f", argc, argv,
+                        "width", &width))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_cr(context, obj);
+    cairo_set_line_width(cr, width);
+
+    return JS_TRUE;
+}
+
+static JSBool
+set_source_rgb_func(JSContext *context,
+                    JSObject  *obj,
+                    uintN      argc,
+                    jsval     *argv,
+                    jsval     *retval)
+{
+    double r, g, b;
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "set_source_rgb", "fff", argc, argv,
+                        "red", &r, "green", &g, "blue", &b))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_cr(context, obj);
+    cairo_set_source_rgb(cr, r, g, b);
+
+    return JS_TRUE;
+}
+
+static JSBool
+stroke_func(JSContext *context,
+            JSObject  *obj,
+            uintN      argc,
+            jsval     *argv,
+            jsval     *retval)
+{
+    cairo_t *cr;
+
+    if (argc > 1) {
+        /* FIXME: set exception */
+        return JS_FALSE;
+    }
+    cr = gjs_cairo_context_get_cr(context, obj);
+    cairo_stroke(cr);
+
+    return JS_TRUE;
+}
+
+/* Template
+static JSBool
+_func(JSContext *context,
+               JSObject  *obj,
+               uintN      argc,
+               jsval     *argv,
+               jsval     *retval)
+{
+    cairo_t *cr;
+
+    if (!gjs_parse_args(context, "", "", argc, argv,
+                        ))
+        return JS_FALSE;
+
+    cr = gjs_cairo_context_get_cr(context, obj);
+    cairo_(cr, );
+
+    return JS_TRUE;
+}
+*/
+
 static struct JSClass gjs_js_cairo_context_class = {
     "CairoContext", /* means "new CairoContext()" works */
     JSCLASS_HAS_PRIVATE |
@@ -135,6 +254,11 @@ static JSPropertySpec gjs_js_cairo_context_proto_props[] = {
 };
 
 static JSFunctionSpec gjs_js_cairo_context_proto_funcs[] = {
+    { "rectangle", rectangle_func, 0, 0 },
+    { "scale", scale_func, 0, 0 },
+    { "set_line_width", set_line_width_func, 0, 0 },
+    { "set_source_rgb", set_source_rgb_func, 0, 0 },
+    { "stroke", stroke_func, 0, 0 },
     { NULL }
 };
 
@@ -178,3 +302,14 @@ gjs_cairo_context_create_proto(JSContext *context)
     return cairo_context;
 }
 
+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 050beac..e996886 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -29,9 +29,12 @@ JSBool gjs_js_define_cairo_stuff(JSContext *context,
                                  JSObject  *module_obj);
 
 jsval gjs_cairo_context_create_proto(JSContext *context);
+cairo_t *gjs_cairo_context_get_cr(JSContext *context,
+                                  JSObject *object);
 jsval gjs_cairo_image_surface_create_proto(JSContext *context);
 cairo_surface_t * gjs_cairo_image_surface_get_surface(JSContext *context,
                                                       JSObject *object);
 
+
 #endif /* __CAIRO_PRIVATE_H__ */
 



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