Debian Codegraph @

lib/gvc/gvc.c

raw
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
24 GVC_t *gvContext(void)
25 {
26 GVC_t *gvc;
27
28 agattr_text(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
35 GVC_t *gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
36 {
37 GVC_t *gvc;
38
39 agattr_text(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 */
52 int gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
53 {
54 char buf[256];
55 int rc;
56
57 rc = gvlayout_select(gvc, engine);
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 */
84 int 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 */
90 bool r = gvjobs_output_langname(gvc, 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
98 job->output_lang = gvrender_select(job, job->output_langname);
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)
105 job->flags |= OUTPUT_NOT_REQUIRED;
106 rc = gvRenderJobs(gvc, g);
107 gvrender_end_job(job);
108 gvjobs_delete(gvc);
109
110 return rc;
111 }
112
113 /* Render layout in a specified format to a file with the given name */
114 int 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 */
120 bool r = gvjobs_output_langname(gvc, 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
128 job->output_lang = gvrender_select(job, job->output_langname);
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);
136 gvdevice_finalize(job);
137 gvjobs_delete(gvc);
138
139 return rc;
140 }
141
142 /* Render layout in a specified format to an external context */
143 int 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 */
149 bool r = gvjobs_output_langname(gvc, 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
157 job->output_lang = gvrender_select(job, job->output_langname);
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);
168 gvdevice_finalize(job);
169 gvjobs_delete(gvc);
170
171 return rc;
172 }
173
174 /* Render layout in a specified format to a malloc'ed string */
175 int 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 */
181 bool r = gvjobs_output_langname(gvc, 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
189 job->output_lang = gvrender_select(job, job->output_langname);
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;
204 job->output_data_allocated = OUTPUT_DATA_INITIAL_ALLOCATION;
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 }
214 gvjobs_delete(gvc);
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 */
223 void gvFreeRenderData (char* data)
224 {
225 free (data);
226 }
227
228 void gvAddLibrary(GVC_t *gvc, gvplugin_library_t *lib)
229 {
230 gvconfig_plugin_install_from_library(gvc, NULL, lib);
231 }
232
233 char **gvcInfo(GVC_t* gvc) { return gvc->common.info; }
234 char *gvcVersion(GVC_t* gvc) { return gvc->common.info[1]; }
235 char *gvcBuildDate(GVC_t* gvc) { return gvc->common.info[2]; }
236
237 /**
238 * @dir lib/gvc
239 * @brief Graphviz context library, API gvc.h
240 *
241 * [man 3 gvc](https://graphviz.org/pdf/gvc.3.pdf)
242 *
243 */