]> granicus.if.org Git - graphviz/commitdiff
improve code structure
authorellson <devnull@localhost>
Mon, 13 Oct 2008 17:38:16 +0000 (17:38 +0000)
committerellson <devnull@localhost>
Mon, 13 Oct 2008 17:38:16 +0000 (17:38 +0000)
plugin/gs/gvloadimage_gs.c

index a76cc90556fb49d5996b5f01782dab6a88d88ec5..f4d4b180fe4a5523bfa706a4bc85b95ebd7160e6 100644 (file)
@@ -83,12 +83,26 @@ static void gs_error(GVJ_t * job, const char *name, const char *funstr, int err)
                name, funstr, err, gs_error_names[-err - 1], errsrc);
 }
 
-static cairo_pattern_t* gvloadimage_gs_load(GVJ_t * job, usershape_t *us)
+static int gvloadimage_process_file(GVJ_t *job, usershape_t *us, void *instance)
 {
-    gs_t *gs = NULL;
-    gsapi_revision_t gsapi_revision_info;
-    void *instance;
-    int rc, exit_code;
+    int rc = 0, exit_code;
+
+    if (! gvusershape_file_access(us)) {
+       job->common->errorfn("Failure to read shape file\n");
+       return -1;
+    }
+    rc = gsapi_run_file(instance, us->name, -1, &exit_code);
+    if (rc) {
+       gs_error(job, us->name, "gsapi_run_file", rc);
+    }
+    gvusershape_file_release(us);
+    return rc;
+}
+
+static int gvloadimage_process_surface(GVJ_t *job, usershape_t *us, gs_t *gs, void *instance)
+{
+    cairo_t *cr; /* temp cr for gs */
+    int rc, rc2;
     char width_height[20], dpi[10], cairo_context[30];
     char *gs_args[] = {
        "-dQUIET",
@@ -100,12 +114,58 @@ static cairo_pattern_t* gvloadimage_gs_load(GVJ_t * job, usershape_t *us)
     };
 #define GS_ARGC sizeof(gs_args)/sizeof(gs_args[0])
 
+    gs->surface = cairo_surface_create_similar( 
+       cairo_get_target(gs->cr),
+       CAIRO_CONTENT_COLOR_ALPHA,
+       us->x + us->w,
+       us->y + us->h);
+
+    cr = cairo_create(gs->surface);  /* temp context for gs */
+
+    sprintf(width_height, "-g%dx%d", us->x + us->w, us->y + us->h);
+    sprintf(dpi, "-r%d", us->dpi);
+    sprintf(cairo_context, "-sCairoContext=%p", cr);
+
+    rc = gsapi_init_with_args(instance, GS_ARGC, gs_args);
+
+    cairo_destroy(cr); /* finished with temp context */
+
+    if (rc)
+       gs_error(job, us->name, "gsapi_init_with_args", rc);
+    else
+       rc = gvloadimage_process_file(job, us, instance);
+
+    if (rc) {
+       cairo_surface_destroy(gs->surface);
+       gs->surface = NULL;
+    }
+
+    rc2 = gsapi_exit(instance);
+    if (rc2) {
+       gs_error(job, us->name, "gsapi_exit", rc2);
+       return rc2;
+    }
+
+    if (!rc) 
+        gs->pattern = cairo_pattern_create_for_surface (gs->surface);
+
+    return rc;
+}
+
+static cairo_pattern_t* gvloadimage_gs_load(GVJ_t * job, usershape_t *us)
+{
+    gs_t *gs = NULL;
+    gsapi_revision_t gsapi_revision_info;
+    void *instance;
+    int rc;
+
     assert(job);
     assert(us);
     assert(us->name);
 
     if (us->data) {
-        if (us->datafree == gvloadimage_gs_free && ((gs_t*)(us->data))->cr == (cairo_t *)job->context)
+        if (us->datafree == gvloadimage_gs_free
+       && ((gs_t*)(us->data))->cr == (cairo_t *)job->context)
            gs = (gs_t*)(us->data); /* use cached data */
        else {
            us->datafree(us);        /* free incompatible cache data */
@@ -113,12 +173,18 @@ static cairo_pattern_t* gvloadimage_gs_load(GVJ_t * job, usershape_t *us)
        }
     }
     if (!gs) {
-       cairo_t *cr; /* temp cr for gs */
-
-       if (!gvusershape_file_access(us)) {
-           job->common->errorfn("Failure to read shape file\n");
+       gs = (gs_t *)malloc(sizeof(gs_t));
+       if (!gs) {
+           job->common->errorfn("malloc() failure\n");
            return NULL;
        }
+       gs->cr = (cairo_t *)job->context;
+       gs->surface = NULL;
+       gs->pattern = NULL;
+
+       /* cache this - even if things go bad below - avoids repeats */
+       us->data = (void*)gs;
+       us->datafree = gvloadimage_gs_free;
 
 #define GSAPI_REVISION_REQUIRED 863
        rc = gsapi_revision(&gsapi_revision_info, sizeof(gsapi_revision_t));
@@ -133,70 +199,16 @@ static cairo_pattern_t* gvloadimage_gs_load(GVJ_t * job, usershape_t *us)
        }
 
        rc = gsapi_new_instance(&instance, (void*)job);
-       if (rc) {
+       if (rc)
            gs_error(job, us->name, "gsapi_new_instance", rc);
-           return NULL;
-       }
-
-       rc = gsapi_set_stdio(instance, NULL, gs_writer, gs_writer);
-       if (rc) {
-           gs_error(job, us->name, "gsapi_set_stdio", rc);
-           gsapi_delete_instance(instance);
-           return NULL;
-       }
-
-       gs = (gs_t *)malloc(sizeof(gs_t));
-       if (!gs) {
-           job->common->errorfn("malloc() failure\n");
-           return NULL;
-       }
-       gs->cr = (cairo_t *)job->context;
-       gs->surface = cairo_surface_create_similar( 
-                       cairo_get_target(gs->cr),
-                       CAIRO_CONTENT_COLOR_ALPHA,
-                       us->x + us->w,
-                       us->y + us->h);
-       gs->pattern = cairo_pattern_create_for_surface (gs->surface);
-
-       sprintf(width_height, "-g%dx%d", us->x + us->w, us->y + us->h);
-       sprintf(dpi, "-r%d", us->dpi);
-       cr = cairo_create(gs->surface);  /* temp context for gs */
-       sprintf(cairo_context, "-sCairoContext=%p", cr);
-       rc = gsapi_init_with_args(instance, GS_ARGC, gs_args);
-       cairo_destroy(cr); /* finished with temp context */
-       if (rc) {
-           gs_error(job, us->name, "gsapi_init_with_args", rc);
+       else {
+           rc = gsapi_set_stdio(instance, NULL, gs_writer, gs_writer);
+           if (rc)
+               gs_error(job, us->name, "gsapi_set_stdio", rc);
+           else
+                rc = gvloadimage_process_surface(job, us, gs, instance);
            gsapi_delete_instance(instance);
-           cairo_surface_destroy(gs->surface);
-           cairo_pattern_destroy(gs->pattern);
-           free(gs);
-           return NULL;
        }
-
-       rc = gsapi_run_file(instance, us->name, -1, &exit_code); 
-       if (rc) {
-           gs_error(job, us->name, "gsapi_run_file", rc);
-           /* cache the result anyway, so that we don't repeat */
-           cairo_surface_destroy(gs->surface);
-           gs->surface = NULL;
-           cairo_pattern_destroy(gs->pattern);
-           gs->pattern = NULL;
-       }
-       rc = gsapi_exit(instance);
-       if (rc) {
-           gs_error(job, us->name, "gsapi_exit", rc);
-           /* cache the result anyway, so that we don't repeat */
-           cairo_surface_destroy(gs->surface);
-           gs->surface = NULL;
-           cairo_pattern_destroy(gs->pattern);
-           gs->pattern = NULL;
-       }
-       gsapi_delete_instance(instance);
-
-       us->data = (void*)gs;
-       us->datafree = gvloadimage_gs_free;
-
-        gvusershape_file_release(us);
     }
     return gs->pattern;
 }