Graphviz  2.41.20170921.2350
gvc.c
Go to the documentation of this file.
1  /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #include "config.h"
15 
16 #include "gvc.h"
17 #include "const.h"
18 #include "gvcjob.h"
19 #include "gvcint.h"
20 #include "gvcproc.h"
21 #include "gvconfig.h"
22 #include "gvio.h"
23 #include <stdlib.h>
24 
26 {
27  GVC_t *gvc;
28 
29  agattr(NULL, AGNODE, "label", NODENAME_ESC);
30  /* default to no builtins, demand loading enabled */
31  gvc = gvNEWcontext(NULL, TRUE);
32  gvconfig(gvc, FALSE); /* configure for available plugins */
33  return gvc;
34 }
35 
36 GVC_t *gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
37 {
38  GVC_t *gvc;
39 
40  agattr(NULL, AGNODE, "label", NODENAME_ESC);
41  gvc = gvNEWcontext(builtins, demand_loading);
42  gvconfig(gvc, FALSE); /* configure for available plugins */
43  return gvc;
44 }
45 
46 
47 
48 /* gvLayout:
49  * Selects layout based on engine and binds it to gvc;
50  * does the layout and sets the graph's bbox.
51  * Return 0 on success.
52  */
53 int gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
54 {
55  char buf[256];
56  int rc;
57 
58  rc = gvlayout_select(gvc, engine);
59  if (rc == NO_SUPPORT) {
60  agerr (AGERR, "Layout type: \"%s\" not recognized. Use one of:%s\n",
61  engine, gvplugin_list(gvc, API_layout, engine));
62  return -1;
63  }
64 
65  if (gvLayoutJobs(gvc, g) == -1)
66  return -1;
67 
68 /* set bb attribute for basic layout.
69  * doesn't yet include margins, scaling or page sizes because
70  * those depend on the renderer being used. */
71  if (GD_drawing(g)->landscape)
72  sprintf(buf, "%d %d %d %d",
73  ROUND(GD_bb(g).LL.y), ROUND(GD_bb(g).LL.x),
74  ROUND(GD_bb(g).UR.y), ROUND(GD_bb(g).UR.x));
75  else
76  sprintf(buf, "%d %d %d %d",
77  ROUND(GD_bb(g).LL.x), ROUND(GD_bb(g).LL.y),
78  ROUND(GD_bb(g).UR.x), ROUND(GD_bb(g).UR.y));
79  agsafeset(g, "bb", buf, "");
80 
81  return 0;
82 }
83 
84 /* Render layout in a specified format to an open FILE */
85 int gvRender(GVC_t *gvc, graph_t *g, const char *format, FILE *out)
86 {
87  int rc;
88  GVJ_t *job;
89 
90  g = g->root;
91 
92  /* create a job for the required format */
93  rc = gvjobs_output_langname(gvc, format);
94  job = gvc->job;
95  if (rc == NO_SUPPORT) {
96  agerr (AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
97  format, gvplugin_list(gvc, API_device, format));
98  return -1;
99  }
100 
101  job->output_lang = gvrender_select(job, job->output_langname);
102  if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
103  agerrorf( "Layout was not done\n");
104  return -1;
105  }
106  job->output_file = out;
107  if (out == NULL)
108  job->flags |= OUTPUT_NOT_REQUIRED;
109  rc = gvRenderJobs(gvc, g);
110  gvrender_end_job(job);
111  gvjobs_delete(gvc);
112 
113  return rc;
114 }
115 
116 /* Render layout in a specified format to a file with the given name */
117 int gvRenderFilename(GVC_t *gvc, graph_t *g, const char *format, const char *filename)
118 {
119  int rc;
120  GVJ_t *job;
121 
122  g = g->root;
123 
124  /* create a job for the required format */
125  rc = gvjobs_output_langname(gvc, format);
126  job = gvc->job;
127  if (rc == NO_SUPPORT) {
128  agerr(AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
129  format, gvplugin_list(gvc, API_device, format));
130  return -1;
131  }
132 
133  job->output_lang = gvrender_select(job, job->output_langname);
134  if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
135  agerrorf( "Layout was not done\n");
136  return -1;
137  }
138  gvjobs_output_filename(gvc, filename);
139  rc = gvRenderJobs(gvc, g);
140  gvrender_end_job(job);
141  gvdevice_finalize(job);
142  gvjobs_delete(gvc);
143 
144  return rc;
145 }
146 
147 /* Render layout in a specified format to an external context */
148 int gvRenderContext(GVC_t *gvc, graph_t *g, const char *format, void *context)
149 {
150  int rc;
151  GVJ_t *job;
152 
153  g = g->root;
154 
155  /* create a job for the required format */
156  rc = gvjobs_output_langname(gvc, format);
157  job = gvc->job;
158  if (rc == NO_SUPPORT) {
159  agerr(AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
160  format, gvplugin_list(gvc, API_device, format));
161  return -1;
162  }
163 
164  job->output_lang = gvrender_select(job, job->output_langname);
165  if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
166  agerrorf( "Layout was not done\n");
167  return -1;
168  }
169 
170  job->context = context;
171  job->external_context = TRUE;
172 
173  rc = gvRenderJobs(gvc, g);
174  gvrender_end_job(job);
175  gvdevice_finalize(job);
176  gvjobs_delete(gvc);
177 
178  return rc;
179 }
180 
181 /* Render layout in a specified format to a malloc'ed string */
182 int gvRenderData(GVC_t *gvc, graph_t *g, const char *format, char **result, unsigned int *length)
183 {
184  int rc;
185  GVJ_t *job;
186 
187  g = g->root;
188 
189  /* create a job for the required format */
190  rc = gvjobs_output_langname(gvc, format);
191  job = gvc->job;
192  if (rc == NO_SUPPORT) {
193  agerr(AGERR, "Format: \"%s\" not recognized. Use one of:%s\n",
194  format, gvplugin_list(gvc, API_device, format));
195  return -1;
196  }
197 
198  job->output_lang = gvrender_select(job, job->output_langname);
199  if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
200  agerrorf( "Layout was not done\n");
201  return -1;
202  }
203 
204 /* page size on Linux, Mac OS X and Windows */
205 #define OUTPUT_DATA_INITIAL_ALLOCATION 4096
206 
207  if(!result || !(*result = malloc(OUTPUT_DATA_INITIAL_ALLOCATION))) {
208  agerr(AGERR, "failure malloc'ing for result string");
209  return -1;
210  }
211 
212  job->output_data = *result;
214  job->output_data_position = 0;
215 
216  rc = gvRenderJobs(gvc, g);
217  gvrender_end_job(job);
218 
219  if (rc == 0) {
220  *result = job->output_data;
221  *length = job->output_data_position;
222  }
223  gvjobs_delete(gvc);
224 
225  return rc;
226 }
227 
228 /* gvFreeRenderData:
229  * Utility routine to free memory allocated in gvRenderData, as the application code may use
230  * a different runtime library.
231  */
232 void gvFreeRenderData (char* data)
233 {
234  free (data);
235 }
236 
238 {
240 }
241 
242 char **gvcInfo(GVC_t* gvc) { return gvc->common.info; }
243 char *gvcVersion(GVC_t* gvc) { return gvc->common.info[1]; }
244 char *gvcBuildDate(GVC_t* gvc) { return gvc->common.info[2]; }
char ** gvcInfo(GVC_t *gvc)
Definition: gvc.c:242
Definition: cgraph.h:388
Agsym_t * agattr(Agraph_t *g, int kind, char *name, char *value)
Definition: attr.c:324
#define LAYOUT_NOT_REQUIRED
Definition: gvcjob.h:110
int gvRenderFilename(GVC_t *gvc, graph_t *g, const char *format, const char *filename)
Definition: gvc.c:117
void gvconfig(GVC_t *gvc, boolean rescan)
Definition: gvconfig.c:479
unsigned int output_data_allocated
Definition: gvcjob.h:288
void * context
Definition: gvcjob.h:304
int gvRenderData(GVC_t *gvc, graph_t *g, const char *format, char **result, unsigned int *length)
Definition: gvc.c:182
char * gvcBuildDate(GVC_t *gvc)
Definition: gvc.c:244
#define ROUND(f)
Definition: arith.h:84
int gvRenderJobs(GVC_t *gvc, graph_t *g)
Definition: emit.c:4094
int agerr(agerrlevel_t level, const char *fmt,...)
Definition: agerror.c:141
int flags
Definition: gvcjob.h:308
char * gvcVersion(GVC_t *gvc)
Definition: gvc.c:243
Definition: gvcjob.h:271
void gvFreeRenderData(char *data)
Definition: gvc.c:232
#define NODENAME_ESC
Definition: const.h:81
#define NO_SUPPORT
Definition: const.h:151
int gvlayout_select(GVC_t *gvc, const char *str)
Definition: gvlayout.c:33
int gvLayoutJobs(GVC_t *gvc, graph_t *g)
Definition: gvlayout.c:55
char * output_data
Definition: gvcjob.h:287
unsigned int output_data_position
Definition: gvcjob.h:289
#define LAYOUT_DONE(g)
Definition: gvc.h:39
void gvdevice_finalize(GVJ_t *job)
Definition: gvdevice.c:323
Definition: gvcint.h:70
GVC_t * gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
Definition: gvc.c:36
char ** info
Definition: gvcommon.h:22
void gvconfig_plugin_install_from_library(GVC_t *gvc, char *path, gvplugin_library_t *library)
Definition: gvconfig.c:210
int agsafeset(void *obj, char *name, char *value, char *def)
Definition: attr.c:497
#define AGNODE
Definition: cgraph.h:101
int gvRenderContext(GVC_t *gvc, graph_t *g, const char *format, void *context)
Definition: gvc.c:148
#define NULL
Definition: logic.h:39
int gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
Definition: gvc.c:53
boolean external_context
Definition: gvcjob.h:305
GVC_t * gvc
Definition: htmlparse.c:87
char * gvplugin_list(GVC_t *gvc, api_t api, const char *str)
Definition: gvplugin.c:342
int gvRender(GVC_t *gvc, graph_t *g, const char *format, FILE *out)
Definition: gvc.c:85
#define OUTPUT_DATA_INITIAL_ALLOCATION
boolean gvjobs_output_langname(GVC_t *gvc, const char *name)
Definition: gvjobs.c:64
GVC_t * gvContext(void)
Definition: gvc.c:25
void gvjobs_output_filename(GVC_t *gvc, const char *name)
Definition: gvjobs.c:45
GVCOMMON_t common
Definition: gvcint.h:71
#define GD_bb(g)
Definition: types.h:357
void gvrender_end_job(GVJ_t *job)
Definition: gvrender.c:123
Agraph_t * root
Definition: cgraph.h:247
#define OUTPUT_NOT_REQUIRED
Definition: gvcjob.h:111
int gvrender_select(GVJ_t *job, const char *lang)
Definition: gvrender.c:47
int output_lang
Definition: gvcjob.h:292
GVC_t * gvNEWcontext(const lt_symlist_t *builtins, int demand_loading)
Definition: gvcontext.c:52
void gvAddLibrary(GVC_t *gvc, gvplugin_library_t *lib)
Definition: gvc.c:237
#define GD_drawing(g)
Definition: types.h:356
void gvjobs_delete(GVC_t *gvc)
Definition: gvjobs.c:135
FILE * output_file
Definition: gvcjob.h:286
GVJ_t * job
Definition: gvcint.h:104
#define FALSE
Definition: cgraph.h:35
Definition: legal.c:60
const char * output_langname
Definition: gvcjob.h:291
void agerrorf(const char *fmt,...)
Definition: agerror.c:152
#define TRUE
Definition: cgraph.h:38