Graphviz 13.0.0~dev.20250121.0651
Loading...
Searching...
No Matches
gvpack.cpp
Go to the documentation of this file.
1
6/*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16
17/*
18 * Written by Emden R. Gansner
19 */
20
21
22
23#include "config.h"
24
25#include <getopt.h>
26#include <algorithm>
27#include <cassert>
28#include <gvc/gvc.h>
29#include <cgraph/ingraphs.h>
30#include <common/render.h>
31#include <common/utils.h>
32#include <neatogen/neatoprocs.h>
33#include <iostream>
34#include <limits>
35#include <map>
36#include <optional>
37#include <pack/pack.h>
38#include <set>
39#include <stddef.h>
40#include <string>
41#include <utility>
42#include <util/alloc.h>
43#include <util/exit.h>
44#include <vector>
45#include "openFile.h"
46
47extern "C" {
48#ifdef GVDLL
49 __declspec(dllimport)
50#endif
52}
53
55#ifdef GVDLL
56 { "gvplugin_neato_layout_LTX_library", 0 },
57#else
58 { "gvplugin_neato_layout_LTX_library", &gvplugin_neato_layout_LTX_library },
59#endif
60 { 0, 0 }
61};
62
63/* gvpack:
64 * Input consists of graphs in dot format.
65 * The graphs should have pos, width and height set for nodes,
66 * bb set for clusters, and, optionally, spline info for edges.
67 * The graphs are packed geometrically and combined
68 * into a single output graph, ready to be sent to neato -s -n2.
69 * -m <i> specifies the margin, in points, about each graph.
70 */
71
72typedef struct {
73 char *name;
74 char *value;
75} attr_t;
76
77static int verbose = 0;
78static char **myFiles = 0;
79static FILE *outfp; /* output; stdout by default */
80static std::vector<attr_t> G_args; // Storage for -G arguments
81static bool doPack; /* Do packing if true */
82static char* gname = const_cast<char*>("root");
83
84#define NEWNODE(n) ((node_t*)ND_alg(n))
85
86static const char useString[] =
87 "Usage: gvpack [-gnuv?] [-m<margin>] {-array[_rc][n]] [-o<outf>] <files>\n\
88 -n - use node granularity\n\
89 -g - use graph granularity\n\
90 -array* - pack as array of graphs\n\
91 -G<n>=<v> - attach name/value attribute to output graph\n\
92 -m<n> - set margin to <n> points\n\
93 -s<gname> - use <gname> for name of root graph\n\
94 -o<outfile> - write output to <outfile>\n\
95 -u - no packing; just combine graphs\n\
96 -v - verbose\n\
97 -? - print usage\n\
98If no files are specified, stdin is used\n";
99
100static void usage(int v)
101{
102 std::cout << useString;
103 graphviz_exit(v);
104}
105
106/* setNameValue:
107 * If arg is a name-value pair, add it to the list
108 * and return 0; otherwise, return 1.
109 */
110static int setNameValue(char *arg)
111{
112 char *rhs = const_cast<char*>("true");
113
114 if (char *p = strchr(arg, '=')) {
115 *p++ = '\0';
116 rhs = p;
117 }
118 G_args.push_back(attr_t{arg, rhs});
119
120 return 0;
121}
122
123/* setUInt:
124 * If arg is an integer, value is stored in v
125 * and function returns 0; otherwise, returns 1.
126 */
127static int setUInt(unsigned int *v, char *arg)
128{
129 char *p;
130 unsigned int i;
131
132 i = (unsigned int) strtol(arg, &p, 10);
133 if (p == arg) {
134 std::cerr << "Error: bad value in flag -" << (arg - 1) << " - ignored\n";
135 return 1;
136 }
137 *v = i;
138 return 0;
139}
140
141static Agsym_t *agraphattr(Agraph_t *g, char *name, const char *value) {
142 return agattr(g, AGRAPH, name, value);
143}
144
145static Agsym_t *agnodeattr(Agraph_t *g, char *name, const char *value) {
146 return agattr(g, AGNODE, name, value);
147}
148
149static Agsym_t *agedgeattr(Agraph_t *g, char *name, const char *value) {
150 return agattr(g, AGEDGE, name, value);
151}
152
153static void init(int argc, char *argv[], pack_info* pinfo)
154{
155 int c;
156
157 agnodeattr(nullptr, const_cast<char*>("label"), NODENAME_ESC);
158 pinfo->mode = l_clust;
159 pinfo->margin = CL_OFFSET;
160 pinfo->doSplines = true; // Use edges in packing
161 pinfo->fixed = nullptr;
162 pinfo->sz = 0;
163
164 opterr = 0;
165 while ((c = getopt(argc, argv, ":na:gvum:s:o:G:?")) != -1) {
166 switch (c) {
167 case 'a': {
168 auto buf = std::string("a") + optarg + "\n";
169 parsePackModeInfo(buf.c_str(), pinfo->mode, pinfo);
170 break;
171 }
172 case 'n':
173 parsePackModeInfo ("node", pinfo->mode, pinfo);
174 break;
175 case 's':
176 gname = optarg;
177 break;
178 case 'g':
179 parsePackModeInfo ("graph", pinfo->mode, pinfo);
180 break;
181 case 'm':
182 setUInt(&pinfo->margin, optarg);
183 break;
184 case 'o':
185 if (outfp != nullptr)
186 fclose(outfp);
187 outfp = openFile("gvpack", optarg, "w");
188 break;
189 case 'u':
190 pinfo->mode = l_undef;
191 break;
192 case 'G':
193 if (*optarg)
194 setNameValue(optarg);
195 else
196 std::cerr << "gvpack: option -G missing argument - ignored\n";
197 break;
198 case 'v':
199 verbose = 1;
200 Verbose = 1;
201 break;
202 case ':':
203 std::cerr << "gvpack: option -" << (char)optopt
204 << " missing argument - ignored\n";
205 break;
206 case '?':
207 if (optopt == '\0' || optopt == '?')
208 usage(0);
209 else {
210 std::cerr << "gvpack: option -" << (char)optopt << " unrecognized\n";
211 usage(1);
212 }
213 break;
214 }
215 }
216 argv += optind;
217 argc -= optind;
218
219 if (argc > 0) {
220 myFiles = argv;
221 }
222 if (!outfp)
223 outfp = stdout; /* stdout the default */
224 if (verbose)
225 std::cerr << " margin " << pinfo->margin << '\n';
226}
227
228/* init_node_edge:
229 * initialize node and edge attributes
230 */
231static void init_node_edge(Agraph_t * g)
232{
233 node_t *n;
234 edge_t *e;
235 int nG = agnnodes(g);
236 attrsym_t *N_pos = agfindnodeattr(g, const_cast<char*>("pos"));
237 attrsym_t *N_pin = agfindnodeattr(g, const_cast<char*>("pin"));
238
239 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
241 user_pos(N_pos, N_pin, n, nG); /* set user position if given */
242 }
243 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
244 for (e = agfstout(g, n); e; e = agnxtout(g, e))
246 }
247}
248
249/* init_graph:
250 * Initialize attributes. We always do the minimum required by
251 * libcommon. If fill is true, we use init_nop (neato -n) to
252 * read in attributes relevant to the layout.
253 */
254static void init_graph(Agraph_t *g, bool fill, GVC_t *gvc) {
255 int d;
256 node_t *n;
257 edge_t *e;
258
259 aginit (g, AGRAPH, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
260 aginit (g, AGNODE, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true);
261 aginit (g, AGEDGE, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
262 GD_gvc(g) = gvc;
263 graph_init(g, false);
264 d = late_int(g, agfindgraphattr(g, const_cast<char*>("dim")), 2, 2);
265 if (d != 2) {
266 std::cerr << "Error: graph " << agnameof(g) << " has dim = " << d
267 << " (!= 2)\n";
268 graphviz_exit(1);
269 }
270 Ndim = GD_ndim(g) = 2;
272 if (fill) {
273 if (int ret = init_nop(g, 0)) {
274 if (ret < 0)
275 std::cerr << "Error loading layout info from graph " << agnameof(g) << '\n';
276 else if (ret > 0)
277 std::cerr << "gvpack does not support backgrounds as found in graph "
278 << agnameof(g) << '\n';
279 graphviz_exit(1);
280 }
281 if (Concentrate) { /* check for edges without pos info */
282 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
283 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
284 if (ED_spl(e) == nullptr) ED_edge_type(e) = IGNORED;
285 }
286 }
287 }
288 }
289}
290
291/* cloneAttrs:
292 * Copy all attributes from old object to new. Assume
293 * attributes have been initialized.
294 */
295static void cloneDfltAttrs(Agraph_t *old, Agraph_t *new_graph, int attr_kind) {
296 Agsym_t *a;
297
298 for (a = agnxtattr(old, attr_kind, 0); a; a = agnxtattr(old, attr_kind, a)) {
299 if (aghtmlstr(a->defval)) {
300 agattr_html(new_graph, attr_kind, a->name, a->defval);
301 } else {
302 agattr(new_graph, attr_kind, a->name, a->defval);
303 }
304 }
305}
306static void cloneAttrs(void *old, void *new_graph) {
307 int attr_kind = AGTYPE(old);
308 Agsym_t *a;
309 char* s;
310 Agraph_t *g = agroot(old);
311 Agraph_t *ng = agroot(new_graph);
312
313 for (a = agnxtattr(g, attr_kind, 0); a; a = agnxtattr(g, attr_kind, a)) {
314 s = agxget (old, a);
315 if (aghtmlstr(s)) {
316 char *scopy = agstrdup_html(ng, s);
317 agset(new_graph, a->name, scopy);
318 agstrfree(ng, scopy, true); // drop the extra reference count we bumped for scopy
319 } else {
320 agset(new_graph, a->name, s);
321 }
322 }
323}
324
325/* cloneEdge:
326 * Note that here, and in cloneNode and cloneCluster,
327 * we do a shallow copy. We thus assume that attributes
328 * are not disturbed. In particular, we assume they are
329 * not deallocated.
330 */
331static void cloneEdge(Agedge_t *old, Agedge_t *new_edge) {
332 cloneAttrs(old, new_edge);
333 ED_spl(new_edge) = ED_spl(old);
334 ED_edge_type(new_edge) = ED_edge_type(old);
335 ED_label(new_edge) = ED_label(old);
336 ED_head_label(new_edge) = ED_head_label(old);
337 ED_tail_label(new_edge) = ED_tail_label(old);
338 ED_xlabel(new_edge) = ED_xlabel(old);
339}
340
341static void cloneNode(Agnode_t *old, Agnode_t *new_node) {
342 cloneAttrs(old, new_node);
343 ND_coord(new_node).x = POINTS(ND_pos(old)[0]);
344 ND_coord(new_node).y = POINTS(ND_pos(old)[1]);
345 ND_height(new_node) = ND_height(old);
346 ND_ht(new_node) = ND_ht(old);
347 ND_width(new_node) = ND_width(old);
348 ND_lw(new_node) = ND_lw(old);
349 ND_rw(new_node) = ND_rw(old);
350 ND_shape(new_node) = ND_shape(old);
351 ND_shape_info(new_node) = ND_shape_info(old);
352 ND_xlabel(new_node) = ND_xlabel(old);
353}
354
355static void cloneCluster(Agraph_t *old, Agraph_t *new_cluster) {
356 // string attributes were cloned as subgraphs
357 GD_label(new_cluster) = GD_label(old);
358 GD_bb(new_cluster) = GD_bb(old);
359}
360
361namespace {
363struct AttributeValue {
364 std::string value;
365 size_t instances;
366};
367} // namespace
368
370using attr_map_t = std::map<std::string, AttributeValue>;
371
372/* fillDict:
373 * Fill newdict with all the name-value attributes of
374 * objp. If the attribute has already been defined and
375 * has a different default, set default to "".
376 */
377static void fillDict(attr_map_t &newdict, Agraph_t *g, int attr_kind) {
378
379 for (Agsym_t *a = agnxtattr(g, attr_kind, 0); a; a = agnxtattr(g, attr_kind, a)) {
380 char *name = a->name;
381 char *value = a->defval;
382 auto it = newdict.find(name);
383 if (it == newdict.end()) {
384 newdict.insert({name, AttributeValue{value, 1}});
385 } else if (it->second.value == value)
386 ++it->second.instances;
387 }
388}
389
390/* fillGraph:
391 * Use all the name-value entries in the dictionary d to define
392 * to define universal node/edge/graph attributes for g.
393 * For a non-empty default value, the attribute must be defined and the
394 * same in all graphs.
395 */
396static void fillGraph(Agraph_t *g, const attr_map_t &d,
397 Agsym_t *(*setf)(Agraph_t *, char *, const char *),
398 size_t cnt) {
399 for (const auto &kv : d) {
400 const std::string &name = kv.first;
401 const std::string &value = kv.second.value;
402 const size_t &attr_cnt = kv.second.instances;
403 if (cnt == attr_cnt)
404 setf(g, const_cast<char *>(name.c_str()), value.c_str());
405 else
406 setf(g, const_cast<char *>(name.c_str()), "");
407 }
408}
409
410/* initAttrs:
411 * Initialize the attributes of root as the union of the
412 * attributes in the graphs gs.
413 */
414static void initAttrs(Agraph_t *root, std::vector<Agraph_t*> &gs) {
415 attr_map_t n_attrs;
416 attr_map_t e_attrs;
417 attr_map_t g_attrs;
418
419 for (Agraph_t *g : gs) {
420 fillDict(g_attrs, g, AGRAPH);
421 fillDict(n_attrs, g, AGNODE);
422 fillDict(e_attrs, g, AGEDGE);
423 }
424
425 fillGraph(root, g_attrs, agraphattr, gs.size());
426 fillGraph(root, n_attrs, agnodeattr, gs.size());
427 fillGraph(root, e_attrs, agedgeattr, gs.size());
428}
429
430static void cloneGraphAttr(Agraph_t * g, Agraph_t * ng)
431{
432 cloneAttrs(g, ng);
433 cloneDfltAttrs(g, ng, AGNODE);
434 cloneDfltAttrs(g, ng, AGEDGE);
435}
436
438using used_t = std::multiset<std::string>;
439
440/* xName:
441 * Create a name for an object in the new graph using the
442 * dictionary names and the old name. If the old name has not
443 * been used, use it and add it to names. If it has been used,
444 * create a new name using the old name and a number.
445 * Note that returned string will immediately made into an agstring.
446 */
447static std::string xName(used_t &names, char *oldname) {
448 size_t previous_instances = names.count(oldname);
449 names.insert(oldname);
450 if (previous_instances > 0) {
451 return std::string(oldname) + "_gv" + std::to_string(previous_instances);
452 }
453 return oldname;
454}
455
456#define MARK(e) (ED_alg(e) = e)
457#define MARKED(e) (ED_alg(e))
458#define SETCLUST(g,h) (GD_alg(g) = h)
459#define GETCLUST(g) ((Agraph_t*)GD_alg(g))
460
461/* cloneSubg:
462 * Create a copy of g in ng, copying attributes, inserting nodes
463 * and adding edges.
464 */
465static void
466cloneSubg(Agraph_t *g, Agraph_t *ng, Agsym_t *G_bb, used_t &gnames) {
467 node_t *n;
468 node_t *nn;
469 edge_t *e;
470 edge_t *ne;
471 node_t *nt;
472 node_t *nh;
473 Agraph_t *subg;
474 Agraph_t *nsubg;
475
476 cloneGraphAttr(g, ng);
477 if (doPack)
478 agxset(ng, G_bb, ""); /* Unset all subgraph bb */
479
480 /* clone subgraphs */
481 for (subg = agfstsubg (g); subg; subg = agnxtsubg (subg)) {
482 nsubg = agsubg(ng, xName(gnames, agnameof(subg)).data(), 1);
483 agbindrec (nsubg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
484 cloneSubg(subg, nsubg, G_bb, gnames);
485 /* if subgraphs are clusters, point to the new
486 * one so we can find it later.
487 */
488 if (is_a_cluster(subg))
489 SETCLUST(subg, nsubg);
490 }
491
492 /* add remaining nodes */
493 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
494 nn = NEWNODE(n);
495 agsubnode(ng, nn, 1);
496 }
497
498 /* add remaining edges. libgraph doesn't provide a way to find
499 * multiedges, so we add edges bottom up, marking edges when added.
500 */
501 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
502 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
503 if (MARKED(e))
504 continue;
505 nt = NEWNODE(agtail(e));
506 nh = NEWNODE(aghead(e));
507 ne = agedge(ng, nt, nh, nullptr, 1);
508 agbindrec (ne, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
509 cloneEdge(e, ne);
510 MARK(e);
511 }
512 }
513}
514
515/* cloneClusterTree:
516 * Given old and new subgraphs which are corresponding
517 * clusters, recursively create the subtree of clusters
518 * under ng using the subtree of clusters under g.
519 */
520static void cloneClusterTree(Agraph_t * g, Agraph_t * ng)
521{
522 int i;
523
524 cloneCluster(g, ng);
525
526 if (GD_n_cluster(g)) {
527 GD_n_cluster(ng) = GD_n_cluster(g);
528 GD_clust(ng) = reinterpret_cast<Agraph_t**>(gv_calloc(1 + GD_n_cluster(g), sizeof(Agraph_t*)));
529 for (i = 1; i <= GD_n_cluster(g); i++) {
530 Agraph_t *c = GETCLUST(GD_clust(g)[i]);
531 GD_clust(ng)[i] = c;
532 cloneClusterTree(GD_clust(g)[i], c);
533 }
534 }
535}
536
537/* cloneGraph:
538 * Create and return a new graph which is the logical union
539 * of the graphs gs.
540 */
541static Agraph_t *cloneGraph(std::vector<Agraph_t *> &gs, GVC_t *gvc,
542 Agdesc_t kind) {
543 Agraph_t *root;
544 Agraph_t *subg;
545 Agnode_t *n;
546 Agnode_t *np;
547 Agsym_t *G_bb;
548 bool doWarn = true;
549
550 if (verbose)
551 std::cerr << "Creating clone graph\n";
552 root = agopen(gname, kind, &AgDefaultDisc);
553 initAttrs(root, gs);
554 G_bb = agfindgraphattr(root, const_cast<char*>("bb"));
555 if (doPack) assert(G_bb);
556
557 /* add command-line attributes */
558 for (attr_t &a : G_args) {
559 if (Agsym_t *rv = agfindgraphattr(root, a.name))
560 agxset(root, rv, a.value);
561 else
562 agattr(root, AGRAPH, a.name, a.value);
563 }
564
565 /* do common initialization. This will handle root's label. */
566 init_graph(root, false, gvc);
568
569 used_t gnames; // dict of used subgraph names
570 used_t nnames; // dict of used node names
571 for (size_t i = 0; i < gs.size(); i++) {
572 Agraph_t *g = gs[i];
573 if (verbose)
574 std::cerr << "Cloning graph " << agnameof(g) << '\n';
575 GD_n_cluster(root) += GD_n_cluster(g);
576 GD_has_labels(root) |= GD_has_labels(g);
577
578 /* Clone nodes, checking for node name conflicts */
579 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
580 if (doWarn && agfindnode(root, agnameof(n))) {
581 std::cerr << "Warning: node " << agnameof(n) << " in graph[" << i << "] "
582 << agnameof(g) << " already defined\n"
583 << "Some nodes will be renamed.\n";
584 doWarn = false;
585 }
586 np = agnode(root, xName(nnames, agnameof(n)).data(), 1);
587 agbindrec (np, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true);
588 ND_alg(n) = np;
589 cloneNode(n, np);
590 }
591
592 /* wrap the clone of g in a subgraph of root */
593 subg = agsubg(root, xName(gnames, agnameof(g)).data(), 1);
594 agbindrec (subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
595 cloneSubg(g, subg, G_bb, gnames);
596 }
597
598 /* set up cluster tree */
599 if (GD_n_cluster(root)) {
600 int j, idx;
601 GD_clust(root) = reinterpret_cast<graph_t**>(gv_calloc(1 + GD_n_cluster(root), sizeof(graph_t*)));
602
603 idx = 1;
604 for (Agraph_t *g : gs) {
605 for (j = 1; j <= GD_n_cluster(g); j++) {
606 Agraph_t *c = GETCLUST(GD_clust(g)[j]);
607 GD_clust(root)[idx++] = c;
608 cloneClusterTree(GD_clust(g)[j], c);
609 }
610 }
611 }
612
613 return root;
614}
615
616/* readGraphs:
617 * Read input, parse the graphs, use init_nop (neato -n) to
618 * read in all attributes need for layout.
619 * Return the list of graphs. If cp != nullptr, set it to the number
620 * of graphs read.
621 * We keep track of the types of graphs read. They all must be
622 * either directed or undirected. If all graphs are strict, the
623 * combined graph will be strict; other, the combined graph will
624 * be non-strict.
625 *
626 * @param kind [out] The type to use for the combined graph
627 */
628static std::vector<Agraph_t *> readGraphs(GVC_t *gvc,
629 std::optional<Agdesc_t> &kind) {
630 std::vector<Agraph_t*> gs;
631 ingraph_state ig;
632
633 /* set various state values */
635 Nop = 2;
636
637 newIngraph(&ig, myFiles);
638 while (Agraph_t *g = nextGraph(&ig)) {
639 if (verbose)
640 std::cerr << "Reading graph " << agnameof(g) << '\n';
641 if (agnnodes(g) == 0) {
642 std::cerr << "Graph " << agnameof(g) << " is empty - ignoring\n";
643 continue;
644 }
645 if (!kind.has_value()) {
646 kind = g->desc;
647 }
648 else if (kind->directed != g->desc.directed) {
649 std::cerr << "Error: all graphs must be directed or undirected\n";
650 graphviz_exit(1);
651 } else if (!agisstrict(g))
652 kind = g->desc;
653 init_graph(g, doPack, gvc);
654 gs.push_back(g);
655 }
656
657 return gs;
658}
659
660/* compBB:
661 * Compute the bounding box containing the graphs.
662 * We can just use the bounding boxes of the graphs.
663 */
664static boxf compBB(std::vector<Agraph_t*> &gs) {
665 boxf bb, bb2;
666
667 bb = GD_bb(gs[0]);
668
669 for (size_t i = 1; i < gs.size(); i++) {
670 bb2 = GD_bb(gs[i]);
671 bb.LL.x = std::min(bb.LL.x, bb2.LL.x);
672 bb.LL.y = std::min(bb.LL.y, bb2.LL.y);
673 bb.UR.x = std::max(bb.UR.x, bb2.UR.x);
674 bb.UR.y = std::max(bb.UR.y, bb2.UR.y);
675 }
676
677 return bb;
678}
679
680#ifdef DEBUG
681void dump(Agraph_t * g)
682{
683 node_t *v;
684 edge_t *e;
685
686 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
687 std::cerr << agnameof(v) << '\n';
688 for (e = agfstout(g, v); e; e = agnxtout(g, e)) {
689 std::cerr << " " << agnameof(agtail(e)) << " -- " << agnameof(aghead(e))
690 << '\n';
691 }
692 }
693}
694
695void dumps(Agraph_t * g)
696{
697 graph_t *subg;
698
699 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
700 dump(subg);
701 std::cerr << "====\n";
702 }
703}
704#endif
705
706int main(int argc, char *argv[])
707{
708 Agraph_t *g;
709 pack_info pinfo;
710 GVC_t * gvc;
711
712 init(argc, argv, &pinfo);
713
714 doPack = (pinfo.mode != l_undef);
715
716#if defined(_WIN32)
718#endif
719 gvc = gvContextPlugins(lt_preloaded_symbols, DEMAND_LOADING);
720 std::optional<Agdesc_t> kind; // type of graph
721 std::vector<Agraph_t*> gs = readGraphs(gvc, kind);
722 if (gs.empty())
723 graphviz_exit(0);
724
725 /* pack graphs */
726 if (doPack) {
727 if (packGraphs(gs.size(), gs.data(), 0, &pinfo)) {
728 std::cerr << "gvpack: packing of graphs failed.\n";
729 graphviz_exit(1);
730 }
731 }
732
733 /* create union graph and copy attributes */
734 assert(kind.has_value());
735 g = cloneGraph(gs, gvc, *kind);
736
737 /* compute new top-level bb and set */
738 if (doPack) {
739 GD_bb(g) = compBB(gs);
741 attach_attrs(g);
742 }
743 agwrite(g, outfp);
744 graphviz_exit(0);
745}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
int late_int(void *obj, attrsym_t *attr, int defaultValue, int minimum)
Definition utils.c:35
void common_init_edge(edge_t *e)
Definition utils.c:506
bool is_a_cluster(Agraph_t *g)
Definition utils.c:692
#define CL_OFFSET
Definition const.h:151
#define NODENAME_ESC
Definition const.h:80
#define IGNORED
Definition const.h:30
#define GVSPLINES
Definition const.h:173
#define agnodeattr(g, n, v)
Definition dotinit.c:273
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
#define POINTS(a_inches)
Definition geom.h:62
#define POINTS_PER_INCH
Definition geom.h:58
int State
Definition globals.h:62
bool Concentrate
Definition globals.h:58
int Nop
Definition globals.h:54
double PSinputscale
Definition globals.h:55
unsigned short Ndim
Definition globals.h:61
static bool Verbose
Definition gml2gv.c:23
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:206
int agnnodes(Agraph_t *g)
Definition graph.c:165
Agsym_t * agattr(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up attributes of a graph
Definition attr.c:371
int agset(void *obj, char *name, const char *value)
Definition attr.c:492
Agsym_t * agnxtattr(Agraph_t *g, int kind, Agsym_t *attr)
permits traversing the list of attributes of a given type
Definition attr.c:379
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:532
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:481
Agsym_t * agattr_html(Agraph_t *g, int kind, char *name, const char *value)
agattr, but creates HTML-like values
Definition attr.c:375
Agdisc_t AgDefaultDisc
Definition graph.c:285
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:256
#define ED_xlabel(e)
Definition types.h:590
#define ED_head_label(e)
Definition types.h:587
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
#define ED_spl(e)
Definition types.h:595
#define agtail(e)
Definition cgraph.h:888
#define ED_edge_type(e)
Definition types.h:582
#define ED_tail_label(e)
Definition types.h:596
#define aghead(e)
Definition cgraph.h:889
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
#define ED_label(e)
Definition types.h:589
#define agfindgraphattr(g, a)
Definition types.h:613
#define GD_has_labels(g)
Definition types.h:368
#define GD_clust(g)
Definition types.h:360
int agisstrict(Agraph_t *g)
Definition graph.c:196
#define GD_bb(g)
Definition types.h:354
#define GD_n_cluster(g)
Definition types.h:389
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:44
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:693
#define GD_ndim(g)
Definition types.h:390
#define GD_label(g)
Definition types.h:374
#define GD_gvc(g)
Definition types.h:355
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Definition node.c:140
#define ND_ht(n)
Definition types.h:500
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:254
#define agfindnodeattr(g, a)
Definition types.h:615
#define ND_alg(n)
Definition types.h:484
#define ND_rw(n)
Definition types.h:525
#define ND_height(n)
Definition types.h:498
#define ND_width(n)
Definition types.h:536
#define ND_lw(n)
Definition types.h:506
#define ND_xlabel(n)
Definition types.h:503
#define ND_shape_info(n)
Definition types.h:529
#define ND_pos(n)
Definition types.h:520
#define agfindnode(g, n)
Definition types.h:611
#define ND_coord(n)
Definition types.h:490
#define ND_shape(n)
Definition types.h:528
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Definition cgraph.h:216
Agraph_t * agroot(void *obj)
Definition obj.c:168
@ AGEDGE
Definition cgraph.h:207
@ AGNODE
Definition cgraph.h:207
@ AGRAPH
Definition cgraph.h:207
void aginit(Agraph_t *g, int kind, const char *rec_name, int rec_size, int move_to_front)
attach new records to objects of specified kind
Definition rec.c:170
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Definition rec.c:89
int aghtmlstr(const char *)
Definition refstr.c:401
int agstrfree(Agraph_t *, const char *, bool is_html)
Definition refstr.c:378
char * agstrdup_html(Agraph_t *, const char *)
Definition refstr.c:374
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:75
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:80
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Definition subg.c:55
GVC_t * gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
Definition gvc.c:35
void attach_attrs(graph_t *g)
Definition output.c:394
static GVC_t * gvc
Definition gv.cpp:23
#define agraphattr(g, n, s)
Definition gv.cpp:19
Graphviz context library.
static int verbose
Definition gvpack.cpp:77
static void cloneClusterTree(Agraph_t *g, Agraph_t *ng)
Definition gvpack.cpp:520
#define MARK(e)
Definition gvpack.cpp:456
static void cloneCluster(Agraph_t *old, Agraph_t *new_cluster)
Definition gvpack.cpp:355
static void cloneDfltAttrs(Agraph_t *old, Agraph_t *new_graph, int attr_kind)
Definition gvpack.cpp:295
static Agraph_t * cloneGraph(std::vector< Agraph_t * > &gs, GVC_t *gvc, Agdesc_t kind)
Definition gvpack.cpp:541
std::multiset< std::string > used_t
names that have already been used during generation
Definition gvpack.cpp:438
static void init_node_edge(Agraph_t *g)
Definition gvpack.cpp:231
lt_symlist_t lt_preloaded_symbols[]
Definition gvpack.cpp:54
static std::vector< attr_t > G_args
Definition gvpack.cpp:80
static char * gname
Definition gvpack.cpp:82
static int setNameValue(char *arg)
Definition gvpack.cpp:110
static void init_graph(Agraph_t *g, bool fill, GVC_t *gvc)
Definition gvpack.cpp:254
#define NEWNODE(n)
Definition gvpack.cpp:84
static void cloneSubg(Agraph_t *g, Agraph_t *ng, Agsym_t *G_bb, used_t &gnames)
Definition gvpack.cpp:466
#define MARKED(e)
Definition gvpack.cpp:457
static void cloneNode(Agnode_t *old, Agnode_t *new_node)
Definition gvpack.cpp:341
static FILE * outfp
Definition gvpack.cpp:79
static boxf compBB(std::vector< Agraph_t * > &gs)
Definition gvpack.cpp:664
static bool doPack
Definition gvpack.cpp:81
static void fillDict(attr_map_t &newdict, Agraph_t *g, int attr_kind)
Definition gvpack.cpp:377
static std::string xName(used_t &names, char *oldname)
Definition gvpack.cpp:447
static void cloneEdge(Agedge_t *old, Agedge_t *new_edge)
Definition gvpack.cpp:331
static void fillGraph(Agraph_t *g, const attr_map_t &d, Agsym_t *(*setf)(Agraph_t *, char *, const char *), size_t cnt)
Definition gvpack.cpp:396
#define SETCLUST(g, h)
Definition gvpack.cpp:458
static int setUInt(unsigned int *v, char *arg)
Definition gvpack.cpp:127
static const char useString[]
Definition gvpack.cpp:86
static void cloneGraphAttr(Agraph_t *g, Agraph_t *ng)
Definition gvpack.cpp:430
static void initAttrs(Agraph_t *root, std::vector< Agraph_t * > &gs)
Definition gvpack.cpp:414
static void cloneAttrs(void *old, void *new_graph)
Definition gvpack.cpp:306
static std::vector< Agraph_t * > readGraphs(GVC_t *gvc, std::optional< Agdesc_t > &kind)
Definition gvpack.cpp:628
static void init(int argc, char *argv[], pack_info *pinfo)
Definition gvpack.cpp:153
static char ** myFiles
Definition gvpack.cpp:78
std::map< std::string, AttributeValue > attr_map_t
attribute name → value collection of those we have seen
Definition gvpack.cpp:370
gvplugin_library_t gvplugin_neato_layout_LTX_library
static Agsym_t * agedgeattr(Agraph_t *g, char *name, const char *value)
Definition gvpack.cpp:149
#define GETCLUST(g)
Definition gvpack.cpp:459
static const char * usage
Definition gvpr.c:51
Agraph_t * nextGraph(ingraph_state *sp)
Definition ingraphs.c:61
ingraph_state * newIngraph(ingraph_state *sp, char **files)
Definition ingraphs.c:140
supports user-supplied data
void graph_init(graph_t *g, bool use_rankdir)
Definition input.c:592
bool user_pos(attrsym_t *posptr, attrsym_t *pinptr, node_t *np, int nG)
Definition neatoinit.c:75
int init_nop(Agraph_t *g, int adjust)
Definition neatoinit.c:536
static attrsym_t * N_pos
Definition neatoinit.c:54
void neato_init_node(node_t *n)
Definition neatoinit.c:60
static FILE * openFile(const char *argv0, const char *name, const char *mode)
Definition openFile.h:8
pack_mode parsePackModeInfo(const char *p, pack_mode dflt, pack_info *pinfo)
Definition pack.c:1209
int packGraphs(size_t ng, Agraph_t **gs, Agraph_t *root, pack_info *info)
Definition pack.c:1090
support for connected components
@ l_clust
Definition pack.h:55
@ l_undef
Definition pack.h:55
void dotneato_postprocess(Agraph_t *g)
Definition postproc.c:690
graph descriptor
Definition cgraph.h:284
unsigned directed
Definition cgraph.h:285
graph or subgraph
Definition cgraph.h:424
Agdesc_t desc
Definition cgraph.h:426
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:641
char * name
Definition cgraph.h:643
char * defval
Definition cgraph.h:644
Definition gvcint.h:80
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
Definition legal.c:50
void * address
Definition gvcext.h:41
pack_mode mode
Definition pack.h:72
int sz
Definition pack.h:69
bool doSplines
use splines in constructing graph shape
Definition pack.h:71
bool * fixed
Definition pack.h:73
unsigned int margin
Definition pack.h:70
double x
Definition geom.h:29
double y
Definition geom.h:29
int main()
Definition grammar.c:93