Debian Codegraph @

lib/gvc/gvlayout.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 /*
12 * layout engine wrapper
13 *
14 */
15
16 #include "config.h"
17
18 #include <common/const.h>
19 #include <gvc/gvplugin_layout.h>
20 #include <gvc/gvcint.h>
21 #include <cgraph/cgraph.h>
22 #include <gvc/gvcproc.h>
23 #include <gvc/gvc.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26
27 extern void graph_init(Agraph_t *g, bool use_rankdir);
28 extern void graph_cleanup(Agraph_t *g);
29 extern void gv_fixLocale (int set);
30
31 int gvlayout_select(GVC_t * gvc, const char *layout)
32 {
33 gvplugin_available_t *plugin;
34 gvplugin_installed_t *typeptr;
35
36 plugin = gvplugin_load(gvc, API_layout, layout, NULL);
37 if (plugin) {
38 typeptr = plugin->typeptr;
39 gvc->layout.type = typeptr->type;
40 gvc->layout.engine = typeptr->engine;
41 gvc->layout.id = typeptr->id;
42 gvc->layout.features = typeptr->features;
43 return GVRENDER_PLUGIN; /* FIXME - need better return code */
44 }
45 return NO_SUPPORT;
46 }
47
48 /* gvLayoutJobs:
49 * Layout input graph g based on layout engine attached to gvc.
50 * Check that the root graph has been initialized. If not, initialize it.
51 * Return 0 on success.
52 */
53 int gvLayoutJobs(GVC_t * gvc, Agraph_t * g)
54 {
55 gvlayout_engine_t *gvle;
56 char *p;
57 int rc;
58
59 agbindrec(g, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
60 GD_gvc(g) = gvc;
61 if (g != agroot(g)) {
62 agbindrec(agroot(g), "Agraphinfo_t", sizeof(Agraphinfo_t), true);
63 GD_gvc(agroot(g)) = gvc;
64 }
65
66 if ((p = agget(g, "layout"))) {
67 gvc->layout.engine = NULL;
68 rc = gvlayout_select(gvc, p);
69 if (rc == NO_SUPPORT) {
70 agerrorf("Layout type: \"%s\" not recognized. Use one of:%s\n",
71 p, gvplugin_list(gvc, API_layout, p));
72 return -1;
73 }
74 }
75
76 gvle = gvc->layout.engine;
77 if (! gvle)
78 return -1;
79
80 gv_fixLocale (1);
81 graph_init(g, !!(gvc->layout.features->flags & LAYOUT_USES_RANKDIR));
82 GD_drawing(agroot(g)) = GD_drawing(g);
83 if (gvle && gvle->layout) {
84 gvle->layout(g);
85
86
87 if (gvle->cleanup)
88 GD_cleanup(g) = gvle->cleanup;
89 }
90 gv_fixLocale (0);
91 return 0;
92 }
93
94 bool gvLayoutDone(Agraph_t * g)
95 {
96 return LAYOUT_DONE(g);
97 }
98
99 /* gvFreeLayout:
100 * Free layout resources.
101 * First, if the graph has a layout-specific cleanup function attached,
102 * use it and reset.
103 * Then do the general graph cleanup.
104 */
105 int gvFreeLayout(GVC_t * gvc, Agraph_t * g)
106 {
107 (void)gvc;
108
109 /* skip if no Agraphinfo_t yet */
110 if (! agbindrec(g, "Agraphinfo_t", 0, true))
111 return 0;
112
113 if (GD_cleanup(g)) {
114 (GD_cleanup(g))(g);
115 GD_cleanup(g) = NULL;
116 }
117
118 graph_cleanup(g);
119 return 0;
120 }