Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gvc.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <gvc/gvc.h>
14#include <common/const.h>
15#include <gvc/gvcjob.h>
16#include <gvc/gvcint.h>
17#include <gvc/gvcproc.h>
18#include <gvc/gvconfig.h>
19#include <gvc/gvio.h>
20#include <math.h>
21#include <stdbool.h>
22#include <stdlib.h>
23
25{
26 GVC_t *gvc;
27
28 agattr(NULL, AGNODE, "label", NODENAME_ESC);
29 /* default to no builtins, demand loading enabled */
30 gvc = gvNEWcontext(NULL, true);
31 gvconfig(gvc, false); /* configure for available plugins */
32 return gvc;
33}
34
35GVC_t *gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
36{
37 GVC_t *gvc;
38
39 agattr(NULL, AGNODE, "label", NODENAME_ESC);
40 gvc = gvNEWcontext(builtins, demand_loading);
41 gvconfig(gvc, false); /* configure for available plugins */
42 return gvc;
43}
44
45
46
47/* gvLayout:
48 * Selects layout based on engine and binds it to gvc;
49 * does the layout and sets the graph's bbox.
50 * Return 0 on success.
51 */
52int gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
53{
54 char buf[256];
55 int rc;
56
58 if (rc == NO_SUPPORT) {
59 agerrorf("Layout type: \"%s\" not recognized. Use one of:%s\n",
60 engine, gvplugin_list(gvc, API_layout, engine));
61 return -1;
62 }
63
64 if (gvLayoutJobs(gvc, g) == -1)
65 return -1;
66
67/* set bb attribute for basic layout.
68 * doesn't yet include margins, scaling or page sizes because
69 * those depend on the renderer being used. */
70 if (GD_drawing(g)->landscape)
71 snprintf(buf, sizeof(buf), "%.0f %.0f %.0f %.0f",
72 round(GD_bb(g).LL.y), round(GD_bb(g).LL.x),
73 round(GD_bb(g).UR.y), round(GD_bb(g).UR.x));
74 else
75 snprintf(buf, sizeof(buf), "%.0f %.0f %.0f %.0f",
76 round(GD_bb(g).LL.x), round(GD_bb(g).LL.y),
77 round(GD_bb(g).UR.x), round(GD_bb(g).UR.y));
78 agsafeset(g, "bb", buf, "");
79
80 return 0;
81}
82
83/* Render layout in a specified format to an open FILE */
84int gvRender(GVC_t *gvc, graph_t *g, const char *format, FILE *out)
85{
86 int rc;
87 GVJ_t *job;
88
89 /* create a job for the required format */
91 job = gvc->job;
92 if (!r) {
93 agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
94 format, gvplugin_list(gvc, API_device, format));
95 return -1;
96 }
97
99 if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
100 agerrorf( "Layout was not done\n");
101 return -1;
102 }
103 job->output_file = out;
104 if (out == NULL)
106 rc = gvRenderJobs(gvc, g);
107 gvrender_end_job(job);
109
110 return rc;
111}
112
113/* Render layout in a specified format to a file with the given name */
114int gvRenderFilename(GVC_t *gvc, graph_t *g, const char *format, const char *filename)
115{
116 int rc;
117 GVJ_t *job;
118
119 /* create a job for the required format */
121 job = gvc->job;
122 if (!r) {
123 agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
124 format, gvplugin_list(gvc, API_device, format));
125 return -1;
126 }
127
129 if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
130 agerrorf( "Layout was not done\n");
131 return -1;
132 }
133 gvjobs_output_filename(gvc, filename);
134 rc = gvRenderJobs(gvc, g);
135 gvrender_end_job(job);
138
139 return rc;
140}
141
142/* Render layout in a specified format to an external context */
143int gvRenderContext(GVC_t *gvc, graph_t *g, const char *format, void *context)
144{
145 int rc;
146 GVJ_t *job;
147
148 /* create a job for the required format */
150 job = gvc->job;
151 if (!r) {
152 agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
153 format, gvplugin_list(gvc, API_device, format));
154 return -1;
155 }
156
158 if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
159 agerrorf( "Layout was not done\n");
160 return -1;
161 }
162
163 job->context = context;
164 job->external_context = true;
165
166 rc = gvRenderJobs(gvc, g);
167 gvrender_end_job(job);
170
171 return rc;
172}
173
174/* Render layout in a specified format to a malloc'ed string */
175int gvRenderData(GVC_t *gvc, graph_t *g, const char *format, char **result,
176 size_t *length) {
177 int rc;
178 GVJ_t *job;
179
180 /* create a job for the required format */
182 job = gvc->job;
183 if (!r) {
184 agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
185 format, gvplugin_list(gvc, API_device, format));
186 return -1;
187 }
188
190 if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
191 agerrorf( "Layout was not done\n");
192 return -1;
193 }
194
195/* page size on Linux, Mac OS X and Windows */
196#define OUTPUT_DATA_INITIAL_ALLOCATION 4096
197
198 if(!result || !(*result = malloc(OUTPUT_DATA_INITIAL_ALLOCATION))) {
199 agerrorf("failure malloc'ing for result string");
200 return -1;
201 }
202
203 job->output_data = *result;
205 job->output_data_position = 0;
206
207 rc = gvRenderJobs(gvc, g);
208 gvrender_end_job(job);
209
210 if (rc == 0) {
211 *result = job->output_data;
212 *length = job->output_data_position;
213 }
215
216 return rc;
217}
218
219/* gvFreeRenderData:
220 * Utility routine to free memory allocated in gvRenderData, as the application code may use
221 * a different runtime library.
222 */
224{
225 free (data);
226}
227
232
233char **gvcInfo(GVC_t* gvc) { return gvc->common.info; }
234char *gvcVersion(GVC_t* gvc) { return gvc->common.info[1]; }
235char *gvcBuildDate(GVC_t* gvc) { return gvc->common.info[2]; }
236
static void out(agerrlevel_t level, const char *fmt, va_list args)
Report messages using a user-supplied or default write function.
Definition agerror.c:84
#define NO_SUPPORT
Definition const.h:147
#define NODENAME_ESC
Definition const.h:80
void * malloc(YYSIZE_T)
void free(void *)
node NULL
Definition grammar.y:163
Agsym_t * agattr(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up attributes of a graph
Definition attr.c:338
int agsafeset(void *obj, char *name, const char *value, const char *def)
ensures the given attribute is declared before setting it locally on an object
Definition attr.c:507
void agerrorf(const char *fmt,...)
Definition agerror.c:165
#define GD_drawing(g)
Definition types.h:353
#define GD_bb(g)
Definition types.h:354
@ AGNODE
Definition cgraph.h:207
void gvAddLibrary(GVC_t *gvc, gvplugin_library_t *lib)
Definition gvc.c:228
#define LAYOUT_DONE(g)
Definition gvc.h:52
GVC_t * gvNEWcontext(const lt_symlist_t *builtins, int demand_loading)
Definition gvcontext.c:45
char * gvcBuildDate(GVC_t *gvc)
Definition gvc.c:235
int gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
Definition gvc.c:52
GVC_t * gvContext(void)
Definition gvc.c:24
int gvRender(GVC_t *gvc, graph_t *g, const char *format, FILE *out)
Definition gvc.c:84
int gvRenderJobs(GVC_t *gvc, graph_t *g)
Definition emit.c:3893
int gvRenderData(GVC_t *gvc, graph_t *g, const char *format, char **result, size_t *length)
Definition gvc.c:175
int gvLayoutJobs(GVC_t *gvc, graph_t *g)
Definition gvlayout.c:53
GVC_t * gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
Definition gvc.c:35
char ** gvcInfo(GVC_t *gvc)
Definition gvc.c:233
char * gvcVersion(GVC_t *gvc)
Definition gvc.c:234
void gvFreeRenderData(char *data)
Definition gvc.c:223
int gvRenderContext(GVC_t *gvc, graph_t *g, const char *format, void *context)
Definition gvc.c:143
int gvRenderFilename(GVC_t *gvc, graph_t *g, const char *format, const char *filename)
Definition gvc.c:114
static GVC_t * gvc
Definition gv.cpp:23
#define OUTPUT_DATA_INITIAL_ALLOCATION
Graphviz context library.
#define OUTPUT_NOT_REQUIRED
Definition gvcjob.h:108
#define LAYOUT_NOT_REQUIRED
Definition gvcjob.h:107
void gvconfig(GVC_t *gvc, bool rescan)
Definition gvconfig.c:552
void gvconfig_plugin_install_from_library(GVC_t *gvc, char *package_path, gvplugin_library_t *library)
Definition gvconfig.c:219
void gvrender_end_job(GVJ_t *job)
Definition gvrender.c:116
void gvjobs_output_filename(GVC_t *gvc, const char *name)
Definition gvjobs.c:44
void gvdevice_finalize(GVJ_t *job)
Definition gvdevice.c:334
bool gvjobs_output_langname(GVC_t *gvc, const char *name)
Definition gvjobs.c:63
int gvlayout_select(GVC_t *gvc, const char *str)
Definition gvlayout.c:31
void gvjobs_delete(GVC_t *gvc)
Definition gvjobs.c:104
int gvrender_select(GVJ_t *job, const char *lang)
Definition gvrender.c:40
char * gvplugin_list(GVC_t *gvc, api_t api, const char *str)
Definition gvplugin.c:356
GVIO_API const char * format
Definition gvio.h:51
static gvloadimage_engine_t engine
graph or subgraph
Definition cgraph.h:425
char ** info
Definition gvcommon.h:20
Definition gvcint.h:80
GVCOMMON_t common
Definition gvcint.h:81
GVJ_t * job
Definition gvcint.h:115
size_t output_data_allocated
Definition gvcjob.h:279
size_t output_data_position
Definition gvcjob.h:280
int flags
Definition gvcjob.h:299
int output_lang
Definition gvcjob.h:283
char * output_data
Definition gvcjob.h:278
void * context
Definition gvcjob.h:295
bool external_context
Definition gvcjob.h:296
FILE * output_file
Definition gvcjob.h:277
const char * output_langname
Definition gvcjob.h:282
Definition legal.c:50