/misc/src/release/graphviz-2.18-1/src/graphviz-2.18/lib/gvc/gvc.c

Go to the documentation of this file.
00001 /* $Id: gvc.c,v 1.32 2008/02/02 09:11:28 glenlow Exp $ $Revision: 1.32 $ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /**********************************************************
00005 *      This software is part of the graphviz package      *
00006 *                http://www.graphviz.org/                 *
00007 *                                                         *
00008 *            Copyright (c) 1994-2004 AT&T Corp.           *
00009 *                and is licensed under the                *
00010 *            Common Public License, Version 1.0           *
00011 *                      by AT&T Corp.                      *
00012 *                                                         *
00013 *        Information and Software Systems Research        *
00014 *              AT&T Research, Florham Park NJ             *
00015 **********************************************************/
00016 
00017 #ifdef HAVE_CONFIG_H
00018 #include "config.h"
00019 #endif
00020 
00021 #include "builddate.h"
00022 #include "types.h"
00023 #include "graph.h"
00024 #include "const.h"
00025 #include "gvplugin.h"
00026 #include "gvcjob.h"
00027 #include "gvcint.h"
00028 #include "gvcproc.h"
00029 
00030 extern GVC_t *gvNEWcontext(char **info, char *user);
00031 extern char *gvUsername(void);
00032 extern int gvRenderJobs (GVC_t * gvc, graph_t * g);
00033 
00034 static char *LibInfo[] = {
00035     "libgvc",           /* Program */
00036     VERSION,            /* Version */
00037     BUILDDATE           /* Build Date */
00038 };
00039 
00040 GVC_t *gvContext(void)
00041 {
00042     GVC_t *gvc;
00043 
00044     aginit();
00045     agnodeattr(NULL, "label", NODENAME_ESC);
00046     gvc = gvNEWcontext(LibInfo, gvUsername());
00047     gvconfig(gvc, FALSE); /* configure for available plugins and codegens */
00048     return gvc;
00049 }
00050 
00051 /* gvLayout:
00052  * Selects layout based on engine and binds it to gvc;
00053  * does the layout and sets the graph's bbox.
00054  * Return 0 on success.
00055  */
00056 int gvLayout(GVC_t *gvc, graph_t *g, char *engine)
00057 {
00058     char buf[256];
00059     int rc;
00060 
00061     rc = gvlayout_select(gvc, engine);
00062     if (rc == NO_SUPPORT) {
00063         agerr (AGERR, "Layout type: \"%s\" not recognized. Use one of:%s\n",
00064                 engine, gvplugin_list(gvc, API_layout, engine));
00065         return -1;
00066     }
00067 
00068     gvLayoutJobs(gvc, g);
00069 
00070 /* set bb attribute for basic layout.
00071  * doesn't yet include margins, scaling or page sizes because
00072  * those depend on the renderer being used. */
00073     if (GD_drawing(g)->landscape)
00074         sprintf(buf, "%d %d %d %d",
00075                 ROUND(GD_bb(g).LL.y), ROUND(GD_bb(g).LL.x),
00076                 ROUND(GD_bb(g).UR.y), ROUND(GD_bb(g).UR.x));
00077     else
00078         sprintf(buf, "%d %d %d %d",
00079                 ROUND(GD_bb(g).LL.x), ROUND(GD_bb(g).LL.y),
00080                 ROUND(GD_bb(g).UR.x), ROUND(GD_bb(g).UR.y));
00081     agsafeset(g, "bb", buf, "");
00082 
00083     return 0;
00084 }
00085 
00086 /* Render layout in a specified format to an open FILE */
00087 int gvRender(GVC_t *gvc, graph_t *g, char *format, FILE *out)
00088 {
00089     int rc;
00090     GVJ_t *job;
00091 
00092     g = g->root;
00093 
00094     /* create a job for the required format */
00095     rc = gvjobs_output_langname(gvc, format);
00096     job = gvc->job;
00097     if (rc == NO_SUPPORT) {
00098         agerr (AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
00099                 format, gvplugin_list(gvc, API_device, format));
00100         return -1;
00101     }
00102 
00103     job->output_lang = gvrender_select(job, job->output_langname);
00104     if (!GD_drawing(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
00105         fprintf(stderr, "Layout was not done\n");
00106         return -1;
00107     }
00108     job->output_file = out;
00109     if (out == NULL)
00110         job->flags |= OUTPUT_NOT_REQUIRED;
00111     gvRenderJobs(gvc, g);
00112     gvrender_end_job(job);
00113     gvdevice_finalize(job);
00114     fflush(job->output_file);
00115     gvjobs_delete(gvc);
00116 
00117     return 0;
00118 }
00119 
00120 /* Render layout in a specified format to an open FILE */
00121 int gvRenderFilename(GVC_t *gvc, graph_t *g, char *format, char *filename)
00122 {
00123     int rc;
00124     GVJ_t *job;
00125 
00126     g = g->root;
00127 
00128     /* create a job for the required format */
00129     rc = gvjobs_output_langname(gvc, format);
00130     job = gvc->job;
00131     if (rc == NO_SUPPORT) {
00132         agerr(AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
00133                 format, gvplugin_list(gvc, API_device, format));
00134         return -1;
00135     }
00136 
00137     job->output_lang = gvrender_select(job, job->output_langname);
00138     if (!GD_drawing(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
00139         fprintf(stderr, "Layout was not done\n");
00140         return -1;
00141     }
00142     gvjobs_output_filename(gvc, filename);
00143     gvRenderJobs(gvc, g);
00144     gvrender_end_job(job);
00145     gvdevice_finalize(job);
00146     fflush(job->output_file);
00147     gvjobs_delete(gvc);
00148 
00149     return 0;
00150 }
00151 
00152 /* Render layout in a specified format to a malloc'ed string */
00153 int gvRenderData(GVC_t *gvc, graph_t *g, char *format, char **result, unsigned int *length)
00154 {
00155     int rc;
00156     GVJ_t *job;
00157 
00158     g = g->root;
00159 
00160     /* create a job for the required format */
00161     rc = gvjobs_output_langname(gvc, format);
00162     job = gvc->job;
00163     if (rc == NO_SUPPORT) {
00164         agerr(AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
00165                 format, gvplugin_list(gvc, API_device, format));
00166         return -1;
00167     }
00168 
00169     job->output_lang = gvrender_select(job, job->output_langname);
00170     if (!GD_drawing(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
00171         fprintf(stderr, "Layout was not done\n");
00172         return -1;
00173     }
00174 
00175 /* page size on Linux, Mac OS X and Windows */
00176 #define OUTPUT_DATA_INITIAL_ALLOCATION 4096
00177 
00178     if(!result || !(*result = malloc(OUTPUT_DATA_INITIAL_ALLOCATION))) {
00179         agerr(AGERR, "failure malloc'ing for result string");
00180         return -1;
00181     }
00182 
00183     job->output_data = *result;
00184     job->output_data_allocated = OUTPUT_DATA_INITIAL_ALLOCATION;
00185     job->output_data_position = 0;
00186 
00187     gvRenderJobs(gvc, g);
00188     gvrender_end_job(job);
00189     gvdevice_finalize(job);
00190 
00191     *result = job->output_data;
00192         *length = job->output_data_position;
00193     gvjobs_delete(gvc);
00194 
00195     return 0;
00196 }
00197 
00198 char **gvcInfo(GVC_t* gvc) { return gvc->common.info; }
00199 char *gvcUsername(GVC_t* gvc) { return gvc->common.user; }
00200 char *gvcVersion(GVC_t* gvc) { return gvc->common.info[1]; }
00201 char *gvcBuildDate(GVC_t* gvc) { return gvc->common.info[2]; }

Generated on Mon Mar 31 19:03:26 2008 for Graphviz by  doxygen 1.5.1