[gjs: 32/45] [cairo] Return the correct surface type



commit 7086b514d872fe2b6dea15d6e1a2ef6c3ef0855d
Author: Johan Dahlin <johan gnome org>
Date:   Tue Mar 2 11:45:01 2010 -0300

    [cairo] Return the correct surface type
    
    When creating a wrapper for a cairo_surface_t, check the type before
    chosing which wrapper class to use.

 modules/cairo-image-surface.c |   21 +++++++++++++++++++++
 modules/cairo-pdf-surface.c   |   21 +++++++++++++++++++++
 modules/cairo-private.h       |    9 +++++++++
 modules/cairo-ps-surface.c    |   21 +++++++++++++++++++++
 modules/cairo-surface.c       |   13 +++++++++++++
 modules/cairo-svg-surface.c   |   21 +++++++++++++++++++++
 6 files changed, 106 insertions(+), 0 deletions(-)
---
diff --git a/modules/cairo-image-surface.c b/modules/cairo-image-surface.c
index 1616e48..f746605 100644
--- a/modules/cairo-image-surface.c
+++ b/modules/cairo-image-surface.c
@@ -112,6 +112,27 @@ static JSFunctionSpec gjs_cairo_image_surface_proto_funcs[] = {
     { NULL }
 };
 
+JSObject *
+gjs_cairo_image_surface_from_surface(JSContext       *context,
+                                     cairo_surface_t *surface)
+{
+    JSObject *object;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(surface != NULL, NULL);
+    g_return_val_if_fail(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_image_surface_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create image surface");
+        return NULL;
+    }
+
+    gjs_cairo_surface_construct(context, object, surface);
+
+    return object;
+}
+
 void
 gjs_cairo_image_surface_init(JSContext *context, JSObject *module_obj)
 {
diff --git a/modules/cairo-pdf-surface.c b/modules/cairo-pdf-surface.c
index cef856f..081c410 100644
--- a/modules/cairo-pdf-surface.c
+++ b/modules/cairo-pdf-surface.c
@@ -81,4 +81,25 @@ static JSFunctionSpec gjs_cairo_pdf_surface_proto_funcs[] = {
     { NULL }
 };
 
+JSObject *
+gjs_cairo_pdf_surface_from_surface(JSContext       *context,
+                                   cairo_surface_t *surface)
+{
+    JSObject *object;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(surface != NULL, NULL);
+    g_return_val_if_fail(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_PDF, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_pdf_surface_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create pdf surface");
+        return NULL;
+    }
+
+    gjs_cairo_surface_construct(context, object, surface);
+
+    return object;
+}
+
 #endif /* CAIRO_HAS_PDF_SURFACE */
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 4ad267c..5371152 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -63,12 +63,17 @@ jsval            gjs_cairo_image_surface_create_proto   (JSContext       *contex
                                                          JSObject        *parent);
 void             gjs_cairo_image_surface_init           (JSContext       *context,
                                                          JSObject        *object);
+JSObject *       gjs_cairo_image_surface_from_surface   (JSContext       *context,
+                                                         cairo_surface_t *surface);
+
 /* postscript surface */
 #ifdef CAIRO_HAS_PS_SURFACE
 jsval            gjs_cairo_ps_surface_create_proto      (JSContext       *context,
                                                          JSObject        *module,
                                                          const char      *proto_name,
                                                          JSObject        *parent);
+JSObject *       gjs_cairo_ps_surface_from_surface       (JSContext       *context,
+                                                          cairo_surface_t *surface);
 #endif
 
 /* pdf surface */
@@ -77,6 +82,8 @@ jsval            gjs_cairo_pdf_surface_create_proto     (JSContext       *contex
                                                          JSObject        *module,
                                                          const char      *proto_name,
                                                          JSObject        *parent);
+JSObject *       gjs_cairo_pdf_surface_from_surface     (JSContext       *context,
+                                                         cairo_surface_t *surface);
 #endif
 
 /* svg surface */
@@ -85,6 +92,8 @@ jsval            gjs_cairo_svg_surface_create_proto     (JSContext       *contex
                                                          JSObject        *module,
                                                          const char      *proto_name,
                                                          JSObject        *parent);
+JSObject *       gjs_cairo_svg_surface_from_surface     (JSContext       *context,
+                                                         cairo_surface_t *surface);
 #endif
 
 /* pattern */
diff --git a/modules/cairo-ps-surface.c b/modules/cairo-ps-surface.c
index fb1fb2f..87f6301 100644
--- a/modules/cairo-ps-surface.c
+++ b/modules/cairo-ps-surface.c
@@ -90,4 +90,25 @@ static JSFunctionSpec gjs_cairo_ps_surface_proto_funcs[] = {
     { NULL }
 };
 
+JSObject *
+gjs_cairo_ps_surface_from_surface(JSContext       *context,
+                                  cairo_surface_t *surface)
+{
+    JSObject *object;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(surface != NULL, NULL);
+    g_return_val_if_fail(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_PS, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_ps_surface_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create ps surface");
+        return NULL;
+    }
+
+    gjs_cairo_surface_construct(context, object, surface);
+
+    return object;
+}
+
 #endif /* CAIRO_HAS_PS_SURFACE */
diff --git a/modules/cairo-surface.c b/modules/cairo-surface.c
index 3a4c64f..445d2a2 100644
--- a/modules/cairo-surface.c
+++ b/modules/cairo-surface.c
@@ -190,6 +190,19 @@ gjs_cairo_surface_from_surface(JSContext       *context,
     g_return_val_if_fail(context != NULL, NULL);
     g_return_val_if_fail(surface != NULL, NULL);
 
+    switch (cairo_surface_get_type(surface)) {
+        case CAIRO_SURFACE_TYPE_IMAGE:
+            return gjs_cairo_image_surface_from_surface(context, surface);
+        case CAIRO_SURFACE_TYPE_PDF:
+            return gjs_cairo_pdf_surface_from_surface(context, surface);
+        case CAIRO_SURFACE_TYPE_PS:
+            return gjs_cairo_ps_surface_from_surface(context, surface);
+        case CAIRO_SURFACE_TYPE_SVG:
+            return gjs_cairo_svg_surface_from_surface(context, surface);
+        default:
+            break;
+    }
+
     object = JS_NewObject(context, &gjs_cairo_surface_class, NULL, NULL);
     if (!object) {
         gjs_throw(context, "failed to create surface");
diff --git a/modules/cairo-svg-surface.c b/modules/cairo-svg-surface.c
index 93d46f3..21365ca 100644
--- a/modules/cairo-svg-surface.c
+++ b/modules/cairo-svg-surface.c
@@ -81,4 +81,25 @@ static JSFunctionSpec gjs_cairo_svg_surface_proto_funcs[] = {
     { NULL }
 };
 
+JSObject *
+gjs_cairo_svg_surface_from_surface(JSContext       *context,
+                                   cairo_surface_t *surface)
+{
+    JSObject *object;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(surface != NULL, NULL);
+    g_return_val_if_fail(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_SVG, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_svg_surface_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create svg surface");
+        return NULL;
+    }
+
+    gjs_cairo_surface_construct(context, object, surface);
+
+    return object;
+}
+
 #endif /* CAIRO_HAS_SVG_SURFACE */



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